Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions ddlmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ var (
checkRegexp = regexp.MustCompile(`^(?i)CHECK[\s]*\(`)
constraintRegexp = regexp.MustCompile(fmt.Sprintf(`^(?i)CONSTRAINT\s+%[1]s[\w\d_]+%[1]s[\s]+`, sqliteColumnQuote))
separatorRegexp = regexp.MustCompile(fmt.Sprintf("[%v]", sqliteSeparator))
columnRegexp = regexp.MustCompile(fmt.Sprintf(`^[%v]?([\w\d]+)[%v]?\s+([\w\(\)\d]+)(.*)$`, sqliteSeparator, sqliteSeparator))
columnRegexp = regexp.MustCompile(fmt.Sprintf(`^[%v]?([\w\d]+)[%v]?\s+(\w+(?:\([^)]*\))?)(.*)$`, sqliteSeparator, sqliteSeparator))
defaultValueRegexp = regexp.MustCompile(`(?i) DEFAULT \(?(.+)?\)?( |COLLATE|GENERATED|$)`)
regRealDataType = regexp.MustCompile(`[^\d](\d+)[^\d]?`)
typeSizeRegexp = regexp.MustCompile(`\((\d+)\s*(?:,\s*(\d+))?\)$`)
)

type ddl struct {
Expand Down Expand Up @@ -160,12 +160,17 @@ func parseDDL(strs ...string) (*ddl, error) {
}
}

// data type length
matches := regRealDataType.FindAllStringSubmatch(columnType.DataTypeValue.String, -1)
if len(matches) == 1 && len(matches[0]) == 2 {
size, _ := strconv.Atoi(matches[0][1])
columnType.LengthValue = sql.NullInt64{Valid: true, Int64: int64(size)}
columnType.DataTypeValue.String = strings.TrimSuffix(columnType.DataTypeValue.String, matches[0][0])
// data type length / precision, e.g. varchar(10), decimal(10,2)
if sizeMatches := typeSizeRegexp.FindStringSubmatch(columnType.DataTypeValue.String); sizeMatches != nil {
size, _ := strconv.Atoi(sizeMatches[1])
if sizeMatches[2] != "" {
scale, _ := strconv.Atoi(sizeMatches[2])
columnType.DecimalSizeValue = sql.NullInt64{Valid: true, Int64: int64(size)}
columnType.ScaleValue = sql.NullInt64{Valid: true, Int64: int64(scale)}
} else {
columnType.LengthValue = sql.NullInt64{Valid: true, Int64: int64(size)}
}
columnType.DataTypeValue.String = strings.TrimSuffix(columnType.DataTypeValue.String, sizeMatches[0])
}

result.columns = append(result.columns, columnType)
Expand Down
27 changes: 27 additions & 0 deletions ddlmod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,30 @@ func TestGetColumns(t *testing.T) {
})
}
}

func TestParseDDL_TypePrecision(t *testing.T) {
d, err := parseDDL("CREATE TABLE `t` (`p` decimal(10,2), `s` decimal(10, 2), `v` varchar(25))")
if err != nil {
t.Fatal(err)
}
for _, c := range d.columns {
switch c.NameValue.String {
case "p", "s":
if c.DataTypeValue.String != "decimal" {
t.Errorf("%s: DataType = %q, want decimal", c.NameValue.String, c.DataTypeValue.String)
}
precision, scale, ok := c.DecimalSize()
if !ok || precision != 10 || scale != 2 {
t.Errorf("%s: DecimalSize = (%d,%d,%v), want (10,2,true)", c.NameValue.String, precision, scale, ok)
}
case "v":
if length, ok := c.Length(); !ok || length != 25 {
t.Errorf("v: Length = (%d,%v), want (25,true)", length, ok)
}
}
}
// the full column type is preserved for the first form
if ct, _ := d.columns[0].ColumnType(); ct != "decimal(10,2)" {
t.Errorf("ColumnType = %q, want decimal(10,2)", ct)
}
}
Loading