diff --git a/golalib/golalib_test.go b/golalib/golalib_test.go index 7cc6701..98f79b4 100644 --- a/golalib/golalib_test.go +++ b/golalib/golalib_test.go @@ -20,19 +20,22 @@ import ( //go:embed testdata var fixtures embed.FS -var s *server.Server -var testDBPort int = 33066 -var testDBName string = "testdata" -var testTables = []string{ - "blogs", - "users", "songs", "song_user_favourites", - "profile", "account", - "room", - "gifts", "gifts_nn", - "gifts_with_default", "gifts_nn_with_default", - "wallet", - "worker", -} + +var ( + s *server.Server + testDBPort int = 33066 + testDBName string = "testdata" + testTables = []string{ + "blogs", + "users", "songs", "song_user_favourites", + "profile", "account", + "room", + "gifts", "gifts_nn", + "gifts_with_default", "gifts_nn_with_default", + "wallet", + "worker", + } +) var testDataPath = "testdata" + string(filepath.Separator) var update = flag.Bool("update", false, "update generated files") @@ -70,7 +73,7 @@ func testGen(t *testing.T, wd string, gen genMethod, data ormtpl.TplStruct) { os.Mkdir(expectedFileFolder, os.ModePerm) } - err := ioutil.WriteFile(testDataPath+path, data, 0644) + err := ioutil.WriteFile(testDataPath+path, data, 0o644) if err != nil { panic(err) } @@ -136,7 +139,7 @@ func TestIdx(t *testing.T) { tb := getOne(db.Tables, func(tb *structs.Table) bool { return tb.Name == "blogs" }) - if len(tb.Indexes) != 7 { + if len(tb.Indexes) != 8 { t.Error("Failed to parse blogs table's 7 indexes") } diff --git a/golalib/testdata/blogs.sql b/golalib/testdata/blogs.sql index 297f240..2912ac7 100644 --- a/golalib/testdata/blogs.sql +++ b/golalib/testdata/blogs.sql @@ -9,6 +9,7 @@ CREATE TABLE `blogs` ( `country` varchar(255) NOT NULL DEFAULT '' COMMENT 'Country of the blog user', `created_at` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Created Timestamp', `updated_at` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Updated Timestamp', + `count` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'count', PRIMARY KEY (`id`), KEY `user` (`user_id`), KEY `user_vip` (`user_id`, `is_vip`), @@ -16,5 +17,6 @@ CREATE TABLE `blogs` ( KEY `country_vip` (`country`, `is_vip`), KEY `cate_pinned` (`category_id`, `is_pinned`, `is_vip`), KEY `user_pinned_cate` (`user_id`, `is_pinned`, `category_id`), + KEY `user_id_count` (`user_id`, `count`), UNIQUE KEY `slug` (`slug`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4; diff --git a/golalib/testdata/blogs/blogs.go b/golalib/testdata/blogs/blogs.go index ffee1e0..79a5137 100644 --- a/golalib/testdata/blogs/blogs.go +++ b/golalib/testdata/blogs/blogs.go @@ -36,6 +36,8 @@ type Blog struct { CreatedAt `json:"created_at"` // Updated Timestamp int(11) unsigned UpdatedAt `json:"updated_at"` + // count int(11) unsigned + Count_ `json:"count"` } type withPK interface { @@ -694,6 +696,58 @@ func (c *UpdatedAt) UnmarshalJSON(data []byte) error { return nil } +// Count_ field +// count +type Count_ struct { + _updated bool + val uint +} + +func (c *Count_) GetCount() uint { + return c.val +} + +func (c *Count_) SetCount(val uint) bool { + if c.val == val { + return false + } + c._updated = true + c.val = val + return true +} + +func (c *Count_) IsUpdated() bool { + return c._updated +} + +func (c *Count_) resetUpdated() { + c._updated = false +} + +func (c *Count_) GetColumnName() string { + return "count" +} + +func (c *Count_) GetValPointer() any { + return &c.val +} + +func (c *Count_) getCountForDB() uint { + return c.val +} + +func (c *Count_) MarshalJSON() ([]byte, error) { + return json.Marshal(&c.val) +} + +func (c *Count_) UnmarshalJSON(data []byte) error { + if err := json.Unmarshal(data, &c.val); err != nil { + return err + } + + return nil +} + // New return new *Blog with default values func New() *Blog { return &Blog{ @@ -707,6 +761,7 @@ func New() *Blog { Country{}, CreatedAt{val: uint(0)}, UpdatedAt{val: uint(0)}, + Count_{val: uint(0)}, } } @@ -724,14 +779,15 @@ func NewWithPK(val int) *Blog { Country{}, CreatedAt{val: uint(0)}, UpdatedAt{val: uint(0)}, + Count_{val: uint(0)}, } c.Id.val = val c.Id.isAssigned = true return c } -const insertWithoutPK string = "INSERT INTO `blogs` (`user_id`, `slug`, `title`, `category_id`, `is_pinned`, `is_vip`, `country`, `created_at`, `updated_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)" -const insertWithPK string = "INSERT INTO `blogs` (`id`, `user_id`, `slug`, `title`, `category_id`, `is_pinned`, `is_vip`, `country`, `created_at`, `updated_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" +const insertWithoutPK string = "INSERT INTO `blogs` (`user_id`, `slug`, `title`, `category_id`, `is_pinned`, `is_vip`, `country`, `created_at`, `updated_at`, `count`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" +const insertWithPK string = "INSERT INTO `blogs` (`id`, `user_id`, `slug`, `title`, `category_id`, `is_pinned`, `is_vip`, `country`, `created_at`, `updated_at`, `count`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" // Insert Blog struct to `blogs` table // Deprecated: use the function with context @@ -739,12 +795,12 @@ func (c *Blog) Insert() error { var result sql.Result var err error if c.Id.isAssigned { - result, err = coredb.Exec(DBName, insertWithPK, c.getIdForDB(), c.getUserIdForDB(), c.getSlugForDB(), c.getTitleForDB(), c.getCategoryIdForDB(), c.getIsPinnedForDB(), c.getIsVipForDB(), c.getCountryForDB(), c.getCreatedAtForDB(), c.getUpdatedAtForDB()) + result, err = coredb.Exec(DBName, insertWithPK, c.getIdForDB(), c.getUserIdForDB(), c.getSlugForDB(), c.getTitleForDB(), c.getCategoryIdForDB(), c.getIsPinnedForDB(), c.getIsVipForDB(), c.getCountryForDB(), c.getCreatedAtForDB(), c.getUpdatedAtForDB(), c.getCountForDB()) if err != nil { return err } } else { - result, err = coredb.Exec(DBName, insertWithoutPK, c.getUserIdForDB(), c.getSlugForDB(), c.getTitleForDB(), c.getCategoryIdForDB(), c.getIsPinnedForDB(), c.getIsVipForDB(), c.getCountryForDB(), c.getCreatedAtForDB(), c.getUpdatedAtForDB()) + result, err = coredb.Exec(DBName, insertWithoutPK, c.getUserIdForDB(), c.getSlugForDB(), c.getTitleForDB(), c.getCategoryIdForDB(), c.getIsPinnedForDB(), c.getIsVipForDB(), c.getCountryForDB(), c.getCreatedAtForDB(), c.getUpdatedAtForDB(), c.getCountForDB()) if err != nil { return err } @@ -778,6 +834,7 @@ func (c *Blog) resetUpdated() { c.Country.resetUpdated() c.CreatedAt.resetUpdated() c.UpdatedAt.resetUpdated() + c.Count_.resetUpdated() } // Update Blog struct in `blogs` table @@ -821,6 +878,10 @@ func (obj *Blog) Update() (bool, error) { updatedFields = append(updatedFields, "`updated_at` = ?") params = append(params, obj.getUpdatedAtForDB()) } + if obj.Count_.IsUpdated() { + updatedFields = append(updatedFields, "`count` = ?") + params = append(params, obj.getCountForDB()) + } if len(updatedFields) == 0 { return false, nil @@ -916,6 +977,12 @@ func Update(obj withPK) (bool, error) { params = append(params, c.getUpdatedAtForDB()) resetFuncs = append(resetFuncs, c.resetUpdated) } + case *Count_: + if c.IsUpdated() { + updatedFields = append(updatedFields, "`count` = ?") + params = append(params, c.getCountForDB()) + resetFuncs = append(resetFuncs, c.resetUpdated) + } } } diff --git a/golalib/testdata/blogs/blogs_ctx.go b/golalib/testdata/blogs/blogs_ctx.go index aa20f75..fd743cd 100644 --- a/golalib/testdata/blogs/blogs_ctx.go +++ b/golalib/testdata/blogs/blogs_ctx.go @@ -128,12 +128,12 @@ func (c *Blog) InsertCtx(ctx context.Context) error { var result sql.Result var err error if c.Id.isAssigned { - result, err = coredb.ExecCtx(ctx, DBName, insertWithPK, c.getIdForDB(), c.getUserIdForDB(), c.getSlugForDB(), c.getTitleForDB(), c.getCategoryIdForDB(), c.getIsPinnedForDB(), c.getIsVipForDB(), c.getCountryForDB(), c.getCreatedAtForDB(), c.getUpdatedAtForDB()) + result, err = coredb.ExecCtx(ctx, DBName, insertWithPK, c.getIdForDB(), c.getUserIdForDB(), c.getSlugForDB(), c.getTitleForDB(), c.getCategoryIdForDB(), c.getIsPinnedForDB(), c.getIsVipForDB(), c.getCountryForDB(), c.getCreatedAtForDB(), c.getUpdatedAtForDB(), c.getCountForDB()) if err != nil { return err } } else { - result, err = coredb.ExecCtx(ctx, DBName, insertWithoutPK, c.getUserIdForDB(), c.getSlugForDB(), c.getTitleForDB(), c.getCategoryIdForDB(), c.getIsPinnedForDB(), c.getIsVipForDB(), c.getCountryForDB(), c.getCreatedAtForDB(), c.getUpdatedAtForDB()) + result, err = coredb.ExecCtx(ctx, DBName, insertWithoutPK, c.getUserIdForDB(), c.getSlugForDB(), c.getTitleForDB(), c.getCategoryIdForDB(), c.getIsPinnedForDB(), c.getIsVipForDB(), c.getCountryForDB(), c.getCreatedAtForDB(), c.getUpdatedAtForDB(), c.getCountForDB()) if err != nil { return err } @@ -197,6 +197,10 @@ func (obj *Blog) UpdateCtx(ctx context.Context) (bool, error) { updatedFields = append(updatedFields, "`updated_at` = ?") params = append(params, obj.getUpdatedAtForDB()) } + if obj.Count_.IsUpdated() { + updatedFields = append(updatedFields, "`count` = ?") + params = append(params, obj.getCountForDB()) + } if len(updatedFields) == 0 { return false, nil @@ -291,6 +295,12 @@ func UpdateCtx(ctx context.Context, obj withPK) (bool, error) { params = append(params, c.getUpdatedAtForDB()) resetFuncs = append(resetFuncs, c.resetUpdated) } + case *Count_: + if c.IsUpdated() { + updatedFields = append(updatedFields, "`count` = ?") + params = append(params, c.getCountForDB()) + resetFuncs = append(resetFuncs, c.resetUpdated) + } } } diff --git a/golalib/testdata/blogs/blogs_idx.go b/golalib/testdata/blogs/blogs_idx.go index 5323ac7..1603684 100644 --- a/golalib/testdata/blogs/blogs_idx.go +++ b/golalib/testdata/blogs/blogs_idx.go @@ -40,6 +40,8 @@ const ( CreatedAtDesc UpdatedAtAsc UpdatedAtDesc + CountAsc + CountDesc ) func (q *idxQuery[T]) OrderBy(args ...orderBy) coredb.ReadQuery[T] { @@ -86,6 +88,10 @@ func (q *idxQuery[T]) OrderBy(args ...orderBy) coredb.ReadQuery[T] { q.orders[i] = "`updated_at` asc" case UpdatedAtDesc: q.orders[i] = "`updated_at` desc" + case CountAsc: + q.orders[i] = "`count` asc" + case CountDesc: + q.orders[i] = "`count` desc" } } return q @@ -164,14 +170,16 @@ type iQuery5[T any] interface { } type iQuery9[T any] interface { - AndIsPinnedEQ(val bool) iQuery10[T] - AndIsPinnedIN(vals ...bool) iQuery10[T] + AndCountEQ(val uint) orderReadQuery[T] + AndCountIN(vals ...uint) orderReadQuery[T] + AndIsPinnedEQ(val bool) iQuery11[T] + AndIsPinnedIN(vals ...bool) iQuery11[T] AndIsVipEQ(val bool) orderReadQuery[T] AndIsVipIN(vals ...bool) orderReadQuery[T] orderReadQuery[T] } -type iQuery10[T any] interface { +type iQuery11[T any] interface { AndCategoryIdEQ(val int) orderReadQuery[T] AndCategoryIdIN(vals ...int) orderReadQuery[T] orderReadQuery[T] @@ -267,18 +275,32 @@ type idxQuery9[T any] struct { *idxQuery[T] } -func (q *idxQuery9[T]) AndIsPinnedEQ(val bool) iQuery10[T] { +func (q *idxQuery9[T]) AndCountEQ(val uint) orderReadQuery[T] { + q.whereSql += " and `count` = ?" + q.whereParams = append(q.whereParams, val) + return q.idxQuery +} + +func (q *idxQuery9[T]) AndCountIN(vals ...uint) orderReadQuery[T] { + q.whereSql += " and `count` in (" + coredb.GetParamPlaceHolder(len(vals)) + ")" + for _, val := range vals { + q.whereParams = append(q.whereParams, val) + } + return q.idxQuery +} + +func (q *idxQuery9[T]) AndIsPinnedEQ(val bool) iQuery11[T] { q.whereSql += " and `is_pinned` = ?" q.whereParams = append(q.whereParams, val) - return &idxQuery10[T]{q.idxQuery} + return &idxQuery11[T]{q.idxQuery} } -func (q *idxQuery9[T]) AndIsPinnedIN(vals ...bool) iQuery10[T] { +func (q *idxQuery9[T]) AndIsPinnedIN(vals ...bool) iQuery11[T] { q.whereSql += " and `is_pinned` in (" + coredb.GetParamPlaceHolder(len(vals)) + ")" for _, val := range vals { q.whereParams = append(q.whereParams, val) } - return &idxQuery10[T]{q.idxQuery} + return &idxQuery11[T]{q.idxQuery} } func (q *idxQuery9[T]) AndIsVipEQ(val bool) orderReadQuery[T] { @@ -295,17 +317,17 @@ func (q *idxQuery9[T]) AndIsVipIN(vals ...bool) orderReadQuery[T] { return q.idxQuery } -type idxQuery10[T any] struct { +type idxQuery11[T any] struct { *idxQuery[T] } -func (q *idxQuery10[T]) AndCategoryIdEQ(val int) orderReadQuery[T] { +func (q *idxQuery11[T]) AndCategoryIdEQ(val int) orderReadQuery[T] { q.whereSql += " and `category_id` = ?" q.whereParams = append(q.whereParams, val) return q.idxQuery } -func (q *idxQuery10[T]) AndCategoryIdIN(vals ...int) orderReadQuery[T] { +func (q *idxQuery11[T]) AndCategoryIdIN(vals ...int) orderReadQuery[T] { q.whereSql += " and `category_id` in (" + coredb.GetParamPlaceHolder(len(vals)) + ")" for _, val := range vals { q.whereParams = append(q.whereParams, val) diff --git a/ormtpl/00_struct.gogo b/ormtpl/00_struct.gogo index 12509c8..8c60fb5 100644 --- a/ormtpl/00_struct.gogo +++ b/ormtpl/00_struct.gogo @@ -20,14 +20,14 @@ const TableName string = "{{.Name}}" type {{.ClassName}} struct { {{- range .Columns }} // {{.Comment}} {{.FullDBType}} - {{.GoName}} `json:"{{.Name}}"` + {{.GoTypeName}} `json:"{{.Name}}"` {{- end }} } {{- if .HasCompositePrimaryKey }} type PK struct { {{- range .GetPKColumns }} - {{.GoName}} {{.GoType}} + {{.GoTypeName}} {{.GoType}} {{- end }} } {{- end}} @@ -208,15 +208,15 @@ var {{$table.ClassName}}{{.GoName}}List = []string{ {{- range .Columns }} -// {{.GoName}} field +// {{.GoTypeName}} field // {{.Comment}} {{- if .IsSet}} {{- if .IsNullable}} -type {{.GoName}} struct { +type {{.GoTypeName}} struct { _updated bool val goption.Option[string] } -func (c *{{.GoName}}) Get{{.GoName}}() {{.GoSetNullableType}} { +func (c *{{.GoTypeName}}) Get{{.GoName}}() {{.GoSetNullableType}} { if !c.val.Ok() { return goption.None[[]{{.GoSetEnumType}}]() } @@ -231,7 +231,7 @@ func (c *{{.GoName}}) Get{{.GoName}}() {{.GoSetNullableType}} { return goption.Some(valSlice) } -func (c *{{.GoName}}) Set{{.GoName}}(val {{.GoSetNullableType}}) bool { +func (c *{{.GoTypeName}}) Set{{.GoName}}(val {{.GoSetNullableType}}) bool { if !val.Ok(){ c.val = goption.None[string]() } @@ -245,11 +245,11 @@ func (c *{{.GoName}}) Set{{.GoName}}(val {{.GoSetNullableType}}) bool { } {{- else}} -type {{.GoName}} struct { +type {{.GoTypeName}} struct { _updated bool val string } -func (c *{{.GoName}}) Get{{.GoName}}() []{{.GoSetEnumType}} { +func (c *{{.GoTypeName}}) Get{{.GoName}}() []{{.GoSetEnumType}} { strSlice := strings.Split(c.val, ",") if len(strSlice) == 1 && !coredb.ValueInSet({{$table.ClassName}}{{.GoName}}List, strSlice[0]) { return []{{.GoSetEnumType}}{} @@ -261,7 +261,7 @@ func (c *{{.GoName}}) Get{{.GoName}}() []{{.GoSetEnumType}} { return valSlice } -func (c *{{.GoName}}) Set{{.GoName}}(val []{{.GoSetEnumType}}) bool { +func (c *{{.GoTypeName}}) Set{{.GoName}}(val []{{.GoSetEnumType}}) bool { strSlice := make([]string, 0, len(val)) for _, v := range val { strSlice = append(strSlice, string(v)) @@ -273,7 +273,7 @@ func (c *{{.GoName}}) Set{{.GoName}}(val []{{.GoSetEnumType}}) bool { {{- end}} {{else}} -type {{.GoName}} struct { +type {{.GoTypeName}} struct { {{- if .IsPrimaryKey}} {{- if $table.IsPKAutoGenerated }} isAssigned bool @@ -284,7 +284,7 @@ type {{.GoName}} struct { val {{.ValType}} } -func (c *{{.GoName}}) Get{{.GoName}}() {{.GoType}} { +func (c *{{.GoTypeName}}) Get{{.GoName}}() {{.GoType}} { {{- if .IsNullableBool}} if !c.val.Ok() { return goption.None[bool]() @@ -295,7 +295,7 @@ func (c *{{.GoName}}) Get{{.GoName}}() {{.GoType}} { {{- end}} } {{ if not .IsPrimaryKey}} -func (c *{{.GoName}}) Set{{.GoName}}(val {{.GoType}}) bool { +func (c *{{.GoTypeName}}) Set{{.GoName}}(val {{.GoType}}) bool { {{- if and (ne .GoType "[]byte") (ne .GoType "goption.Option[[]byte]") (not .IsNullableBool)}} if c.val == val { return false @@ -332,32 +332,32 @@ func (c *{{.GoName}}) Set{{.GoName}}(val {{.GoType}}) bool { {{end}} {{- if not .IsPrimaryKey}} -func (c *{{.GoName}}) IsUpdated() bool { +func (c *{{.GoTypeName}}) IsUpdated() bool { return c._updated } -func (c *{{.GoName}}) resetUpdated() { +func (c *{{.GoTypeName}}) resetUpdated() { c._updated = false } {{- end}} -func (c *{{.GoName}}) GetColumnName() string { +func (c *{{.GoTypeName}}) GetColumnName() string { return "{{.Name}}" } -func (c *{{.GoName}}) GetValPointer() any { +func (c *{{.GoTypeName}}) GetValPointer() any { return &c.val } -func (c *{{.GoName}}) get{{.GoName}}ForDB() {{.ValType}} { +func (c *{{.GoTypeName}}) get{{.GoName}}ForDB() {{.ValType}} { return c.val } -func (c *{{.GoName}}) MarshalJSON() ([]byte, error) { +func (c *{{.GoTypeName}}) MarshalJSON() ([]byte, error) { return json.Marshal(&c.val) } -func (c *{{.GoName}}) UnmarshalJSON(data []byte) error { +func (c *{{.GoTypeName}}) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(data, &c.val); err != nil { return err } @@ -372,9 +372,9 @@ func New() *{{.ClassName}} { return &{{.ClassName}}{ {{- range .Columns }} {{- if .HasDefault}} - {{.GoName}}{val:{{.GoDefaultValue}}}, + {{.GoTypeName}}{val:{{.GoDefaultValue}}}, {{- else}} - {{.GoName}}{}, + {{.GoTypeName}}{}, {{- end }} {{- end }} } @@ -387,16 +387,16 @@ func NewWithPK(val {{.GetPrimaryKeyType}}) *{{.ClassName}} { c := &{{.ClassName}}{ {{- range .Columns }} {{- if .HasDefault}} - {{.GoName}}{val:{{.GoDefaultValue}}}, + {{.GoTypeName}}{val:{{.GoDefaultValue}}}, {{- else}} - {{.GoName}}{}, + {{.GoTypeName}}{}, {{- end }} {{- end }} } {{- if .HasCompositePrimaryKey }} {{- range $i, $c := .GetPKColumns }} - c.{{ $c.GoName }}.val = val.{{ $c.GoName }} + c.{{ $c.GoTypeName }}.val = val.{{ $c.GoTypeName }} {{- end }} {{- else }} c.{{.GetPrimaryKey}}.val = val @@ -480,7 +480,7 @@ func (c *{{.ClassName}}) Insert() error { func (c *{{.ClassName}}) resetUpdated() { {{- range .GetNonPKColumns }} - c.{{.GoName}}.resetUpdated() + c.{{.GoTypeName}}.resetUpdated() {{- end }} } @@ -491,7 +491,7 @@ func (obj *{{.ClassName}}) Update() (bool, error) { var params []any {{- range $i, $c := .GetNonPKColumns }} - if obj.{{ $c.GoName }}.IsUpdated() { + if obj.{{ $c.GoTypeName }}.IsUpdated() { updatedFields = append(updatedFields, "`{{ $c.Name }}` = ?") params = append(params, obj.get{{$c.GoName}}ForDB()) } @@ -538,7 +538,7 @@ func Update(obj withPK) (bool, error) { switch c := col.(type) { {{- range $i, $c := .GetNonPKColumns }} - case *{{.GoName}}: + case *{{.GoTypeName}}: if c.IsUpdated() { updatedFields = append(updatedFields, "`{{ $c.Name }}` = ?") params = append(params, c.get{{$c.GoName}}ForDB()) diff --git a/ormtpl/00_struct_ctx.gogo b/ormtpl/00_struct_ctx.gogo index e764f44..d4b631d 100644 --- a/ormtpl/00_struct_ctx.gogo +++ b/ormtpl/00_struct_ctx.gogo @@ -177,7 +177,7 @@ func (obj *{{.ClassName}}) UpdateCtx(ctx context.Context) (bool, error) { var params []any {{- range $i, $c := .GetNonPKColumns }} - if obj.{{ $c.GoName }}.IsUpdated() { + if obj.{{ $c.GoTypeName }}.IsUpdated() { updatedFields = append(updatedFields, "`{{ $c.Name }}` = ?") params = append(params, obj.get{{$c.GoName}}ForDB()) } @@ -223,7 +223,7 @@ func UpdateCtx(ctx context.Context, obj withPK) (bool, error) { switch c := col.(type) { {{- range $i, $c := .GetNonPKColumns }} - case *{{.GoName}}: + case *{{.GoTypeName}}: if c.IsUpdated() { updatedFields = append(updatedFields, "`{{ $c.Name }}` = ?") params = append(params, c.get{{$c.GoName}}ForDB()) diff --git a/structs/column.go b/structs/column.go index 008e46b..5899101 100644 --- a/structs/column.go +++ b/structs/column.go @@ -63,9 +63,11 @@ var dbTypeToGoTypes = map[string]string{ func (c Column) GoSetEnumType() string { return c.Table.ClassName() + c.GoName() } + func (c Column) GoSetNullableType() string { return fmt.Sprintf("goption.Option[[]%s]", c.Table.ClassName()+c.GoName()) } + func (c Column) GoEnumNullableType() string { return fmt.Sprintf("goption.Option[%s]", c.Table.ClassName()+c.GoName()) } @@ -181,6 +183,15 @@ func (c Column) GoName() string { return getGoName(c.Name) } +// GoName returns the variable name for go of the column +func (c Column) GoTypeName() string { + goName := c.GoName() + if goName == "Count" { + goName = "Count_" + } + return goName +} + // IsNullable returns if the column is nullable as string func (c Column) IsNullable() bool { return c.Nullable diff --git a/structs/table.go b/structs/table.go index e1c0c92..7610fc9 100644 --- a/structs/table.go +++ b/structs/table.go @@ -56,7 +56,7 @@ func (t *Table) ClassName() string { func (t *Table) GetPrimaryKey() string { for _, c := range t.Columns { if c.IsPrimaryKey() { - return c.GoName() + return c.GoTypeName() } } @@ -111,7 +111,7 @@ func (t *Table) GetPrimaryKeyVals() string { var result []string for _, c := range cols { - result = append(result, "val."+c.GoName()) + result = append(result, "val."+c.GoTypeName()) } return strings.Join(result, ", ") @@ -224,7 +224,6 @@ func (t *Table) GetIndexNodes() []*IdxNode { } nodes = append(nodes, n) } - } return nodes } diff --git a/tests/blog_test.go b/tests/blog_test.go index 75c19e2..31e2dd8 100644 --- a/tests/blog_test.go +++ b/tests/blog_test.go @@ -9,6 +9,7 @@ import ( func TestBlogMethods(t *testing.T) { blog := blogs.New() blog.SetTitle("foo") + blog.SetCount(55) e := blog.Insert() if e != nil { t.Error(e) @@ -17,6 +18,9 @@ func TestBlogMethods(t *testing.T) { if blog.GetId() != 1 { t.Error("Insert blog 1 failed") } + if blog.GetCount() != 55 { + t.Error("Set count failed") + } e = blogs.DeleteByPK(blog.GetId()) if e != nil { @@ -29,10 +33,12 @@ func TestBlogMethods(t *testing.T) { blog = blogs.New() blog.SetTitle("foo") + blog.SetCount(99) blog.Insert() blog = blogs.New() blog.SetTitle("bar") + blog.SetCount(88) e = blog.Insert() if e != nil { t.Error(e) @@ -108,6 +114,7 @@ func TestBlogSelect(t *testing.T) { objs := blogs.SelectFields[struct { blogs.Id blogs.Title + blogs.Count_ }]().OrderBy(blogs.IdAsc).AllFromMaster() if len(objs) != 2 { @@ -117,14 +124,21 @@ func TestBlogSelect(t *testing.T) { if objs[0].GetTitle() != "foo" { t.Error("Read blog 1 failed") } + if objs[0].GetCount() != 99 { + t.Error("Read blog 1 failed") + } if objs[1].GetTitle() != "bar" { t.Error("Read blog 2 failed") } + if objs[1].GetCount() != 88 { + t.Error("Read blog 2 failed") + } objs = blogs.SelectFields[struct { blogs.Id blogs.Title + blogs.Count_ }]().OrderBy(blogs.IdDesc).All() if len(objs) != 2 {