|
@@ -2,7 +2,6 @@ package db_operations
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"database/sql"
|
|
"database/sql"
|
|
|
- "errors"
|
|
|
|
|
"git.sxidc.com/service-supports/ds-sdk/db_operations/dberr"
|
|
"git.sxidc.com/service-supports/ds-sdk/db_operations/dberr"
|
|
|
"git.sxidc.com/service-supports/fslog"
|
|
"git.sxidc.com/service-supports/fslog"
|
|
|
"github.com/mitchellh/mapstructure"
|
|
"github.com/mitchellh/mapstructure"
|
|
@@ -143,7 +142,7 @@ func (op *Operations) Table(name string, args ...any) DBOperations {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (op *Operations) Raw(sql string, values ...any) DBOperations {
|
|
func (op *Operations) Raw(sql string, values ...any) DBOperations {
|
|
|
- op.processDB = op.initDB.Raw(sql, values...)
|
|
|
|
|
|
|
+ op.processDB = op.processDB.Raw(sql, values...)
|
|
|
return op
|
|
return op
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -199,8 +198,8 @@ func (op *Operations) Paging(pageNo int, pageSize int) DBOperations {
|
|
|
return op
|
|
return op
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (op *Operations) Create(model interface{}) error {
|
|
|
|
|
- err := op.processDB.Create(model).Error
|
|
|
|
|
|
|
+func (op *Operations) Create(tableRow map[string]any) error {
|
|
|
|
|
+ err := op.processDB.Create(tableRow).Error
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
if strings.Contains(err.Error(), "SQLSTATE 23505") {
|
|
if strings.Contains(err.Error(), "SQLSTATE 23505") {
|
|
|
return dberr.ErrDBRecordHasExist
|
|
return dberr.ErrDBRecordHasExist
|
|
@@ -212,12 +211,25 @@ func (op *Operations) Create(model interface{}) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (op *Operations) Delete(idModel interface{}) error {
|
|
|
|
|
- return op.processDB.Delete(idModel).Error
|
|
|
|
|
|
|
+func (op *Operations) CreateBatch(tableRows []map[string]any) error {
|
|
|
|
|
+ err := op.processDB.Create(tableRows).Error
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ if strings.Contains(err.Error(), "SQLSTATE 23505") {
|
|
|
|
|
+ return dberr.ErrDBRecordHasExist
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (op *Operations) Updates(idModel interface{}, updateData map[string]any) error {
|
|
|
|
|
- err := op.processDB.Model(idModel).Updates(updateData).Error
|
|
|
|
|
|
|
+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
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
if strings.Contains(err.Error(), "SQLSTATE 23505") {
|
|
if strings.Contains(err.Error(), "SQLSTATE 23505") {
|
|
|
return dberr.ErrDBRecordHasExist
|
|
return dberr.ErrDBRecordHasExist
|
|
@@ -229,8 +241,8 @@ func (op *Operations) Updates(idModel interface{}, updateData map[string]any) er
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (op *Operations) UpdatesWithRowsAffected(idModel interface{}, updateData map[string]any) (int64, error) {
|
|
|
|
|
- op.processDB = op.processDB.Model(idModel).Updates(updateData)
|
|
|
|
|
|
|
+func (op *Operations) UpdatesWithRowsAffected(updateData map[string]any) (int64, error) {
|
|
|
|
|
+ op.processDB = op.processDB.Updates(updateData)
|
|
|
if op.processDB.Error != nil {
|
|
if op.processDB.Error != nil {
|
|
|
return 0, op.processDB.Error
|
|
return 0, op.processDB.Error
|
|
|
}
|
|
}
|
|
@@ -238,33 +250,23 @@ func (op *Operations) UpdatesWithRowsAffected(idModel interface{}, updateData ma
|
|
|
return op.processDB.RowsAffected, nil
|
|
return op.processDB.RowsAffected, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (op *Operations) Query(models interface{}, pageNo int, pageSize int) error {
|
|
|
|
|
|
|
+func (op *Operations) Rows(pageNo int, pageSize int) ([]map[string]any, error) {
|
|
|
if pageNo != 0 && pageSize != 0 {
|
|
if pageNo != 0 && pageSize != 0 {
|
|
|
offset := (pageNo - 1) * pageSize
|
|
offset := (pageNo - 1) * pageSize
|
|
|
op.processDB = op.processDB.Offset(offset).Limit(pageSize)
|
|
op.processDB = op.processDB.Offset(offset).Limit(pageSize)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- err := op.processDB.Find(models).Error
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- op.processDB = op.processDB.Offset(-1).Limit(-1)
|
|
|
|
|
-
|
|
|
|
|
- return nil
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ defer func() {
|
|
|
|
|
+ op.processDB = op.processDB.Offset(-1).Limit(-1)
|
|
|
|
|
+ }()
|
|
|
|
|
|
|
|
-func (op *Operations) QueryOne(model interface{}) error {
|
|
|
|
|
- err := op.processDB.First(model).Error
|
|
|
|
|
|
|
+ valueMaps := make([]map[string]any, 0)
|
|
|
|
|
+ err := op.processDB.Scan(&valueMaps).Error
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
- return dberr.ErrDBRecordNotExist
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return err
|
|
|
|
|
|
|
+ return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return nil
|
|
|
|
|
|
|
+ return valueMaps, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (op *Operations) Row() (map[string]any, error) {
|
|
func (op *Operations) Row() (map[string]any, error) {
|
|
@@ -281,16 +283,6 @@ func (op *Operations) Row() (map[string]any, error) {
|
|
|
return valueMap, nil
|
|
return valueMap, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (op *Operations) Rows() ([]map[string]any, error) {
|
|
|
|
|
- valueMaps := make([]map[string]any, 0)
|
|
|
|
|
- err := op.processDB.Scan(&valueMaps).Error
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return valueMaps, nil
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
func (op *Operations) Count(count *int64) error {
|
|
func (op *Operations) Count(count *int64) error {
|
|
|
return op.processDB.Count(count).Error
|
|
return op.processDB.Count(count).Error
|
|
|
}
|
|
}
|