yjp il y a 1 an
Parent
commit
c0bf716298
2 fichiers modifiés avec 22 ajouts et 0 suppressions
  1. 2 0
      db_operations/db_operations.go
  2. 20 0
      db_operations/operations.go

+ 2 - 0
db_operations/db_operations.go

@@ -53,4 +53,6 @@ type BaseDBOperations interface {
 
 	// 其他方法
 	Count(count *int64) error
+	CheckExist() (bool, error)
+	CheckHasOnlyOne() (bool, error)
 }

+ 20 - 0
db_operations/operations.go

@@ -275,3 +275,23 @@ func (op *Operations) Row() (*TableRow, error) {
 func (op *Operations) Count(count *int64) error {
 	return op.processDB.Count(count).Error
 }
+
+func (op *Operations) CheckExist() (bool, error) {
+	var count int64
+	err := op.processDB.Count(&count).Error
+	if err != nil {
+		return false, err
+	}
+
+	return count > 0, nil
+}
+
+func (op *Operations) CheckHasOnlyOne() (bool, error) {
+	var count int64
+	err := op.processDB.Count(&count).Error
+	if err != nil {
+		return false, err
+	}
+
+	return count == 1, nil
+}