diff --git a/delete_query.go b/delete_query.go index c8c0f09..bbfb275 100644 --- a/delete_query.go +++ b/delete_query.go @@ -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 } diff --git a/delete_query_test.go b/delete_query_test.go index 56345f4..6b2d209 100644 --- a/delete_query_test.go +++ b/delete_query_test.go @@ -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") } }) @@ -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") } }) @@ -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") } }) @@ -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") } }) diff --git a/insert_query.go b/insert_query.go index 624bc4f..a00b6c0 100644 --- a/insert_query.go +++ b/insert_query.go @@ -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 } diff --git a/insert_query_test.go b/insert_query_test.go index 955f372..4bee86b 100644 --- a/insert_query_test.go +++ b/insert_query_test.go @@ -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") } }) @@ -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") } }) @@ -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") } }) @@ -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") } }) diff --git a/select_query.go b/select_query.go index da0ef45..f3fcf60 100644 --- a/select_query.go +++ b/select_query.go @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. diff --git a/select_query_test.go b/select_query_test.go index 3748baa..140acaa 100644 --- a/select_query_test.go +++ b/select_query_test.go @@ -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") } }) @@ -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") } }) @@ -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") } }) @@ -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") } }) diff --git a/update_query.go b/update_query.go index fc85b05..9527893 100644 --- a/update_query.go +++ b/update_query.go @@ -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 } diff --git a/update_query_test.go b/update_query_test.go index bd7e6ab..13c32a1 100644 --- a/update_query_test.go +++ b/update_query_test.go @@ -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") } }) @@ -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") } }) @@ -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") } }) @@ -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") } })