Skip to content

Commit 95e0014

Browse files
authored
Merge pull request #33 from Software-Developers-IRL/f/lastlink
improved er diagram details
2 parents 6829c27 + 7962688 commit 95e0014

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

samples/mermaid.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
print('```mermaid')
1313
print('erDiagram')
1414
print('')
15+
# generate entity tables
1516
for table in catalog.tables:
1617
print(' ' + table.fullName + ' {')
1718
for column in table.columns:
@@ -28,20 +29,30 @@
2829
# isPartOfIndex
2930
# isPartOfUniqueIndex
3031
keyType = " PK" if column.partOfPrimaryKey else ""
31-
if(keyType != "" and column.partOfForeignKey ):
32+
if(keyType == "" and column.partOfForeignKey ):
3233
keyType = " FK"
3334
# dont need to define FK can get through relationships
3435
# keyType += ("" if keyType == "" else ",") + "FK" if column.partOfForeignKey else ""
3536
extraAttributes = ""
3637
if(columnFullName != ""):
3738
extraAttributes += columnFullName
38-
39+
if(not column.nullable):
40+
extraAttributes += " NOT NULL"
3941
if(extraAttributes != ""):
40-
extraAttributes = " \"" + extraAttributes + "\""
42+
extraAttributes = " \"" + extraAttributes.strip() + "\""
4143
print(' ' + re.sub("[,]", "_",re.sub(r'[()]', '', column.columnDataType.name)) + ' ' + columnShortName + keyType + extraAttributes)
4244
print(' }')
4345
print('')
4446

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
4556
for table in catalog.tables:
4657
for childTable in table.getRelatedTables(TableRelationshipType.child):
4758
print(' ' + table.name + ' ||--o{ ' + childTable.name + ' : "foreign key"')

0 commit comments

Comments
 (0)