@@ -40,6 +40,58 @@ export function fromSQLite(ast, diagramDb = DB.GENERIC) {
4040 const tables = [ ] ;
4141 const relationships = [ ] ;
4242
43+ const addRelationshipFromReferenceDef = (
44+ startTable ,
45+ startFieldName ,
46+ referenceDefinition ,
47+ ) => {
48+ const relationship = { } ;
49+ const endTableName = referenceDefinition . table [ 0 ] . table ;
50+ const endField = referenceDefinition . definition [ 0 ] . column ;
51+
52+ const endTableId = tables . findIndex ( ( t ) => t . name === endTableName ) ;
53+ if ( endTableId === - 1 ) return ;
54+
55+ const endFieldId = tables [ endTableId ] . fields . findIndex (
56+ ( f ) => f . name === endField ,
57+ ) ;
58+ if ( endFieldId === - 1 ) return ;
59+
60+ const startFieldId = startTable . fields . findIndex (
61+ ( f ) => f . name === startFieldName ,
62+ ) ;
63+ if ( startFieldId === - 1 ) return ;
64+
65+ relationship . name = startTable . name + "_" + startFieldName + "_fk" ;
66+ relationship . startTableId = startTable . id ;
67+ relationship . endTableId = endTableId ;
68+ relationship . endFieldId = endFieldId ;
69+ relationship . startFieldId = startFieldId ;
70+ let updateConstraint = "No action" ;
71+ let deleteConstraint = "No action" ;
72+ referenceDefinition . on_action . forEach ( ( c ) => {
73+ if ( c . type === "on update" ) {
74+ updateConstraint = c . value . value ;
75+ updateConstraint =
76+ updateConstraint [ 0 ] . toUpperCase ( ) + updateConstraint . substring ( 1 ) ;
77+ } else if ( c . type === "on delete" ) {
78+ deleteConstraint = c . value . value ;
79+ deleteConstraint =
80+ deleteConstraint [ 0 ] . toUpperCase ( ) + deleteConstraint . substring ( 1 ) ;
81+ }
82+ } ) ;
83+
84+ relationship . updateConstraint = updateConstraint ;
85+ relationship . deleteConstraint = deleteConstraint ;
86+
87+ if ( startTable . fields [ startFieldId ] . unique ) {
88+ relationship . cardinality = Cardinality . ONE_TO_ONE ;
89+ } else {
90+ relationship . cardinality = Cardinality . MANY_TO_ONE ;
91+ }
92+ relationships . push ( relationship ) ;
93+ } ;
94+
4395 const parseSingleStatement = ( e ) => {
4496 if ( e . type === "create" ) {
4597 if ( e . keyword === "table" ) {
@@ -111,8 +163,15 @@ export function fromSQLite(ast, diagramDb = DB.GENERIC) {
111163 if ( d . check ) {
112164 field . check = buildSQLFromAST ( d . check . definition [ 0 ] , DB . SQLITE ) ;
113165 }
114-
115166 table . fields . push ( field ) ;
167+
168+ if ( d . reference_definition ) {
169+ addRelationshipFromReferenceDef (
170+ table ,
171+ field . name ,
172+ d . reference_definition ,
173+ ) ;
174+ }
116175 } else if ( d . resource === "constraint" ) {
117176 if ( d . constraint_type === "primary key" ) {
118177 d . definition . forEach ( ( c ) => {
@@ -123,57 +182,11 @@ export function fromSQLite(ast, diagramDb = DB.GENERIC) {
123182 } ) ;
124183 } ) ;
125184 } else if ( d . constraint_type . toLowerCase ( ) === "foreign key" ) {
126- const relationship = { } ;
127- const startTableId = table . id ;
128- const startTable = e . table [ 0 ] . table ;
129- const startField = d . definition [ 0 ] . column ;
130- const endTable = d . reference_definition . table [ 0 ] . table ;
131- const endField = d . reference_definition . definition [ 0 ] . column ;
132-
133- const endTableId = tables . findIndex ( ( t ) => t . name === endTable ) ;
134- if ( endTableId === - 1 ) return ;
135-
136- const endFieldId = tables [ endTableId ] . fields . findIndex (
137- ( f ) => f . name === endField ,
185+ addRelationshipFromReferenceDef (
186+ table ,
187+ d . definition [ 0 ] . column ,
188+ d . reference_definition ,
138189 ) ;
139- if ( endFieldId === - 1 ) return ;
140-
141- const startFieldId = table . fields . findIndex (
142- ( f ) => f . name === startField ,
143- ) ;
144- if ( startFieldId === - 1 ) return ;
145-
146- relationship . name = startTable + "_" + startField + "_fk" ;
147- relationship . startTableId = startTableId ;
148- relationship . endTableId = endTableId ;
149- relationship . endFieldId = endFieldId ;
150- relationship . startFieldId = startFieldId ;
151- let updateConstraint = "No action" ;
152- let deleteConstraint = "No action" ;
153- d . reference_definition . on_action . forEach ( ( c ) => {
154- if ( c . type === "on update" ) {
155- updateConstraint = c . value . value ;
156- updateConstraint =
157- updateConstraint [ 0 ] . toUpperCase ( ) +
158- updateConstraint . substring ( 1 ) ;
159- } else if ( c . type === "on delete" ) {
160- deleteConstraint = c . value . value ;
161- deleteConstraint =
162- deleteConstraint [ 0 ] . toUpperCase ( ) +
163- deleteConstraint . substring ( 1 ) ;
164- }
165- } ) ;
166-
167- relationship . updateConstraint = updateConstraint ;
168- relationship . deleteConstraint = deleteConstraint ;
169-
170- if ( table . fields [ startFieldId ] . unique ) {
171- relationship . cardinality = Cardinality . ONE_TO_ONE ;
172- } else {
173- relationship . cardinality = Cardinality . MANY_TO_ONE ;
174- }
175-
176- relationships . push ( relationship ) ;
177190 }
178191 }
179192 } ) ;
0 commit comments