|
|
@@ -188,6 +188,10 @@ func Update[T any](executor SqlExecutor, tableName string, e T, callback UpdateC
|
|
|
now := time.Now()
|
|
|
|
|
|
for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
+ if !sqlMappingColumn.CanUpdate {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
fieldType := sqlMappingColumn.ValueFieldType
|
|
|
|
|
|
value := reflect.Zero(fieldType).Interface()
|
|
|
@@ -195,7 +199,7 @@ func Update[T any](executor SqlExecutor, tableName string, e T, callback UpdateC
|
|
|
value = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
}
|
|
|
|
|
|
- if sqlMappingColumn.InsertCallback {
|
|
|
+ if sqlMappingColumn.UpdateCallback {
|
|
|
if callback == nil {
|
|
|
return errors.New("需要使用回调函数但是没有传递回调函数")
|
|
|
}
|
|
|
@@ -255,6 +259,99 @@ func Update[T any](executor SqlExecutor, tableName string, e T, callback UpdateC
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+type QueryCallback[T any] func(e T, fieldName string, columnName string, value any) (retConditionOp string, retConditionValue any, err error)
|
|
|
+
|
|
|
+func Query[T any](executor SqlExecutor, tableName string, e T, pageNo int, pageSize int, callback QueryCallback[T]) ([]map[string]any, error) {
|
|
|
+ if executor == nil {
|
|
|
+ return nil, errors.New("没有传递执行器")
|
|
|
+ }
|
|
|
+
|
|
|
+ if strutils.IsStringEmpty(tableName) {
|
|
|
+ return nil, errors.New("没有传递表名")
|
|
|
+ }
|
|
|
+
|
|
|
+ if reflect.TypeOf(e) == nil {
|
|
|
+ return nil, errors.New("没有传递实体")
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlMapping, err := tag.ParseSqlMapping(e)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ var offset int
|
|
|
+ var limit int
|
|
|
+ if pageNo != 0 && pageSize != 0 {
|
|
|
+ offset = (pageNo - 1) * pageSize
|
|
|
+ limit = pageSize
|
|
|
+ }
|
|
|
+
|
|
|
+ executeParams := raw_sql_tpl.QueryExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ Limit: limit,
|
|
|
+ Offset: offset,
|
|
|
+ }
|
|
|
+
|
|
|
+ for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
+ if !sqlMappingColumn.CanQuery {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ fieldType := sqlMappingColumn.ValueFieldType
|
|
|
+
|
|
|
+ conditionValue := reflect.Zero(fieldType).Interface()
|
|
|
+ if !sqlMappingColumn.ValueFieldValue.IsZero() {
|
|
|
+ conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
+ }
|
|
|
+
|
|
|
+ conditionOp := "="
|
|
|
+
|
|
|
+ if sqlMappingColumn.QueryCallback {
|
|
|
+ if callback == nil {
|
|
|
+ return nil, errors.New("需要使用回调函数但是没有传递回调函数")
|
|
|
+ }
|
|
|
+
|
|
|
+ retConditionOp, retConditionValue, err := callback(e, fieldName, sqlMappingColumn.Name, conditionValue)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ retValueType := reflect.TypeOf(retConditionValue)
|
|
|
+ if retValueType == nil || retValueType.Kind() == reflect.Ptr {
|
|
|
+ return nil, errors.New("返回应当为值类型")
|
|
|
+ }
|
|
|
+
|
|
|
+ conditionValue = retConditionValue
|
|
|
+ conditionOp = retConditionOp
|
|
|
+ }
|
|
|
+
|
|
|
+ tableRowValue, err := parseValue(conditionValue)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if sqlMappingColumn.IsKey {
|
|
|
+ executeParams.Conditions = append(executeParams.Conditions, raw_sql_tpl.Condition{
|
|
|
+ Column: sqlMappingColumn.Name,
|
|
|
+ Operator: conditionOp,
|
|
|
+ Value: tableRowValue,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ executeParamsMap, err := executeParams.Map()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ tableRows, err := executor.ExecuteRawSql(raw_sql_tpl.QueryTpl, executeParamsMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return tableRows, nil
|
|
|
+}
|
|
|
+
|
|
|
func ExecuteRawSql(executor SqlExecutor, sql string, executeParams map[string]any) ([]map[string]any, error) {
|
|
|
if executor == nil {
|
|
|
return nil, errors.New("没有传递执行器")
|