|
|
@@ -1,7 +1,6 @@
|
|
|
package database
|
|
|
|
|
|
import (
|
|
|
- "git.sxidc.com/go-framework/baize/infrastructure/database/sql"
|
|
|
"git.sxidc.com/go-framework/baize/tag/sql/sql_mapping"
|
|
|
"git.sxidc.com/go-tools/utils/reflectutils"
|
|
|
"git.sxidc.com/go-tools/utils/strutils"
|
|
|
@@ -12,8 +11,8 @@ import (
|
|
|
)
|
|
|
|
|
|
type Executor interface {
|
|
|
- ExecuteRawSql(sql string, executeParams map[string]any) ([]sql.Result, error)
|
|
|
- ExecuteSql(name string, executeParams map[string]any) ([]sql.Result, error)
|
|
|
+ ExecuteRawSql(sql string, executeParams map[string]any) ([]Result, error)
|
|
|
+ ExecuteSql(name string, executeParams map[string]any) ([]Result, error)
|
|
|
}
|
|
|
|
|
|
const (
|
|
|
@@ -44,7 +43,7 @@ func InsertEntity(executor Executor, tableName string, e any) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- executeParamsMap, err := sql.InsertExecuteParams{
|
|
|
+ executeParamsMap, err := InsertExecuteParams{
|
|
|
TableName: tableName,
|
|
|
TableRow: formInsertTableRow(fields),
|
|
|
}.Map()
|
|
|
@@ -52,7 +51,7 @@ func InsertEntity(executor Executor, tableName string, e any) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- _, err = executor.ExecuteRawSql(sql.InsertTpl, executeParamsMap)
|
|
|
+ _, err = executor.ExecuteRawSql(InsertTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
if strings.Contains(err.Error(), "SQLSTATE 23505") {
|
|
|
return ErrDBRecordHasExist
|
|
|
@@ -73,7 +72,7 @@ func InsertEntityBatch(executor Executor, tableName string, es []any) error {
|
|
|
return fserr.New("没有传递表名")
|
|
|
}
|
|
|
|
|
|
- tableRowBatch := make([]sql.TableRow, 0)
|
|
|
+ tableRowBatch := make([]TableRow, 0)
|
|
|
|
|
|
for _, e := range es {
|
|
|
if e == nil {
|
|
|
@@ -93,7 +92,7 @@ func InsertEntityBatch(executor Executor, tableName string, es []any) error {
|
|
|
tableRowBatch = append(tableRowBatch, *formInsertTableRow(fields))
|
|
|
}
|
|
|
|
|
|
- executeParamsMap, err := sql.InsertBatchExecuteParams{
|
|
|
+ executeParamsMap, err := InsertBatchExecuteParams{
|
|
|
TableName: tableName,
|
|
|
TableRowBatch: tableRowBatch,
|
|
|
}.Map()
|
|
|
@@ -101,7 +100,7 @@ func InsertEntityBatch(executor Executor, tableName string, es []any) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- _, err = executor.ExecuteRawSql(sql.InsertTpl, executeParamsMap)
|
|
|
+ _, err = executor.ExecuteRawSql(InsertTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
if strings.Contains(err.Error(), "SQLSTATE 23505") {
|
|
|
return ErrDBRecordHasExist
|
|
|
@@ -113,9 +112,9 @@ func InsertEntityBatch(executor Executor, tableName string, es []any) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func formInsertTableRow(fields []sql_mapping.Field) *sql.TableRow {
|
|
|
+func formInsertTableRow(fields []sql_mapping.Field) *TableRow {
|
|
|
now := time.Now().Local()
|
|
|
- tableRow := sql.NewTableRow()
|
|
|
+ tableRow := NewTableRow()
|
|
|
|
|
|
for _, field := range fields {
|
|
|
fieldValue := reflect.ValueOf(field.Value)
|
|
|
@@ -153,7 +152,7 @@ func DeleteEntity(executor Executor, tableName string, e any) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- conditions := sql.NewConditions()
|
|
|
+ conditions := NewConditions()
|
|
|
for _, field := range fields {
|
|
|
// 不是键,字段跳过
|
|
|
if !field.IsKey {
|
|
|
@@ -163,7 +162,7 @@ func DeleteEntity(executor Executor, tableName string, e any) error {
|
|
|
conditions.Equal(field.ColumnName, field.Value)
|
|
|
}
|
|
|
|
|
|
- executeParamsMap, err := sql.DeleteExecuteParams{
|
|
|
+ executeParamsMap, err := DeleteExecuteParams{
|
|
|
TableName: tableName,
|
|
|
Conditions: conditions,
|
|
|
}.Map()
|
|
|
@@ -171,7 +170,7 @@ func DeleteEntity(executor Executor, tableName string, e any) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- _, err = executor.ExecuteRawSql(sql.DeleteTpl, executeParamsMap)
|
|
|
+ _, err = executor.ExecuteRawSql(DeleteTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -203,8 +202,8 @@ func UpdateEntity(executor Executor, tableName string, e any) error {
|
|
|
}
|
|
|
|
|
|
now := time.Now().Local()
|
|
|
- tableRow := sql.NewTableRow()
|
|
|
- conditions := sql.NewConditions()
|
|
|
+ tableRow := NewTableRow()
|
|
|
+ conditions := NewConditions()
|
|
|
|
|
|
for _, field := range fields {
|
|
|
// 不是键字段
|
|
|
@@ -228,7 +227,7 @@ func UpdateEntity(executor Executor, tableName string, e any) error {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- executeParamsMap, err := sql.UpdateExecuteParams{
|
|
|
+ executeParamsMap, err := UpdateExecuteParams{
|
|
|
TableName: tableName,
|
|
|
TableRow: tableRow,
|
|
|
Conditions: conditions,
|
|
|
@@ -237,7 +236,7 @@ func UpdateEntity(executor Executor, tableName string, e any) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- _, err = executor.ExecuteRawSql(sql.UpdateTpl, executeParamsMap)
|
|
|
+ _, err = executor.ExecuteRawSql(UpdateTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -245,7 +244,7 @@ func UpdateEntity(executor Executor, tableName string, e any) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func Insert(executor Executor, executeParams *sql.InsertExecuteParams) error {
|
|
|
+func Insert(executor Executor, executeParams *InsertExecuteParams) error {
|
|
|
if executor == nil {
|
|
|
return fserr.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -259,7 +258,7 @@ func Insert(executor Executor, executeParams *sql.InsertExecuteParams) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- _, err = executor.ExecuteRawSql(sql.InsertTpl, executeParamsMap)
|
|
|
+ _, err = executor.ExecuteRawSql(InsertTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -267,7 +266,7 @@ func Insert(executor Executor, executeParams *sql.InsertExecuteParams) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func InsertBatch(executor Executor, executeParams *sql.InsertBatchExecuteParams) error {
|
|
|
+func InsertBatch(executor Executor, executeParams *InsertBatchExecuteParams) error {
|
|
|
if executor == nil {
|
|
|
return fserr.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -281,7 +280,7 @@ func InsertBatch(executor Executor, executeParams *sql.InsertBatchExecuteParams)
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- _, err = executor.ExecuteRawSql(sql.InsertTpl, executeParamsMap)
|
|
|
+ _, err = executor.ExecuteRawSql(InsertTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -289,7 +288,7 @@ func InsertBatch(executor Executor, executeParams *sql.InsertBatchExecuteParams)
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func Delete(executor Executor, executeParams *sql.DeleteExecuteParams) error {
|
|
|
+func Delete(executor Executor, executeParams *DeleteExecuteParams) error {
|
|
|
if executor == nil {
|
|
|
return fserr.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -303,7 +302,7 @@ func Delete(executor Executor, executeParams *sql.DeleteExecuteParams) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- _, err = executor.ExecuteRawSql(sql.DeleteTpl, executeParamsMap)
|
|
|
+ _, err = executor.ExecuteRawSql(DeleteTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -311,7 +310,7 @@ func Delete(executor Executor, executeParams *sql.DeleteExecuteParams) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func Update(executor Executor, executeParams *sql.UpdateExecuteParams) error {
|
|
|
+func Update(executor Executor, executeParams *UpdateExecuteParams) error {
|
|
|
if executor == nil {
|
|
|
return fserr.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -325,7 +324,7 @@ func Update(executor Executor, executeParams *sql.UpdateExecuteParams) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- _, err = executor.ExecuteRawSql(sql.UpdateTpl, executeParamsMap)
|
|
|
+ _, err = executor.ExecuteRawSql(UpdateTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -333,7 +332,7 @@ func Update(executor Executor, executeParams *sql.UpdateExecuteParams) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func Query(executor Executor, executeParams *sql.QueryExecuteParams) ([]sql.Result, int64, error) {
|
|
|
+func Query(executor Executor, executeParams *QueryExecuteParams) ([]Result, int64, error) {
|
|
|
if executor == nil {
|
|
|
return nil, 0, fserr.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -347,7 +346,7 @@ func Query(executor Executor, executeParams *sql.QueryExecuteParams) ([]sql.Resu
|
|
|
return nil, 0, err
|
|
|
}
|
|
|
|
|
|
- countExecuteParamsMap, err := sql.CountExecuteParams{
|
|
|
+ countExecuteParamsMap, err := CountExecuteParams{
|
|
|
TableName: executeParams.TableName,
|
|
|
Conditions: executeParams.Conditions,
|
|
|
}.Map()
|
|
|
@@ -355,17 +354,17 @@ func Query(executor Executor, executeParams *sql.QueryExecuteParams) ([]sql.Resu
|
|
|
return nil, 0, err
|
|
|
}
|
|
|
|
|
|
- tableRows, err := executor.ExecuteRawSql(sql.QueryTpl, queryExecuteParamsMap)
|
|
|
+ tableRows, err := executor.ExecuteRawSql(QueryTpl, queryExecuteParamsMap)
|
|
|
if err != nil {
|
|
|
return nil, 0, err
|
|
|
}
|
|
|
|
|
|
- countTableRow, err := executor.ExecuteRawSql(sql.CountTpl, countExecuteParamsMap)
|
|
|
+ countTableRow, err := executor.ExecuteRawSql(CountTpl, countExecuteParamsMap)
|
|
|
if err != nil {
|
|
|
return nil, 0, err
|
|
|
}
|
|
|
|
|
|
- results := make([]sql.Result, len(tableRows))
|
|
|
+ results := make([]Result, len(tableRows))
|
|
|
for i, row := range tableRows {
|
|
|
results[i] = row
|
|
|
}
|
|
|
@@ -373,7 +372,7 @@ func Query(executor Executor, executeParams *sql.QueryExecuteParams) ([]sql.Resu
|
|
|
return results, int64(countTableRow[0]["count"].(float64)), nil
|
|
|
}
|
|
|
|
|
|
-func QueryOne(executor Executor, executeParams *sql.QueryOneExecuteParams) (sql.Result, error) {
|
|
|
+func QueryOne(executor Executor, executeParams *QueryOneExecuteParams) (Result, error) {
|
|
|
if executor == nil {
|
|
|
return nil, fserr.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -387,7 +386,7 @@ func QueryOne(executor Executor, executeParams *sql.QueryOneExecuteParams) (sql.
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- tableRows, err := executor.ExecuteRawSql(sql.QueryTpl, executeParamsMap)
|
|
|
+ tableRows, err := executor.ExecuteRawSql(QueryTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
@@ -399,7 +398,7 @@ func QueryOne(executor Executor, executeParams *sql.QueryOneExecuteParams) (sql.
|
|
|
return tableRows[0], nil
|
|
|
}
|
|
|
|
|
|
-func Count(executor Executor, executeParams *sql.CountExecuteParams) (int64, error) {
|
|
|
+func Count(executor Executor, executeParams *CountExecuteParams) (int64, error) {
|
|
|
if executor == nil {
|
|
|
return 0, fserr.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -413,7 +412,7 @@ func Count(executor Executor, executeParams *sql.CountExecuteParams) (int64, err
|
|
|
return 0, err
|
|
|
}
|
|
|
|
|
|
- tableRows, err := executor.ExecuteRawSql(sql.CountTpl, executeParamsMap)
|
|
|
+ tableRows, err := executor.ExecuteRawSql(CountTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
return 0, err
|
|
|
}
|
|
|
@@ -421,7 +420,7 @@ func Count(executor Executor, executeParams *sql.CountExecuteParams) (int64, err
|
|
|
return int64(tableRows[0]["count"].(float64)), nil
|
|
|
}
|
|
|
|
|
|
-func CheckExist(executor Executor, executeParams *sql.CheckExistExecuteParams) (bool, error) {
|
|
|
+func CheckExist(executor Executor, executeParams *CheckExistExecuteParams) (bool, error) {
|
|
|
if executor == nil {
|
|
|
return false, fserr.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -435,7 +434,7 @@ func CheckExist(executor Executor, executeParams *sql.CheckExistExecuteParams) (
|
|
|
return false, err
|
|
|
}
|
|
|
|
|
|
- tableRows, err := executor.ExecuteRawSql(sql.CountTpl, executeParamsMap)
|
|
|
+ tableRows, err := executor.ExecuteRawSql(CountTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
return false, err
|
|
|
}
|
|
|
@@ -443,7 +442,7 @@ func CheckExist(executor Executor, executeParams *sql.CheckExistExecuteParams) (
|
|
|
return int64(tableRows[0]["count"].(float64)) > 0, nil
|
|
|
}
|
|
|
|
|
|
-func CheckHasOnlyOne(executor Executor, executeParams *sql.CheckHasOnlyOneExecuteParams) (bool, error) {
|
|
|
+func CheckHasOnlyOne(executor Executor, executeParams *CheckHasOnlyOneExecuteParams) (bool, error) {
|
|
|
if executor == nil {
|
|
|
return false, fserr.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -457,7 +456,7 @@ func CheckHasOnlyOne(executor Executor, executeParams *sql.CheckHasOnlyOneExecut
|
|
|
return false, err
|
|
|
}
|
|
|
|
|
|
- tableRows, err := executor.ExecuteRawSql(sql.CountTpl, executeParamsMap)
|
|
|
+ tableRows, err := executor.ExecuteRawSql(CountTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
return false, err
|
|
|
}
|
|
|
@@ -465,7 +464,7 @@ func CheckHasOnlyOne(executor Executor, executeParams *sql.CheckHasOnlyOneExecut
|
|
|
return int64(tableRows[0]["count"].(float64)) == 1, nil
|
|
|
}
|
|
|
|
|
|
-func ExecuteRawSql(executor Executor, sql string, executeParams map[string]any) ([]sql.Result, error) {
|
|
|
+func ExecuteRawSql(executor Executor, sql string, executeParams map[string]any) ([]Result, error) {
|
|
|
if executor == nil {
|
|
|
return nil, fserr.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -482,7 +481,7 @@ func ExecuteRawSql(executor Executor, sql string, executeParams map[string]any)
|
|
|
return tableRows, nil
|
|
|
}
|
|
|
|
|
|
-func ExecuteSql(executor Executor, name string, executeParams map[string]any) ([]sql.Result, error) {
|
|
|
+func ExecuteSql(executor Executor, name string, executeParams map[string]any) ([]Result, error) {
|
|
|
if executor == nil {
|
|
|
return nil, fserr.New("没有传递执行器")
|
|
|
}
|