Created
August 5, 2017 05:27
-
-
Save webpatch/3ba76c559e1bfb445823c3173f12f0e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.base.CaseFormat | |
import com.intellij.database.model.DasObject | |
import com.intellij.database.model.DasTable | |
import com.intellij.database.model.ObjectKind | |
import com.intellij.database.util.Case | |
import com.intellij.database.util.DasUtil | |
/** | |
* modification of origin script : | |
* - add comment support (if you are non native English user, it is very important) | |
* - make names with underscore better: such as `t_my_table` -> `tMyTable` | |
*/ | |
/* | |
* Available context bindings: | |
* SELECTION Iterable<DasObject> | |
* PROJECT project | |
* FILES files helper | |
*/ | |
packageName = "com.sample;" | |
typeMapping = [ | |
(~/(?i)int/) : "long", | |
(~/(?i)float|double|decimal|real/): "double", | |
(~/(?i)datetime|timestamp/) : "java.sql.Timestamp", | |
(~/(?i)date/) : "java.sql.Date", | |
(~/(?i)time/) : "java.sql.Time", | |
(~/(?i)/) : "String" | |
] | |
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir -> | |
// assert SELECTION instanceof Iterable<DasObject> //no use | |
SELECTION.filter { it instanceof DasTable && it.getKind() == ObjectKind.TABLE }.each { generate(it, dir) } | |
} | |
def generate(DasObject table, dir) { | |
def className = javaName(table.name, true) | |
def fields = calcFields(table) | |
new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields) } | |
} | |
def generate(out, className, fields) { | |
out.println "package $packageName" | |
out.println "" | |
out.println "" | |
out.println "public class $className {" | |
out.println "" | |
fields.each() { | |
if (it.annos != "") out.println " ${it.annos}" | |
out.println " private ${it.type} ${it.name};" | |
} | |
out.println "" | |
fields.each() { | |
out.println "" | |
out.println " public ${it.type} get${it.name.capitalize()}() {" | |
out.println " return ${it.name};" | |
out.println " }" | |
out.println "" | |
out.println " public void set${it.name.capitalize()}(${it.type} ${it.name}) {" | |
out.println " this.${it.name} = ${it.name};" | |
out.println " }" | |
out.println "" | |
} | |
out.println "}" | |
} | |
def calcFields(DasObject table) { | |
DasUtil.getColumns(table).reduce([]) { fields, col -> | |
def spec = Case.LOWER.apply(col.dataType.specification) | |
def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value | |
fields += [[ | |
name : javaName(col.name, false), | |
type : typeStr, | |
annos: col.comment ? """ | |
/** | |
* $col.comment | |
*/""" : ""]] | |
} | |
} | |
static String javaName(String str, boolean capitalize) { | |
def s = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, str); | |
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment