add Exists func

This commit is contained in:
tangpanqing
2022-12-29 09:38:42 +08:00
parent 7dcd57e3cd
commit 4e7c71a910
4 changed files with 39 additions and 1 deletions

BIN
README.md

Binary file not shown.

Binary file not shown.

View File

@@ -364,7 +364,7 @@ func (ex *Builder) Delete() (int64, error) {
return ex.ExecAffected(sqlStr, paramList...) return ex.ExecAffected(sqlStr, paramList...)
} }
// Truncate 清空记录, sqlte3不支持此操作 // Truncate 清空记录, sqlite3不支持此操作
func (ex *Builder) Truncate() (int64, error) { func (ex *Builder) Truncate() (int64, error) {
sqlStr := "TRUNCATE TABLE " + ex.tableName sqlStr := "TRUNCATE TABLE " + ex.tableName
if ex.driverName == model.Sqlite3 { if ex.driverName == model.Sqlite3 {
@@ -374,6 +374,27 @@ func (ex *Builder) Truncate() (int64, error) {
return ex.ExecAffected(sqlStr) return ex.ExecAffected(sqlStr)
} }
// Exists 存在某记录
func (ex *Builder) Exists() (bool, error) {
var obj IntStruct
err := ex.Select("1 as c").Limit(0, 1).GetOne(&obj)
if err != nil {
return false, err
}
if obj.C.Int64 == 1 {
return true, nil
} else {
return false, nil
}
}
// DoesntExist 不存在某记录
func (ex *Builder) DoesntExist() (bool, error) {
isE, err := ex.Exists()
return !isE, err
}
// Value 字段值 // Value 字段值
func (ex *Builder) Value(fieldName string, dest interface{}) error { func (ex *Builder) Value(fieldName string, dest interface{}) error {
ex.Select(fieldName).Limit(0, 1) ex.Select(fieldName).Limit(0, 1)

View File

@@ -79,7 +79,16 @@ func TestAll(t *testing.T) {
testGetOne(dbItem.DriverName, dbItem.DbLink, id) testGetOne(dbItem.DriverName, dbItem.DbLink, id)
testGetMany(dbItem.DriverName, dbItem.DbLink) testGetMany(dbItem.DriverName, dbItem.DbLink)
testUpdate(dbItem.DriverName, dbItem.DbLink, id) testUpdate(dbItem.DriverName, dbItem.DbLink, id)
isExists := testExists(dbItem.DriverName, dbItem.DbLink, id)
if isExists != true {
panic("应该存在,但是数据库不存在")
}
testDelete(dbItem.DriverName, dbItem.DbLink, id) testDelete(dbItem.DriverName, dbItem.DbLink, id)
isExists2 := testExists(dbItem.DriverName, dbItem.DbLink, id)
if isExists2 == true {
panic("应该不存在,但是数据库存在")
}
id2 := testInsert(dbItem.DriverName, dbItem.DbLink) id2 := testInsert(dbItem.DriverName, dbItem.DbLink)
testTable(dbItem.DriverName, dbItem.DbLink) testTable(dbItem.DriverName, dbItem.DbLink)
@@ -301,6 +310,14 @@ func testDelete(driver string, db *sql.DB, id int64) {
} }
} }
func testExists(driver string, db *sql.DB, id int64) bool {
exists, err := aorm.Use(db).Driver(driver).Debug(false).Where(&Person{Id: null.IntFrom(id)}).OrderBy("id", "DESC").Exists()
if err != nil {
panic(driver + " testExists " + "found err:" + err.Error())
}
return exists
}
func testTable(driver string, db *sql.DB) { func testTable(driver string, db *sql.DB) {
_, err := aorm.Use(db).Debug(false).Driver(driver).Table("person_1").Insert(&Person{Name: null.StringFrom("Cherry")}) _, err := aorm.Use(db).Debug(false).Driver(driver).Table("person_1").Insert(&Person{Name: null.StringFrom("Cherry")})
if err != nil { if err != nil {