forked from schemacrawler/SchemaCrawler-Action-Usage-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbml.py
More file actions
84 lines (80 loc) · 3.27 KB
/
dbml.py
File metadata and controls
84 lines (80 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from __future__ import print_function
import re
from schemacrawler.schema import TableRelationshipType # pylint: disable=import-error
from schemacrawler.schemacrawler import IdentifierQuotingStrategy # pylint: disable=import-error
from schemacrawler.utility import MetaDataUtility # pylint: disable=import-error
print('Project "' + catalog.crawlInfo.runId + '" {')
print(' database_type: "' + re.sub(r'\"', '', catalog.crawlInfo.databaseVersion.toString()) + '"')
print(" Note: '''")
print(catalog.crawlInfo)
print(" '''")
print("}")
# Columns
for table in catalog.getTables():
print('Table "' + re.sub(r'\"', '', table.fullName) + '" {')
for column in table.columns:
print(' "' + column.name + '" "' + column.columnDataType.name + '"', end = '')
# Column attributes
print(' [', end = '')
if not column.nullable:
print('not ', end = '')
print('null', end = '')
if column.hasDefaultValue():
print(', default: "' + column.defaultValue + '"', end = '')
if column.hasRemarks():
print(', note: "' + column.remarks + '"', end = '')
print(']', end = '')
print()
if table.hasRemarks():
print(" Note: '''")
print(table.remarks)
print(" '''")
# Primary keys and indexes
if table.hasPrimaryKey() or not table.indexes.isEmpty():
print(' indexes {')
if table.hasPrimaryKey():
primaryKey = table.primaryKey
print(' (' + MetaDataUtility.getColumnsListAsString(primaryKey, IdentifierQuotingStrategy.quote_all, '"') + ') ' \
+ '[pk]')
if not table.indexes.isEmpty():
for index in table.indexes:
if table.hasPrimaryKey() and \
MetaDataUtility.getColumnsListAsString(table.primaryKey, IdentifierQuotingStrategy.quote_all, '"') == \
MetaDataUtility.getColumnsListAsString(index, IdentifierQuotingStrategy.quote_all, '"'):
continue
print(' (' + MetaDataUtility.getColumnsListAsString(index, IdentifierQuotingStrategy.quote_all, '"') + ')', end = '')
print(' [name: "' + index.name + '"', end = '')
if index.unique:
print(', unique', end = '')
print(']')
print(' }')
print('}')
print('')
# Foreign keys
for table in catalog.tables:
for fk in table.exportedForeignKeys:
print('Ref "' + fk.name + '" {')
pkTable = None
fkTable = None
for columnReference in fk.columnReferences:
pkTable = columnReference.primaryKeyColumn.parent
fkTable = columnReference.foreignKeyColumn.parent
print(' "' \
+ re.sub(r'\"', '', pkTable.fullName) + '".(' \
+ MetaDataUtility.getColumnsListAsString(fk, TableRelationshipType.parent, IdentifierQuotingStrategy.quote_all, '"') \
+ ') < "' \
+ re.sub(r'\"', '', fkTable.fullName) + '".(' \
+ MetaDataUtility.getColumnsListAsString(fk, TableRelationshipType.child, IdentifierQuotingStrategy.quote_all, '"') \
+ ')', end = '')
print(' [update: ' + fk.updateRule.toString() + ', delete: ' + fk.deleteRule.toString() + ']', end = '')
print()
print("}")
print('')
print('')
# Table groups
for schema in catalog.schemas:
print('TableGroup "' + re.sub(r'\"', '', schema.fullName) + '" {')
for table in catalog.getTables(schema):
print(' "' + re.sub(r'\"', '', table.fullName) + '"')
print('}')
print('')