Skip to content

Commit

Permalink
Feature: Support db tag on insert and update (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
zagronitay authored Jan 19, 2024
1 parent 4d21ac6 commit f11b0c7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
15 changes: 8 additions & 7 deletions insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
type insertModel struct {
IntField int64 `db:"int_field"`
OtherValue string
SkipInsert bool `goqux:"skip_insert"`
SkipInsert bool `goqux:"skip_insert"`
DbTag string `db:"another_col_name"`
}

func TestBuildInsert(t *testing.T) {
Expand All @@ -25,8 +26,8 @@ func TestBuildInsert(t *testing.T) {
{
name: "simple_insert",
values: []any{insertModel{IntField: 5}},
expectedQuery: `INSERT INTO "insert_models" ("int_field", "other_value") VALUES ($1, $2)`,
expectedArgs: []interface{}{int64(5), ""},
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "int_field", "other_value") VALUES ($1, $2, $3)`,
expectedArgs: []interface{}{"", int64(5), ""},
expectedError: nil,
},
{
Expand All @@ -35,8 +36,8 @@ func TestBuildInsert(t *testing.T) {
insertModel{IntField: 5},
insertModel{IntField: 6},
},
expectedQuery: `INSERT INTO "insert_models" ("int_field", "other_value") VALUES ($1, $2), ($3, $4)`,
expectedArgs: []interface{}{int64(5), "", int64(6), ""},
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "int_field", "other_value") VALUES ($1, $2, $3), ($4, $5, $6)`,
expectedArgs: []interface{}{"", int64(5), "", "", int64(6), ""},
expectedError: nil,
},
{
Expand All @@ -45,8 +46,8 @@ func TestBuildInsert(t *testing.T) {
insertModel{IntField: 5},
},
opts: []goqux.InsertOption{goqux.WithInsertReturningAll()},
expectedQuery: `INSERT INTO "insert_models" ("int_field", "other_value") VALUES ($1, $2) RETURNING *`,
expectedArgs: []interface{}{int64(5), ""},
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "int_field", "other_value") VALUES ($1, $2, $3) RETURNING *`,
expectedArgs: []interface{}{"", int64(5), ""},
},
}
for _, tt := range testTables {
Expand Down
12 changes: 9 additions & 3 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ func encodeValues(v any, skipType string, skipZeroValues bool) map[string]SQLVal
if skipZeroValues && value.IsZero() {
continue
}

columnName := strcase.ToSnake(f.Name)
if dbTag := f.Tag.Get(tagNameDb); dbTag != "" {
columnName = dbTag
}

switch {
case strings.Contains(f.Tag.Get(tagName), defaultNowUtc):
values[strcase.ToSnake(f.Name)] = SQLValuer{time.Now().UTC()}
values[columnName] = SQLValuer{time.Now().UTC()}
case strings.Contains(f.Tag.Get(tagName), defaultNow):
values[strcase.ToSnake(f.Name)] = SQLValuer{time.Now()}
values[columnName] = SQLValuer{time.Now()}
default:
values[strcase.ToSnake(f.Name)] = SQLValuer{value.Interface()}
values[columnName] = SQLValuer{value.Interface()}
}
}
return values
Expand Down
7 changes: 4 additions & 3 deletions update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type updateModel struct {
IntField int
DbTag string `db:"another_col_name"`
}

func TestBuildUpdate(t *testing.T) {
Expand All @@ -23,9 +24,9 @@ func TestBuildUpdate(t *testing.T) {
}{
{
name: "simple_update",
dst: updateModel{IntField: 5},
expectedQuery: `UPDATE "update_models" SET "int_field"=$1`,
expectedArgs: []interface{}{int64(5)},
dst: updateModel{IntField: 5, DbTag: "test"},
expectedQuery: `UPDATE "update_models" SET "another_col_name"=$1,"int_field"=$2`,
expectedArgs: []interface{}{"test", int64(5)},
expectedError: nil,
},
{
Expand Down

0 comments on commit f11b0c7

Please sign in to comment.