mirror of
https://github.com/tangpanqing/aorm.git
synced 2025-10-17 21:30:42 +08:00
add Exists func
This commit is contained in:
BIN
README_zh.md
BIN
README_zh.md
Binary file not shown.
@@ -364,7 +364,7 @@ func (ex *Builder) Delete() (int64, error) {
|
||||
return ex.ExecAffected(sqlStr, paramList...)
|
||||
}
|
||||
|
||||
// Truncate 清空记录, sqlte3不支持此操作
|
||||
// Truncate 清空记录, sqlite3不支持此操作
|
||||
func (ex *Builder) Truncate() (int64, error) {
|
||||
sqlStr := "TRUNCATE TABLE " + ex.tableName
|
||||
if ex.driverName == model.Sqlite3 {
|
||||
@@ -374,6 +374,27 @@ func (ex *Builder) Truncate() (int64, error) {
|
||||
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 字段值
|
||||
func (ex *Builder) Value(fieldName string, dest interface{}) error {
|
||||
ex.Select(fieldName).Limit(0, 1)
|
||||
|
@@ -79,7 +79,16 @@ func TestAll(t *testing.T) {
|
||||
testGetOne(dbItem.DriverName, dbItem.DbLink, id)
|
||||
testGetMany(dbItem.DriverName, dbItem.DbLink)
|
||||
testUpdate(dbItem.DriverName, dbItem.DbLink, id)
|
||||
isExists := testExists(dbItem.DriverName, dbItem.DbLink, id)
|
||||
if isExists != true {
|
||||
panic("应该存在,但是数据库不存在")
|
||||
}
|
||||
|
||||
testDelete(dbItem.DriverName, dbItem.DbLink, id)
|
||||
isExists2 := testExists(dbItem.DriverName, dbItem.DbLink, id)
|
||||
if isExists2 == true {
|
||||
panic("应该不存在,但是数据库存在")
|
||||
}
|
||||
|
||||
id2 := testInsert(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) {
|
||||
_, err := aorm.Use(db).Debug(false).Driver(driver).Table("person_1").Insert(&Person{Name: null.StringFrom("Cherry")})
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user