11from schemacrawler .schema import TableRelationshipType # pylint: disable=import-error
22import re
3+ from datetime import date
34
5+ today = date .today ()
6+
7+ print ('# Mermaid erDiagram' )
8+ print ('* generated using schemacrawler on:' + str (today ))
9+ print ('* using Little-Mermaid-2-TheSQL formatting' )
10+ # TODO include package version
11+ print ('' )
12+ print ('```mermaid' )
413print ('erDiagram' )
514print ('' )
615for table in catalog .tables :
716 print (' ' + table .fullName + ' {' )
817 for column in table .columns :
9- print (' ' + re .sub (r'\([\d ,]+\)' , '' , column .columnDataType .name ) + ' ' + column .name )
18+ # // need to remove quotes and spaces
19+ # get all column attributes for debugging
20+ # print (dir(column))
21+ columnShortName = re .sub ("[\" \' ]" , "" , column .name ).replace (" " ,"" )
22+ columnFullName = ""
23+ if (columnShortName != column .name ):
24+ columnFullName = "'" + column .name + "'"
25+ # values cared about:
26+ # isNullable
27+ # autoIncremented
28+ # isPartOfIndex
29+ # isPartOfUniqueIndex
30+ keyType = " PK" if column .partOfPrimaryKey else ""
31+ if (keyType != "" and column .partOfForeignKey ):
32+ keyType = " FK"
33+ # dont need to define FK can get through relationships
34+ # keyType += ("" if keyType == "" else ",") + "FK" if column.partOfForeignKey else ""
35+ extraAttributes = ""
36+ if (columnFullName != "" ):
37+ extraAttributes += columnFullName
38+
39+ if (extraAttributes != "" ):
40+ extraAttributes = " \" " + extraAttributes + "\" "
41+ print (' ' + re .sub ("[,]" , "_" ,re .sub (r'[()]' , '' , column .columnDataType .name )) + ' ' + columnShortName + keyType + extraAttributes )
1042 print (' }' )
1143 print ('' )
1244
1345for table in catalog .tables :
1446 for childTable in table .getRelatedTables (TableRelationshipType .child ):
1547 print (' ' + table .name + ' ||--o{ ' + childTable .name + ' : "foreign key"' )
16- print ('' )
48+ print ('' )
49+ print ('```' )
0 commit comments