Skip to content

Commit

Permalink
feat(database/gdb): add Exist support for checking records existance …
Browse files Browse the repository at this point in the history
…for certain condition (#3854)
  • Loading branch information
cyjaysong authored Oct 24, 2024
1 parent 656668f commit e4c4fb5
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 0 deletions.
13 changes: 13 additions & 0 deletions contrib/drivers/clickhouse/clickhouse_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ func Test_Model_Count(t *testing.T) {
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Where(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
12 changes: 12 additions & 0 deletions contrib/drivers/mssql/mssql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,19 @@ func Test_Model_Count(t *testing.T) {
t.AssertNil(err)
t.Assert(count, int64(TableSize))
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Select(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions contrib/drivers/mysql/mysql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,19 @@ func Test_Model_Count(t *testing.T) {
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Value_WithCache(t *testing.T) {
table := createTable()
defer dropTable(table)
Expand Down
13 changes: 13 additions & 0 deletions contrib/drivers/oracle/oracle_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,19 @@ func Test_Model_Count(t *testing.T) {

}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Select(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
13 changes: 13 additions & 0 deletions contrib/drivers/pgsql/pgsql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@ func Test_Model_Count(t *testing.T) {
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Where(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
13 changes: 13 additions & 0 deletions contrib/drivers/sqlite/sqlite_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,19 @@ func Test_Model_Count(t *testing.T) {
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Select(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
13 changes: 13 additions & 0 deletions contrib/drivers/sqlitecgo/sqlitecgo_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,19 @@ func Test_Model_Count(t *testing.T) {
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Select(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
19 changes: 19 additions & 0 deletions database/gdb/gdb_model_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,25 @@ func (m *Model) Count(where ...interface{}) (int, error) {
return 0, nil
}

// Exist does "SELECT 1 FROM ... LIMIT 1" statement for the model.
// The optional parameter `where` is the same as the parameter of Model.Where function,
// see Model.Where.
func (m *Model) Exist(where ...interface{}) (bool, error) {
if len(where) > 0 {
return m.Where(where[0], where[1:]...).Exist()
}
one, err := m.Fields(Raw("1")).One()
if err != nil {
return false, err
}
for _, val := range one {
if val.Bool() {
return true, nil
}
}
return false, nil
}

// CountColumn does "SELECT COUNT(x) FROM ..." statement for the model.
func (m *Model) CountColumn(column string) (int, error) {
if len(column) == 0 {
Expand Down

0 comments on commit e4c4fb5

Please sign in to comment.