瀏覽代碼

修改接口

yjp 1 年之前
父節點
當前提交
9ae66b4a02
共有 4 個文件被更改,包括 45 次插入41 次删除
  1. 9 2
      db_operations/db_operations.go
  2. 4 24
      db_operations/operations.go
  3. 21 0
      db_operations/transaction_operations.go
  4. 11 15
      demo/demo.go

+ 9 - 2
db_operations/db_operations.go

@@ -2,6 +2,7 @@ package db_operations
 
 type DBOperations interface {
 	BeginTransaction() TransactionDBOperations
+	// BeginEvent() EventDBOperations
 	BaseDBOperations
 }
 
@@ -11,6 +12,12 @@ type TransactionDBOperations interface {
 	CommitTransaction()
 }
 
+type EventDBOperations interface {
+	BaseDBOperations
+	RollbackEvent()
+	CommitEvent()
+}
+
 type BaseDBOperations interface {
 	// 会重置数据库连接的方法
 	NewSession() DBOperations
@@ -36,8 +43,8 @@ type BaseDBOperations interface {
 	Create(tableRow map[string]any) error
 	CreateBatch(tableRows []map[string]any) error
 	Delete() error
-	Updates(updateData map[string]any) error
-	UpdatesWithRowsAffected(updateData map[string]any) (int64, error)
+	Updates(newTableRow map[string]any) error
+	UpdatesWithRowsAffected(newTableRow map[string]any) (int64, error)
 
 	// 查询方法
 	Rows(pageNo int, pageSize int) ([]map[string]any, error)

+ 4 - 24
db_operations/operations.go

@@ -10,26 +10,6 @@ import (
 	"time"
 )
 
-type TransactionOperations struct {
-	Operations
-}
-
-func (op *TransactionOperations) RollbackTransaction() {
-	defer func() {
-		op.processDB = op.initDB
-	}()
-
-	op.processDB.Rollback()
-}
-
-func (op *TransactionOperations) CommitTransaction() {
-	defer func() {
-		op.processDB = op.initDB
-	}()
-
-	op.processDB.Commit()
-}
-
 type Operations struct {
 	initDB       *gorm.DB
 	processDB    *gorm.DB
@@ -228,8 +208,8 @@ func (op *Operations) Delete() error {
 	return op.processDB.Delete(make(map[string]any)).Error
 }
 
-func (op *Operations) Updates(updateData map[string]any) error {
-	err := op.processDB.Updates(updateData).Error
+func (op *Operations) Updates(newTableRow map[string]any) error {
+	err := op.processDB.Updates(newTableRow).Error
 	if err != nil {
 		if strings.Contains(err.Error(), "SQLSTATE 23505") {
 			return dberr.ErrDBRecordHasExist
@@ -241,8 +221,8 @@ func (op *Operations) Updates(updateData map[string]any) error {
 	return nil
 }
 
-func (op *Operations) UpdatesWithRowsAffected(updateData map[string]any) (int64, error) {
-	op.processDB = op.processDB.Updates(updateData)
+func (op *Operations) UpdatesWithRowsAffected(newTableRow map[string]any) (int64, error) {
+	op.processDB = op.processDB.Updates(newTableRow)
 	if op.processDB.Error != nil {
 		return 0, op.processDB.Error
 	}

+ 21 - 0
db_operations/transaction_operations.go

@@ -0,0 +1,21 @@
+package db_operations
+
+type TransactionOperations struct {
+	Operations
+}
+
+func (op *TransactionOperations) RollbackTransaction() {
+	defer func() {
+		op.processDB = op.initDB
+	}()
+
+	op.processDB.Rollback()
+}
+
+func (op *TransactionOperations) CommitTransaction() {
+	defer func() {
+		op.processDB = op.initDB
+	}()
+
+	op.processDB.Commit()
+}

+ 11 - 15
demo/demo.go

@@ -111,21 +111,17 @@ func main() {
 
 	err = sdk.GetInstance().GetDBOperations().NewSession().
 		Table("test.classes").
-		Create(map[string]any{
-			"id":          classID1,
-			"name":        className1,
-			"student_num": studentNum1,
-		})
-	if err != nil {
-		panic(err)
-	}
-
-	err = sdk.GetInstance().GetDBOperations().NewSession().
-		Table("test.classes").
-		Create(map[string]any{
-			"id":          classID2,
-			"name":        className2,
-			"student_num": studentNum2,
+		CreateBatch([]map[string]any{
+			{
+				"id":          classID1,
+				"name":        className1,
+				"student_num": studentNum1,
+			},
+			{
+				"id":          classID2,
+				"name":        className2,
+				"student_num": studentNum2,
+			},
 		})
 	if err != nil {
 		panic(err)