-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
60 lines (50 loc) · 1.74 KB
/
sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package gorm
import (
dbi "github.com/hopeio/cherry/utils/dao/database"
"gorm.io/gorm"
)
func DeleteById(db *gorm.DB, tableName string, id uint64) error {
sql := dbi.DeleteSQL(tableName, "id")
return db.Exec(sql, id).Error
}
func Delete(db *gorm.DB, tableName string, column string, value any) error {
sql := dbi.DeleteSQL(tableName, column)
return db.Exec(sql, value).Error
}
func ExistsByIdWithDeletedAt(db *gorm.DB, tableName string, id uint64) (bool, error) {
return ExistsBySQL(db, dbi.ExistsSQL(tableName, "id", true), id)
}
func ExistsById(db *gorm.DB, tableName string, id uint64) (bool, error) {
return ExistsBySQL(db, dbi.ExistsSQL(tableName, "id", false), id)
}
func ExistsByColumn(db *gorm.DB, tableName, column string, value interface{}) (bool, error) {
return ExistsBySQL(db, dbi.ExistsSQL(tableName, column, false), value)
}
func ExistsBySQL(db *gorm.DB, sql string, value ...any) (bool, error) {
var exists bool
err := db.Raw(sql, value...).Scan(&exists).Error
if err != nil {
return false, err
}
return exists, nil
}
// 根据查询语句查询数据是否存在
func ExistsByQuerySQL(db *gorm.DB, qsql string, value ...any) (bool, error) {
var exists bool
err := db.Raw(dbi.ExistsSQLByQuerySQL(qsql), value...).Scan(&exists).Error
if err != nil {
return false, err
}
return exists, nil
}
func Exists(db *gorm.DB, tableName, column string, value interface{}, withDeletedAt bool) (bool, error) {
return ExistsBySQL(db, dbi.ExistsSQL(tableName, column, withDeletedAt), value)
}
func ExistsByFilterExprs(db *gorm.DB, tableName string, filters dbi.FilterExprs) (bool, error) {
var exists bool
err := db.Raw(dbi.ExistsSQLByFilterExprs(tableName, filters)).Scan(&exists).Error
if err != nil {
return false, err
}
return exists, nil
}