Created
January 14, 2020 03:21
-
-
Save xwsg/c6e0c1b742a7fa82340d18e4e6186458 to your computer and use it in GitHub Desktop.
Datagrip groovy scripts
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.ObjectKind | |
import com.intellij.database.util.DasUtil | |
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir -> | |
def schemaMap = new LinkedHashMap<String, Map<String, String>>(); | |
SELECTION.filter { (it.getKind() == ObjectKind.TABLE || it.getKind() == ObjectKind.SCHEMA) }.each { | |
def schemaName = DasUtil.getSchema(it); | |
if (!schemaMap.containsKey(schemaName)) { | |
schemaMap.put(schemaName as String, new LinkedHashMap<String, String>()); | |
} | |
if (it.getKind() == ObjectKind.TABLE) { | |
def tableName = it.getName(); | |
if (!schemaMap.get(schemaName).containsKey(tableName)) { | |
schemaMap.get(schemaName).put(tableName as String, generateTableMarkdown(it) as String); | |
} | |
} else if (it.getKind() == ObjectKind.SCHEMA) { | |
it.getDasChildren(ObjectKind.TABLE).each { table -> | |
def tableName = table.getName(); | |
if (!schemaMap.get(schemaName).containsKey(tableName)) { | |
schemaMap.get(schemaName).put(tableName as String, generateTableMarkdown(table) as String); | |
} | |
} | |
} | |
} | |
def schemaMd = ''; | |
def tableMd = ''; | |
schemaMap.each { schema -> | |
schemaMd += '# ' + schema.key + '\n'; | |
schema.value.each { t -> | |
tableMd += (t.value) +'\n'; | |
} | |
}; | |
def markdown = schemaMd + tableMd; | |
new File(dir, "test.md").withPrintWriter("utf-8") { out -> out.println markdown } | |
} | |
def generateTableMarkdown(table) { | |
def schema = DasUtil.getSchema(table); | |
def tableName = table.getName(); | |
def tableComment = table.getComment(); | |
def columnMd = ''; | |
DasUtil.getColumns(table).reduce([]) { fields, col -> | |
def columnComment = (col.getComment() == null ? '' : col.getComment()); | |
columnComment = (columnComment as String).replaceAll("\r\n|\r|\n", "<br/>"); | |
def columnName = col.getName(); | |
def columnType = col.getDataType().getSpecification(); | |
def isNotNullHtml = (col.isNotNull() ? 'Y' : ''); | |
def defaultVal = (col.getDefault() == null ? '' : col.getDefault()); | |
def isPrimaryHtml = (DasUtil.isPrimary(col) ? 'Y' : ''); | |
columnMd += ' | ' + columnName + ' | ' + columnType + ' | ' + isNotNullHtml + ' | ' + defaultVal + ' | ' + isPrimaryHtml + ' | ' + columnComment + ' |\n'; | |
} | |
def tableCaptionMd = '## ' + (tableComment == null ? '' : tableComment) + '(' + (schema + '.' + tableName) + ')\n'; | |
def tableTitleMd = '| Column | Type | Nullable | Default | PK | Comment |\n|-----|-----|----|-----|-----|-----|\n'; | |
def tableMd = tableCaptionMd + tableTitleMd + columnMd; | |
return tableMd; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment