Last active
February 9, 2021 20:24
-
-
Save itzg/a2ffb550689f806636e202f619b50e7e to your computer and use it in GitHub Desktop.
For use in IntelliJ's Database Tools and SQL scripts
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.intellij.database.model.DasTable | |
import com.intellij.database.util.Case | |
import com.intellij.database.util.DasUtil | |
/* | |
* Derived from "Generate POJOs.groovy" provided by IntelliJ base install | |
* Available context bindings: | |
* SELECTION Iterable<DasObject> | |
* PROJECT project | |
* FILES files helper | |
*/ | |
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" | |
] | |
nullables = [ | |
'long': 'Long', | |
'double': 'Double' | |
] | |
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { File dir -> | |
def packageName = "fixme" | |
def srcMainJavaIndex = dir.path.indexOf("src/main/java/") | |
if (srcMainJavaIndex >= 0) { | |
packageName = dir.path.substring(srcMainJavaIndex+14) | |
.replaceAll("[/\\\\]", ".") | |
} | |
SELECTION.filter { it instanceof DasTable }.each { generate(it, dir, packageName) } | |
} | |
def generate(table, File dir, packageName) { | |
String className = javaName(table.getName(), true) | |
def fields = calcFields(table) | |
new File(dir, className + ".java").withPrintWriter { out -> generate(out, packageName, className, fields) } | |
} | |
static def generate(out, packageName, className, fields) { | |
out.println "package $packageName;" | |
out.println "" | |
out.println "import javax.persistence.Column;" | |
out.println "import lombok.Data;" | |
out.println "" | |
out.println "@Data" | |
out.println "public class $className {" | |
fields.each() { | |
if (it.annos != "") out.println " ${it.annos}" | |
out.println " ${it.type} ${it.name};" | |
} | |
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 | |
// nullable and primitive? | |
if (!col.notNull && nullables.containsKey(typeStr)) { | |
typeStr = nullables.get(typeStr) | |
} | |
def columnParams = [ | |
"name = \"${col.getName()}\"" | |
] | |
if (col.notNull) { | |
columnParams += "nullable = false" | |
} | |
//noinspection GroovyUnusedAssignment | |
fields += [[ | |
name : javaName(col.getName(), false), | |
type : typeStr, | |
annos: "@Column(${columnParams.join(", ")})"]] | |
} | |
} | |
def javaName(str, capitalize) { | |
String s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str) | |
.collect { Case.LOWER.apply(it).capitalize() } | |
.join("") | |
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_") | |
if (s == "Class") { | |
s = "TheClass" | |
} | |
capitalize || s.length() == 1? s : Case.LOWER.apply(s[0]) + s[1..-1] | |
} |
Author
itzg
commented
Feb 9, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment