Skip to content

Commit

Permalink
Adding all the lob types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Sep 10, 2024
1 parent 479b86e commit ab96196
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 65 deletions.
2 changes: 1 addition & 1 deletion lib/mysql/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func parseColumnDataType(originalS string) (DataType, *Opts, error) {
return Binary, nil, nil
case "varbinary":
return Varbinary, nil, nil
case "blob":
case "blob", "tinyblob", "mediumblob", "longblob":
return Blob, nil, nil
case "text":
return Text, nil, nil
Expand Down
126 changes: 62 additions & 64 deletions lib/mysql/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,82 +15,80 @@ func TestQuoteIdentifier(t *testing.T) {
}

func TestParseColumnDataType(t *testing.T) {
testCases := []struct {
input string
expectedType DataType
expectedOpts *Opts
expectedErr string
}{
{
input: "int",
expectedType: Int,
},
{
input: "tinyint(1)",
expectedType: Boolean,
},
{
input: "varchar(255)",
expectedType: Varchar,
expectedOpts: &Opts{Size: ptr.ToInt(255)},
},
{
// Invalid
{
input: "decimal(5,2)",
expectedType: Decimal,
expectedOpts: &Opts{
Precision: ptr.ToInt(5),
Scale: ptr2.ToUint16(2),
},
},
_, _, err := parseColumnDataType("int(10 unsigned")
assert.ErrorContains(t, err, `malformed data type: "int(10 unsigned"`)
}
{
input: "int(10) unsigned",
expectedType: BigInt,
expectedOpts: nil,
},
_, _, err := parseColumnDataType("foo")
assert.ErrorContains(t, err, `unknown data type: "foo"`)
}
{
input: "tinyint unsigned",
expectedType: SmallInt,
expectedOpts: nil,
},
_, _, err := parseColumnDataType("varchar(")
assert.ErrorContains(t, err, `malformed data type: "varchar("`)
}
}
{
// Integers
{
input: "smallint unsigned",
expectedType: Int,
expectedOpts: nil,
},
// int
dataType, _, err := parseColumnDataType("int")
assert.NoError(t, err)
assert.Equal(t, Int, dataType)
}
{
input: "mediumint unsigned",
expectedType: Int,
expectedOpts: nil,
},
// int unsigned
dataType, _, err := parseColumnDataType("int unsigned")
assert.NoError(t, err)
assert.Equal(t, BigInt, dataType)
}
{
input: "int unsigned",
expectedType: BigInt,
expectedOpts: nil,
},
// int(10) unsigned
dataType, _, err := parseColumnDataType("int(10) unsigned")
assert.NoError(t, err)
assert.Equal(t, BigInt, dataType)
}
{
input: "int(10 unsigned",
expectedErr: `malformed data type: "int(10 unsigned"`,
},
// tinyint
dataType, _, err := parseColumnDataType("tinyint")
assert.NoError(t, err)
assert.Equal(t, TinyInt, dataType)
}
{
input: "foo",
expectedErr: `unknown data type: "foo"`,
},
// tinyint unsigned
dataType, _, err := parseColumnDataType("tinyint unsigned")
assert.NoError(t, err)
assert.Equal(t, SmallInt, dataType)
}
{
input: "varchar(",
expectedErr: `malformed data type: "varchar("`,
},
}

for _, testCase := range testCases {
colType, opts, err := parseColumnDataType(testCase.input)
if testCase.expectedErr == "" {
// mediumint unsigned
dataType, _, err := parseColumnDataType("mediumint unsigned")
assert.NoError(t, err)
assert.Equal(t, testCase.expectedType, colType, testCase.input)
assert.Equal(t, testCase.expectedOpts, opts, testCase.input)
} else {
assert.ErrorContains(t, err, testCase.expectedErr, testCase.input)
assert.Equal(t, Int, dataType)
}
}
{
// tinyint(1) or boolean
dataType, _, err := parseColumnDataType("tinyint(1)")
assert.NoError(t, err)
assert.Equal(t, Boolean, dataType)
}
{
// String
dataType, opts, err := parseColumnDataType("varchar(255)")
assert.NoError(t, err)
assert.Equal(t, Varchar, dataType)
assert.Equal(t, &Opts{Size: ptr.ToInt(255)}, opts)
}
{
// Decimal
dataType, opts, err := parseColumnDataType("decimal(5,2)")
assert.NoError(t, err)
assert.Equal(t, Decimal, dataType)
assert.Equal(t, &Opts{Precision: ptr.ToInt(5), Scale: ptr2.ToUint16(2)}, opts)
}
}

func TestBuildPkValuesQuery(t *testing.T) {
Expand Down

0 comments on commit ab96196

Please sign in to comment.