Skip to content

Commit 167807d

Browse files
authored
Merge pull request #36 from blastrain/feature/fix-build-in-query-values
Call builer.Build in building SQL
2 parents 1e76738 + d3f899c commit 167807d

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

query_builder.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,8 @@ func (b *QueryBuilder) indexes() []string {
600600
return sortedIndexes
601601
}
602602

603-
func (b *QueryBuilder) SelectSQL(typ *Struct) (string, []interface{}) {
603+
func (b *QueryBuilder) SelectSQL(factory *ValueFactory, typ *Struct) (string, []interface{}) {
604+
b.Build(factory)
604605
where := []string{}
605606
args := []interface{}{}
606607
for _, condition := range b.conditions.conditions {
@@ -623,7 +624,8 @@ func (b *QueryBuilder) SelectSQL(typ *Struct) (string, []interface{}) {
623624
), args
624625
}
625626

626-
func (b *QueryBuilder) UpdateSQL(updateMap map[string]interface{}) (string, []interface{}) {
627+
func (b *QueryBuilder) UpdateSQL(factory *ValueFactory, updateMap map[string]interface{}) (string, []interface{}) {
628+
b.Build(factory)
627629
where := []string{}
628630
args := []interface{}{}
629631
for _, condition := range b.conditions.conditions {
@@ -640,7 +642,8 @@ func (b *QueryBuilder) UpdateSQL(updateMap map[string]interface{}) (string, []in
640642
return fmt.Sprintf("UPDATE `%s` SET %s WHERE %s", b.tableName, strings.Join(setList, ","), strings.Join(where, " AND ")), values
641643
}
642644

643-
func (b *QueryBuilder) DeleteSQL() (string, []interface{}) {
645+
func (b *QueryBuilder) DeleteSQL(factory *ValueFactory) (string, []interface{}) {
646+
b.Build(factory)
644647
where := []string{}
645648
args := []interface{}{}
646649
for _, condition := range b.conditions.conditions {

second_level_cache.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ func (c *SecondLevelCache) UpdateByQueryBuilder(ctx context.Context, tx *Tx, bui
968968
}
969969
foundValues = values
970970
} else {
971-
sql, args := builder.SelectSQL(c.typ)
971+
sql, args := builder.SelectSQL(c.valueFactory, c.typ)
972972
rows, err := tx.conn.QueryContext(ctx, sql, args...)
973973
if err != nil {
974974
return xerrors.Errorf("failed sql %s %v: %w", sql, args, err)
@@ -989,7 +989,7 @@ func (c *SecondLevelCache) UpdateByQueryBuilder(ctx context.Context, tx *Tx, bui
989989
log.GetFromDB(tx.id, sql, "", value)
990990
}
991991
}
992-
sql, values := builder.UpdateSQL(updateMap)
992+
sql, values := builder.UpdateSQL(c.valueFactory, updateMap)
993993
if _, err := tx.conn.ExecContext(ctx, sql, values...); err != nil {
994994
return xerrors.Errorf("failed update sql %s %v: %w", sql, values, err)
995995
}
@@ -1297,7 +1297,8 @@ func (c *SecondLevelCache) isUsedPrimaryKeyBuilder(queries *Queries) bool {
12971297
}
12981298

12991299
func (c *SecondLevelCache) deleteCacheFromSQL(ctx context.Context, tx *Tx, builder *QueryBuilder) (e error) {
1300-
sql, args := builder.SelectSQL(c.typ)
1300+
sql, args := builder.SelectSQL(c.valueFactory, c.typ)
1301+
13011302
rows, err := tx.conn.QueryContext(ctx, sql, args...)
13021303
if err != nil {
13031304
return xerrors.Errorf("failed sql %s %v: %w", sql, args, err)
@@ -1332,7 +1333,7 @@ func (c *SecondLevelCache) DeleteByQueryBuilder(ctx context.Context, tx *Tx, bui
13321333
return xerrors.Errorf("failed to delete cache by SQL: %w", err)
13331334
}
13341335
}
1335-
sql, args := builder.DeleteSQL()
1336+
sql, args := builder.DeleteSQL(c.valueFactory)
13361337
if _, err := tx.conn.ExecContext(ctx, sql, args...); err != nil {
13371338
return xerrors.Errorf("failed sql %s %v: %w", sql, args, err)
13381339
}
@@ -1355,7 +1356,7 @@ func (c *SecondLevelCache) DeleteByQueryBuilder(ctx context.Context, tx *Tx, bui
13551356
}
13561357
}
13571358
}
1358-
sql, args := builder.DeleteSQL()
1359+
sql, args := builder.DeleteSQL(c.valueFactory)
13591360
if _, err := tx.conn.ExecContext(ctx, sql, args...); err != nil {
13601361
return xerrors.Errorf("failed sql %s %v: %w", sql, args, err)
13611362
}
@@ -1409,7 +1410,7 @@ func (c *SecondLevelCache) deleteKeyByValue(tx *Tx, value *StructValue) error {
14091410
}
14101411

14111412
func (c *SecondLevelCache) findValuesByQueryBuilderWithoutCache(ctx context.Context, tx *Tx, builder *QueryBuilder) (ssv *StructSliceValue, e error) {
1412-
sql, args := builder.SelectSQL(c.typ)
1413+
sql, args := builder.SelectSQL(c.valueFactory, c.typ)
14131414
rows, err := tx.conn.QueryContext(ctx, sql, args...)
14141415
if err != nil {
14151416
return nil, xerrors.Errorf("failed sql %s %v: %w", sql, args, err)

tx_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ func TestTx_FindByQueryBuilderContext(t *testing.T) {
477477
tx, err := cache.Begin(conn)
478478
NoError(t, err)
479479
defer func() { NoError(t, tx.RollbackUnlessCommitted()) }()
480-
builder := NewQueryBuilder("user_logs").Eq("id", uint64(1)).Gte("content_id", uint64(1)).Lte("content_id", uint64(1))
480+
builder := NewQueryBuilder("user_logs").In("id", []uint64{1}).Gte("content_id", uint64(1)).Lte("content_id", uint64(1))
481481
var userLogs UserLogs
482482
NoError(t, tx.FindByQueryBuilderContext(context.Background(), builder, &userLogs))
483483
NoError(t, tx.Commit())

0 commit comments

Comments
 (0)