Created
July 5, 2020 01:46
-
-
Save kwon37xi/404b075c2258e1df3ccbb04fdd0be525 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 | |
/* | |
IntelliJ Database Extension script for Java 8 or Later JPA entity mapping generation. | |
*/ | |
/* | |
* Available context bindings: | |
* SELECTION Iterable<DasObject> | |
* PROJECT project | |
* FILES files helper | |
*/ | |
packageName = "com.sample;" | |
typeMapping = [ | |
(~/(?i)int/) : "int", | |
(~/(?i)bigint/) : "long", | |
(~/(?i)decimal|numeric|number/) : "BigDecimal", | |
(~/(?i)float|double|decimal|real/): "double", | |
(~/(?i)datetime|timestamp/) : "LocalDateTime", | |
(~/(?i)date/) : "LocalDate", | |
(~/(?i)time/) : "Time", | |
(~/(?i)/) : "String" | |
] | |
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(tableName, true) | |
def fields = calcFields(table) | |
new File(dir, className + "Entity.java").withPrintWriter { out -> generate(out, tableName, className, fields) } | |
} | |
def generate(out, tableName, className, fields) { | |
out.println "package $packageName" | |
out.println "" | |
out.println "import lombok.Data;" | |
out.println "import lombok.NoArgsConstructor;" | |
out.println "" | |
out.println "import javax.persistence.*;" | |
out.println "import java.time.LocalDate;" | |
out.println "import java.time.LocalDateTime;" | |
out.println "import java.time.LocalTime;" | |
out.println "import java.math.BigDecimal;" | |
out.println "" | |
out.println "@NoArgsConstructor" | |
out.println "@Entity" | |
out.println "@Table(name = \"$tableName\", schema = \"\", catalog = \"\")" | |
out.println "public class $className" + "Entity {" | |
out.println "" | |
fields.each() { | |
if (it.annos != "") out.println " ${it.annos}" | |
out.println " @Column(name = \"${it.oriName}\", nullable = false)" | |
out.println " private ${it.type} ${it.name};\n" | |
} | |
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 += [[ | |
name : javaName(col.getName(), false), | |
oriName : col.getName(), | |
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