Created
October 4, 2023 10:16
-
-
Save m-wild/017a62d926575380c8ea50b880c69522 to your computer and use it in GitHub Desktop.
Jetbrains Rider/DataGrip C# POCO extractor
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
/* | |
* 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() } | |
*/ | |
NEWLINE = System.getProperty("line.separator") | |
TYPE_MAPPING = [ | |
(~/(?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", | |
] | |
def className = TABLE != null ? TABLE.getName() : "MyClass" | |
OUT.append("public class ${className}").append(NEWLINE) | |
.append("{").append(NEWLINE) | |
COLUMNS.each { column -> | |
def dbType = FORMATTER.getTypeName(null, column) | |
def clrType = TYPE_MAPPING.find { p, t -> p.matcher(dbType).find() }?.value ?: "string" | |
def name = column.name() | |
OUT.append(" public ${clrType} ${name} { get; set; }") | |
.append(NEWLINE) | |
} | |
OUT.append("}").append(NEWLINE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Creates a C# class definition for the type of the query results.
Only uses the column types & names, not any of the values.