|
12 | 12 | print('```mermaid') |
13 | 13 | print('erDiagram') |
14 | 14 | print('') |
| 15 | +# generate entity tables |
15 | 16 | for table in catalog.tables: |
16 | 17 | print(' ' + table.fullName + ' {') |
17 | 18 | for column in table.columns: |
|
28 | 29 | # isPartOfIndex |
29 | 30 | # isPartOfUniqueIndex |
30 | 31 | keyType = " PK" if column.partOfPrimaryKey else "" |
31 | | - if(keyType != "" and column.partOfForeignKey ): |
| 32 | + if(keyType == "" and column.partOfForeignKey ): |
32 | 33 | keyType = " FK" |
33 | 34 | # dont need to define FK can get through relationships |
34 | 35 | # keyType += ("" if keyType == "" else ",") + "FK" if column.partOfForeignKey else "" |
35 | 36 | extraAttributes = "" |
36 | 37 | if(columnFullName != ""): |
37 | 38 | extraAttributes += columnFullName |
38 | | - |
| 39 | + if(not column.nullable): |
| 40 | + extraAttributes += " NOT NULL" |
39 | 41 | if(extraAttributes != ""): |
40 | | - extraAttributes = " \"" + extraAttributes + "\"" |
| 42 | + extraAttributes = " \"" + extraAttributes.strip() + "\"" |
41 | 43 | print(' ' + re.sub("[,]", "_",re.sub(r'[()]', '', column.columnDataType.name)) + ' ' + columnShortName + keyType + extraAttributes) |
42 | 44 | print(' }') |
43 | 45 | print('') |
44 | 46 |
|
| 47 | +# generate relationships, need to display keys in the description for sql generation |
| 48 | +# https://www.tabnine.com/code/java/methods/schemacrawler.schemacrawler.SchemaCrawlerOptionsBuilder/includeGreppedColumns |
| 49 | +for table in catalog.tables: |
| 50 | + for foreignKeys in table.foreignKeys: |
| 51 | + relationshipTxt = str(foreignKeys.primaryKeyTable.primaryKey.columns) + " to " + str(foreignKeys.columns) |
| 52 | + relationshipTxt = "\"" + re.sub("[\"]", "\'", relationshipTxt) + "\"" |
| 53 | + print(' ' + table.fullName + ' ||--o{ ' + str(foreignKeys.foreignKeyTable) + ' : '+ relationshipTxt) |
| 54 | + print('') |
| 55 | +# old way |
45 | 56 | for table in catalog.tables: |
46 | 57 | for childTable in table.getRelatedTables(TableRelationshipType.child): |
47 | 58 | print(' ' + table.name + ' ||--o{ ' + childTable.name + ' : "foreign key"') |
|
0 commit comments