Skip to content

Commit

Permalink
BREAKING CHANGE: remove unnecessary functions and refactor MySQL co…
Browse files Browse the repository at this point in the history
…degen
  • Loading branch information
si3nloong committed Sep 25, 2024
1 parent dd75360 commit 4902e39
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 418 deletions.
141 changes: 35 additions & 106 deletions codegen/dialect/mysql/column_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (s *mysqlDriver) ColumnDataTypes() map[string]*dialect.ColumnType {
return map[string]*dialect.ColumnType{
dataTypes := map[string]*dialect.ColumnType{
"rune": {
DataType: s.columnDataType("CHAR(1)"),
Valuer: "(string)({{goPath}})",
Expand All @@ -26,56 +26,6 @@ func (s *mysqlDriver) ColumnDataTypes() map[string]*dialect.ColumnType {
Valuer: "(bool)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Bool({{addrOfGoPath}})",
},
"int": {
DataType: s.columnDataType("INTEGER", int64(0)),
Valuer: "(int64)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
},
"int8": {
DataType: s.columnDataType("TINYINT", int64(0)),
Valuer: "(int64)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
},
"int16": {
DataType: s.columnDataType("SMALLINT", int64(0)),
Valuer: "(int64)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
},
"int32": {
DataType: s.columnDataType("MEDIUMINT", int64(0)),
Valuer: "(int64)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
},
"int64": {
DataType: s.columnDataType("BIGINT", int64(0)),
Valuer: "(int64)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
},
"uint": {
DataType: s.columnDataType("INTEGER UNSIGNED", int64(0)),
Valuer: "(int64)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
},
"uint8": {
DataType: s.columnDataType("TINYINT UNSIGNED", int64(0)),
Valuer: "(int64)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
},
"uint16": {
DataType: s.columnDataType("SMALLINT UNSIGNED", int64(0)),
Valuer: "(int64)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
},
"uint32": {
DataType: s.columnDataType("MEDIUMINT UNSIGNED", int64(0)),
Valuer: "(int64)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
},
"uint64": {
DataType: s.columnDataType("BIGINT UNSIGNED", int64(0)),
Valuer: "(int64)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
},
"float32": {
DataType: s.columnDataType("FLOAT", int64(0)),
Valuer: "(float64)({{goPath}})",
Expand All @@ -94,80 +44,32 @@ func (s *mysqlDriver) ColumnDataTypes() map[string]*dialect.ColumnType {
"*string": {
DataType: s.columnDataType("VARCHAR(255)"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.String({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfString({{addrOfGoPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.String({{addrOfGoPath}})",
},
"*[]byte": {
DataType: s.columnDataType("BLOB"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.String({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfString({{addrOfGoPath}})"},
Scanner: "github.com/si3nloong/sqlgen/sequel/types.String({{addrOfGoPath}})",
},
"*bool": {
DataType: s.columnDataType("BOOL"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Bool({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfBool({{addrOfGoPath}})"},
"*uint": {
DataType: s.columnDataType("INTEGER UNSIGNED"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Integer({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfInt({{addrOfGoPath}})",
},
"*uint8": {
DataType: s.columnDataType("TINYINT UNSIGNED"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Integer({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfInt({{addrOfGoPath}})",
},
"*uint16": {
DataType: s.columnDataType("SMALLINT UNSIGNED"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Integer({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfInt({{addrOfGoPath}})",
},
"*uint32": {
DataType: s.columnDataType("MEDIUMINT UNSIGNED"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Integer({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfInt({{addrOfGoPath}})",
},
"*uint64": {
DataType: s.columnDataType("BIGINT UNSIGNED"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Integer({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfInt({{addrOfGoPath}})",
},
"*int": {
DataType: s.columnDataType("INTEGER"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Integer({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfInt({{addrOfGoPath}})",
},
"*int8": {
DataType: s.columnDataType("TINYINT"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Integer({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfInt({{addrOfGoPath}})",
},
"*int16": {
DataType: s.columnDataType("SMALLINT"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Integer({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfInt({{addrOfGoPath}})",
},
"*int32": {
DataType: s.columnDataType("MEDIUMINT"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Integer({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfInt({{addrOfGoPath}})",
},
"*int64": {
DataType: s.columnDataType("BIGINT"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Integer({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfInt({{addrOfGoPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Bool({{addrOfGoPath}})",
},
"*float32": {
DataType: s.columnDataType("FLOAT"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Float({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfFloat({{addrOfGoPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Float({{addrOfGoPath}})",
},
"*float64": {
DataType: s.columnDataType("FLOAT"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Float({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfFloat({{addrOfGoPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Float({{addrOfGoPath}})",
},
"*time.Time": {
DataType: s.columnDataType("TIMESTAMP"),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Time({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.PtrOfTime({{addrOfGoPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Time({{addrOfGoPath}})",
},
"sql.RawBytes": {
DataType: s.columnDataType("TEXT"),
Expand Down Expand Up @@ -274,6 +176,33 @@ func (s *mysqlDriver) ColumnDataTypes() map[string]*dialect.ColumnType {
Scanner: "github.com/si3nloong/sqlgen/sequel/types.JSONUnmarshaler({{addrOfGoPath}})",
},
}
s.mapIntegers(dataTypes)
return dataTypes
}

func (s *mysqlDriver) mapIntegers(dict map[string]*dialect.ColumnType) {
types := [][2]string{
{"int", "INTEGER"}, {"int8", "TINYINT"}, {"int16", "SMALLINT"}, {"int32", "MEDIUMINT"}, {"int64", "BIGINT"},
{"uint", "INTEGER UNSIGNED"}, {"uint8", "TINYINT UNSIGNED"}, {"uint16", "SMALLINT UNSIGNED"}, {"uint32", "MEDIUMINT UNSIGNED"}, {"uint64", "BIGINT UNSIGNED"},
}
for _, t := range types {
dict[t[0]] = &dialect.ColumnType{
DataType: s.columnDataType(t[1], int64(0)),
Valuer: "(int64)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
}
}
types = [][2]string{
{"*int", "INTEGER"}, {"*int8", "TINYINT"}, {"*int16", "SMALLINT"}, {"*int32", "MEDIUMINT"}, {"*int64", "BIGINT"},
{"*uint", "INTEGER UNSIGNED"}, {"*uint8", "TINYINT UNSIGNED"}, {"*uint16", "SMALLINT UNSIGNED"}, {"*uint32", "MEDIUMINT UNSIGNED"}, {"*uint64", "BIGINT UNSIGNED"},
}
for _, t := range types {
dict[t[0]] = &dialect.ColumnType{
DataType: s.columnDataType(t[1], int64(0)),
Valuer: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Integer({{addrOfGoPath}})",
}
}
}

func (*mysqlDriver) columnDataType(dataType string, defaultValue ...any) func(dialect.GoColumn) string {
Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/gofrs/uuid/v5 v5.0.0
github.com/google/uuid v1.6.0
github.com/jaswdr/faker v1.16.0
github.com/lib/pq v1.10.7
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v1.14.16
github.com/paulmach/orb v0.11.1
github.com/si3nloong/sqlgen v1.0.0-alpha.3.0.20231118095154-390f9683bb93
Expand Down
4 changes: 2 additions & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
Expand Down
32 changes: 16 additions & 16 deletions examples/testcase/struct-field/pointer/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,67 +28,67 @@ func (v *Ptr) Addrs() []any {
if v.Str == nil {
v.Str = new(string)
}
addrs[1] = types.PtrOfString(&v.Str)
addrs[1] = types.String(v.Str)
if v.Bytes == nil {
v.Bytes = new([]byte)
}
addrs[2] = types.PtrOfString(&v.Bytes)
addrs[2] = types.String(v.Bytes)
if v.Bool == nil {
v.Bool = new(bool)
}
addrs[3] = types.PtrOfBool(&v.Bool)
addrs[3] = types.Bool(v.Bool)
if v.Int == nil {
v.Int = new(int)
}
addrs[4] = types.PtrOfInt(&v.Int)
addrs[4] = types.Integer(v.Int)
if v.Int8 == nil {
v.Int8 = new(int8)
}
addrs[5] = types.PtrOfInt(&v.Int8)
addrs[5] = types.Integer(v.Int8)
if v.Int16 == nil {
v.Int16 = new(int16)
}
addrs[6] = types.PtrOfInt(&v.Int16)
addrs[6] = types.Integer(v.Int16)
if v.Int32 == nil {
v.Int32 = new(int32)
}
addrs[7] = types.PtrOfInt(&v.Int32)
addrs[7] = types.Integer(v.Int32)
if v.Int64 == nil {
v.Int64 = new(int64)
}
addrs[8] = types.PtrOfInt(&v.Int64)
addrs[8] = types.Integer(v.Int64)
if v.Uint == nil {
v.Uint = new(uint)
}
addrs[9] = types.PtrOfInt(&v.Uint)
addrs[9] = types.Integer(v.Uint)
if v.Uint8 == nil {
v.Uint8 = new(uint8)
}
addrs[10] = types.PtrOfInt(&v.Uint8)
addrs[10] = types.Integer(v.Uint8)
if v.Uint16 == nil {
v.Uint16 = new(uint16)
}
addrs[11] = types.PtrOfInt(&v.Uint16)
addrs[11] = types.Integer(v.Uint16)
if v.Uint32 == nil {
v.Uint32 = new(uint32)
}
addrs[12] = types.PtrOfInt(&v.Uint32)
addrs[12] = types.Integer(v.Uint32)
if v.Uint64 == nil {
v.Uint64 = new(uint64)
}
addrs[13] = types.PtrOfInt(&v.Uint64)
addrs[13] = types.Integer(v.Uint64)
if v.F32 == nil {
v.F32 = new(float32)
}
addrs[14] = types.PtrOfFloat(&v.F32)
addrs[14] = types.Float(v.F32)
if v.F64 == nil {
v.F64 = new(float64)
}
addrs[15] = types.PtrOfFloat(&v.F64)
addrs[15] = types.Float(v.F64)
if v.Time == nil {
v.Time = new(time.Time)
}
addrs[16] = types.PtrOfTime(&v.Time)
addrs[16] = types.Time(v.Time)
return addrs
}
func (Ptr) InsertPlaceholders(row int) string {
Expand Down
32 changes: 16 additions & 16 deletions examples/testcase/struct-field/pointer/generated.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,67 +28,67 @@ func (v *Ptr) Addrs() []any {
if v.Str == nil {
v.Str = new(string)
}
addrs[1] = types.PtrOfString(&v.Str)
addrs[1] = types.String(v.Str)
if v.Bytes == nil {
v.Bytes = new([]byte)
}
addrs[2] = types.PtrOfString(&v.Bytes)
addrs[2] = types.String(v.Bytes)
if v.Bool == nil {
v.Bool = new(bool)
}
addrs[3] = types.PtrOfBool(&v.Bool)
addrs[3] = types.Bool(v.Bool)
if v.Int == nil {
v.Int = new(int)
}
addrs[4] = types.PtrOfInt(&v.Int)
addrs[4] = types.Integer(v.Int)
if v.Int8 == nil {
v.Int8 = new(int8)
}
addrs[5] = types.PtrOfInt(&v.Int8)
addrs[5] = types.Integer(v.Int8)
if v.Int16 == nil {
v.Int16 = new(int16)
}
addrs[6] = types.PtrOfInt(&v.Int16)
addrs[6] = types.Integer(v.Int16)
if v.Int32 == nil {
v.Int32 = new(int32)
}
addrs[7] = types.PtrOfInt(&v.Int32)
addrs[7] = types.Integer(v.Int32)
if v.Int64 == nil {
v.Int64 = new(int64)
}
addrs[8] = types.PtrOfInt(&v.Int64)
addrs[8] = types.Integer(v.Int64)
if v.Uint == nil {
v.Uint = new(uint)
}
addrs[9] = types.PtrOfInt(&v.Uint)
addrs[9] = types.Integer(v.Uint)
if v.Uint8 == nil {
v.Uint8 = new(uint8)
}
addrs[10] = types.PtrOfInt(&v.Uint8)
addrs[10] = types.Integer(v.Uint8)
if v.Uint16 == nil {
v.Uint16 = new(uint16)
}
addrs[11] = types.PtrOfInt(&v.Uint16)
addrs[11] = types.Integer(v.Uint16)
if v.Uint32 == nil {
v.Uint32 = new(uint32)
}
addrs[12] = types.PtrOfInt(&v.Uint32)
addrs[12] = types.Integer(v.Uint32)
if v.Uint64 == nil {
v.Uint64 = new(uint64)
}
addrs[13] = types.PtrOfInt(&v.Uint64)
addrs[13] = types.Integer(v.Uint64)
if v.F32 == nil {
v.F32 = new(float32)
}
addrs[14] = types.PtrOfFloat(&v.F32)
addrs[14] = types.Float(v.F32)
if v.F64 == nil {
v.F64 = new(float64)
}
addrs[15] = types.PtrOfFloat(&v.F64)
addrs[15] = types.Float(v.F64)
if v.Time == nil {
v.Time = new(time.Time)
}
addrs[16] = types.PtrOfTime(&v.Time)
addrs[16] = types.Time(v.Time)
return addrs
}
func (Ptr) InsertPlaceholders(row int) string {
Expand Down
2 changes: 1 addition & 1 deletion examples/testcase/struct-field/sql/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (v *AutoPkLocation) Addrs() []any {
if v.PtrGeoPoint == nil {
v.PtrGeoPoint = new(orb.Point)
}
addrs[2] = types.JSONUnmarshaler(&v.PtrGeoPoint)
addrs[2] = types.JSONUnmarshaler(v.PtrGeoPoint)
if v.PtrUUID == nil {
v.PtrUUID = new(uuid.UUID)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/testcase/struct-field/sql/generated.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (v *AutoPkLocation) Addrs() []any {
if v.PtrGeoPoint == nil {
v.PtrGeoPoint = new(orb.Point)
}
addrs[2] = types.JSONUnmarshaler(&v.PtrGeoPoint)
addrs[2] = types.JSONUnmarshaler(v.PtrGeoPoint)
if v.PtrUUID == nil {
v.PtrUUID = new(uuid.UUID)
}
Expand Down
Loading

0 comments on commit 4902e39

Please sign in to comment.