Cleanup / audit

This commit is contained in:
jdl
2026-06-25 13:35:41 +02:00
parent 9a0beaf9cb
commit 58adb4b2a6
8 changed files with 49 additions and 25 deletions

View File

@@ -77,7 +77,14 @@ func parseTable(driver string, schema *schema, tokens []string) ([]string, error
table.NoDelete = true
tokens = tokens[1:]
case "(":
return parseTableBody(table, tokens[1:])
tokens, err := parseTableBody(table, tokens[1:])
if err != nil {
return tokens, err
}
if len(table.pkCols()) == 0 {
return tokens, errors.New("table " + table.Name + " has no PK column")
}
return tokens, nil
default:
return tokens, errors.New("unexpected token in table definition: " + tokens[0])
}
@@ -87,17 +94,25 @@ func parseTable(driver string, schema *schema, tokens []string) ([]string, error
}
func parseTableBody(table *table, tokens []string) ([]string, error) {
var err error
if len(tokens) > 0 && tokens[0] == ")" {
return tokens, errors.New("empty table body")
}
for len(tokens) > 0 && tokens[0] != ";" {
var err error
for len(tokens) > 0 && tokens[0] != ")" {
tokens, err = parseTableColumn(table, tokens)
if err != nil {
return tokens, err
}
}
if len(tokens) < 1 || tokens[0] != ";" {
return tokens, errors.New("incomplete table column definitions")
if len(tokens) == 0 || tokens[0] != ")" {
return tokens, errors.New("missing closing ) in table definition")
}
tokens = tokens[1:] // consume ")"
if len(tokens) == 0 || tokens[0] != ";" {
return tokens, errors.New("missing ; after table definition")
}
return tokens[1:], nil
@@ -141,5 +156,10 @@ func parseTableColumn(table *table, tokens []string) ([]string, error) {
return tokens, errors.New("incomplete column definition")
}
return tokens[1:], nil
// Leave the closing ")" for parseTableBody; consume a "," separator.
// A trailing comma before ")" is allowed.
if tokens[0] == "," {
return tokens[1:], nil
}
return tokens, nil
}