fusenapi/utils/fssql/metadata.go

203 lines
4.4 KiB
Go
Raw Normal View History

2023-09-08 10:56:17 +00:00
package fssql
import (
"encoding/json"
"fmt"
"reflect"
"gorm.io/gorm"
)
// func MetadataPATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, rawID int64) error {
// stype := reflect.TypeOf(tableStructPointer)
// if stype.Kind() == reflect.Pointer {
// stype = stype.Elem()
// }
// updatesql := `UPDATE %s
// SET metadata = CASE
// WHEN metadata IS NULL THEN ?
// ELSE JSON_MERGE_PATCH(metadata, ?)
// END
// WHERE module = '%s' and id = ?;`
// var err error
// var metadata []byte
// switch mdata := updateMetadata.(type) {
// case []byte:
// metadata = mdata
// case string:
// metadata = []byte(mdata)
// default:
// metadata, err = json.Marshal(updateMetadata)
// if err != nil {
// return err
// }
// }
// updatesql = fmt.Sprintf(updatesql, tx.NamingStrategy.TableName(stype.Name()), module)
// logx.Error(updatesql)
// err = tx.Exec(updatesql, metadata, metadata, rawID).Error
// if err != nil {
// return err
// }
// return nil
// }
2023-09-08 11:01:29 +00:00
// tableStructPointer 表结构 updateMetadata 可以是json的[]byte也可以结构体
2023-09-09 18:58:56 +00:00
func MetadataModulePATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, WhereKeysCond string, values ...any) error {
2023-09-08 10:56:17 +00:00
stype := reflect.TypeOf(tableStructPointer)
if stype.Kind() == reflect.Pointer {
stype = stype.Elem()
}
2023-09-21 10:12:22 +00:00
tname := tx.NamingStrategy.TableName(stype.Name())
2023-09-08 10:56:17 +00:00
updatesql := `UPDATE %s
SET metadata = CASE
WHEN metadata IS NULL THEN ?
ELSE JSON_MERGE_PATCH(metadata, ?)
END
2023-09-21 15:47:34 +00:00
WHERE module = '%s' and %s;`
2023-09-08 10:56:17 +00:00
var err error
var metadata []byte
switch mdata := updateMetadata.(type) {
case []byte:
metadata = mdata
case string:
metadata = []byte(mdata)
default:
metadata, err = json.Marshal(updateMetadata)
if err != nil {
return err
}
}
2023-09-09 18:58:56 +00:00
var args []any
2023-09-08 10:56:17 +00:00
2023-09-09 18:58:56 +00:00
args = append(args, metadata, metadata)
args = append(args, values...)
2023-09-08 10:56:17 +00:00
2023-09-21 10:12:22 +00:00
updatesql = fmt.Sprintf(updatesql, tname, module, WhereKeysCond)
2023-09-08 11:02:37 +00:00
// logx.Error(updatesql)
2023-09-09 18:58:56 +00:00
err = tx.Exec(updatesql, args...).Error
if err != nil {
return err
}
return nil
}
func MetadataResourcePATCH(tx *gorm.DB, rid string, updateMetadata any, values ...any) error {
// stype := reflect.TypeOf(tableStructPointer)
// if stype.Kind() == reflect.Pointer {
// stype = stype.Elem()
// }
var updatesql string
if len(values) == 0 {
updatesql = `UPDATE fs_resource
SET metadata = CASE
WHEN metadata IS NULL THEN ?
ELSE JSON_MERGE_PATCH(metadata, ?)
END
WHERE resource_id = '%s';`
} else {
cond, ok := values[0].(string)
if !ok {
return fmt.Errorf("values[0] must be wherecond")
}
updatesql = `UPDATE fs_resource
SET metadata = CASE
WHEN metadata IS NULL THEN ?
ELSE JSON_MERGE_PATCH(metadata, ?)
END
2023-09-09 19:09:48 +00:00
WHERE resource_id = '%s'` + " and " + cond + `;`
2023-09-09 18:58:56 +00:00
}
var err error
var metadata []byte
switch mdata := updateMetadata.(type) {
case []byte:
metadata = mdata
case string:
metadata = []byte(mdata)
default:
metadata, err = json.Marshal(updateMetadata)
if err != nil {
return err
}
}
var args []any
args = append(args, metadata, metadata)
args = append(args, values...)
updatesql = fmt.Sprintf(updatesql, rid)
// logx.Error(updatesql)
err = tx.Exec(updatesql, args...).Error
2023-09-08 10:56:17 +00:00
if err != nil {
return err
}
return nil
}
2023-09-21 11:01:48 +00:00
func MetadataOrderPATCH(tx *gorm.DB, sql string, orderSn string, tableStructPointer any, updateMetadata any, WhereKeysCond string, values ...any) error {
stype := reflect.TypeOf(tableStructPointer)
if stype.Kind() == reflect.Pointer {
stype = stype.Elem()
}
updatesql := `UPDATE %s
SET metadata = CASE
WHEN metadata IS NULL THEN ?
ELSE JSON_MERGE_PATCH(metadata, ?)
END%s
WHERE order_sn = '%s' and %s;`
var err error
var metadata []byte
switch mdata := updateMetadata.(type) {
case []byte:
metadata = mdata
case string:
metadata = []byte(mdata)
default:
metadata, err = json.Marshal(updateMetadata)
if err != nil {
return err
}
}
var args []any
args = append(args, metadata, metadata)
args = append(args, values...)
updatesql = fmt.Sprintf(updatesql, tx.NamingStrategy.TableName(stype.Name()), sql, orderSn, WhereKeysCond)
// logx.Error(updatesql)
err = tx.Exec(updatesql, args...).Error
if err != nil {
return err
}
return nil
}
2023-09-26 09:16:10 +00:00
func GetGormTableName(tx *gorm.DB, tableStructPointer any) string {
stype := reflect.TypeOf(tableStructPointer)
if stype.Kind() == reflect.Pointer {
stype = stype.Elem()
}
tname := tx.NamingStrategy.TableName(stype.Name())
return tname
}