SQL-Insert-Statements.sql.groovy 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. SEP = ", "
  2. QUOTE = "\'"
  3. NEWLINE = System.getProperty("line.separator")
  4. KEYWORDS_LOWERCASE = com.intellij.database.util.DbSqlUtil.areKeywordsLowerCase(PROJECT)
  5. KW_INSERT_INTO = KEYWORDS_LOWERCASE ? "insert into " : "INSERT INTO "
  6. KW_VALUES = KEYWORDS_LOWERCASE ? ") values (" : ") VALUES ("
  7. KW_NULL = KEYWORDS_LOWERCASE ? "null" : "NULL"
  8. i = 0
  9. def record(columns, dataRow) {
  10. if(i == 0) {
  11. OUT.append(KW_INSERT_INTO)
  12. if (TABLE == null) OUT.append("MY_TABLE")
  13. else OUT.append(TABLE.getParent().getName()).append(".").append(TABLE.getName())
  14. }
  15. i = 1
  16. OUT.append(" (")
  17. columns.eachWithIndex { column, idx ->
  18. OUT.append(column.name()).append(idx != columns.size() - 1 ? SEP : "")
  19. }
  20. OUT.append(KW_VALUES)
  21. columns.eachWithIndex { column, idx ->
  22. def value = dataRow.value(column)
  23. def skipQuote = value.toString().isNumber() || value == null
  24. def stringValue = value != null ? FORMATTER.format(dataRow, column) : KW_NULL
  25. if (DIALECT.getFamilyId().isMysql()) stringValue = stringValue.replace("\\", "\\\\")
  26. OUT.append(skipQuote ? "": QUOTE).append(stringValue.replace(QUOTE, QUOTE + QUOTE))
  27. .append(skipQuote ? "": QUOTE).append(idx != columns.size() - 1 ? SEP : "")
  28. }
  29. OUT.append(");").append(NEWLINE)
  30. }
  31. ROWS.each { row -> record(COLUMNS, row) }