Skip to content

Commit

Permalink
final foreign key er diagram generator
Browse files Browse the repository at this point in the history
  • Loading branch information
lastlink committed Oct 1, 2022
1 parent b9e5bf9 commit 3d8d6bf
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions samples/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
print('')
# generate entity tables
for table in catalog.tables:
print(' ' + table.fullName + ' {')
tableFullName = re.sub("[\"\']", "", table.fullName).replace(" ","_")
print(' ' + tableFullName + ' {')
for column in table.columns:
# // need to remove quotes and spaces
# get all column attributes for debugging
Expand Down Expand Up @@ -47,11 +48,22 @@
# generate relationships, need to display keys in the description for sql generation
# https://www.tabnine.com/code/java/methods/schemacrawler.schemacrawler.SchemaCrawlerOptionsBuilder/includeGreppedColumns
for table in catalog.tables:
tableOriginal = re.sub("[\"\']", "", table.fullName)
tableFullName = tableOriginal.replace(" ","_")
# print (tableFullName)
addSpace = False
for foreignKeys in table.foreignKeys:
relationshipTxt = str(foreignKeys.primaryKeyTable.primaryKey.columns) + " to " + str(foreignKeys.columns)
relationshipTxt = "\"" + re.sub("[\"]", "\'", relationshipTxt) + "\""
print(' ' + table.fullName + ' ||--o{ ' + str(foreignKeys.foreignKeyTable) + ' : '+ relationshipTxt)
print('')
fkTableOriginal = str(foreignKeys.primaryKeyTable)
fkTableFullName = fkTableOriginal.replace(" ", "_")
if(tableFullName != fkTableFullName):
addSpace = True
relationshipTxt = str(foreignKeys.primaryKeyTable.primaryKey.columns) + " to " + str(foreignKeys.columns)
relationshipTxt = "\"" + re.sub("[\"]", "\'", relationshipTxt) + "\""
relationshipTxt = relationshipTxt.replace(tableOriginal,tableFullName).replace(fkTableOriginal, fkTableFullName)
# str(foreignKeys.foreignKeyTable)
print(' ' + fkTableOriginal + ' ||--o{ ' + tableFullName + ' : '+ relationshipTxt)
if(addSpace):
print('')
# old way
for table in catalog.tables:
for childTable in table.getRelatedTables(TableRelationshipType.child):
Expand Down

0 comments on commit 3d8d6bf

Please sign in to comment.