-
-
Save LSTANCZYK/e3ff1d4d60cd22001e24e42f8d733f3d to your computer and use it in GitHub Desktop.
Generate C# Classes from Tables in DataGrip
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
package extensions.data.extractors | |
/* | |
* Available context bindings: | |
* COLUMNS List<DataColumn> | |
* ROWS Iterable<DataRow> | |
* OUT { append() } | |
* FORMATTER { format(row, col); formatValue(Object, col); getTypeName(Object, col); isStringLiteral(Object, col); } | |
* TRANSPOSED Boolean | |
* plus ALL_COLUMNS, TABLE, DIALECT | |
* | |
* where: | |
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object } | |
* DataColumn { columnNumber(), name() } | |
*/ | |
import com.intellij.database.model.DasTable | |
import com.intellij.database.model.ObjectKind | |
import com.intellij.database.util.Case | |
import com.intellij.database.util.DasUtil | |
NEWLINE = System.getProperty("line.separator") | |
typeMapping = [ | |
(~/(?i)^bit$/) : "bool", | |
(~/(?i)^tinyint$/) : "byte", | |
(~/(?i)^uniqueidentifier|uuid$/) : "Guid", | |
(~/(?i)^int|integer|number$/) : "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|time$/) : "DateTime", | |
(~/(?i)^char$/) : "char", | |
] | |
notNullableTypes = [ "string", "byte[]" ] | |
def toCsType = { type -> | |
if (DIALECT.getDbms().isOracle()){ | |
switch (type) { | |
case 'CHAR': | |
return 'string' | |
case 'VARCHAR2': | |
return 'string' | |
case 'NCHAR': | |
return 'string' | |
case 'NVARCHAR2': | |
return 'string' | |
case 'NUMBER': | |
return 'decimal' | |
case 'DATE': | |
return 'DateTime' | |
case 'TIMESTAMP': | |
return 'DateTime' | |
case 'BLOB': | |
return 'byte[]' | |
case 'CLOB': | |
return 'string' | |
case 'RAW': | |
return 'byte[]' | |
case 'ROWID': | |
return 'string' | |
case 'BOOLEAN': | |
return 'bool' | |
case 'BINARY_INTEGER': | |
return 'int' | |
case 'NATURAL': | |
return 'int' | |
case 'PLS_INTEGER': | |
return 'int' | |
case 'LONG': | |
return 'long' | |
case 'UNSIGNED_INTEGER': | |
return 'int' | |
case 'UNSIGNED_NATURAL': | |
return 'int' | |
case 'UNSIGNED_PLS_INTEGER': | |
return 'int' | |
case 'UNSIGNED_LONG': | |
return 'long' | |
case 'OBJECT': | |
return 'object' | |
case 'REF CURSOR': | |
return 'object' | |
case 'XMLTYPE': | |
return 'XElement' | |
default: | |
"string" | |
} | |
} else { | |
typeMapping.find { p, t -> p.matcher(type).find() }?.value ?: "string" | |
} | |
} | |
def csharpName(str) { | |
com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str) | |
.collect { Case.LOWER.apply(it).capitalize() } | |
.join("") | |
.replace("Objkey","ObjKey") | |
.replace("Objid","ObjId") | |
.replace("Objversion","ObjVersion") | |
.replace("Cf\$","") | |
} | |
OUT.append ("using System;" + NEWLINE) | |
OUT.append ("using System.ComponentModel.DataAnnotations.Schema;" + NEWLINE) | |
OUT.append ("" + NEWLINE) | |
OUT.append ("public class POCO" + NEWLINE) | |
OUT.append ( "{" + NEWLINE) | |
def firstRow = ROWS[0]; | |
COLUMNS.each() { it -> | |
def typeName = FORMATTER.getTypeName(firstRow, it) | |
def typeStr = toCsType(typeName) | |
OUT.append " [Column(\"${it.name()}\")]"+ NEWLINE | |
OUT.append " public ${typeStr} ${csharpName(it.name())} { get; set; }"+ NEWLINE | |
OUT.append NEWLINE | |
} | |
OUT.append("}" + NEWLINE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment