Created
February 8, 2023 03:32
-
-
Save jungyoung/5b56ad94ce897481219b8edbc35c2bdf 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.intellij.database.model.DasTable | |
import com.intellij.database.util.Case | |
import com.intellij.database.util.DasUtil | |
import javax.swing.JFrame | |
import javax.swing.JOptionPane | |
/* | |
* Available context bindings: | |
* SELECTION Iterable<DasObject> | |
* PROJECT project | |
* FILES files helper | |
*/ | |
typeMapping = [ | |
(~/(?i)bigint/) : "Long", | |
(~/(?i)int/) : "Long", | |
(~/(?i)float|double|decimal|real/): "Double", | |
(~/(?i)numeric/) : "BigDecimal", | |
(~/(?i)varchar/) : "String", | |
(~/(?i)char/) : "String", | |
(~/(?i)datetime|timestamp/) : "LocalDateTime", | |
(~/(?i)date/) : "java.sql.Date", | |
(~/(?i)time/) : "java.sql.Time", | |
(~/(?i)/) : "String" | |
] | |
def input = { | |
JFrame jframe = new JFrame() | |
String answer = JOptionPane.showInputDialog(jframe, it) | |
jframe.dispose() | |
answer | |
} | |
packageName = input("Enter your package name") | |
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir -> | |
SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) } | |
} | |
def generate(table, dir) { | |
def tableName = table.getName() | |
def className = javaName(table.getName(), true) | |
def fields = calcFields(table) | |
new File(dir, className + ".kt").withPrintWriter { out -> generate(out, tableName, className, fields) } | |
} | |
def generate(out, tableName, className, fields) { | |
out.println "package $packageName" | |
out.println "" | |
out.println "import jakarta.persistence.Entity" | |
out.println "import jakarta.persistence.Id" | |
out.println "import jakarta.persistence.Table" | |
out.println "import jakarta.persistence.Column" | |
out.println "import java.time.LocalDateTime" | |
out.println "" | |
out.println "@Entity" | |
out.println "@Table(name = \"$tableName\")" | |
out.println "class ${className}(" | |
out.println " @Id" | |
fields.each() { | |
if (it.annos != "") { | |
out.println " ${it.annos}" | |
} | |
if (it.type == "Int") { | |
out.println " @Column(name = \"${it.colName}\")" | |
out.println " val ${it.name}: ${it.type}? = 0," | |
} else if (it.type == "Boolean") { | |
out.println " @Column(name = \"${it.colName}\")" | |
out.println " val ${it.name}: ${it.type} = false," | |
} else if (it.type == 'varchar') { | |
out.println " @Column(name = \"${it.colName}\")" | |
out.println " val String ${it.name}," | |
} else { | |
out.println " @Column(name = \"${it.colName}\")" | |
out.println " val ${it.name}: ${it.type}? = null," | |
} | |
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 | |
fields += [[ | |
colName: col.getName(), | |
name : javaName(col.getName(), false), | |
type : typeStr, | |
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] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment