Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate Squash classes from existing DB table
import com.intellij.database.model.DasTable
import com.intellij.database.model.ObjectKind
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
/*
* Available context bindings:
* SELECTION Iterable<DasObject>
* PROJECT project
* FILES files helper
*/
packageName = "com.sample;"
typeMapping = [
(~/(?i)int/) : "Int",
(~/(?i)float|double|decimal|real/): "Double",
(~/(?i)numeric/) : "BigDecimal",
(~/(?i)datetime|timestamp/) : "java.sql.Timestamp",
(~/(?i)date/) : "java.sql.Date",
(~/(?i)time/) : "java.sql.Time",
(~/(?i)/) : "String"
]
squashTypeMapping = [
(~/(?i)int/) : "integer",
(~/(?i)float|double|decimal|real/): "decimal",
(~/(?i)numeric/) : "decimal",
(~/(?i)datetime|timestamp/) : "datetime",
(~/(?i)date/) : "date",
(~/(?i)time/) : "time",
(~/(?i)/) : "text"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter { it instanceof DasTable && it.getKind() == ObjectKind.TABLE }.each { generate(it, dir) }
}
def generate(table, dir) {
def className = javaName(table.getName().replace("al_", ""), true)
def fields = calcFields(table)
new File(dir, className + ".kt").withPrintWriter { out -> generateDataClass(out, className, fields) }
new File(dir, className + "Table.kt").withPrintWriter { out -> generateTable(out, table.getName(), className, fields) }
new File(dir, className + "DAO.kt").withPrintWriter { out -> generateDAO(out, className, fields) }
}
def generateDataClass(out, className, fields) {
out.println "package $packageName"
out.println ""
out.println ""
out.println "data class ${className}("
fields.each() {
if (it.annos != "") out.println " ${it.annos}"
out.println " val ${it.name}: ${it.type},"
}
out.println ")"
}
def generateTable(out, tableName, className, fields) {
out.println "package $packageName"
out.println ""
out.println "import org.jetbrains.squash.definition.*"
out.println ""
out.println "object ${className}Table: TableDefinition(\"$tableName\") {"
fields.each() {
if (it.annos != "") out.println " ${it.annos}"
out.println " val ${it.name} = ${it.squashType}(\"$it.column\")"
}
out.println "}"
}
def generateDAO(out, className, fields) {
out.println "package $packageName"
out.println ""
out.println "import org.jetbrains.squash.results.ResultRow"
out.println ""
out.println "class ${className}DAO {"
out.println ""
out.println "private fun map${className}(row: ResultRow): $className {"
out.println " with(${className}Table) { "
out.println " return $className("
fields.each() {
if (it.annos != "") out.println " ${it.annos}"
out.println " ${it.name} = row[${it.name}],"
}
out.println " )"
out.println " }"
out.println "}"
}
def calcFields(table) {
DasUtil.getColumns(table).reduce([]) { fields, col ->
def spec = Case.LOWER.apply(col.getDataType().getSpecification())
def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
def squashTypeStr = squashTypeMapping.find { p, t -> p.matcher(spec).find() }.value
fields += [[
name : javaName(col.getName(), false),
column: col.getName(),
type : typeStr,
squashType: squashTypeStr,
annos: ""]]
}
}
def javaName(str, capitalize) {
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
.collect { Case.LOWER.apply(it).capitalize() }
.join("")
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
capitalize || s.length() == 1? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
@gderaco

This comment has been minimized.

Copy link

@gderaco gderaco commented Dec 27, 2018

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment