diff --git a/go.mod b/go.mod index 577afdd..c1f51cf 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/sirupsen/logrus v1.9.3 github.com/tyzbit/go-archive v0.0.0-20230720150823-69f1618c0490 gorm.io/driver/mysql v1.5.7 - gorm.io/driver/sqlite v1.5.6 + gorm.io/driver/sqlite v1.5.7 gorm.io/gorm v1.25.12 ) diff --git a/go.sum b/go.sum index fa963e0..fe48ff9 100644 --- a/go.sum +++ b/go.sum @@ -185,6 +185,8 @@ gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E= gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE= gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= +gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I= +gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A= gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= diff --git a/vendor/gorm.io/driver/sqlite/ddlmod.go b/vendor/gorm.io/driver/sqlite/ddlmod.go index 9c93e6a..c839cd7 100644 --- a/vendor/gorm.io/driver/sqlite/ddlmod.go +++ b/vendor/gorm.io/driver/sqlite/ddlmod.go @@ -17,23 +17,11 @@ var ( indexRegexp = regexp.MustCompile(fmt.Sprintf(`(?is)CREATE(?: UNIQUE)? INDEX [%v]?[\w\d-]+[%v]?(?s:.*?)ON (.*)$`, sqliteSeparator, sqliteSeparator)) tableRegexp = regexp.MustCompile(fmt.Sprintf(`(?is)(CREATE TABLE [%v]?[\w\d-]+[%v]?)(?:\s*\((.*)\))?`, sqliteSeparator, sqliteSeparator)) separatorRegexp = regexp.MustCompile(fmt.Sprintf("[%v]", sqliteSeparator)) - columnsRegexp = regexp.MustCompile(fmt.Sprintf(`[(,][%v]?(\w+)[%v]?`, sqliteSeparator, sqliteSeparator)) columnRegexp = regexp.MustCompile(fmt.Sprintf(`^[%v]?([\w\d]+)[%v]?\s+([\w\(\)\d]+)(.*)$`, sqliteSeparator, sqliteSeparator)) defaultValueRegexp = regexp.MustCompile(`(?i) DEFAULT \(?(.+)?\)?( |COLLATE|GENERATED|$)`) regRealDataType = regexp.MustCompile(`[^\d](\d+)[^\d]?`) ) -func getAllColumns(s string) []string { - allMatches := columnsRegexp.FindAllStringSubmatch(s, -1) - columns := make([]string, 0, len(allMatches)) - for _, matches := range allMatches { - if len(matches) > 1 { - columns = append(columns, matches[1]) - } - } - return columns -} - type ddl struct { head string fields []string @@ -110,9 +98,10 @@ func parseDDL(strs ...string) (*ddl, error) { if strings.HasPrefix(fUpper, "CONSTRAINT") { matches := uniqueRegexp.FindStringSubmatch(f) if len(matches) > 0 { - if columns := getAllColumns(matches[1]); len(columns) == 1 { + cols, err := parseAllColumns(matches[1]) + if err == nil && len(cols) == 1 { for idx, column := range result.columns { - if column.NameValue.String == columns[0] { + if column.NameValue.String == cols[0] { column.UniqueValue = sql.NullBool{Bool: true, Valid: true} result.columns[idx] = column break @@ -123,12 +112,15 @@ func parseDDL(strs ...string) (*ddl, error) { continue } if strings.HasPrefix(fUpper, "PRIMARY KEY") { - for _, name := range getAllColumns(f) { - for idx, column := range result.columns { - if column.NameValue.String == name { - column.PrimaryKeyValue = sql.NullBool{Bool: true, Valid: true} - result.columns[idx] = column - break + cols, err := parseAllColumns(f) + if err == nil { + for _, name := range cols { + for idx, column := range result.columns { + if column.NameValue.String == name { + column.PrimaryKeyValue = sql.NullBool{Bool: true, Valid: true} + result.columns[idx] = column + break + } } } } diff --git a/vendor/gorm.io/driver/sqlite/ddlmod_parse_all_columns.go b/vendor/gorm.io/driver/sqlite/ddlmod_parse_all_columns.go new file mode 100644 index 0000000..760acf8 --- /dev/null +++ b/vendor/gorm.io/driver/sqlite/ddlmod_parse_all_columns.go @@ -0,0 +1,117 @@ +package sqlite + +import ( + "errors" + "fmt" +) + +type parseAllColumnsState int + +const ( + parseAllColumnsState_NONE parseAllColumnsState = iota + parseAllColumnsState_Beginning + parseAllColumnsState_ReadingRawName + parseAllColumnsState_ReadingQuotedName + parseAllColumnsState_EndOfName + parseAllColumnsState_State_End +) + +func parseAllColumns(in string) ([]string, error) { + s := []rune(in) + columns := make([]string, 0) + state := parseAllColumnsState_NONE + quote := rune(0) + name := make([]rune, 0) + for i := 0; i < len(s); i++ { + switch state { + case parseAllColumnsState_NONE: + if s[i] == '(' { + state = parseAllColumnsState_Beginning + } + case parseAllColumnsState_Beginning: + if isSpace(s[i]) { + continue + } + if isQuote(s[i]) { + state = parseAllColumnsState_ReadingQuotedName + quote = s[i] + continue + } + if s[i] == '[' { + state = parseAllColumnsState_ReadingQuotedName + quote = ']' + continue + } else if s[i] == ')' { + return columns, fmt.Errorf("unexpected token: %s", string(s[i])) + } + state = parseAllColumnsState_ReadingRawName + name = append(name, s[i]) + case parseAllColumnsState_ReadingRawName: + if isSeparator(s[i]) { + state = parseAllColumnsState_Beginning + columns = append(columns, string(name)) + name = make([]rune, 0) + continue + } + if s[i] == ')' { + state = parseAllColumnsState_State_End + columns = append(columns, string(name)) + } + if isQuote(s[i]) { + return nil, fmt.Errorf("unexpected token: %s", string(s[i])) + } + if isSpace(s[i]) { + state = parseAllColumnsState_EndOfName + columns = append(columns, string(name)) + name = make([]rune, 0) + continue + } + name = append(name, s[i]) + case parseAllColumnsState_ReadingQuotedName: + if s[i] == quote { + // check if quote character is escaped + if i+1 < len(s) && s[i+1] == quote { + name = append(name, quote) + i++ + continue + } + state = parseAllColumnsState_EndOfName + columns = append(columns, string(name)) + name = make([]rune, 0) + continue + } + name = append(name, s[i]) + case parseAllColumnsState_EndOfName: + if isSpace(s[i]) { + continue + } + if isSeparator(s[i]) { + state = parseAllColumnsState_Beginning + continue + } + if s[i] == ')' { + state = parseAllColumnsState_State_End + continue + } + return nil, fmt.Errorf("unexpected token: %s", string(s[i])) + case parseAllColumnsState_State_End: + break + } + } + if state != parseAllColumnsState_State_End { + return nil, errors.New("unexpected end") + } + return columns, nil +} + +func isSpace(r rune) bool { + return r == ' ' || r == '\t' +} + +func isQuote(r rune) bool { + return r == '`' || r == '"' || r == '\'' +} + +func isSeparator(r rune) bool { + return r == ',' +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 61d68e9..028a6fb 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -231,7 +231,7 @@ gopkg.in/yaml.v3 # gorm.io/driver/mysql v1.5.7 ## explicit; go 1.14 gorm.io/driver/mysql -# gorm.io/driver/sqlite v1.5.6 +# gorm.io/driver/sqlite v1.5.7 ## explicit; go 1.20 gorm.io/driver/sqlite # gorm.io/gorm v1.25.12