@@ -2,7 +2,6 @@ package internal
22
33import (
44 "cmp"
5- "fmt"
65 "regexp"
76 "slices"
87 "strings"
@@ -119,6 +118,7 @@ type TableContent struct {
119118type TableField struct {
120119 Name string
121120 Type string
121+ Ref string
122122 VarStart int
123123 VarEnd int
124124 TypeStart int
@@ -172,7 +172,11 @@ func findFields(table string, offset int) []TableField {
172172 }
173173
174174 if string (char ) == "," || pos == len (table )- 1 {
175- line := table [from :pos ]
175+ line := table [from : pos + 1 ]
176+ if string (char ) == "," || string (char ) == ";" {
177+ line = table [from :pos ]
178+ }
179+
176180 clean := strings .ToLower (strings .TrimSpace (line ))
177181 if strings .HasPrefix (clean , "primary" ) || strings .HasPrefix (clean , "constraint" ) || strings .HasPrefix (clean , "unique" ) || strings .HasPrefix (clean , "foreign" ) || strings .HasPrefix (clean , "key" ) {
178182 from = pos + 1
@@ -187,11 +191,13 @@ func findFields(table string, offset int) []TableField {
187191 data := TableField {
188192 Name : varname ,
189193 Type : vartype ,
194+ Ref : "" ,
190195 VarStart : offset + start ,
191196 VarEnd : offset + start + len (varname ),
192197 TypeStart : offset + start + len (varname ) + 1 ,
193198 TypeEnd : offset + from + len (line ),
194199 }
200+ data .Type , data .Ref = splitTypeRef (data .Type )
195201
196202 fields = append (fields , data )
197203 from = pos + 1
@@ -201,6 +207,22 @@ func findFields(table string, offset int) []TableField {
201207 return fields
202208}
203209
210+ func splitTypeRef (sql string ) (string , string ) {
211+ ref := - 1
212+ if r := strings .Index (strings .ToLower (sql ), " references " ); r > - 1 {
213+ ref = r
214+ }
215+ if r := strings .Index (strings .ToLower (sql ), " foreign key " ); r > - 1 && r < ref {
216+ ref = r
217+ }
218+
219+ if ref > - 1 {
220+ return sql [:ref ], sql [ref :]
221+ }
222+
223+ return sql , ""
224+ }
225+
204226func replaceMysqlTypes (sql string ) string {
205227 tables := findTableContents (sql )
206228 slices .Reverse (tables )
@@ -217,16 +239,17 @@ func replaceMysqlTypes(sql string) string {
217239 vartype = replaceMysqlFloatTypes (vartype )
218240 vartype = replaceMysqlTextTypes (vartype )
219241 vartype = replaceMysqlDataTypes (vartype )
242+ vartype = replaceMysqlDataSubtypes (vartype )
220243 vartype = replaceMysqlDateTypes (vartype )
221244 vartype = replaceMysqlEnumTypes (vartype )
222245 vartype = replaceMysqlComment (vartype )
223246 vartype = replaceMysqlNumIncrement (vartype )
224247
225- if field .Type != vartype && strings . Contains ( field . Type , "INCREMENT" ) {
226- fmt .Println ("Replace" , field .Type , "->" , vartype )
227- }
248+ // if field.Type != vartype {
249+ // fmt.Println("Replace", field.Type, "->", vartype)
250+ // }
228251
229- sql = sql [:field .VarStart ] + varName + " " + vartype + sql [field .TypeEnd :]
252+ sql = sql [:field .VarStart ] + varName + " " + vartype + " " + field . Ref + sql [field .TypeEnd :]
230253 }
231254 }
232255
@@ -321,6 +344,18 @@ func replaceMysqlDataTypes(sql string) string {
321344 return sql
322345}
323346
347+ var regLineTypeSubtype = regexp .MustCompile (`(?i)SUB_TYPE \w*` )
348+
349+ func replaceMysqlDataSubtypes (sql string ) string {
350+ matches := regLineTypeSubtype .FindAllStringSubmatchIndex (sql , - 1 )
351+ slices .Reverse (matches )
352+ for _ , match := range matches {
353+ sql = sql [:match [0 ]] + "" + sql [match [1 ]:]
354+ }
355+
356+ return sql
357+ }
358+
324359var regLineCascade = regexp .MustCompile (`(?i)\sON\s(DELETE|UPDATE)(\sSET)?\s\w*` )
325360
326361func replaceMysqlUpdate (sql string ) string {
0 commit comments