1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00

feat(database/gdb): add Exist support for checking records existance for certain condition (#3854)

This commit is contained in:
CyJaySong 2024-10-24 15:29:03 +08:00 committed by GitHub
parent 656668f444
commit e4c4fb591e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 109 additions and 0 deletions

View File

@ -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)

View File

@ -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) {

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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 {