Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. import groovy.sql.Sql
  2. import groovy.transform.Canonical
  3. import groovy.transform.Field
  4.  
  5. Sql casinoDb = Sql.newInstance('jdbc:oracle:thin:@host1:1521/testdb1', 'uname', 'passwd')
  6. Sql ewDb = Sql.newInstance('jdbc:oracle:thin:@host2:1521/testdb' , 'uname', 'passwd')
  7.  
  8. def sourceDataSql = new File('/some/file/path.sql').text
  9.  
  10. @Canonical
  11. class TransKey {
  12. String type
  13. String code
  14. static TransKey key(type, code) { new TransKey(type: type, code: code)}
  15. }
  16.  
  17. @Canonical
  18. class TransValue {
  19. String locale
  20. String value
  21. static TransValue val(locale, val) { new TransValue(locale: locale, value: val)}
  22. }
  23.  
  24. class SourceData {
  25. Map<TransKey, Set<TransValue>> data = [:]
  26. void add(result) {
  27. def val = TransValue.val(result.locale_code, result.localized_value)
  28. def current = data.putIfAbsent(TransKey.key(result.type, result.code), [val] as Set)
  29. if (current) current.add(val)
  30. }
  31. }
  32.  
  33. def sourceData = new SourceData()
  34. ewDb.rows(sourceDataSql).each(sourceData.&add)
  35.  
  36. sourceData.data.each { TransKey k, Set<TransValue> vals ->
  37. casinoDb.withTransaction {
  38. def ownerId = findOwnerIdByCode(k.type, k.code, casinoDb)
  39. def ownerType = k.type
  40. println "$ownerType: $ownerId"
  41. vals.each { TransValue val ->
  42. println "Work with $val.locale"
  43. def rowIds = casinoDb.executeInsert('INSERT INTO TRANSCATEGORY(id, ownerid, ownertype) VALUES(SQ_TRANSCATEGORY.nextval, ?, ?)', ownerId, ownerType)
  44. def trcatId = casinoDb.firstRow('SELECT id FROM TRANSCATEGORY WHERE (rowid = :row_id)', [row_id: rowIds[0][0]]).id
  45.  
  46. def localeId = findLocaleId(val.locale, casinoDb)
  47.  
  48. def localizeItemId = casinoDb.firstRow('SELECT SQ_LOCALIZE.nextval nextval FROM dual')?.nextval
  49.  
  50. casinoDb.executeInsert("INSERT INTO LOCALIZATION(ID, HASH) VALUES(?, 'EMPTY')", localizeItemId)
  51.  
  52. casinoDb.executeInsert("INSERT INTO LOCALIZEITEM(LOCALIZEID, LOCALE_ID, WORD) VALUES(?, ?, ?)",
  53. localizeItemId, localeId, val.value)
  54.  
  55. casinoDb.executeInsert('INSERT INTO TRANS(ID, CATEGORYID, LOCALIZATIONID, FIELDTYPE) VALUES (SQ_TRANS.nextval, ?,?,?)',
  56. trcatId, localizeItemId, "${k.type}Name".toString())
  57.  
  58. }
  59. }
  60. }
  61.  
  62.  
  63. @Field
  64. Map locales = [:]
  65. def findLocaleId(String code, Sql sql) {
  66. def cachedId = locales.get(code)
  67. if (!cachedId) {
  68. def id = sql.firstRow('SELECT id FROM locale WHERE code = ?', code)?.id
  69. locales.put(code, id)
  70. return id
  71. }
  72. cachedId
  73. }
  74.  
  75. @Field
  76. Map ownerIds = [:]
  77. Object findOwnerIdByCode(String type, String code, Sql sql) {
  78. def key = "$type:$code"
  79. def cachedId = ownerIds.get(key)
  80. if (!cachedId) {
  81. def id = sql.firstRow("SELECT id FROM $type WHERE code=:code", code: code)?.id
  82. ownerIds.put(key, id)
  83. return id
  84. }
  85. cachedId
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement