@@ -80,6 +80,7 @@ export class SqlSimpleParser {
80
80
const removedComments = chunk
81
81
// remove database comments, multiline, --, and //
82
82
. replace ( / \/ \* [ \s \S ] * ?\* \/ | \/ \/ | - - .* / g, "" )
83
+ . replace ( / I F N O T E X I S T S / gi, "" )
83
84
. trim ( ) ;
84
85
const cleanedLines = removedComments
85
86
. split ( "\n" )
@@ -92,20 +93,38 @@ export class SqlSimpleParser {
92
93
const lines : string [ ] = [ ] ;
93
94
let insertSameLine = false ;
94
95
cleanedLines . forEach ( ( n ) => {
95
- if (
96
- ( lines . length > 0 &&
97
- n [ 0 ] == "(" &&
98
- lines [ lines . length - 1 ] . toLocaleLowerCase ( ) . indexOf ( CreateTable ) ==
99
- - 1 ) ||
100
- insertSameLine
101
- ) {
102
- if ( lines . length > 0 ) {
103
- insertSameLine = true ;
104
- lines [ lines . length - 1 ] += n ;
105
- if ( n [ 0 ] == ")" ) insertSameLine = false ;
106
- }
107
- } else {
108
- lines . push ( n ) ;
96
+ if ( lines . length > 0 ) {
97
+ if ( ( n [ 0 ] == "(" &&
98
+ lines [ lines . length - 1 ] . toLocaleLowerCase ( ) . indexOf ( CreateTable ) ==
99
+ - 1 ) ||
100
+ insertSameLine ) {
101
+ if ( lines . length > 0 ) {
102
+ insertSameLine = true ;
103
+ lines [ lines . length - 1 ] += ` ${ n } ` ;
104
+ if ( n [ 0 ] == ")" )
105
+ insertSameLine = false ;
106
+ }
107
+ }
108
+ else if ( lines [ lines . length - 1 ] . match ( / C O N S T R A I N T / gi) &&
109
+ ( n . match ( / F O R E I G N K E Y / gi) && ! n . match ( / C O N S T R A I N T / gi) )
110
+ ) {
111
+ lines [ lines . length - 1 ] += ` ${ n } ` ;
112
+ }
113
+ // add to previous line if current has references and previous has foreign key
114
+ else if ( lines [ lines . length - 1 ] . match ( / F O R E I G N K E Y / gi) &&
115
+ ( n . match ( / R E F E R E N C E S / gi) && ! n . match ( / F O R E I G N K E Y / gi) )
116
+ ) {
117
+ lines [ lines . length - 1 ] += ` ${ n } ` ;
118
+ }
119
+ else if ( n . substring ( 0 , 2 ) . toUpperCase ( ) == "ON" ) {
120
+ lines [ lines . length - 1 ] += ` ${ n } ` ;
121
+ }
122
+ else {
123
+ lines . push ( n ) ;
124
+ }
125
+ }
126
+ else {
127
+ lines . push ( n ) ;
109
128
}
110
129
} ) ;
111
130
// dx = 0,
@@ -543,7 +562,8 @@ export class SqlSimpleParser {
543
562
544
563
private ParseMySQLForeignKey ( name : string , currentTableModel : TableModel ) {
545
564
const referencesIndex = name . toLowerCase ( ) . indexOf ( "references" ) ;
546
- const foreignKeySQL = name . substring ( 0 , referencesIndex ) ;
565
+ let foreignKeySQL = name . substring ( 0 , referencesIndex ) ;
566
+ foreignKeySQL = foreignKeySQL . substring ( foreignKeySQL . toUpperCase ( ) . indexOf ( "FOREIGN KEY" ) ) ;
547
567
let referencesSQL = name . substring ( referencesIndex , name . length ) ;
548
568
549
569
//Remove references syntax
0 commit comments