diff --git a/examples/generated.go b/examples/generated.go new file mode 100644 index 0000000..acc0dc0 --- /dev/null +++ b/examples/generated.go @@ -0,0 +1,3 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package examples diff --git a/examples/go.mod b/examples/go.mod index 49ed2a5..99f173d 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -5,6 +5,7 @@ go 1.21 toolchain go1.21.3 require ( + cloud.google.com/go v0.110.10 github.com/go-sql-driver/mysql v1.7.0 github.com/google/uuid v1.4.0 github.com/jaswdr/faker v1.16.0 @@ -21,7 +22,7 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect golang.org/x/mod v0.12.0 // indirect - golang.org/x/sys v0.12.0 // indirect + golang.org/x/sys v0.13.0 // indirect golang.org/x/tools v0.13.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/examples/go.sum b/examples/go.sum index 8cc4c81..78c3164 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -1,7 +1,11 @@ +cloud.google.com/go v0.110.10 h1:LXy9GEO+timppncPIAZoOj3l58LIU9k+kn48AN7IO3Y= +cloud.google.com/go v0.110.10/go.mod h1:v1OoFqYxiBkUrruItNM3eT4lLByNjxmJSV/xDKJNnic= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jaswdr/faker v1.16.0 h1:5ZjusQbqIZwJnUymPirNKJI1yFCuozdSR9oeYPgD5Uk= @@ -24,8 +28,8 @@ golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/examples/testcase/schema/a/generated.go b/examples/testcase/schema/a/generated.go new file mode 100644 index 0000000..ada669f --- /dev/null +++ b/examples/testcase/schema/a/generated.go @@ -0,0 +1,3 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package a diff --git a/examples/testcase/schema/a/generated.go.tpl b/examples/testcase/schema/a/generated.go.tpl new file mode 100644 index 0000000..ada669f --- /dev/null +++ b/examples/testcase/schema/a/generated.go.tpl @@ -0,0 +1,3 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package a diff --git a/examples/testcase/schema/generated.go b/examples/testcase/schema/generated.go new file mode 100644 index 0000000..0bd96ee --- /dev/null +++ b/examples/testcase/schema/generated.go @@ -0,0 +1,118 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package schema + +import ( + "database/sql" + "database/sql/driver" + "time" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Model) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS model ();" +} +func (Model) AlterTableStmt() string { + return "ALTER TABLE model ;" +} +func (Model) TableName() string { + return "model" +} +func (Model) Columns() []string { + return []string{} +} +func (v Model) Values() []any { + return []any{} +} +func (v *Model) Addrs() []any { + return []any{} +} + +func (A) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS a (id VARCHAR(255) NOT NULL,created_at DATETIME NOT NULL);" +} +func (A) AlterTableStmt() string { + return "ALTER TABLE a MODIFY id VARCHAR(255) NOT NULL,MODIFY created_at DATETIME NOT NULL AFTER id;" +} +func (A) TableName() string { + return "a" +} +func (A) Columns() []string { + return []string{"id", "created_at"} +} +func (v A) Values() []any { + return []any{string(v.ID), time.Time(v.CreatedAt)} +} +func (v *A) Addrs() []any { + return []any{types.String(&v.ID), (*time.Time)(&v.CreatedAt)} +} + +func (B) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS b (id VARCHAR(255) NOT NULL,created_at DATETIME NOT NULL);" +} +func (B) AlterTableStmt() string { + return "ALTER TABLE b MODIFY id VARCHAR(255) NOT NULL,MODIFY created_at DATETIME NOT NULL AFTER id;" +} +func (B) TableName() string { + return "b" +} +func (B) Columns() []string { + return []string{"id", "created_at"} +} +func (v B) Values() []any { + return []any{string(v.ID), time.Time(v.CreatedAt)} +} +func (v *B) Addrs() []any { + return []any{types.String(&v.ID), (*time.Time)(&v.CreatedAt)} +} + +func (C) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS c (id BIGINT NOT NULL,PRIMARY KEY (id));" +} +func (C) AlterTableStmt() string { + return "ALTER TABLE c MODIFY id BIGINT NOT NULL;" +} +func (C) TableName() string { + return "c" +} +func (C) Columns() []string { + return []string{"id"} +} +func (v C) IsAutoIncr() bool { + return false +} +func (v C) PK() (columnName string, pos int, value driver.Value) { + return "id", 0, int64(v.ID) +} +func (v C) Values() []any { + return []any{int64(v.ID)} +} +func (v *C) Addrs() []any { + return []any{types.Integer(&v.ID)} +} + +func (D) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS d (id VARCHAR(255) NOT NULL,PRIMARY KEY (id));" +} +func (D) AlterTableStmt() string { + return "ALTER TABLE d MODIFY id VARCHAR(255) NOT NULL;" +} +func (D) TableName() string { + return "d" +} +func (D) Columns() []string { + return []string{"id"} +} +func (v D) IsAutoIncr() bool { + return false +} +func (v D) PK() (columnName string, pos int, value driver.Value) { + return "id", 0, (driver.Valuer)(v.ID) +} +func (v D) Values() []any { + return []any{(driver.Valuer)(v.ID)} +} +func (v *D) Addrs() []any { + return []any{(sql.Scanner)(&v.ID)} +} diff --git a/examples/testcase/schema/generated.go.tpl b/examples/testcase/schema/generated.go.tpl new file mode 100644 index 0000000..0bd96ee --- /dev/null +++ b/examples/testcase/schema/generated.go.tpl @@ -0,0 +1,118 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package schema + +import ( + "database/sql" + "database/sql/driver" + "time" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Model) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS model ();" +} +func (Model) AlterTableStmt() string { + return "ALTER TABLE model ;" +} +func (Model) TableName() string { + return "model" +} +func (Model) Columns() []string { + return []string{} +} +func (v Model) Values() []any { + return []any{} +} +func (v *Model) Addrs() []any { + return []any{} +} + +func (A) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS a (id VARCHAR(255) NOT NULL,created_at DATETIME NOT NULL);" +} +func (A) AlterTableStmt() string { + return "ALTER TABLE a MODIFY id VARCHAR(255) NOT NULL,MODIFY created_at DATETIME NOT NULL AFTER id;" +} +func (A) TableName() string { + return "a" +} +func (A) Columns() []string { + return []string{"id", "created_at"} +} +func (v A) Values() []any { + return []any{string(v.ID), time.Time(v.CreatedAt)} +} +func (v *A) Addrs() []any { + return []any{types.String(&v.ID), (*time.Time)(&v.CreatedAt)} +} + +func (B) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS b (id VARCHAR(255) NOT NULL,created_at DATETIME NOT NULL);" +} +func (B) AlterTableStmt() string { + return "ALTER TABLE b MODIFY id VARCHAR(255) NOT NULL,MODIFY created_at DATETIME NOT NULL AFTER id;" +} +func (B) TableName() string { + return "b" +} +func (B) Columns() []string { + return []string{"id", "created_at"} +} +func (v B) Values() []any { + return []any{string(v.ID), time.Time(v.CreatedAt)} +} +func (v *B) Addrs() []any { + return []any{types.String(&v.ID), (*time.Time)(&v.CreatedAt)} +} + +func (C) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS c (id BIGINT NOT NULL,PRIMARY KEY (id));" +} +func (C) AlterTableStmt() string { + return "ALTER TABLE c MODIFY id BIGINT NOT NULL;" +} +func (C) TableName() string { + return "c" +} +func (C) Columns() []string { + return []string{"id"} +} +func (v C) IsAutoIncr() bool { + return false +} +func (v C) PK() (columnName string, pos int, value driver.Value) { + return "id", 0, int64(v.ID) +} +func (v C) Values() []any { + return []any{int64(v.ID)} +} +func (v *C) Addrs() []any { + return []any{types.Integer(&v.ID)} +} + +func (D) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS d (id VARCHAR(255) NOT NULL,PRIMARY KEY (id));" +} +func (D) AlterTableStmt() string { + return "ALTER TABLE d MODIFY id VARCHAR(255) NOT NULL;" +} +func (D) TableName() string { + return "d" +} +func (D) Columns() []string { + return []string{"id"} +} +func (v D) IsAutoIncr() bool { + return false +} +func (v D) PK() (columnName string, pos int, value driver.Value) { + return "id", 0, (driver.Valuer)(v.ID) +} +func (v D) Values() []any { + return []any{(driver.Valuer)(v.ID)} +} +func (v *D) Addrs() []any { + return []any{(sql.Scanner)(&v.ID)} +} diff --git a/examples/testcase/schema/model.go b/examples/testcase/schema/model.go new file mode 100644 index 0000000..54b5537 --- /dev/null +++ b/examples/testcase/schema/model.go @@ -0,0 +1,29 @@ +package schema + +import ( + "database/sql" + t "time" + + s "github.com/si3nloong/sqlgen/sequel" +) + +type A struct { + s.Name `sql:"Apple"` + ID string + CreatedAt t.Time +} + +type B struct { + // go:staticcheck + // n, p schema.Name `sql:"Boy"` + ID string + CreatedAt t.Time +} + +type C struct { + ID int64 `sql:",pk"` +} + +type D struct { + ID sql.NullString `sql:",pk"` +} diff --git a/examples/testcase/schema/model_test.go b/examples/testcase/schema/model_test.go new file mode 100644 index 0000000..ab7e7cd --- /dev/null +++ b/examples/testcase/schema/model_test.go @@ -0,0 +1 @@ +package schema_test diff --git a/examples/testcase/struct-field/alias/generated.go b/examples/testcase/struct-field/alias/generated.go new file mode 100644 index 0000000..4d3982d --- /dev/null +++ b/examples/testcase/struct-field/alias/generated.go @@ -0,0 +1,74 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package alias + +import ( + "database/sql" + "database/sql/driver" + "time" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (AliasStruct) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS alias_struct (b VARCHAR(255) NOT NULL,Id BIGINT NOT NULL,header VARCHAR(255) NOT NULL,raw BLOB,text VARCHAR(255) NOT NULL,null_str VARCHAR(255) NOT NULL,created DATETIME NOT NULL,updated DATETIME NOT NULL,PRIMARY KEY (Id));" +} +func (AliasStruct) AlterTableStmt() string { + return "ALTER TABLE alias_struct MODIFY b VARCHAR(255) NOT NULL,MODIFY Id BIGINT NOT NULL AFTER b,MODIFY header VARCHAR(255) NOT NULL AFTER Id,MODIFY raw BLOB AFTER header,MODIFY text VARCHAR(255) NOT NULL AFTER raw,MODIFY null_str VARCHAR(255) NOT NULL AFTER text,MODIFY created DATETIME NOT NULL AFTER null_str,MODIFY updated DATETIME NOT NULL AFTER created;" +} +func (AliasStruct) TableName() string { + return "alias_struct" +} +func (AliasStruct) Columns() []string { + return []string{"b", "Id", "header", "raw", "text", "null_str", "created", "updated"} +} +func (v AliasStruct) IsAutoIncr() bool { + return false +} +func (v AliasStruct) PK() (columnName string, pos int, value driver.Value) { + return "Id", 1, int64(v.pk.ID) +} +func (v AliasStruct) Values() []any { + return []any{float64(v.B), int64(v.pk.ID), string(v.Header), v.Raw, string(v.Text), (driver.Valuer)(v.NullStr), time.Time(v.model.Created), time.Time(v.model.Updated)} +} +func (v *AliasStruct) Addrs() []any { + return []any{types.Float(&v.B), types.Integer(&v.pk.ID), types.String(&v.Header), &v.Raw, types.String(&v.Text), (sql.Scanner)(&v.NullStr), (*time.Time)(&v.model.Created), (*time.Time)(&v.model.Updated)} +} + +func (B) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS b (name VARCHAR(255) NOT NULL);" +} +func (B) AlterTableStmt() string { + return "ALTER TABLE b MODIFY name VARCHAR(255) NOT NULL;" +} +func (B) TableName() string { + return "b" +} +func (B) Columns() []string { + return []string{"name"} +} +func (v B) Values() []any { + return []any{string(v.Name)} +} +func (v *B) Addrs() []any { + return []any{types.String(&v.Name)} +} + +func (C) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS c (id BIGINT NOT NULL);" +} +func (C) AlterTableStmt() string { + return "ALTER TABLE c MODIFY id BIGINT NOT NULL;" +} +func (C) TableName() string { + return "c" +} +func (C) Columns() []string { + return []string{"id"} +} +func (v C) Values() []any { + return []any{int64(v.ID)} +} +func (v *C) Addrs() []any { + return []any{types.Integer(&v.ID)} +} diff --git a/examples/testcase/struct-field/alias/generated.go.tpl b/examples/testcase/struct-field/alias/generated.go.tpl new file mode 100644 index 0000000..4d3982d --- /dev/null +++ b/examples/testcase/struct-field/alias/generated.go.tpl @@ -0,0 +1,74 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package alias + +import ( + "database/sql" + "database/sql/driver" + "time" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (AliasStruct) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS alias_struct (b VARCHAR(255) NOT NULL,Id BIGINT NOT NULL,header VARCHAR(255) NOT NULL,raw BLOB,text VARCHAR(255) NOT NULL,null_str VARCHAR(255) NOT NULL,created DATETIME NOT NULL,updated DATETIME NOT NULL,PRIMARY KEY (Id));" +} +func (AliasStruct) AlterTableStmt() string { + return "ALTER TABLE alias_struct MODIFY b VARCHAR(255) NOT NULL,MODIFY Id BIGINT NOT NULL AFTER b,MODIFY header VARCHAR(255) NOT NULL AFTER Id,MODIFY raw BLOB AFTER header,MODIFY text VARCHAR(255) NOT NULL AFTER raw,MODIFY null_str VARCHAR(255) NOT NULL AFTER text,MODIFY created DATETIME NOT NULL AFTER null_str,MODIFY updated DATETIME NOT NULL AFTER created;" +} +func (AliasStruct) TableName() string { + return "alias_struct" +} +func (AliasStruct) Columns() []string { + return []string{"b", "Id", "header", "raw", "text", "null_str", "created", "updated"} +} +func (v AliasStruct) IsAutoIncr() bool { + return false +} +func (v AliasStruct) PK() (columnName string, pos int, value driver.Value) { + return "Id", 1, int64(v.pk.ID) +} +func (v AliasStruct) Values() []any { + return []any{float64(v.B), int64(v.pk.ID), string(v.Header), v.Raw, string(v.Text), (driver.Valuer)(v.NullStr), time.Time(v.model.Created), time.Time(v.model.Updated)} +} +func (v *AliasStruct) Addrs() []any { + return []any{types.Float(&v.B), types.Integer(&v.pk.ID), types.String(&v.Header), &v.Raw, types.String(&v.Text), (sql.Scanner)(&v.NullStr), (*time.Time)(&v.model.Created), (*time.Time)(&v.model.Updated)} +} + +func (B) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS b (name VARCHAR(255) NOT NULL);" +} +func (B) AlterTableStmt() string { + return "ALTER TABLE b MODIFY name VARCHAR(255) NOT NULL;" +} +func (B) TableName() string { + return "b" +} +func (B) Columns() []string { + return []string{"name"} +} +func (v B) Values() []any { + return []any{string(v.Name)} +} +func (v *B) Addrs() []any { + return []any{types.String(&v.Name)} +} + +func (C) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS c (id BIGINT NOT NULL);" +} +func (C) AlterTableStmt() string { + return "ALTER TABLE c MODIFY id BIGINT NOT NULL;" +} +func (C) TableName() string { + return "c" +} +func (C) Columns() []string { + return []string{"id"} +} +func (v C) Values() []any { + return []any{int64(v.ID)} +} +func (v *C) Addrs() []any { + return []any{types.Integer(&v.ID)} +} diff --git a/examples/testcase/struct-field/array/generated.go b/examples/testcase/struct-field/array/generated.go new file mode 100644 index 0000000..de79f61 --- /dev/null +++ b/examples/testcase/struct-field/array/generated.go @@ -0,0 +1,49 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package array + +import ( + "database/sql" + "database/sql/driver" + "time" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Address) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS address (line_1 VARCHAR(255) NOT NULL,line_2 VARCHAR(255) NOT NULL,city VARCHAR(255) NOT NULL,post_code INTEGER UNSIGNED NOT NULL,state_code VARCHAR(255) NOT NULL,country_code VARCHAR(255) NOT NULL);" +} +func (Address) AlterTableStmt() string { + return "ALTER TABLE address MODIFY line_1 VARCHAR(255) NOT NULL,MODIFY line_2 VARCHAR(255) NOT NULL AFTER line_1,MODIFY city VARCHAR(255) NOT NULL AFTER line_2,MODIFY post_code INTEGER UNSIGNED NOT NULL AFTER city,MODIFY state_code VARCHAR(255) NOT NULL AFTER post_code,MODIFY country_code VARCHAR(255) NOT NULL AFTER state_code;" +} +func (Address) TableName() string { + return "address" +} +func (Address) Columns() []string { + return []string{"line_1", "line_2", "city", "post_code", "state_code", "country_code"} +} +func (v Address) Values() []any { + return []any{string(v.Line1), (driver.Valuer)(v.Line2), string(v.City), int64(v.PostCode), string(v.StateCode), string(v.CountryCode)} +} +func (v *Address) Addrs() []any { + return []any{types.String(&v.Line1), (sql.Scanner)(&v.Line2), types.String(&v.City), types.Integer(&v.PostCode), types.String(&v.StateCode), types.String(&v.CountryCode)} +} + +func (Customer) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS customer (id BIGINT NOT NULL,howOld TINYINT UNSIGNED NOT NULL,name VARCHAR(255) NOT NULL,address VARCHAR(255) NOT NULL,nicknames VARCHAR(255) NOT NULL,status VARCHAR(255) NOT NULL,join_at DATETIME NOT NULL);" +} +func (Customer) AlterTableStmt() string { + return "ALTER TABLE customer MODIFY id BIGINT NOT NULL,MODIFY howOld TINYINT UNSIGNED NOT NULL AFTER id,MODIFY name VARCHAR(255) NOT NULL AFTER howOld,MODIFY address VARCHAR(255) NOT NULL AFTER name,MODIFY nicknames VARCHAR(255) NOT NULL AFTER address,MODIFY status VARCHAR(255) NOT NULL AFTER nicknames,MODIFY join_at DATETIME NOT NULL AFTER status;" +} +func (Customer) TableName() string { + return "customer" +} +func (Customer) Columns() []string { + return []string{"id", "howOld", "name", "address", "nicknames", "status", "join_at"} +} +func (v Customer) Values() []any { + return []any{int64(v.ID), int64(v.Age), (driver.Valuer)(v.Name), (driver.Valuer)(v.Address), v.Nicknames, string(v.Status), time.Time(v.JoinAt)} +} +func (v *Customer) Addrs() []any { + return []any{types.Integer(&v.ID), types.Integer(&v.Age), (sql.Scanner)(&v.Name), &v.Address, &v.Nicknames, types.String(&v.Status), (*time.Time)(&v.JoinAt)} +} diff --git a/examples/testcase/struct-field/array/generated.go.tpl b/examples/testcase/struct-field/array/generated.go.tpl new file mode 100644 index 0000000..de79f61 --- /dev/null +++ b/examples/testcase/struct-field/array/generated.go.tpl @@ -0,0 +1,49 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package array + +import ( + "database/sql" + "database/sql/driver" + "time" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Address) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS address (line_1 VARCHAR(255) NOT NULL,line_2 VARCHAR(255) NOT NULL,city VARCHAR(255) NOT NULL,post_code INTEGER UNSIGNED NOT NULL,state_code VARCHAR(255) NOT NULL,country_code VARCHAR(255) NOT NULL);" +} +func (Address) AlterTableStmt() string { + return "ALTER TABLE address MODIFY line_1 VARCHAR(255) NOT NULL,MODIFY line_2 VARCHAR(255) NOT NULL AFTER line_1,MODIFY city VARCHAR(255) NOT NULL AFTER line_2,MODIFY post_code INTEGER UNSIGNED NOT NULL AFTER city,MODIFY state_code VARCHAR(255) NOT NULL AFTER post_code,MODIFY country_code VARCHAR(255) NOT NULL AFTER state_code;" +} +func (Address) TableName() string { + return "address" +} +func (Address) Columns() []string { + return []string{"line_1", "line_2", "city", "post_code", "state_code", "country_code"} +} +func (v Address) Values() []any { + return []any{string(v.Line1), (driver.Valuer)(v.Line2), string(v.City), int64(v.PostCode), string(v.StateCode), string(v.CountryCode)} +} +func (v *Address) Addrs() []any { + return []any{types.String(&v.Line1), (sql.Scanner)(&v.Line2), types.String(&v.City), types.Integer(&v.PostCode), types.String(&v.StateCode), types.String(&v.CountryCode)} +} + +func (Customer) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS customer (id BIGINT NOT NULL,howOld TINYINT UNSIGNED NOT NULL,name VARCHAR(255) NOT NULL,address VARCHAR(255) NOT NULL,nicknames VARCHAR(255) NOT NULL,status VARCHAR(255) NOT NULL,join_at DATETIME NOT NULL);" +} +func (Customer) AlterTableStmt() string { + return "ALTER TABLE customer MODIFY id BIGINT NOT NULL,MODIFY howOld TINYINT UNSIGNED NOT NULL AFTER id,MODIFY name VARCHAR(255) NOT NULL AFTER howOld,MODIFY address VARCHAR(255) NOT NULL AFTER name,MODIFY nicknames VARCHAR(255) NOT NULL AFTER address,MODIFY status VARCHAR(255) NOT NULL AFTER nicknames,MODIFY join_at DATETIME NOT NULL AFTER status;" +} +func (Customer) TableName() string { + return "customer" +} +func (Customer) Columns() []string { + return []string{"id", "howOld", "name", "address", "nicknames", "status", "join_at"} +} +func (v Customer) Values() []any { + return []any{int64(v.ID), int64(v.Age), (driver.Valuer)(v.Name), (driver.Valuer)(v.Address), v.Nicknames, string(v.Status), time.Time(v.JoinAt)} +} +func (v *Customer) Addrs() []any { + return []any{types.Integer(&v.ID), types.Integer(&v.Age), (sql.Scanner)(&v.Name), &v.Address, &v.Nicknames, types.String(&v.Status), (*time.Time)(&v.JoinAt)} +} diff --git a/examples/testcase/struct-field/enum/generated.go b/examples/testcase/struct-field/enum/generated.go new file mode 100644 index 0000000..a09b2f0 --- /dev/null +++ b/examples/testcase/struct-field/enum/generated.go @@ -0,0 +1,26 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package enum + +import ( + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Custom) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS custom (text VARCHAR(255) NOT NULL,e INTEGER NOT NULL,num SMALLINT UNSIGNED NOT NULL);" +} +func (Custom) AlterTableStmt() string { + return "ALTER TABLE custom MODIFY text VARCHAR(255) NOT NULL,MODIFY e INTEGER NOT NULL AFTER text,MODIFY num SMALLINT UNSIGNED NOT NULL AFTER e;" +} +func (Custom) TableName() string { + return "custom" +} +func (Custom) Columns() []string { + return []string{"text", "e", "num"} +} +func (v Custom) Values() []any { + return []any{string(v.Str), int64(v.Enum), int64(v.Num)} +} +func (v *Custom) Addrs() []any { + return []any{types.String(&v.Str), types.Integer(&v.Enum), types.Integer(&v.Num)} +} diff --git a/examples/testcase/struct-field/enum/generated.go.tpl b/examples/testcase/struct-field/enum/generated.go.tpl new file mode 100644 index 0000000..a09b2f0 --- /dev/null +++ b/examples/testcase/struct-field/enum/generated.go.tpl @@ -0,0 +1,26 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package enum + +import ( + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Custom) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS custom (text VARCHAR(255) NOT NULL,e INTEGER NOT NULL,num SMALLINT UNSIGNED NOT NULL);" +} +func (Custom) AlterTableStmt() string { + return "ALTER TABLE custom MODIFY text VARCHAR(255) NOT NULL,MODIFY e INTEGER NOT NULL AFTER text,MODIFY num SMALLINT UNSIGNED NOT NULL AFTER e;" +} +func (Custom) TableName() string { + return "custom" +} +func (Custom) Columns() []string { + return []string{"text", "e", "num"} +} +func (v Custom) Values() []any { + return []any{string(v.Str), int64(v.Enum), int64(v.Num)} +} +func (v *Custom) Addrs() []any { + return []any{types.String(&v.Str), types.Integer(&v.Enum), types.Integer(&v.Num)} +} diff --git a/examples/testcase/struct-field/enum/generated.go.txt b/examples/testcase/struct-field/enum/generated.go.txt deleted file mode 100644 index 8357bc2..0000000 --- a/examples/testcase/struct-field/enum/generated.go.txt +++ /dev/null @@ -1,24 +0,0 @@ -// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. - -package enum - -import ( - "github.com/si3nloong/sqlgen/sql/types" -) - -// Implements `sql.Valuer` interface. -func (Custom) Table() string { - return "custom" -} - -func (Custom) Columns() []string { - return []string{"text", "e", "num"} -} - -func (v Custom) Values() []any { - return []any{string(v.Str), int64(v.Enum), int64(v.Num)} -} - -func (v *Custom) Addrs() []any { - return []any{types.String(&v.Str), types.Integer(&v.Enum), types.Integer(&v.Num)} -} diff --git a/examples/testcase/struct-field/imported/generated.go b/examples/testcase/struct-field/imported/generated.go new file mode 100644 index 0000000..40af180 --- /dev/null +++ b/examples/testcase/struct-field/imported/generated.go @@ -0,0 +1,46 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package imported + +import ( + "database/sql" + "database/sql/driver" +) + +func (Model) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS model (str VARCHAR(255) NOT NULL,bool VARCHAR(255) NOT NULL,bytes BLOB,int_16 VARCHAR(255) NOT NULL,int_32 VARCHAR(255) NOT NULL,int_64 VARCHAR(255) NOT NULL,time VARCHAR(255) NOT NULL);" +} +func (Model) AlterTableStmt() string { + return "ALTER TABLE model MODIFY str VARCHAR(255) NOT NULL,MODIFY bool VARCHAR(255) NOT NULL AFTER str,MODIFY bytes BLOB AFTER bool,MODIFY int_16 VARCHAR(255) NOT NULL AFTER bytes,MODIFY int_32 VARCHAR(255) NOT NULL AFTER int_16,MODIFY int_64 VARCHAR(255) NOT NULL AFTER int_32,MODIFY time VARCHAR(255) NOT NULL AFTER int_64;" +} +func (Model) TableName() string { + return "model" +} +func (Model) Columns() []string { + return []string{"str", "bool", "bytes", "int_16", "int_32", "int_64", "time"} +} +func (v Model) Values() []any { + return []any{(driver.Valuer)(v.Str), (driver.Valuer)(v.Bool), v.Bytes, (driver.Valuer)(v.Int16), (driver.Valuer)(v.Int32), (driver.Valuer)(v.Int64), (driver.Valuer)(v.Time)} +} +func (v *Model) Addrs() []any { + return []any{(sql.Scanner)(&v.Str), (sql.Scanner)(&v.Bool), &v.Bytes, (sql.Scanner)(&v.Int16), (sql.Scanner)(&v.Int32), (sql.Scanner)(&v.Int64), (sql.Scanner)(&v.Time)} +} + +func (Some) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS some (id VARCHAR(36) NOT NULL);" +} +func (Some) AlterTableStmt() string { + return "ALTER TABLE some MODIFY id VARCHAR(36) NOT NULL;" +} +func (Some) TableName() string { + return "some" +} +func (Some) Columns() []string { + return []string{"id"} +} +func (v Some) Values() []any { + return []any{(driver.Valuer)(v.ID)} +} +func (v *Some) Addrs() []any { + return []any{(sql.Scanner)(&v.ID)} +} diff --git a/examples/testcase/struct-field/imported/generated.go.tpl b/examples/testcase/struct-field/imported/generated.go.tpl new file mode 100644 index 0000000..40af180 --- /dev/null +++ b/examples/testcase/struct-field/imported/generated.go.tpl @@ -0,0 +1,46 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package imported + +import ( + "database/sql" + "database/sql/driver" +) + +func (Model) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS model (str VARCHAR(255) NOT NULL,bool VARCHAR(255) NOT NULL,bytes BLOB,int_16 VARCHAR(255) NOT NULL,int_32 VARCHAR(255) NOT NULL,int_64 VARCHAR(255) NOT NULL,time VARCHAR(255) NOT NULL);" +} +func (Model) AlterTableStmt() string { + return "ALTER TABLE model MODIFY str VARCHAR(255) NOT NULL,MODIFY bool VARCHAR(255) NOT NULL AFTER str,MODIFY bytes BLOB AFTER bool,MODIFY int_16 VARCHAR(255) NOT NULL AFTER bytes,MODIFY int_32 VARCHAR(255) NOT NULL AFTER int_16,MODIFY int_64 VARCHAR(255) NOT NULL AFTER int_32,MODIFY time VARCHAR(255) NOT NULL AFTER int_64;" +} +func (Model) TableName() string { + return "model" +} +func (Model) Columns() []string { + return []string{"str", "bool", "bytes", "int_16", "int_32", "int_64", "time"} +} +func (v Model) Values() []any { + return []any{(driver.Valuer)(v.Str), (driver.Valuer)(v.Bool), v.Bytes, (driver.Valuer)(v.Int16), (driver.Valuer)(v.Int32), (driver.Valuer)(v.Int64), (driver.Valuer)(v.Time)} +} +func (v *Model) Addrs() []any { + return []any{(sql.Scanner)(&v.Str), (sql.Scanner)(&v.Bool), &v.Bytes, (sql.Scanner)(&v.Int16), (sql.Scanner)(&v.Int32), (sql.Scanner)(&v.Int64), (sql.Scanner)(&v.Time)} +} + +func (Some) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS some (id VARCHAR(36) NOT NULL);" +} +func (Some) AlterTableStmt() string { + return "ALTER TABLE some MODIFY id VARCHAR(36) NOT NULL;" +} +func (Some) TableName() string { + return "some" +} +func (Some) Columns() []string { + return []string{"id"} +} +func (v Some) Values() []any { + return []any{(driver.Valuer)(v.ID)} +} +func (v *Some) Addrs() []any { + return []any{(sql.Scanner)(&v.ID)} +} diff --git a/examples/testcase/struct-field/imported/model.go b/examples/testcase/struct-field/imported/model.go new file mode 100644 index 0000000..1f1c12f --- /dev/null +++ b/examples/testcase/struct-field/imported/model.go @@ -0,0 +1,21 @@ +package imported + +import ( + "database/sql" + + "github.com/google/uuid" +) + +type Model struct { + Str sql.NullString + Bool sql.NullBool + Bytes sql.RawBytes + Int16 sql.NullInt16 + Int32 sql.NullInt32 + Int64 sql.NullInt64 + Time sql.NullTime +} + +type Some struct { + ID uuid.UUID +} diff --git a/examples/testcase/struct-field/pk/auto-incr/generated.go b/examples/testcase/struct-field/pk/auto-incr/generated.go new file mode 100644 index 0000000..8f4553b --- /dev/null +++ b/examples/testcase/struct-field/pk/auto-incr/generated.go @@ -0,0 +1,34 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package pkautoincr + +import ( + "database/sql/driver" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Model) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS model (name VARCHAR(255) NOT NULL,f TINYINT NOT NULL,id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,n BIGINT NOT NULL,PRIMARY KEY (id));" +} +func (Model) AlterTableStmt() string { + return "ALTER TABLE model MODIFY name VARCHAR(255) NOT NULL,MODIFY f TINYINT NOT NULL AFTER name,MODIFY id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT AFTER f,MODIFY n BIGINT NOT NULL AFTER id;" +} +func (Model) TableName() string { + return "model" +} +func (Model) Columns() []string { + return []string{"name", "f", "id", "n"} +} +func (v Model) IsAutoIncr() bool { + return true +} +func (v Model) PK() (columnName string, pos int, value driver.Value) { + return "id", 2, int64(v.ID) +} +func (v Model) Values() []any { + return []any{string(v.Name), bool(v.F), int64(v.ID), int64(v.N)} +} +func (v *Model) Addrs() []any { + return []any{types.String(&v.Name), types.Bool(&v.F), types.Integer(&v.ID), types.Integer(&v.N)} +} diff --git a/examples/testcase/struct-field/pk/auto-incr/generated.go.tpl b/examples/testcase/struct-field/pk/auto-incr/generated.go.tpl new file mode 100644 index 0000000..8f4553b --- /dev/null +++ b/examples/testcase/struct-field/pk/auto-incr/generated.go.tpl @@ -0,0 +1,34 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package pkautoincr + +import ( + "database/sql/driver" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Model) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS model (name VARCHAR(255) NOT NULL,f TINYINT NOT NULL,id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,n BIGINT NOT NULL,PRIMARY KEY (id));" +} +func (Model) AlterTableStmt() string { + return "ALTER TABLE model MODIFY name VARCHAR(255) NOT NULL,MODIFY f TINYINT NOT NULL AFTER name,MODIFY id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT AFTER f,MODIFY n BIGINT NOT NULL AFTER id;" +} +func (Model) TableName() string { + return "model" +} +func (Model) Columns() []string { + return []string{"name", "f", "id", "n"} +} +func (v Model) IsAutoIncr() bool { + return true +} +func (v Model) PK() (columnName string, pos int, value driver.Value) { + return "id", 2, int64(v.ID) +} +func (v Model) Values() []any { + return []any{string(v.Name), bool(v.F), int64(v.ID), int64(v.N)} +} +func (v *Model) Addrs() []any { + return []any{types.String(&v.Name), types.Bool(&v.F), types.Integer(&v.ID), types.Integer(&v.N)} +} diff --git a/examples/testcase/struct-field/pk/auto-incr/model.go b/examples/testcase/struct-field/pk/auto-incr/model.go new file mode 100644 index 0000000..cbb2add --- /dev/null +++ b/examples/testcase/struct-field/pk/auto-incr/model.go @@ -0,0 +1,12 @@ +package pkautoincr + +type Flag bool + +type LongText string + +type Model struct { + Name LongText + F Flag + ID uint `sql:",pk,auto_increment"` + N int64 +} diff --git a/examples/testcase/struct-field/pk/car.go b/examples/testcase/struct-field/pk/car.go new file mode 100644 index 0000000..032151a --- /dev/null +++ b/examples/testcase/struct-field/pk/car.go @@ -0,0 +1,17 @@ +package pk + +import "time" + +type Color int + +const ( + Red Color = iota + White +) + +type Car struct { + ID PK `sql:",pk"` + No string + Color Color + ManucDate time.Time +} diff --git a/examples/testcase/struct-field/pk/generated.go b/examples/testcase/struct-field/pk/generated.go new file mode 100644 index 0000000..e2f94c4 --- /dev/null +++ b/examples/testcase/struct-field/pk/generated.go @@ -0,0 +1,85 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package pk + +import ( + "database/sql/driver" + "time" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Car) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS car (id BIGINT NOT NULL,no VARCHAR(255) NOT NULL,color INTEGER NOT NULL,manuc_date DATETIME NOT NULL,PRIMARY KEY (id));" +} +func (Car) AlterTableStmt() string { + return "ALTER TABLE car MODIFY id BIGINT NOT NULL,MODIFY no VARCHAR(255) NOT NULL AFTER id,MODIFY color INTEGER NOT NULL AFTER no,MODIFY manuc_date DATETIME NOT NULL AFTER color;" +} +func (Car) TableName() string { + return "car" +} +func (Car) Columns() []string { + return []string{"id", "no", "color", "manuc_date"} +} +func (v Car) IsAutoIncr() bool { + return false +} +func (v Car) PK() (columnName string, pos int, value driver.Value) { + return "id", 0, (driver.Valuer)(v.ID) +} +func (v Car) Values() []any { + return []any{(driver.Valuer)(v.ID), string(v.No), int64(v.Color), time.Time(v.ManucDate)} +} +func (v *Car) Addrs() []any { + return []any{types.Integer(&v.ID), types.String(&v.No), types.Integer(&v.Color), (*time.Time)(&v.ManucDate)} +} + +func (User) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS user (id BIGINT NOT NULL,name VARCHAR(255) NOT NULL,age TINYINT UNSIGNED NOT NULL,email VARCHAR(255) NOT NULL,PRIMARY KEY (id));" +} +func (User) AlterTableStmt() string { + return "ALTER TABLE user MODIFY id BIGINT NOT NULL,MODIFY name VARCHAR(255) NOT NULL AFTER id,MODIFY age TINYINT UNSIGNED NOT NULL AFTER name,MODIFY email VARCHAR(255) NOT NULL AFTER age;" +} +func (User) TableName() string { + return "user" +} +func (User) Columns() []string { + return []string{"id", "name", "age", "email"} +} +func (v User) IsAutoIncr() bool { + return false +} +func (v User) PK() (columnName string, pos int, value driver.Value) { + return "id", 0, int64(v.ID) +} +func (v User) Values() []any { + return []any{int64(v.ID), string(v.Name), int64(v.Age), string(v.Email)} +} +func (v *User) Addrs() []any { + return []any{types.Integer(&v.ID), types.String(&v.Name), types.Integer(&v.Age), types.String(&v.Email)} +} + +func (House) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS house (id INTEGER UNSIGNED NOT NULL,no VARCHAR(255) NOT NULL,PRIMARY KEY (id));" +} +func (House) AlterTableStmt() string { + return "ALTER TABLE house MODIFY id INTEGER UNSIGNED NOT NULL,MODIFY no VARCHAR(255) NOT NULL AFTER id;" +} +func (House) TableName() string { + return "house" +} +func (House) Columns() []string { + return []string{"id", "no"} +} +func (v House) IsAutoIncr() bool { + return false +} +func (v House) PK() (columnName string, pos int, value driver.Value) { + return "id", 0, int64(v.ID) +} +func (v House) Values() []any { + return []any{int64(v.ID), string(v.No)} +} +func (v *House) Addrs() []any { + return []any{types.Integer(&v.ID), types.String(&v.No)} +} diff --git a/examples/testcase/struct-field/pk/generated.go.tpl b/examples/testcase/struct-field/pk/generated.go.tpl new file mode 100644 index 0000000..e2f94c4 --- /dev/null +++ b/examples/testcase/struct-field/pk/generated.go.tpl @@ -0,0 +1,85 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package pk + +import ( + "database/sql/driver" + "time" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Car) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS car (id BIGINT NOT NULL,no VARCHAR(255) NOT NULL,color INTEGER NOT NULL,manuc_date DATETIME NOT NULL,PRIMARY KEY (id));" +} +func (Car) AlterTableStmt() string { + return "ALTER TABLE car MODIFY id BIGINT NOT NULL,MODIFY no VARCHAR(255) NOT NULL AFTER id,MODIFY color INTEGER NOT NULL AFTER no,MODIFY manuc_date DATETIME NOT NULL AFTER color;" +} +func (Car) TableName() string { + return "car" +} +func (Car) Columns() []string { + return []string{"id", "no", "color", "manuc_date"} +} +func (v Car) IsAutoIncr() bool { + return false +} +func (v Car) PK() (columnName string, pos int, value driver.Value) { + return "id", 0, (driver.Valuer)(v.ID) +} +func (v Car) Values() []any { + return []any{(driver.Valuer)(v.ID), string(v.No), int64(v.Color), time.Time(v.ManucDate)} +} +func (v *Car) Addrs() []any { + return []any{types.Integer(&v.ID), types.String(&v.No), types.Integer(&v.Color), (*time.Time)(&v.ManucDate)} +} + +func (User) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS user (id BIGINT NOT NULL,name VARCHAR(255) NOT NULL,age TINYINT UNSIGNED NOT NULL,email VARCHAR(255) NOT NULL,PRIMARY KEY (id));" +} +func (User) AlterTableStmt() string { + return "ALTER TABLE user MODIFY id BIGINT NOT NULL,MODIFY name VARCHAR(255) NOT NULL AFTER id,MODIFY age TINYINT UNSIGNED NOT NULL AFTER name,MODIFY email VARCHAR(255) NOT NULL AFTER age;" +} +func (User) TableName() string { + return "user" +} +func (User) Columns() []string { + return []string{"id", "name", "age", "email"} +} +func (v User) IsAutoIncr() bool { + return false +} +func (v User) PK() (columnName string, pos int, value driver.Value) { + return "id", 0, int64(v.ID) +} +func (v User) Values() []any { + return []any{int64(v.ID), string(v.Name), int64(v.Age), string(v.Email)} +} +func (v *User) Addrs() []any { + return []any{types.Integer(&v.ID), types.String(&v.Name), types.Integer(&v.Age), types.String(&v.Email)} +} + +func (House) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS house (id INTEGER UNSIGNED NOT NULL,no VARCHAR(255) NOT NULL,PRIMARY KEY (id));" +} +func (House) AlterTableStmt() string { + return "ALTER TABLE house MODIFY id INTEGER UNSIGNED NOT NULL,MODIFY no VARCHAR(255) NOT NULL AFTER id;" +} +func (House) TableName() string { + return "house" +} +func (House) Columns() []string { + return []string{"id", "no"} +} +func (v House) IsAutoIncr() bool { + return false +} +func (v House) PK() (columnName string, pos int, value driver.Value) { + return "id", 0, int64(v.ID) +} +func (v House) Values() []any { + return []any{int64(v.ID), string(v.No)} +} +func (v *House) Addrs() []any { + return []any{types.Integer(&v.ID), types.String(&v.No)} +} diff --git a/examples/testcase/struct-field/pk/types.go b/examples/testcase/struct-field/pk/types.go new file mode 100644 index 0000000..fcc83ad --- /dev/null +++ b/examples/testcase/struct-field/pk/types.go @@ -0,0 +1,13 @@ +package pk + +import "database/sql/driver" + +type LongText string + +type PK int64 + +var _ driver.Valuer = (*PK)(nil) + +func (pk PK) Value() (driver.Value, error) { + return int64(pk), nil +} diff --git a/examples/testcase/struct-field/pk/user.go b/examples/testcase/struct-field/pk/user.go new file mode 100644 index 0000000..4aab9aa --- /dev/null +++ b/examples/testcase/struct-field/pk/user.go @@ -0,0 +1,13 @@ +package pk + +type User struct { + ID int64 `sql:",pk"` + Name LongText + Age uint8 + Email string +} + +type House struct { + ID uint `sql:",pk"` + No string +} diff --git a/examples/testcase/struct-field/pk/uuid/generated.go b/examples/testcase/struct-field/pk/uuid/generated.go new file mode 100644 index 0000000..068a542 --- /dev/null +++ b/examples/testcase/struct-field/pk/uuid/generated.go @@ -0,0 +1,29 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package main + +import ( + "database/sql" + "database/sql/driver" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (User) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS user (id VARCHAR(36) NOT NULL,name VARCHAR(255) NOT NULL);" +} +func (User) AlterTableStmt() string { + return "ALTER TABLE user MODIFY id VARCHAR(36) NOT NULL,MODIFY name VARCHAR(255) NOT NULL AFTER id;" +} +func (User) TableName() string { + return "user" +} +func (User) Columns() []string { + return []string{"id", "name"} +} +func (v User) Values() []any { + return []any{(driver.Valuer)(v.ID), string(v.Name)} +} +func (v *User) Addrs() []any { + return []any{(sql.Scanner)(&v.ID), types.String(&v.Name)} +} diff --git a/examples/testcase/struct-field/pk/uuid/generated.go.tpl b/examples/testcase/struct-field/pk/uuid/generated.go.tpl new file mode 100644 index 0000000..068a542 --- /dev/null +++ b/examples/testcase/struct-field/pk/uuid/generated.go.tpl @@ -0,0 +1,29 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package main + +import ( + "database/sql" + "database/sql/driver" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (User) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS user (id VARCHAR(36) NOT NULL,name VARCHAR(255) NOT NULL);" +} +func (User) AlterTableStmt() string { + return "ALTER TABLE user MODIFY id VARCHAR(36) NOT NULL,MODIFY name VARCHAR(255) NOT NULL AFTER id;" +} +func (User) TableName() string { + return "user" +} +func (User) Columns() []string { + return []string{"id", "name"} +} +func (v User) Values() []any { + return []any{(driver.Valuer)(v.ID), string(v.Name)} +} +func (v *User) Addrs() []any { + return []any{(sql.Scanner)(&v.ID), types.String(&v.Name)} +} diff --git a/examples/testcase/struct-field/pk/uuid/uuid.go b/examples/testcase/struct-field/pk/uuid/uuid.go new file mode 100644 index 0000000..2faad6a --- /dev/null +++ b/examples/testcase/struct-field/pk/uuid/uuid.go @@ -0,0 +1,8 @@ +package main + +import "github.com/google/uuid" + +type User struct { + ID uuid.UUID + Name string +} diff --git a/examples/testcase/struct-field/pointer/generated.go b/examples/testcase/struct-field/pointer/generated.go new file mode 100644 index 0000000..4197ebb --- /dev/null +++ b/examples/testcase/struct-field/pointer/generated.go @@ -0,0 +1,26 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package pointer + +import ( + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Ptr) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS ptr (str VARCHAR(255),bytes BLOB,uint_16 SMALLINT UNSIGNED,uint_32 MEDIUMINT UNSIGNED,uint_64 BIGINT UNSIGNED,f_32 VARCHAR(255),f_64 VARCHAR(255),time DATETIME,bool TINYINT,int INTEGER,int_8 TINYINT,int_16 SMALLINT,int_32 MEDIUMINT,int_64 BIGINT,uint INTEGER UNSIGNED,uint_8 TINYINT UNSIGNED);" +} +func (Ptr) AlterTableStmt() string { + return "ALTER TABLE ptr MODIFY str VARCHAR(255),MODIFY bytes BLOB AFTER str,MODIFY uint_16 SMALLINT UNSIGNED AFTER bytes,MODIFY uint_32 MEDIUMINT UNSIGNED AFTER uint_16,MODIFY uint_64 BIGINT UNSIGNED AFTER uint_32,MODIFY f_32 VARCHAR(255) AFTER uint_64,MODIFY f_64 VARCHAR(255) AFTER f_32,MODIFY time DATETIME AFTER f_64,MODIFY bool TINYINT AFTER time,MODIFY int INTEGER AFTER bool,MODIFY int_8 TINYINT AFTER int,MODIFY int_16 SMALLINT AFTER int_8,MODIFY int_32 MEDIUMINT AFTER int_16,MODIFY int_64 BIGINT AFTER int_32,MODIFY uint INTEGER UNSIGNED AFTER int_64,MODIFY uint_8 TINYINT UNSIGNED AFTER uint;" +} +func (Ptr) TableName() string { + return "ptr" +} +func (Ptr) Columns() []string { + return []string{"str", "bytes", "uint_16", "uint_32", "uint_64", "f_32", "f_64", "time", "bool", "int", "int_8", "int_16", "int_32", "int_64", "uint", "uint_8"} +} +func (v Ptr) Values() []any { + return []any{types.String(v.Str), types.String(v.Bytes), types.Integer(v.Uint16), types.Integer(v.Uint32), types.Integer(v.Uint64), types.Float(v.F32), types.Float(v.F64), types.Time(v.Time), types.Bool(v.Bool), types.Integer(v.Int), types.Integer(v.Int8), types.Integer(v.Int16), types.Integer(v.Int32), types.Integer(v.Int64), types.Integer(v.Uint), types.Integer(v.Uint8)} +} +func (v *Ptr) Addrs() []any { + return []any{types.PtrOfString(&v.Str), types.PtrOfString(&v.Bytes), types.PtrOfInt(&v.Uint16), types.PtrOfInt(&v.Uint32), types.PtrOfInt(&v.Uint64), types.PtrOfFloat(&v.F32), types.PtrOfFloat(&v.F64), types.PtrOfTime(&v.Time), types.PtrOfBool(&v.Bool), types.PtrOfInt(&v.Int), types.PtrOfInt(&v.Int8), types.PtrOfInt(&v.Int16), types.PtrOfInt(&v.Int32), types.PtrOfInt(&v.Int64), types.PtrOfInt(&v.Uint), types.PtrOfInt(&v.Uint8)} +} diff --git a/examples/testcase/struct-field/pointer/generated.go.tpl b/examples/testcase/struct-field/pointer/generated.go.tpl new file mode 100644 index 0000000..4197ebb --- /dev/null +++ b/examples/testcase/struct-field/pointer/generated.go.tpl @@ -0,0 +1,26 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package pointer + +import ( + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Ptr) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS ptr (str VARCHAR(255),bytes BLOB,uint_16 SMALLINT UNSIGNED,uint_32 MEDIUMINT UNSIGNED,uint_64 BIGINT UNSIGNED,f_32 VARCHAR(255),f_64 VARCHAR(255),time DATETIME,bool TINYINT,int INTEGER,int_8 TINYINT,int_16 SMALLINT,int_32 MEDIUMINT,int_64 BIGINT,uint INTEGER UNSIGNED,uint_8 TINYINT UNSIGNED);" +} +func (Ptr) AlterTableStmt() string { + return "ALTER TABLE ptr MODIFY str VARCHAR(255),MODIFY bytes BLOB AFTER str,MODIFY uint_16 SMALLINT UNSIGNED AFTER bytes,MODIFY uint_32 MEDIUMINT UNSIGNED AFTER uint_16,MODIFY uint_64 BIGINT UNSIGNED AFTER uint_32,MODIFY f_32 VARCHAR(255) AFTER uint_64,MODIFY f_64 VARCHAR(255) AFTER f_32,MODIFY time DATETIME AFTER f_64,MODIFY bool TINYINT AFTER time,MODIFY int INTEGER AFTER bool,MODIFY int_8 TINYINT AFTER int,MODIFY int_16 SMALLINT AFTER int_8,MODIFY int_32 MEDIUMINT AFTER int_16,MODIFY int_64 BIGINT AFTER int_32,MODIFY uint INTEGER UNSIGNED AFTER int_64,MODIFY uint_8 TINYINT UNSIGNED AFTER uint;" +} +func (Ptr) TableName() string { + return "ptr" +} +func (Ptr) Columns() []string { + return []string{"str", "bytes", "uint_16", "uint_32", "uint_64", "f_32", "f_64", "time", "bool", "int", "int_8", "int_16", "int_32", "int_64", "uint", "uint_8"} +} +func (v Ptr) Values() []any { + return []any{types.String(v.Str), types.String(v.Bytes), types.Integer(v.Uint16), types.Integer(v.Uint32), types.Integer(v.Uint64), types.Float(v.F32), types.Float(v.F64), types.Time(v.Time), types.Bool(v.Bool), types.Integer(v.Int), types.Integer(v.Int8), types.Integer(v.Int16), types.Integer(v.Int32), types.Integer(v.Int64), types.Integer(v.Uint), types.Integer(v.Uint8)} +} +func (v *Ptr) Addrs() []any { + return []any{types.PtrOfString(&v.Str), types.PtrOfString(&v.Bytes), types.PtrOfInt(&v.Uint16), types.PtrOfInt(&v.Uint32), types.PtrOfInt(&v.Uint64), types.PtrOfFloat(&v.F32), types.PtrOfFloat(&v.F64), types.PtrOfTime(&v.Time), types.PtrOfBool(&v.Bool), types.PtrOfInt(&v.Int), types.PtrOfInt(&v.Int8), types.PtrOfInt(&v.Int16), types.PtrOfInt(&v.Int32), types.PtrOfInt(&v.Int64), types.PtrOfInt(&v.Uint), types.PtrOfInt(&v.Uint8)} +} diff --git a/examples/testcase/struct-field/primitive/generated.go b/examples/testcase/struct-field/primitive/generated.go new file mode 100644 index 0000000..9a3ab3f --- /dev/null +++ b/examples/testcase/struct-field/primitive/generated.go @@ -0,0 +1,28 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package primitive + +import ( + "time" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Primitive) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS primitive (str VARCHAR(255) NOT NULL,bytes BLOB,uint_16 SMALLINT UNSIGNED NOT NULL,uint_32 MEDIUMINT UNSIGNED NOT NULL,uint_64 BIGINT UNSIGNED NOT NULL,f_32 VARCHAR(255) NOT NULL,f_64 VARCHAR(255) NOT NULL,time DATETIME NOT NULL,bool TINYINT NOT NULL,int INTEGER NOT NULL,int_8 TINYINT NOT NULL,int_16 SMALLINT NOT NULL,int_32 MEDIUMINT NOT NULL,int_64 BIGINT NOT NULL,uint INTEGER UNSIGNED NOT NULL,uint_8 TINYINT UNSIGNED NOT NULL);" +} +func (Primitive) AlterTableStmt() string { + return "ALTER TABLE primitive MODIFY str VARCHAR(255) NOT NULL,MODIFY bytes BLOB AFTER str,MODIFY uint_16 SMALLINT UNSIGNED NOT NULL AFTER bytes,MODIFY uint_32 MEDIUMINT UNSIGNED NOT NULL AFTER uint_16,MODIFY uint_64 BIGINT UNSIGNED NOT NULL AFTER uint_32,MODIFY f_32 VARCHAR(255) NOT NULL AFTER uint_64,MODIFY f_64 VARCHAR(255) NOT NULL AFTER f_32,MODIFY time DATETIME NOT NULL AFTER f_64,MODIFY bool TINYINT NOT NULL AFTER time,MODIFY int INTEGER NOT NULL AFTER bool,MODIFY int_8 TINYINT NOT NULL AFTER int,MODIFY int_16 SMALLINT NOT NULL AFTER int_8,MODIFY int_32 MEDIUMINT NOT NULL AFTER int_16,MODIFY int_64 BIGINT NOT NULL AFTER int_32,MODIFY uint INTEGER UNSIGNED NOT NULL AFTER int_64,MODIFY uint_8 TINYINT UNSIGNED NOT NULL AFTER uint;" +} +func (Primitive) TableName() string { + return "primitive" +} +func (Primitive) Columns() []string { + return []string{"str", "bytes", "uint_16", "uint_32", "uint_64", "f_32", "f_64", "time", "bool", "int", "int_8", "int_16", "int_32", "int_64", "uint", "uint_8"} +} +func (v Primitive) Values() []any { + return []any{string(v.Str), v.Bytes, int64(v.Uint16), int64(v.Uint32), int64(v.Uint64), float64(v.F32), float64(v.F64), time.Time(v.Time), bool(v.Bool), int64(v.Int), int64(v.Int8), int64(v.Int16), int64(v.Int32), int64(v.Int64), int64(v.Uint), int64(v.Uint8)} +} +func (v *Primitive) Addrs() []any { + return []any{types.String(&v.Str), &v.Bytes, types.Integer(&v.Uint16), types.Integer(&v.Uint32), types.Integer(&v.Uint64), types.Float(&v.F32), types.Float(&v.F64), (*time.Time)(&v.Time), types.Bool(&v.Bool), types.Integer(&v.Int), types.Integer(&v.Int8), types.Integer(&v.Int16), types.Integer(&v.Int32), types.Integer(&v.Int64), types.Integer(&v.Uint), types.Integer(&v.Uint8)} +} diff --git a/examples/testcase/struct-field/primitive/generated.go.tpl b/examples/testcase/struct-field/primitive/generated.go.tpl new file mode 100644 index 0000000..9a3ab3f --- /dev/null +++ b/examples/testcase/struct-field/primitive/generated.go.tpl @@ -0,0 +1,28 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package primitive + +import ( + "time" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (Primitive) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS primitive (str VARCHAR(255) NOT NULL,bytes BLOB,uint_16 SMALLINT UNSIGNED NOT NULL,uint_32 MEDIUMINT UNSIGNED NOT NULL,uint_64 BIGINT UNSIGNED NOT NULL,f_32 VARCHAR(255) NOT NULL,f_64 VARCHAR(255) NOT NULL,time DATETIME NOT NULL,bool TINYINT NOT NULL,int INTEGER NOT NULL,int_8 TINYINT NOT NULL,int_16 SMALLINT NOT NULL,int_32 MEDIUMINT NOT NULL,int_64 BIGINT NOT NULL,uint INTEGER UNSIGNED NOT NULL,uint_8 TINYINT UNSIGNED NOT NULL);" +} +func (Primitive) AlterTableStmt() string { + return "ALTER TABLE primitive MODIFY str VARCHAR(255) NOT NULL,MODIFY bytes BLOB AFTER str,MODIFY uint_16 SMALLINT UNSIGNED NOT NULL AFTER bytes,MODIFY uint_32 MEDIUMINT UNSIGNED NOT NULL AFTER uint_16,MODIFY uint_64 BIGINT UNSIGNED NOT NULL AFTER uint_32,MODIFY f_32 VARCHAR(255) NOT NULL AFTER uint_64,MODIFY f_64 VARCHAR(255) NOT NULL AFTER f_32,MODIFY time DATETIME NOT NULL AFTER f_64,MODIFY bool TINYINT NOT NULL AFTER time,MODIFY int INTEGER NOT NULL AFTER bool,MODIFY int_8 TINYINT NOT NULL AFTER int,MODIFY int_16 SMALLINT NOT NULL AFTER int_8,MODIFY int_32 MEDIUMINT NOT NULL AFTER int_16,MODIFY int_64 BIGINT NOT NULL AFTER int_32,MODIFY uint INTEGER UNSIGNED NOT NULL AFTER int_64,MODIFY uint_8 TINYINT UNSIGNED NOT NULL AFTER uint;" +} +func (Primitive) TableName() string { + return "primitive" +} +func (Primitive) Columns() []string { + return []string{"str", "bytes", "uint_16", "uint_32", "uint_64", "f_32", "f_64", "time", "bool", "int", "int_8", "int_16", "int_32", "int_64", "uint", "uint_8"} +} +func (v Primitive) Values() []any { + return []any{string(v.Str), v.Bytes, int64(v.Uint16), int64(v.Uint32), int64(v.Uint64), float64(v.F32), float64(v.F64), time.Time(v.Time), bool(v.Bool), int64(v.Int), int64(v.Int8), int64(v.Int16), int64(v.Int32), int64(v.Int64), int64(v.Uint), int64(v.Uint8)} +} +func (v *Primitive) Addrs() []any { + return []any{types.String(&v.Str), &v.Bytes, types.Integer(&v.Uint16), types.Integer(&v.Uint32), types.Integer(&v.Uint64), types.Float(&v.F32), types.Float(&v.F64), (*time.Time)(&v.Time), types.Bool(&v.Bool), types.Integer(&v.Int), types.Integer(&v.Int8), types.Integer(&v.Int16), types.Integer(&v.Int32), types.Integer(&v.Int64), types.Integer(&v.Uint), types.Integer(&v.Uint8)} +} diff --git a/examples/testcase/struct-field/unamed/generated.go b/examples/testcase/struct-field/unamed/generated.go new file mode 100644 index 0000000..0baf7b0 --- /dev/null +++ b/examples/testcase/struct-field/unamed/generated.go @@ -0,0 +1,3 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package unamed diff --git a/examples/testcase/struct-field/unamed/generated.go.tpl b/examples/testcase/struct-field/unamed/generated.go.tpl new file mode 100644 index 0000000..0baf7b0 --- /dev/null +++ b/examples/testcase/struct-field/unamed/generated.go.tpl @@ -0,0 +1,3 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package unamed diff --git a/examples/testcase/struct-field/valuer/generated.go b/examples/testcase/struct-field/valuer/generated.go new file mode 100644 index 0000000..ec1f803 --- /dev/null +++ b/examples/testcase/struct-field/valuer/generated.go @@ -0,0 +1,28 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package valuer + +import ( + "database/sql/driver" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (B) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS b (id BIGINT NOT NULL,value VARCHAR(255) NOT NULL,n VARCHAR(255) NOT NULL);" +} +func (B) AlterTableStmt() string { + return "ALTER TABLE b MODIFY id BIGINT NOT NULL,MODIFY value VARCHAR(255) NOT NULL AFTER id,MODIFY n VARCHAR(255) NOT NULL AFTER value;" +} +func (B) TableName() string { + return "b" +} +func (B) Columns() []string { + return []string{"id", "value", "n"} +} +func (v B) Values() []any { + return []any{int64(v.ID), (driver.Valuer)(v.Value), string(v.N)} +} +func (v *B) Addrs() []any { + return []any{types.Integer(&v.ID), &v.Value, types.String(&v.N)} +} diff --git a/examples/testcase/struct-field/valuer/generated.go.tpl b/examples/testcase/struct-field/valuer/generated.go.tpl new file mode 100644 index 0000000..ec1f803 --- /dev/null +++ b/examples/testcase/struct-field/valuer/generated.go.tpl @@ -0,0 +1,28 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package valuer + +import ( + "database/sql/driver" + + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (B) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS b (id BIGINT NOT NULL,value VARCHAR(255) NOT NULL,n VARCHAR(255) NOT NULL);" +} +func (B) AlterTableStmt() string { + return "ALTER TABLE b MODIFY id BIGINT NOT NULL,MODIFY value VARCHAR(255) NOT NULL AFTER id,MODIFY n VARCHAR(255) NOT NULL AFTER value;" +} +func (B) TableName() string { + return "b" +} +func (B) Columns() []string { + return []string{"id", "value", "n"} +} +func (v B) Values() []any { + return []any{int64(v.ID), (driver.Valuer)(v.Value), string(v.N)} +} +func (v *B) Addrs() []any { + return []any{types.Integer(&v.ID), &v.Value, types.String(&v.N)} +} diff --git a/examples/testcase/struct/alias/alias.go b/examples/testcase/struct/alias/alias.go new file mode 100644 index 0000000..cdc7c48 --- /dev/null +++ b/examples/testcase/struct/alias/alias.go @@ -0,0 +1,7 @@ +package aliasstruct + +import ( + "cloud.google.com/go/civil" +) + +type A = civil.DateTime diff --git a/examples/testcase/struct/alias/generated.go b/examples/testcase/struct/alias/generated.go new file mode 100644 index 0000000..5481f2c --- /dev/null +++ b/examples/testcase/struct/alias/generated.go @@ -0,0 +1,3 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package aliasstruct diff --git a/examples/testcase/struct/alias/generated.go.tpl b/examples/testcase/struct/alias/generated.go.tpl new file mode 100644 index 0000000..5481f2c --- /dev/null +++ b/examples/testcase/struct/alias/generated.go.tpl @@ -0,0 +1,3 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package aliasstruct diff --git a/examples/testcase/struct/embedded/generated.go b/examples/testcase/struct/embedded/generated.go index bfe76aa..f5ca28e 100644 --- a/examples/testcase/struct/embedded/generated.go +++ b/examples/testcase/struct/embedded/generated.go @@ -1,3 +1,5 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + package embedded import ( diff --git a/examples/testcase/struct/embedded/generated.go.tpl b/examples/testcase/struct/embedded/generated.go.tpl new file mode 100644 index 0000000..f5ca28e --- /dev/null +++ b/examples/testcase/struct/embedded/generated.go.tpl @@ -0,0 +1,26 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package embedded + +import ( + "github.com/si3nloong/sqlgen/sequel/types" +) + +func (B) CreateTableStmt() string { + return "CREATE TABLE IF NOT EXISTS b (id BIGINT NOT NULL,name VARCHAR(255) NOT NULL,z TINYINT NOT NULL);" +} +func (B) AlterTableStmt() string { + return "ALTER TABLE b MODIFY id BIGINT NOT NULL,MODIFY name VARCHAR(255) NOT NULL AFTER id,MODIFY z TINYINT NOT NULL AFTER name;" +} +func (B) TableName() string { + return "b" +} +func (B) Columns() []string { + return []string{"id", "name", "z"} +} +func (v B) Values() []any { + return []any{int64(v.a.ID), string(v.a.Name), bool(v.a.Z)} +} +func (v *B) Addrs() []any { + return []any{types.Integer(&v.a.ID), types.String(&v.a.Name), types.Bool(&v.a.Z)} +} diff --git a/examples/testcase/struct/empty/generated.go b/examples/testcase/struct/empty/generated.go new file mode 100644 index 0000000..47d1f0d --- /dev/null +++ b/examples/testcase/struct/empty/generated.go @@ -0,0 +1,3 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package emptystruct diff --git a/examples/testcase/struct/empty/generated.go.tpl b/examples/testcase/struct/empty/generated.go.tpl new file mode 100644 index 0000000..47d1f0d --- /dev/null +++ b/examples/testcase/struct/empty/generated.go.tpl @@ -0,0 +1,3 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package emptystruct diff --git a/examples/testcase/struct/private/generated.go b/examples/testcase/struct/private/generated.go new file mode 100644 index 0000000..4bff5c9 --- /dev/null +++ b/examples/testcase/struct/private/generated.go @@ -0,0 +1,3 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package privatestruct diff --git a/examples/testcase/struct/private/generated.go.tpl b/examples/testcase/struct/private/generated.go.tpl new file mode 100644 index 0000000..4bff5c9 --- /dev/null +++ b/examples/testcase/struct/private/generated.go.tpl @@ -0,0 +1,3 @@ +// Code generated by sqlgen, version 1.0.0. DO NOT EDIT. + +package privatestruct diff --git a/examples/testcase/struct/private/model.go b/examples/testcase/struct/private/model.go new file mode 100644 index 0000000..1d33a3c --- /dev/null +++ b/examples/testcase/struct/private/model.go @@ -0,0 +1,9 @@ +package privatestruct + +type a struct { + ID int64 +} + +type b struct { + ID string +} diff --git a/examples/testdata_test.go b/examples/testdata_test.go index 5840c41..184001c 100644 --- a/examples/testdata_test.go +++ b/examples/testdata_test.go @@ -1,6 +1,7 @@ package examples_test import ( + "fmt" "io/fs" "os" "path/filepath" @@ -8,6 +9,7 @@ import ( "github.com/si3nloong/sqlgen/codegen" "github.com/si3nloong/sqlgen/codegen/config" + "github.com/si3nloong/sqlgen/internal/fileutil" "github.com/stretchr/testify/require" ) @@ -19,27 +21,30 @@ func TestAll(t *testing.T) { } // Re-generate all files - if err := filepath.Walk(".", func(path string, info fs.FileInfo, e error) error { + if err := filepath.Walk("./testcase", func(path string, info fs.FileInfo, e error) error { if e != nil { return e } + if !info.IsDir() { + return nil + } - if info.IsDir() || filepath.Base(path) != "generated.go" { + if fileutil.IsDirEmptyFiles(path) { return nil } - actual, err := os.ReadFile(path) + actual, err := os.ReadFile(filepath.Join(path, config.DefaultGeneratedFile)) if err != nil { return err } // Read result file - expected, err := os.ReadFile(path + ".gotpl") + expected, err := os.ReadFile(filepath.Join(path, config.DefaultGeneratedFile+".tpl")) if err != nil { - return err + return fmt.Errorf("%w, happened in directory %q", err, path) } - t.Run(path, func(t *testing.T) { + t.Run("Compare the []byte in directory "+path, func(t *testing.T) { require.Equal(t, expected, actual) }) diff --git a/go.work.sum b/go.work.sum index 5b43b1b..060e4be 100644 --- a/go.work.sum +++ b/go.work.sum @@ -1,5 +1,26 @@ +cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA= +cloud.google.com/go/storage v1.30.1/go.mod h1:NfxhC0UJE1aXSx7CIIbCf7y9HKT7BiccwkR7+P7gN8E= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/martian/v3 v3.3.2/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk= +github.com/google/s2a-go v0.1.4/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkjEwM0A= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= +golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= +google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=