Last active
February 1, 2024 08:28
-
-
Save surgicalcoder/b1f82d3e7334e6a39d7f271b52c910e4 to your computer and use it in GitHub Desktop.
Generate Dapper Contrib from database with DataGrip
This file contains hidden or 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.model.ObjectKind | |
import com.intellij.database.util.Case | |
import com.intellij.database.util.DasUtil | |
import com.intellij.database.model.DasIndex | |
import com.intellij.util.ObjectUtils | |
import groovy.json.* | |
typeMapping = [ | |
(~/(?i)^bit$/) : "bool", | |
(~/(?i)^tinyint\(1\)$/) : "bool", | |
(~/(?i)^uniqueidentifier|uuid$/) : "Guid", | |
(~/(?i)^int|integer|tinyint.*$/) : "int", | |
(~/(?i)^bigint$/) : "long", | |
(~/(?i)^varbinary|image$/) : "byte[]", | |
(~/(?i)^double|float|real$/) : "double", | |
(~/(?i)^decimal|money|numeric|smallmoney$/) : "decimal", | |
(~/(?i)^datetimeoffset$/) : "DateTimeOffset", | |
(~/(?i)^datetime|datetime2|timestamp|date$/) : "DateTime", | |
(~/(?i)^time$/) : "TimeSpan", | |
(~/(?i)^char$/) : "char", | |
(~/(?i)^(n)?varchar.*$/) : "string", | |
(~/(?i)^(n)?text$/) : "string", | |
] | |
notNullableTypes = [ "string", "byte[]" ] | |
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 tableName = table.getName() | |
def className = csharpName(tableName) | |
def fields = calcFields(table) | |
def file = new File(dir,className+".cs") | |
def packageName = dir.toString().replaceAll("\\\\", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";" | |
def writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8")) | |
writer.withPrintWriter { out -> generate(out, table, fields, packageName) } | |
} | |
def generate(out, table, fields, packageName) { | |
def tableName = table.getName() | |
def className = csharpName(tableName) | |
def tableComment = table.getComment() | |
def hasPK = DasUtil.getPrimaryKey(table)!=null | |
def hasDatetime = false | |
fields.each() {hasDatetime=hasDatetime||it.type=="LocalDateTime"} | |
out.println "using System;" | |
out.println "using Dapper.Contrib.Extensions;" | |
out.println "" | |
out.println "[Table(\"${tableName}\")]" | |
out.println "public class ${csharpName(tableName)}" | |
out.println "{" | |
fields.each() { | |
out.println "" | |
if (it.isId) out.println " [Key]" | |
if (it.isAuto) out.println " [Computed]" | |
if (it.isId && !it.isAuto) out.println " [ExplicitKey]" | |
if (it.isFK) out.println " [ForeignKey]" | |
if (it.indexes){ | |
for(wibble in it.indexes) | |
{ | |
if (wibble != null) | |
{ | |
for(meep in wibble.getColumnsRef().names()) | |
{ | |
if (meep == it.col.getName()) | |
{ | |
if (wibble.isUnique()) | |
{ | |
out.println " [UniqueIndex]" | |
} | |
else | |
{ | |
out.println " [Index]" | |
} | |
} | |
} | |
} | |
} | |
} | |
out.println " public ${it.type}${it.nullable} ${csharpName(it.name)} { get; set; }" | |
} | |
out.println "}" | |
} | |
def calcFields(table) { | |
def primaryKey = DasUtil.getPrimaryKey(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 ?: "string /* Unknown type : ${spec} */" | |
def indicies = table.getColumnAttrs(col) | |
def indicies2 = DasUtil.getIndices(table) | |
def colName = col.getName() | |
fields += [[ | |
name : col.getName(), | |
type : typeStr, | |
comment : "", | |
spec : spec, | |
isId : primaryKey != null && DasUtil.containsName(colName, primaryKey.getColumnsRef()), | |
isAuto : DasUtil.isAutoGenerated(col), | |
isIndex : DasUtil.isIndexColumn(col), | |
isFK : DasUtil.isForeign(col), | |
indexes : indicies2, | |
col : col, | |
annos : "@Column(name = \"${colName}\")", | |
nullable : col.isNotNull() || typeStr in notNullableTypes ? "" : "?" | |
]] | |
} | |
} | |
def csharpName(str) { | |
com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str) | |
.collect { Case.LOWER.apply(it).capitalize() } | |
.join("_") | |
.replaceAll('\\$','_') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment