Skip to content

Commit

Permalink
don't set fields in query structs if fields have already been set
Browse files Browse the repository at this point in the history
  • Loading branch information
bokwoon95 committed May 9, 2024
1 parent 534360c commit e1322f1
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 32 deletions.
7 changes: 5 additions & 2 deletions delete_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,11 @@ func (q DeleteQuery) Where(predicates ...Predicate) DeleteQuery {
func (q DeleteQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
switch q.Dialect {
case DialectPostgres, DialectSQLite:
q.ReturningFields = fields
return q, true
if len(q.ReturningFields) == 0 {
q.ReturningFields = fields
return q, true
}
return q, false
default:
return q, false
}
Expand Down
28 changes: 24 additions & 4 deletions delete_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ func TestSQLiteDeleteQuery(t *testing.T) {
t.Error(testutil.Callers(), diff)
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if !ok {
t.Fatal(testutil.Callers(), "not ok")
t.Fatal(testutil.Callers(), "field should have been set")
}
})

Expand Down Expand Up @@ -72,8 +77,13 @@ func TestPostgresDeleteQuery(t *testing.T) {
t.Error(testutil.Callers(), diff)
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if !ok {
t.Fatal(testutil.Callers(), "not ok")
t.Fatal(testutil.Callers(), "field should have been set")
}
})

Expand Down Expand Up @@ -140,7 +150,12 @@ func TestMySQLDeleteQuery(t *testing.T) {
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Error(testutil.Callers(), "expected not ok but got ok")
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
})

Expand Down Expand Up @@ -236,7 +251,12 @@ func TestSQLServerDeleteQuery(t *testing.T) {
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Error(testutil.Callers(), "expected not ok but got ok")
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
})

Expand Down
7 changes: 5 additions & 2 deletions insert_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,11 @@ func (c ConflictClause) WriteSQL(ctx context.Context, dialect string, buf *bytes
func (q InsertQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
switch q.Dialect {
case DialectPostgres, DialectSQLite:
q.ReturningFields = fields
return q, true
if len(q.ReturningFields) == 0 {
q.ReturningFields = fields
return q, true
}
return q, false
default:
return q, false
}
Expand Down
28 changes: 24 additions & 4 deletions insert_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ func TestSQLiteInsertQuery(t *testing.T) {
t.Error(testutil.Callers(), diff)
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if !ok {
t.Fatal(testutil.Callers(), "not ok")
t.Fatal(testutil.Callers(), "field should have been set")
}
})

Expand Down Expand Up @@ -156,8 +161,13 @@ func TestPostgresInsertQuery(t *testing.T) {
t.Error(testutil.Callers(), diff)
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if !ok {
t.Fatal(testutil.Callers(), "not ok")
t.Fatal(testutil.Callers(), "field should have been set")
}
})

Expand Down Expand Up @@ -285,7 +295,12 @@ func TestMySQLInsertQuery(t *testing.T) {
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Error(testutil.Callers(), "expected not ok but got ok")
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
})

Expand Down Expand Up @@ -408,7 +423,12 @@ func TestSQLServerInsertQuery(t *testing.T) {
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Error(testutil.Callers(), "expected not ok but got ok")
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
})

Expand Down
35 changes: 25 additions & 10 deletions select_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,11 @@ func (q SelectQuery) Field(name string) AnyField {

// SetFetchableFields implements the Query interface.
func (q SelectQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
q.SelectFields = fields
return q, true
if len(q.SelectFields) == 0 {
q.SelectFields = fields
return q, true
}
return q, false
}

// GetFetchableFields returns the fetchable fields of the query.
Expand Down Expand Up @@ -626,8 +629,11 @@ func (q SQLiteSelectQuery) Field(name string) AnyField {

// SetFetchableFields implements the Query interface.
func (q SQLiteSelectQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
q.SelectFields = fields
return q, true
if len(q.SelectFields) == 0 {
q.SelectFields = fields
return q, true
}
return q, false
}

// GetFetchableFields returns the fetchable fields of the query.
Expand Down Expand Up @@ -882,8 +888,11 @@ func (q PostgresSelectQuery) Field(name string) AnyField {

// SetFetchableFields implements the Query interface.
func (q PostgresSelectQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
q.SelectFields = fields
return q, true
if len(q.SelectFields) == 0 {
q.SelectFields = fields
return q, true
}
return q, false
}

// GetFetchableFields returns the fetchable fields of the query.
Expand Down Expand Up @@ -1120,8 +1129,11 @@ func (q MySQLSelectQuery) Field(name string) AnyField {

// SetFetchableFields implements the Query interface.
func (q MySQLSelectQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
q.SelectFields = fields
return q, true
if len(q.SelectFields) == 0 {
q.SelectFields = fields
return q, true
}
return q, false
}

// GetFetchableFields returns the fetchable fields of the query.
Expand Down Expand Up @@ -1363,8 +1375,11 @@ func (q SQLServerSelectQuery) Field(name string) AnyField {

// SetFetchableFields implements the Query interface.
func (q SQLServerSelectQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
q.SelectFields = fields
return q, true
if len(q.SelectFields) == 0 {
q.SelectFields = fields
return q, true
}
return q, false
}

// GetFetchableFields returns the fetchable fields of the query.
Expand Down
28 changes: 24 additions & 4 deletions select_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ func TestSQLiteSelectQuery(t *testing.T) {
t.Error(testutil.Callers(), diff)
}
_, ok := q1.SetFetchableFields([]Field{a.ACTOR_ID})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.SelectFields = q1.SelectFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.ACTOR_ID})
if !ok {
t.Fatal(testutil.Callers(), "not ok")
t.Fatal(testutil.Callers(), "field should have been set")
}
})

Expand Down Expand Up @@ -102,8 +107,13 @@ func TestPostgresSelectQuery(t *testing.T) {
t.Error(testutil.Callers(), diff)
}
_, ok := q1.SetFetchableFields([]Field{a.ACTOR_ID})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.SelectFields = q1.SelectFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.ACTOR_ID})
if !ok {
t.Fatal(testutil.Callers(), "not ok")
t.Fatal(testutil.Callers(), "field should have been set")
}
})

Expand Down Expand Up @@ -213,8 +223,13 @@ func TestMySQLSelectQuery(t *testing.T) {
t.Error(testutil.Callers(), diff)
}
_, ok := q1.SetFetchableFields([]Field{a.ACTOR_ID})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.SelectFields = q1.SelectFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.ACTOR_ID})
if !ok {
t.Fatal(testutil.Callers(), "not ok")
t.Fatal(testutil.Callers(), "field should have been set")
}
})

Expand Down Expand Up @@ -309,8 +324,13 @@ func TestSQLServerSelectQuery(t *testing.T) {
t.Error(testutil.Callers(), diff)
}
_, ok := q1.SetFetchableFields([]Field{a.ACTOR_ID})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.SelectFields = q1.SelectFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.ACTOR_ID})
if !ok {
t.Fatal(testutil.Callers(), "not ok")
t.Fatal(testutil.Callers(), "field should have been set")
}
})

Expand Down
7 changes: 5 additions & 2 deletions update_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,11 @@ func (q UpdateQuery) Where(predicates ...Predicate) UpdateQuery {
func (q UpdateQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
switch q.Dialect {
case DialectPostgres, DialectSQLite:
q.ReturningFields = fields
return q, true
if len(q.ReturningFields) == 0 {
q.ReturningFields = fields
return q, true
}
return q, false
default:
return q, false
}
Expand Down
28 changes: 24 additions & 4 deletions update_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ func TestSQLiteUpdateQuery(t *testing.T) {
t.Error(testutil.Callers(), diff)
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if !ok {
t.Fatal(testutil.Callers(), "not ok")
t.Fatal(testutil.Callers(), "field should have been set")
}
})

Expand Down Expand Up @@ -125,8 +130,13 @@ func TestPostgresUpdateQuery(t *testing.T) {
t.Error(testutil.Callers(), diff)
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if !ok {
t.Fatal(testutil.Callers(), "not ok")
t.Fatal(testutil.Callers(), "field should have been set")
}
})

Expand Down Expand Up @@ -225,7 +235,12 @@ func TestMySQLUpdateQuery(t *testing.T) {
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Error(testutil.Callers(), "expected not ok but got ok")
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
})

Expand Down Expand Up @@ -324,7 +339,12 @@ func TestSQLServerUpdateQuery(t *testing.T) {
}
_, ok := q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Error(testutil.Callers(), "expected not ok but got ok")
t.Fatal(testutil.Callers(), "field should not have been set")
}
q1.ReturningFields = q1.ReturningFields[:0]
_, ok = q1.SetFetchableFields([]Field{a.LAST_NAME})
if ok {
t.Fatal(testutil.Callers(), "field should not have been set")
}
})

Expand Down

0 comments on commit e1322f1

Please sign in to comment.