fusenapi/utils/fssql/metadata.go

151 lines
3.3 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()
}
updatesql := `UPDATE %s
SET metadata = CASE
WHEN metadata IS NULL THEN ?
ELSE JSON_MERGE_PATCH(metadata, ?)
END
WHERE module = '%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
}
}
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
updatesql = fmt.Sprintf(updatesql, tx.NamingStrategy.TableName(stype.Name()), 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
cond, ok := values[0].(string)
WHERE resource_id = '%s'` + "and " + cond + `;`
}
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
}