@@ -298,6 +298,40 @@ func splitColStr(colStr string) []string {
298298 return results
299299}
300300
301+ func parseString (colStr string ) (* core.Column , error ) {
302+ fields := splitColStr (colStr )
303+ col := new (core.Column )
304+ col .Indexes = make (map [string ]int )
305+ col .Nullable = true
306+ col .DefaultIsEmpty = true
307+
308+ for idx , field := range fields {
309+ if idx == 0 {
310+ col .Name = strings .Trim (strings .Trim (field , "`[] " ), `"` )
311+ continue
312+ } else if idx == 1 {
313+ col .SQLType = core.SQLType {Name : field , DefaultLength : 0 , DefaultLength2 : 0 }
314+ continue
315+ }
316+ switch field {
317+ case "PRIMARY" :
318+ col .IsPrimaryKey = true
319+ case "AUTOINCREMENT" :
320+ col .IsAutoIncrement = true
321+ case "NULL" :
322+ if fields [idx - 1 ] == "NOT" {
323+ col .Nullable = false
324+ } else {
325+ col .Nullable = true
326+ }
327+ case "DEFAULT" :
328+ col .Default = fields [idx + 1 ]
329+ col .DefaultIsEmpty = false
330+ }
331+ }
332+ return col , nil
333+ }
334+
301335func (db * sqlite3 ) GetColumns (tableName string ) ([]string , map [string ]* core.Column , error ) {
302336 args := []interface {}{tableName }
303337 s := "SELECT sql FROM sqlite_master WHERE type='table' and name = ?"
@@ -327,6 +361,7 @@ func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Colu
327361 colCreates := reg .FindAllString (name [nStart + 1 :nEnd ], - 1 )
328362 cols := make (map [string ]* core.Column )
329363 colSeq := make ([]string , 0 )
364+
330365 for _ , colStr := range colCreates {
331366 reg = regexp .MustCompile (`,\s` )
332367 colStr = reg .ReplaceAllString (colStr , "," )
@@ -343,35 +378,11 @@ func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Colu
343378 continue
344379 }
345380
346- fields := splitColStr (colStr )
347- col := new (core.Column )
348- col .Indexes = make (map [string ]int )
349- col .Nullable = true
350- col .DefaultIsEmpty = true
351-
352- for idx , field := range fields {
353- if idx == 0 {
354- col .Name = strings .Trim (strings .Trim (field , "`[] " ), `"` )
355- continue
356- } else if idx == 1 {
357- col .SQLType = core.SQLType {Name : field , DefaultLength : 0 , DefaultLength2 : 0 }
358- }
359- switch field {
360- case "PRIMARY" :
361- col .IsPrimaryKey = true
362- case "AUTOINCREMENT" :
363- col .IsAutoIncrement = true
364- case "NULL" :
365- if fields [idx - 1 ] == "NOT" {
366- col .Nullable = false
367- } else {
368- col .Nullable = true
369- }
370- case "DEFAULT" :
371- col .Default = fields [idx + 1 ]
372- col .DefaultIsEmpty = false
373- }
381+ col , err := parseString (colStr )
382+ if err != nil {
383+ return colSeq , cols , err
374384 }
385+
375386 cols [col .Name ] = col
376387 colSeq = append (colSeq , col .Name )
377388 }
0 commit comments