yjp 1 éve
szülő
commit
8c19f91be4

+ 161 - 0
sdk/raw_sql_tpl/condition.go

@@ -0,0 +1,161 @@
+package raw_sql_tpl
+
+type Conditions struct {
+	Conditions []string
+	Err        error
+}
+
+func NewConditions() *Conditions {
+	return &Conditions{
+		Conditions: make([]string, 0),
+	}
+}
+
+func (conditions *Conditions) AddCondition(condition string) *Conditions {
+	conditions.Conditions = append(conditions.Conditions, condition)
+	return conditions
+}
+
+func (conditions *Conditions) Equal(columnName string, value any) *Conditions {
+	if conditions.Err != nil {
+		return conditions
+	}
+
+	parsedValue, err := parseValue(value)
+	if err != nil {
+		conditions.Err = err
+		return conditions
+	}
+
+	conditions.Conditions = append(conditions.Conditions, columnName+" = "+parsedValue)
+
+	return conditions
+}
+
+func (conditions *Conditions) Like(columnName string, value string) *Conditions {
+	if conditions.Err != nil {
+		return conditions
+	}
+
+	parsedValue, err := parseValue(value)
+	if err != nil {
+		conditions.Err = err
+		return conditions
+	}
+
+	conditions.Conditions = append(conditions.Conditions, columnName+" LIKE "+parsedValue)
+
+	return conditions
+}
+
+func (conditions *Conditions) In(columnName string, value any) *Conditions {
+	if conditions.Err != nil {
+		return conditions
+	}
+
+	parsedValue, err := parseValue(value)
+	if err != nil {
+		conditions.Err = err
+		return conditions
+	}
+
+	conditions.Conditions = append(conditions.Conditions, columnName+" IN "+parsedValue)
+
+	return conditions
+}
+
+func (conditions *Conditions) NotIn(columnName string, value any) *Conditions {
+	if conditions.Err != nil {
+		return conditions
+	}
+
+	parsedValue, err := parseValue(value)
+	if err != nil {
+		conditions.Err = err
+		return conditions
+	}
+
+	conditions.Conditions = append(conditions.Conditions, columnName+" NOT IN "+parsedValue)
+
+	return conditions
+}
+
+func (conditions *Conditions) Not(columnName string, value any) *Conditions {
+	if conditions.Err != nil {
+		return conditions
+	}
+
+	parsedValue, err := parseValue(value)
+	if err != nil {
+		conditions.Err = err
+		return conditions
+	}
+
+	conditions.Conditions = append(conditions.Conditions, columnName+" != "+parsedValue)
+
+	return conditions
+}
+
+func (conditions *Conditions) LessThan(columnName string, value any) *Conditions {
+	if conditions.Err != nil {
+		return conditions
+	}
+
+	parsedValue, err := parseValue(value)
+	if err != nil {
+		conditions.Err = err
+		return conditions
+	}
+
+	conditions.Conditions = append(conditions.Conditions, columnName+" < "+parsedValue)
+
+	return conditions
+}
+
+func (conditions *Conditions) LessThanAndEqual(columnName string, value any) *Conditions {
+	if conditions.Err != nil {
+		return conditions
+	}
+
+	parsedValue, err := parseValue(value)
+	if err != nil {
+		conditions.Err = err
+		return conditions
+	}
+
+	conditions.Conditions = append(conditions.Conditions, columnName+" <= "+parsedValue)
+
+	return conditions
+}
+
+func (conditions *Conditions) GreaterThan(columnName string, value any) *Conditions {
+	if conditions.Err != nil {
+		return conditions
+	}
+
+	parsedValue, err := parseValue(value)
+	if err != nil {
+		conditions.Err = err
+		return conditions
+	}
+
+	conditions.Conditions = append(conditions.Conditions, columnName+" > "+parsedValue)
+
+	return conditions
+}
+
+func (conditions *Conditions) GreaterThanAndEqual(columnName string, value any) *Conditions {
+	if conditions.Err != nil {
+		return conditions
+	}
+
+	parsedValue, err := parseValue(value)
+	if err != nil {
+		conditions.Err = err
+		return conditions
+	}
+
+	conditions.Conditions = append(conditions.Conditions, columnName+" >= "+parsedValue)
+
+	return conditions
+}

+ 65 - 248
sdk/raw_sql_tpl/raw_sql_tpl.go

@@ -1,64 +1,6 @@
 package raw_sql_tpl
 
-import (
-	"errors"
-	"git.sxidc.com/go-tools/utils/strutils"
-	"github.com/fatih/structs"
-	"reflect"
-	"strconv"
-	"time"
-)
-
-type TableRow struct {
-	Column      string `structs:"column"`
-	Value       any    `structs:"-"`
-	ParsedValue string `structs:"value"`
-}
-
-func (tableRow *TableRow) parse() error {
-	if strutils.IsStringEmpty(tableRow.Column) {
-		return errors.New("没有传递列名")
-	}
-
-	if tableRow.Value == nil {
-		return errors.New("没有传递列值")
-	}
-
-	parsedValue, err := parseValue(tableRow.Value)
-	if err != nil {
-		return err
-	}
-
-	tableRow.ParsedValue = parsedValue
-
-	return nil
-}
-
-type Condition struct {
-	Column      string `structs:"column"`
-	Operator    string `structs:"operator"`
-	Value       any    `structs:"-"`
-	ParsedValue string `structs:"value"`
-}
-
-func (cond *Condition) parse() error {
-	if strutils.IsStringEmpty(cond.Column) {
-		return errors.New("没有传递列名")
-	}
-
-	if cond.Value == nil {
-		return errors.New("没有传递条件值")
-	}
-
-	parsedValue, err := parseValue(cond.Value)
-	if err != nil {
-		return err
-	}
-
-	cond.ParsedValue = parsedValue
-
-	return nil
-}
+import "errors"
 
 const InsertTpl = `
 INSERT INTO
@@ -69,37 +11,15 @@ VALUES
 
 type InsertExecuteParams struct {
 	TableName string
-	TableRows []*TableRow
-}
-
-func (params InsertExecuteParams) check() error {
-	if strutils.IsStringEmpty(params.TableName) {
-		return errors.New("没有传递表名")
-	}
-
-	if params.TableRows == nil || len(params.TableRows) == 0 {
-		return errors.New("没有传递行数据")
-	}
-
-	for _, tableRow := range params.TableRows {
-		if err := tableRow.parse(); err != nil {
-			return err
-		}
-	}
-
-	return nil
+	*TableRows
 }
 
 func (params InsertExecuteParams) Map() (map[string]any, error) {
-	if err := params.check(); err != nil {
-		return nil, err
-	}
-
 	columns := make([]string, 0)
 	values := make([]any, 0)
-	for _, tableRow := range params.TableRows {
-		columns = append(columns, tableRow.Column)
-		values = append(values, tableRow.ParsedValue)
+	for _, row := range params.TableRows.Rows {
+		columns = append(columns, row.Column)
+		values = append(values, row.Value)
 	}
 
 	return map[string]any{
@@ -113,95 +33,56 @@ const DeleteTpl = `
 DELETE FROM
 	{{ .table_name }}
 WHERE
-	{{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1
+	{{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
 `
 
 type DeleteExecuteParams struct {
-	TableName  string       `structs:"table_name"`
-	Conditions []*Condition `structs:"conditions"`
-}
-
-func (params DeleteExecuteParams) check() error {
-	if strutils.IsStringEmpty(params.TableName) {
-		return errors.New("没有传递表名")
-	}
-
-	if params.Conditions == nil || len(params.Conditions) == 0 {
-		return errors.New("没有传递条件")
-	}
-
-	for _, condition := range params.Conditions {
-		if err := condition.parse(); err != nil {
-			return err
-		}
-	}
-
-	return nil
+	TableName string
+	*Conditions
 }
 
 func (params DeleteExecuteParams) Map() (map[string]any, error) {
-	if err := params.check(); err != nil {
-		return nil, err
+	if params.Conditions == nil {
+		return nil, errors.New("没有传递删除条件")
 	}
 
-	return structs.Map(params), nil
+	return map[string]any{
+		"table_name": params.TableName,
+		"conditions": params.Conditions.Conditions,
+	}, nil
 }
 
 const UpdateTpl = `
-{{- $setList := list -}}
-{{- range .table_rows -}}
-{{- $set := printf "%s = %s" .column .value -}}
-{{- $setList = append $setList $set -}}
-{{- end }}
-
 UPDATE
 	{{ .table_name }}
 SET
-	{{ $setList | join "," }}
+	{{ .set_list | join "," }}
 WHERE
-	{{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1
+	{{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
 `
 
 type UpdateExecuteParams struct {
-	TableName  string       `structs:"table_name"`
-	TableRows  []*TableRow  `structs:"table_rows"`
-	Conditions []*Condition `structs:"conditions"`
+	TableName string
+	*TableRows
+	*Conditions
 }
 
-func (params UpdateExecuteParams) check() error {
-	if strutils.IsStringEmpty(params.TableName) {
-		return errors.New("没有传递表名")
-	}
-
-	if params.TableRows == nil || len(params.TableRows) == 0 {
-		return errors.New("没有传递表行")
-	}
-
-	for _, tableRow := range params.TableRows {
-		if err := tableRow.parse(); err != nil {
-			return err
-		}
-	}
-
-	if params.Conditions == nil || len(params.Conditions) == 0 {
-		return errors.New("没有传递条件")
-	}
-
-	for _, condition := range params.Conditions {
-		if err := condition.parse(); err != nil {
-			return err
-		}
+func (params UpdateExecuteParams) Map() (map[string]any, error) {
+	setList := make([]string, 0)
+	for _, row := range params.TableRows.Rows {
+		setList = append(setList, row.Column+" = "+row.Value)
 	}
 
-	return nil
-}
-
-func (params UpdateExecuteParams) Map() (map[string]any, error) {
-	if err := params.check(); err != nil {
-		return nil, err
+	conditions := make([]string, 0)
+	if params.Conditions != nil {
+		conditions = params.Conditions.Conditions
 	}
 
-	return structs.Map(params), nil
+	return map[string]any{
+		"table_name": params.TableName,
+		"set_list":   setList,
+		"conditions": conditions,
+	}, nil
 }
 
 const QueryTpl = `
@@ -210,41 +91,40 @@ SELECT
 FROM
 	{{ .table_name }}
 WHERE
-	{{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1 
+	{{ range .conditions }} {{ . }} AND {{ end }} 1 = 1 
 {{ if .limit }}LIMIT {{ .limit }}{{ end }}
 {{ if .offset }}OFFSET {{ .offset }}{{ end }}
 `
 
 type QueryExecuteParams struct {
-	TableName     string       `structs:"table_name"`
-	SelectColumns []string     `structs:"select_columns"`
-	Conditions    []*Condition `structs:"conditions"`
-	Limit         int          `structs:"limit"`
-	Offset        int          `structs:"offset"`
+	TableName     string
+	SelectColumns []string
+	*Conditions
+	PageNo   int
+	PageSize int
 }
 
-func (params QueryExecuteParams) check() error {
-	if strutils.IsStringEmpty(params.TableName) {
-		return errors.New("没有传递表名")
-	}
+func (params QueryExecuteParams) Map() (map[string]any, error) {
+	var limit int
+	var offset int
 
-	if params.Conditions != nil && len(params.Conditions) != 0 {
-		for _, condition := range params.Conditions {
-			if err := condition.parse(); err != nil {
-				return err
-			}
-		}
+	if params.PageNo != 0 && params.PageSize != 0 {
+		offset = (params.PageNo - 1) * params.PageSize
+		limit = params.PageSize
 	}
 
-	return nil
-}
-
-func (params QueryExecuteParams) Map() (map[string]any, error) {
-	if err := params.check(); err != nil {
-		return nil, err
+	conditions := make([]string, 0)
+	if params.Conditions != nil {
+		conditions = params.Conditions.Conditions
 	}
 
-	return structs.Map(params), nil
+	return map[string]any{
+		"table_name":     params.TableName,
+		"select_columns": params.SelectColumns,
+		"conditions":     conditions,
+		"limit":          limit,
+		"offset":         offset,
+	}, nil
 }
 
 const CountTpl = `
@@ -253,84 +133,21 @@ SELECT
 FROM
 	{{ .table_name }}
 WHERE
-	{{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1`
-
-type CountExecuteParams struct {
-	TableName  string       `structs:"table_name"`
-	Conditions []*Condition `structs:"conditions"`
-}
-
-func (params CountExecuteParams) check() error {
-	if strutils.IsStringEmpty(params.TableName) {
-		return errors.New("没有传递表名")
-	}
-
-	if params.Conditions != nil && len(params.Conditions) != 0 {
-		for _, condition := range params.Conditions {
-			if err := condition.parse(); err != nil {
-				return err
-			}
-		}
-	}
-
-	return nil
-}
-
-func (params CountExecuteParams) Map() (map[string]any, error) {
-	if err := params.check(); err != nil {
-		return nil, err
-	}
+	{{ range .conditions }} {{ . }} AND {{ end }} 1 = 1`
 
-	return structs.Map(params), nil
+type ConditionsExecuteParams struct {
+	TableName string
+	*Conditions
 }
 
-const (
-	timeWriteFormat = time.DateTime + ".000000 +08:00"
-)
-
-func parseValue(value any) (string, error) {
-	valueValue := reflect.ValueOf(value)
-
-	if !valueValue.IsValid() {
-		return "", errors.New("无效值")
+func (params ConditionsExecuteParams) Map() (map[string]any, error) {
+	conditions := make([]string, 0)
+	if params.Conditions != nil {
+		conditions = params.Conditions.Conditions
 	}
 
-	if valueValue.Kind() == reflect.Ptr && valueValue.IsNil() {
-		return "", errors.New("空值")
-	}
-
-	if valueValue.Kind() == reflect.Ptr {
-		valueValue = valueValue.Elem()
-	}
-
-	switch v := valueValue.Interface().(type) {
-	case string:
-		return "'" + v + "'", nil
-	case bool:
-		return strconv.FormatBool(v), nil
-	case time.Time:
-		return "'" + v.Format(timeWriteFormat) + "'", nil
-	case int:
-		return strconv.Itoa(v), nil
-	case int8:
-		return strconv.FormatInt(int64(v), 10), nil
-	case int16:
-		return strconv.FormatInt(int64(v), 10), nil
-	case int32:
-		return strconv.FormatInt(int64(v), 10), nil
-	case int64:
-		return strconv.FormatInt(v, 10), nil
-	case uint:
-		return strconv.FormatUint(uint64(v), 10), nil
-	case uint8:
-		return strconv.FormatUint(uint64(v), 10), nil
-	case uint16:
-		return strconv.FormatUint(uint64(v), 10), nil
-	case uint32:
-		return strconv.FormatUint(uint64(v), 10), nil
-	case uint64:
-		return strconv.FormatUint(v, 10), nil
-	default:
-		return "", errors.New("不支持的类型")
-	}
+	return map[string]any{
+		"table_name": params.TableName,
+		"conditions": conditions,
+	}, nil
 }

+ 36 - 0
sdk/raw_sql_tpl/table_row.go

@@ -0,0 +1,36 @@
+package raw_sql_tpl
+
+type TableRows struct {
+	Rows []TableRow
+	Err  error
+}
+
+type TableRow struct {
+	Column string
+	Value  string
+}
+
+func NewTableRows() *TableRows {
+	return &TableRows{
+		Rows: make([]TableRow, 0),
+	}
+}
+
+func (tableRows *TableRows) Add(column string, value any) *TableRows {
+	if tableRows.Err != nil {
+		return tableRows
+	}
+
+	parsedValue, err := parseValue(value)
+	if err != nil {
+		tableRows.Err = err
+		return tableRows
+	}
+
+	tableRows.Rows = append(tableRows.Rows, TableRow{
+		Column: column,
+		Value:  parsedValue,
+	})
+
+	return tableRows
+}

+ 59 - 0
sdk/raw_sql_tpl/value.go

@@ -0,0 +1,59 @@
+package raw_sql_tpl
+
+import (
+	"errors"
+	"reflect"
+	"strconv"
+	"time"
+)
+
+const (
+	timeWriteFormat = time.DateTime + ".000000 +08:00"
+)
+
+func parseValue(value any) (string, error) {
+	valueValue := reflect.ValueOf(value)
+
+	if !valueValue.IsValid() {
+		return "", errors.New("无效值")
+	}
+
+	if valueValue.Kind() == reflect.Ptr && valueValue.IsNil() {
+		return "", errors.New("空值")
+	}
+
+	if valueValue.Kind() == reflect.Ptr {
+		valueValue = valueValue.Elem()
+	}
+
+	switch v := valueValue.Interface().(type) {
+	case string:
+		return "'" + v + "'", nil
+	case bool:
+		return strconv.FormatBool(v), nil
+	case time.Time:
+		return "'" + v.Format(timeWriteFormat) + "'", nil
+	case int:
+		return strconv.Itoa(v), nil
+	case int8:
+		return strconv.FormatInt(int64(v), 10), nil
+	case int16:
+		return strconv.FormatInt(int64(v), 10), nil
+	case int32:
+		return strconv.FormatInt(int64(v), 10), nil
+	case int64:
+		return strconv.FormatInt(v, 10), nil
+	case uint:
+		return strconv.FormatUint(uint64(v), 10), nil
+	case uint8:
+		return strconv.FormatUint(uint64(v), 10), nil
+	case uint16:
+		return strconv.FormatUint(uint64(v), 10), nil
+	case uint32:
+		return strconv.FormatUint(uint64(v), 10), nil
+	case uint64:
+		return strconv.FormatUint(v, 10), nil
+	default:
+		return "", errors.New("不支持的类型")
+	}
+}

+ 106 - 394
sdk/sql.go

@@ -21,10 +21,7 @@ const (
 	lastUpdatedTimeFieldName = "LastUpdatedTime"
 )
 
-type ValueCallback[T any] func(e T, fieldName string, value any) (retValue any, err error)
-type ConditionCallback[T any] func(e T, fieldName string, columnName string, value any) (retConditionOp string, retConditionValue any, err error)
-
-func Insert[T any](executor SqlExecutor, tableName string, e T, callback ValueCallback[T]) error {
+func InsertEntity[T any](executor SqlExecutor, tableName string, e T) error {
 	if executor == nil {
 		return errors.New("没有传递执行器")
 	}
@@ -42,9 +39,7 @@ func Insert[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
 		return err
 	}
 
-	executeParams := raw_sql_tpl.InsertExecuteParams{
-		TableName: tableName,
-	}
+	tableRow := raw_sql_tpl.NewTableRows()
 
 	now := time.Now()
 
@@ -56,36 +51,21 @@ func Insert[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
 			value = sqlMappingColumn.ValueFieldValue.Interface()
 		}
 
-		if sqlMappingColumn.InsertCallback {
-			if callback == nil {
-				return errors.New("需要使用回调函数但是没有传递回调函数")
-			}
-
-			retValue, err := callback(e, fieldName, value)
-			if err != nil {
-				return err
-			}
-
-			retValueType := reflect.TypeOf(retValue)
-			if retValueType == nil || retValueType.Kind() == reflect.Ptr {
-				return errors.New("返回应当为值类型")
-			}
-
-			value = retValue
-		}
-
 		if (fieldName == createdTimeFieldName || fieldName == lastUpdatedTimeFieldName) &&
 			fieldType.String() == "time.Time" && value.(time.Time).IsZero() {
 			value = now
 		}
 
-		executeParams.TableRows = append(executeParams.TableRows, &raw_sql_tpl.TableRow{
-			Column: sqlMappingColumn.Name,
-			Value:  value,
-		})
+		err := tableRow.Add(sqlMappingColumn.Name, value).Err
+		if err != nil {
+			return err
+		}
 	}
 
-	executeParamsMap, err := executeParams.Map()
+	executeParamsMap, err := raw_sql_tpl.InsertExecuteParams{
+		TableName: tableName,
+		TableRows: tableRow,
+	}.Map()
 	if err != nil {
 		return err
 	}
@@ -102,7 +82,7 @@ func Insert[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
 	return nil
 }
 
-func Delete[T any](executor SqlExecutor, tableName string, e T) error {
+func DeleteEntity[T any](executor SqlExecutor, tableName string, e T) error {
 	if executor == nil {
 		return errors.New("没有传递执行器")
 	}
@@ -120,10 +100,7 @@ func Delete[T any](executor SqlExecutor, tableName string, e T) error {
 		return err
 	}
 
-	executeParams := raw_sql_tpl.DeleteExecuteParams{
-		TableName:  tableName,
-		Conditions: make([]*raw_sql_tpl.Condition, 0),
-	}
+	conditions := raw_sql_tpl.NewConditions()
 
 	for _, sqlMappingColumn := range sqlMapping.ColumnMap {
 		if !sqlMappingColumn.IsKey {
@@ -137,14 +114,16 @@ func Delete[T any](executor SqlExecutor, tableName string, e T) error {
 			value = sqlMappingColumn.ValueFieldValue.Interface()
 		}
 
-		executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
-			Column:   sqlMappingColumn.Name,
-			Operator: "=",
-			Value:    value,
-		})
+		err := conditions.Equal(sqlMappingColumn.Name, value).Err
+		if err != nil {
+			return err
+		}
 	}
 
-	executeParamsMap, err := executeParams.Map()
+	executeParamsMap, err := raw_sql_tpl.DeleteExecuteParams{
+		TableName:  tableName,
+		Conditions: conditions,
+	}.Map()
 	if err != nil {
 		return err
 	}
@@ -157,7 +136,7 @@ func Delete[T any](executor SqlExecutor, tableName string, e T) error {
 	return nil
 }
 
-func Update[T any](executor SqlExecutor, tableName string, e T, callback ValueCallback[T]) error {
+func UpdateEntity[T any](executor SqlExecutor, tableName string, e T) error {
 	if executor == nil {
 		return errors.New("没有传递执行器")
 	}
@@ -175,10 +154,8 @@ func Update[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
 		return err
 	}
 
-	executeParams := raw_sql_tpl.UpdateExecuteParams{
-		TableName:  tableName,
-		Conditions: make([]*raw_sql_tpl.Condition, 0),
-	}
+	tableRows := raw_sql_tpl.NewTableRows()
+	conditions := raw_sql_tpl.NewConditions()
 
 	now := time.Now()
 
@@ -194,51 +171,36 @@ func Update[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
 			value = sqlMappingColumn.ValueFieldValue.Interface()
 		}
 
-		if sqlMappingColumn.UpdateCallback {
-			if callback == nil {
-				return errors.New("需要使用回调函数但是没有传递回调函数")
-			}
-
-			retValue, err := callback(e, fieldName, value)
-			if err != nil {
-				return err
-			}
-
-			retValueType := reflect.TypeOf(retValue)
-			if retValueType == nil || retValueType.Kind() == reflect.Ptr {
-				return errors.New("返回应当为值类型")
-			}
-
-			value = retValue
-		}
-
 		if fieldName == lastUpdatedTimeFieldName &&
 			fieldType.String() == "time.Time" && value.(time.Time).IsZero() {
 			value = now
 		}
 
-		// 字段为空不更新
+		// 字段为空且不能清空,不更新
 		if reflect.ValueOf(value).IsZero() && !sqlMappingColumn.CanUpdateClear {
 			continue
 		}
 
 		if !sqlMappingColumn.IsKey {
-			executeParams.TableRows = append(executeParams.TableRows, &raw_sql_tpl.TableRow{
-				Column: sqlMappingColumn.Name,
-				Value:  value,
-			})
+			err := tableRows.Add(sqlMappingColumn.Name, value).Err
+			if err != nil {
+				return err
+			}
 		}
 
 		if sqlMappingColumn.IsKey {
-			executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
-				Column:   sqlMappingColumn.Name,
-				Operator: "=",
-				Value:    value,
-			})
+			err := conditions.Equal(sqlMappingColumn.Name, value).Err
+			if err != nil {
+				return err
+			}
 		}
 	}
 
-	executeParamsMap, err := executeParams.Map()
+	executeParamsMap, err := raw_sql_tpl.UpdateExecuteParams{
+		TableName:  tableName,
+		TableRows:  tableRows,
+		Conditions: conditions,
+	}.Map()
 	if err != nil {
 		return err
 	}
@@ -251,110 +213,100 @@ func Update[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
 	return nil
 }
 
-func Query[T any](executor SqlExecutor, tableName string, e T, pageNo int, pageSize int, callback ConditionCallback[T]) ([]map[string]any, int64, error) {
+func Insert(executor SqlExecutor, executeParams *raw_sql_tpl.InsertExecuteParams) error {
 	if executor == nil {
-		return nil, 0, errors.New("没有传递执行器")
+		return errors.New("没有传递执行器")
 	}
 
-	if strutils.IsStringEmpty(tableName) {
-		return nil, 0, errors.New("没有传递表名")
+	if executeParams == nil {
+		return errors.New("没有传递执行参数")
 	}
 
-	if reflect.TypeOf(e) == nil {
-		return nil, 0, errors.New("没有传递实体")
+	executeParamsMap, err := executeParams.Map()
+	if err != nil {
+		return err
 	}
 
-	sqlMapping, err := tag.ParseSqlMapping(e)
+	_, err = executor.ExecuteRawSql(raw_sql_tpl.InsertTpl, executeParamsMap)
 	if err != nil {
-		return nil, 0, err
+		return err
 	}
 
-	var offset int
-	var limit int
-	if pageNo != 0 && pageSize != 0 {
-		offset = (pageNo - 1) * pageSize
-		limit = pageSize
-	}
+	return nil
+}
 
-	executeParams := raw_sql_tpl.QueryExecuteParams{
-		TableName:  tableName,
-		Conditions: make([]*raw_sql_tpl.Condition, 0),
-		Limit:      limit,
-		Offset:     offset,
+func Delete(executor SqlExecutor, executeParams *raw_sql_tpl.DeleteExecuteParams) error {
+	if executor == nil {
+		return errors.New("没有传递执行器")
 	}
 
-	countParams := raw_sql_tpl.CountExecuteParams{
-		TableName:  tableName,
-		Conditions: make([]*raw_sql_tpl.Condition, 0),
+	if executeParams == nil {
+		return errors.New("没有传递执行参数")
 	}
 
-	for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
-		if !sqlMappingColumn.CanQuery {
-			continue
-		}
-
-		fieldType := sqlMappingColumn.ValueFieldType
+	executeParamsMap, err := executeParams.Map()
+	if err != nil {
+		return err
+	}
 
-		conditionValue := reflect.Zero(fieldType).Interface()
-		if !sqlMappingColumn.ValueFieldValue.IsZero() {
-			conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
-		}
+	_, err = executor.ExecuteRawSql(raw_sql_tpl.DeleteTpl, executeParamsMap)
+	if err != nil {
+		return err
+	}
 
-		conditionOp := "="
+	return nil
+}
 
-		if sqlMappingColumn.QueryCallback {
-			if callback == nil {
-				return nil, 0, errors.New("需要使用回调函数但是没有传递回调函数")
-			}
+func Update(executor SqlExecutor, executeParams *raw_sql_tpl.UpdateExecuteParams) error {
+	if executor == nil {
+		return errors.New("没有传递执行器")
+	}
 
-			retConditionOp, retConditionValue, err := callback(e, fieldName, sqlMappingColumn.Name, conditionValue)
-			if err != nil {
-				return nil, 0, err
-			}
+	if executeParams == nil {
+		return errors.New("没有传递执行参数")
+	}
 
-			retValueType := reflect.TypeOf(retConditionValue)
-			if retValueType == nil || retValueType.Kind() == reflect.Ptr {
-				return nil, 0, errors.New("返回应当为值类型")
-			}
+	executeParamsMap, err := executeParams.Map()
+	if err != nil {
+		return err
+	}
 
-			conditionValue = retConditionValue
-			conditionOp = retConditionOp
-		}
+	_, err = executor.ExecuteRawSql(raw_sql_tpl.UpdateTpl, executeParamsMap)
+	if err != nil {
+		return err
+	}
 
-		// 字段为空不更新
-		if reflect.ValueOf(conditionValue).IsZero() {
-			continue
-		}
+	return nil
+}
 
-		executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
-			Column:   sqlMappingColumn.Name,
-			Operator: conditionOp,
-			Value:    conditionValue,
-		})
+func Query(executor SqlExecutor, executeParams *raw_sql_tpl.QueryExecuteParams) ([]map[string]any, int64, error) {
+	if executor == nil {
+		return nil, 0, errors.New("没有传递执行器")
+	}
 
-		countParams.Conditions = append(countParams.Conditions, &raw_sql_tpl.Condition{
-			Column:   sqlMappingColumn.Name,
-			Operator: conditionOp,
-			Value:    conditionValue,
-		})
+	if executeParams == nil {
+		return nil, 0, errors.New("没有传递执行参数")
 	}
 
-	executeParamsMap, err := executeParams.Map()
+	queryExecuteParamsMap, err := executeParams.Map()
 	if err != nil {
 		return nil, 0, err
 	}
 
-	countParamsMap, err := countParams.Map()
+	countExecuteParamsMap, err := raw_sql_tpl.ConditionsExecuteParams{
+		TableName:  executeParams.TableName,
+		Conditions: executeParams.Conditions,
+	}.Map()
 	if err != nil {
 		return nil, 0, err
 	}
 
-	tableRows, err := executor.ExecuteRawSql(raw_sql_tpl.QueryTpl, executeParamsMap)
+	tableRows, err := executor.ExecuteRawSql(raw_sql_tpl.QueryTpl, queryExecuteParamsMap)
 	if err != nil {
 		return nil, 0, err
 	}
 
-	countTableRow, err := executor.ExecuteRawSql(raw_sql_tpl.CountTpl, countParamsMap)
+	countTableRow, err := executor.ExecuteRawSql(raw_sql_tpl.CountTpl, countExecuteParamsMap)
 	if err != nil {
 		return nil, 0, err
 	}
@@ -362,48 +314,13 @@ func Query[T any](executor SqlExecutor, tableName string, e T, pageNo int, pageS
 	return tableRows, int64(countTableRow[0]["count"].(float64)), nil
 }
 
-func QueryByKeys[T any](executor SqlExecutor, tableName string, e T) (map[string]any, error) {
+func QueryOne(executor SqlExecutor, executeParams *raw_sql_tpl.QueryExecuteParams) (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
-	}
-
-	executeParams := raw_sql_tpl.QueryExecuteParams{
-		TableName:  tableName,
-		Conditions: make([]*raw_sql_tpl.Condition, 0),
-		Limit:      0,
-		Offset:     0,
-	}
-
-	for _, sqlMappingColumn := range sqlMapping.ColumnMap {
-		if !sqlMappingColumn.IsKey {
-			continue
-		}
-
-		fieldType := sqlMappingColumn.ValueFieldType
-
-		conditionValue := reflect.Zero(fieldType).Interface()
-		if !sqlMappingColumn.ValueFieldValue.IsZero() {
-			conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
-		}
-
-		executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
-			Column:   sqlMappingColumn.Name,
-			Operator: "=",
-			Value:    conditionValue,
-		})
+	if executeParams == nil {
+		return nil, errors.New("没有传递执行参数")
 	}
 
 	executeParamsMap, err := executeParams.Map()
@@ -423,63 +340,13 @@ func QueryByKeys[T any](executor SqlExecutor, tableName string, e T) (map[string
 	return tableRows[0], nil
 }
 
-func Count[T any](executor SqlExecutor, tableName string, e T, callback ConditionCallback[T]) (int64, error) {
+func Count(executor SqlExecutor, executeParams *raw_sql_tpl.ConditionsExecuteParams) (int64, error) {
 	if executor == nil {
 		return 0, errors.New("没有传递执行器")
 	}
 
-	if strutils.IsStringEmpty(tableName) {
-		return 0, errors.New("没有传递表名")
-	}
-
-	if reflect.TypeOf(e) == nil {
-		return 0, errors.New("没有传递实体")
-	}
-
-	sqlMapping, err := tag.ParseSqlMapping(e)
-	if err != nil {
-		return 0, err
-	}
-
-	executeParams := raw_sql_tpl.CountExecuteParams{
-		TableName:  tableName,
-		Conditions: make([]*raw_sql_tpl.Condition, 0),
-	}
-
-	for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
-		fieldType := sqlMappingColumn.ValueFieldType
-
-		conditionValue := reflect.Zero(fieldType).Interface()
-		if !sqlMappingColumn.ValueFieldValue.IsZero() {
-			conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
-		}
-
-		conditionOp := "="
-
-		if sqlMappingColumn.CountCallback {
-			if callback == nil {
-				return 0, errors.New("需要使用回调函数但是没有传递回调函数")
-			}
-
-			retConditionOp, retConditionValue, err := callback(e, fieldName, sqlMappingColumn.Name, conditionValue)
-			if err != nil {
-				return 0, err
-			}
-
-			retValueType := reflect.TypeOf(retConditionValue)
-			if retValueType == nil || retValueType.Kind() == reflect.Ptr {
-				return 0, errors.New("返回应当为值类型")
-			}
-
-			conditionValue = retConditionValue
-			conditionOp = retConditionOp
-		}
-
-		executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
-			Column:   sqlMappingColumn.Name,
-			Operator: conditionOp,
-			Value:    conditionValue,
-		})
+	if executeParams == nil {
+		return 0, errors.New("没有传递执行参数")
 	}
 
 	executeParamsMap, err := executeParams.Map()
@@ -495,63 +362,13 @@ func Count[T any](executor SqlExecutor, tableName string, e T, callback Conditio
 	return int64(tableRows[0]["count"].(float64)), nil
 }
 
-func CheckExist[T any](executor SqlExecutor, tableName string, e T, callback ConditionCallback[T]) (bool, error) {
+func CheckExist(executor SqlExecutor, executeParams *raw_sql_tpl.ConditionsExecuteParams) (bool, error) {
 	if executor == nil {
 		return false, errors.New("没有传递执行器")
 	}
 
-	if strutils.IsStringEmpty(tableName) {
-		return false, errors.New("没有传递表名")
-	}
-
-	if reflect.TypeOf(e) == nil {
-		return false, errors.New("没有传递实体")
-	}
-
-	sqlMapping, err := tag.ParseSqlMapping(e)
-	if err != nil {
-		return false, err
-	}
-
-	executeParams := raw_sql_tpl.CountExecuteParams{
-		TableName:  tableName,
-		Conditions: make([]*raw_sql_tpl.Condition, 0),
-	}
-
-	for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
-		fieldType := sqlMappingColumn.ValueFieldType
-
-		conditionValue := reflect.Zero(fieldType).Interface()
-		if !sqlMappingColumn.ValueFieldValue.IsZero() {
-			conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
-		}
-
-		conditionOp := "="
-
-		if sqlMappingColumn.CheckExistCallback {
-			if callback == nil {
-				return false, errors.New("需要使用回调函数但是没有传递回调函数")
-			}
-
-			retConditionOp, retConditionValue, err := callback(e, fieldName, sqlMappingColumn.Name, conditionValue)
-			if err != nil {
-				return false, err
-			}
-
-			retValueType := reflect.TypeOf(retConditionValue)
-			if retValueType == nil || retValueType.Kind() == reflect.Ptr {
-				return false, errors.New("返回应当为值类型")
-			}
-
-			conditionValue = retConditionValue
-			conditionOp = retConditionOp
-		}
-
-		executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
-			Column:   sqlMappingColumn.Name,
-			Operator: conditionOp,
-			Value:    conditionValue,
-		})
+	if executeParams == nil {
+		return false, errors.New("没有传递执行参数")
 	}
 
 	executeParamsMap, err := executeParams.Map()
@@ -567,118 +384,13 @@ func CheckExist[T any](executor SqlExecutor, tableName string, e T, callback Con
 	return int64(tableRows[0]["count"].(float64)) > 0, nil
 }
 
-func CheckExistByKey[T any](executor SqlExecutor, tableName string, e T) (bool, error) {
+func CheckHasOnlyOne(executor SqlExecutor, executeParams *raw_sql_tpl.ConditionsExecuteParams) (bool, error) {
 	if executor == nil {
 		return false, errors.New("没有传递执行器")
 	}
 
-	if strutils.IsStringEmpty(tableName) {
-		return false, errors.New("没有传递表名")
-	}
-
-	if reflect.TypeOf(e) == nil {
-		return false, errors.New("没有传递实体")
-	}
-
-	sqlMapping, err := tag.ParseSqlMapping(e)
-	if err != nil {
-		return false, err
-	}
-
-	executeParams := raw_sql_tpl.CountExecuteParams{
-		TableName:  tableName,
-		Conditions: make([]*raw_sql_tpl.Condition, 0),
-	}
-
-	for _, sqlMappingColumn := range sqlMapping.ColumnMap {
-		if !sqlMappingColumn.IsKey {
-			continue
-		}
-
-		fieldType := sqlMappingColumn.ValueFieldType
-
-		conditionValue := reflect.Zero(fieldType).Interface()
-		if !sqlMappingColumn.ValueFieldValue.IsZero() {
-			conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
-		}
-
-		executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
-			Column:   sqlMappingColumn.Name,
-			Operator: "=",
-			Value:    conditionValue,
-		})
-	}
-
-	executeParamsMap, err := executeParams.Map()
-	if err != nil {
-		return false, err
-	}
-
-	tableRows, err := executor.ExecuteRawSql(raw_sql_tpl.CountTpl, executeParamsMap)
-	if err != nil {
-		return false, err
-	}
-
-	return int64(tableRows[0]["count"].(float64)) > 0, nil
-}
-
-func CheckHasOnlyOne[T any](executor SqlExecutor, tableName string, e T, callback ConditionCallback[T]) (bool, error) {
-	if executor == nil {
-		return false, errors.New("没有传递执行器")
-	}
-
-	if strutils.IsStringEmpty(tableName) {
-		return false, errors.New("没有传递表名")
-	}
-
-	if reflect.TypeOf(e) == nil {
-		return false, errors.New("没有传递实体")
-	}
-
-	sqlMapping, err := tag.ParseSqlMapping(e)
-	if err != nil {
-		return false, err
-	}
-
-	executeParams := raw_sql_tpl.CountExecuteParams{
-		TableName:  tableName,
-		Conditions: make([]*raw_sql_tpl.Condition, 0),
-	}
-
-	for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
-		fieldType := sqlMappingColumn.ValueFieldType
-
-		conditionValue := reflect.Zero(fieldType).Interface()
-		if !sqlMappingColumn.ValueFieldValue.IsZero() {
-			conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
-		}
-
-		conditionOp := "="
-
-		if sqlMappingColumn.QueryCallback {
-			if callback == nil {
-				return false, errors.New("需要使用回调函数但是没有传递回调函数")
-			}
-
-			retConditionOp, retConditionValue, err := callback(e, fieldName, sqlMappingColumn.Name, conditionValue)
-			if err != nil {
-				return false, err
-			}
-
-			retValueType := reflect.TypeOf(retConditionValue)
-			if retValueType == nil || retValueType.Kind() == reflect.Ptr {
-				return false, errors.New("返回应当为值类型")
-			}
-
-			conditionValue = retConditionValue
-			conditionOp = retConditionOp
-		}
-
-		executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
-			Column:   sqlMappingColumn.Name,
-			Operator: conditionOp,
-			Value:    conditionValue,
-		})
+	if executeParams == nil {
+		return false, errors.New("没有传递执行参数")
 	}
 
 	executeParamsMap, err := executeParams.Map()
@@ -732,7 +444,7 @@ const (
 	sqlResultTimeFormat = "2006-01-02T15:04:05.000000+08:00"
 )
 
-func parseSqlResults(results any, e any) error {
+func ParseSqlResults(results any, e any) error {
 	decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
 		DecodeHook: mapstructure.StringToTimeHookFunc(sqlResultTimeFormat),
 		Result:     e,

+ 19 - 53
sdk/tag/sql_mapping.go

@@ -10,22 +10,16 @@ import (
 const (
 	sqlMappingTagPartSeparator         = ";"
 	sqlMappingTagPartKeyValueSeparator = ":"
+	sqlMappingQueryConditionSeparator  = "##"
 )
 
 const (
-	sqlMappingTagKey                  = "sqlmapping"
-	sqlMappingIgnore                  = "-"
-	sqlMappingColumn                  = "column"
-	sqlMappingKey                     = "key"
-	sqlMappingNotUpdate               = "notUpdate"
-	sqlMappingUpdateClear             = "updateClear"
-	sqlMappingNotQuery                = "notQuery"
-	sqlMappingInsertCallback          = "insertCallback"
-	sqlMappingUpdateCallback          = "updateCallback"
-	sqlMappingQueryCallback           = "queryCallback"
-	sqlMappingCountCallback           = "countCallback"
-	sqlMappingCheckExistCallback      = "checkExistCallback"
-	sqlMappingCheckHasOnlyOneCallback = "checkHasOnlyCallback"
+	sqlMappingTagKey      = "sqlmapping"
+	sqlMappingIgnore      = "-"
+	sqlMappingColumn      = "column"
+	sqlMappingKey         = "key"
+	sqlMappingNotUpdate   = "notUpdate"
+	sqlMappingUpdateClear = "updateClear"
 )
 
 type SqlMapping struct {
@@ -75,17 +69,10 @@ func ParseSqlMapping(e any) (*SqlMapping, error) {
 }
 
 type SqlMappingColumn struct {
-	Name                    string
-	IsKey                   bool
-	CanUpdate               bool
-	CanUpdateClear          bool
-	CanQuery                bool
-	InsertCallback          bool
-	UpdateCallback          bool
-	QueryCallback           bool
-	CountCallback           bool
-	CheckExistCallback      bool
-	CheckHasOnlyOneCallback bool
+	Name           string
+	IsKey          bool
+	CanUpdate      bool
+	CanUpdateClear bool
 
 	// 原字段的反射结构
 	OriginFieldType  reflect.Type
@@ -111,21 +98,14 @@ func parseSqlMappingColumn(field reflect.StructField, fieldValue reflect.Value)
 	}
 
 	sqlColumn := &SqlMappingColumn{
-		Name:                    strcase.ToSnake(field.Name),
-		IsKey:                   false,
-		CanUpdate:               true,
-		CanUpdateClear:          false,
-		CanQuery:                true,
-		InsertCallback:          false,
-		UpdateCallback:          false,
-		QueryCallback:           false,
-		CountCallback:           false,
-		CheckExistCallback:      false,
-		CheckHasOnlyOneCallback: false,
-		OriginFieldType:         field.Type,
-		OriginFieldValue:        fieldValue,
-		ValueFieldType:          valueFieldType,
-		ValueFieldValue:         valueFieldValue,
+		Name:             strcase.ToSnake(field.Name),
+		IsKey:            false,
+		CanUpdate:        true,
+		CanUpdateClear:   false,
+		OriginFieldType:  field.Type,
+		OriginFieldValue: fieldValue,
+		ValueFieldType:   valueFieldType,
+		ValueFieldValue:  valueFieldValue,
 	}
 
 	sqlMappingTag, ok := field.Tag.Lookup(sqlMappingTagKey)
@@ -151,20 +131,6 @@ func parseSqlMappingColumn(field reflect.StructField, fieldValue reflect.Value)
 				sqlColumn.CanUpdate = false
 			case sqlMappingUpdateClear:
 				sqlColumn.CanUpdateClear = true
-			case sqlMappingNotQuery:
-				sqlColumn.CanQuery = false
-			case sqlMappingInsertCallback:
-				sqlColumn.InsertCallback = true
-			case sqlMappingUpdateCallback:
-				sqlColumn.UpdateCallback = true
-			case sqlMappingQueryCallback:
-				sqlColumn.QueryCallback = true
-			case sqlMappingCountCallback:
-				sqlColumn.CountCallback = true
-			case sqlMappingCheckExistCallback:
-				sqlColumn.CheckExistCallback = true
-			case sqlMappingCheckHasOnlyOneCallback:
-				sqlColumn.CheckHasOnlyOneCallback = true
 			default:
 				continue
 			}

+ 53 - 207
test/sdk_test.go

@@ -15,8 +15,8 @@ import (
 
 type Class struct {
 	ID              string     `sqlmapping:"key;" mapstructure:"id"`
-	Name            string     `sqlmapping:"updateClear;notQuery;insertCallback;updateCallback;" mapstructure:"name"`
-	StudentNum      int        `sqlmapping:"column:student_num;notUpdate;queryCallback;countCallback;checkExistCallback;checkHasOnlyCallback;" mapstructure:"student_num_alias"`
+	Name            string     `sqlmapping:"updateClear;" mapstructure:"name"`
+	StudentNum      int        `sqlmapping:"column:student_num;notUpdate;" mapstructure:"student_num_alias"`
 	GraduatedTime   time.Time  `sqlresult:"callback" mapstructure:"graduated_time"`
 	CreatedTime     *time.Time `mapstructure:"created_time"`
 	LastUpdatedTime time.Time  `mapstructure:"last_updated_time"`
@@ -49,32 +49,12 @@ func TestBasic(t *testing.T) {
 
 	insertExecuteParams, err := raw_sql_tpl.InsertExecuteParams{
 		TableName: tableName,
-		TableRows: []*raw_sql_tpl.TableRow{
-			{
-				Column: "id",
-				Value:  classID,
-			},
-			{
-				Column: "name",
-				Value:  className,
-			},
-			{
-				Column: "student_num",
-				Value:  studentNum,
-			},
-			{
-				Column: "graduated_time",
-				Value:  now,
-			},
-			{
-				Column: "created_time",
-				Value:  now,
-			},
-			{
-				Column: "last_updated_time",
-				Value:  now,
-			},
-		},
+		TableRows: raw_sql_tpl.NewTableRows().Add("id", classID).
+			Add("name", className).
+			Add("student_num", studentNum).
+			Add("graduated_time", now).
+			Add("created_time", now).
+			Add("last_updated_time", now),
 	}.Map()
 	if err != nil {
 		t.Fatal(err)
@@ -166,45 +146,20 @@ func TestRawSqlTemplate(t *testing.T) {
 
 	insertExecuteParams, err := raw_sql_tpl.InsertExecuteParams{
 		TableName: tableName,
-		TableRows: []*raw_sql_tpl.TableRow{
-			{
-				Column: "id",
-				Value:  classID,
-			},
-			{
-				Column: "name",
-				Value:  className,
-			},
-			{
-				Column: "student_num",
-				Value:  studentNum,
-			},
-			{
-				Column: "graduated_time",
-				Value:  now,
-			},
-			{
-				Column: "created_time",
-				Value:  now,
-			},
-			{
-				Column: "last_updated_time",
-				Value:  now,
-			},
-		},
+		TableRows: raw_sql_tpl.NewTableRows().Add("id", classID).
+			Add("name", className).
+			Add("student_num", studentNum).
+			Add("graduated_time", now).
+			Add("created_time", now).
+			Add("last_updated_time", now),
 	}.Map()
 	if err != nil {
 		t.Fatal(err)
 	}
 
 	deleteExecuteParams, err := raw_sql_tpl.DeleteExecuteParams{
-		TableName: tableName,
-		Conditions: []*raw_sql_tpl.Condition{
-			{
-				Column: "id",
-				Value:  classID,
-			},
-		},
+		TableName:  tableName,
+		Conditions: raw_sql_tpl.NewConditions().Equal("id", classID),
 	}.Map()
 	if err != nil {
 		t.Fatal(err)
@@ -212,22 +167,9 @@ func TestRawSqlTemplate(t *testing.T) {
 
 	updateExecuteParams, err := raw_sql_tpl.UpdateExecuteParams{
 		TableName: tableName,
-		TableRows: []*raw_sql_tpl.TableRow{
-			{
-				Column: "name",
-				Value:  newClassName,
-			},
-			{
-				Column: "student_num",
-				Value:  newStudentNum,
-			},
-		},
-		Conditions: []*raw_sql_tpl.Condition{
-			{
-				Column: "id",
-				Value:  classID,
-			},
-		},
+		TableRows: raw_sql_tpl.NewTableRows().Add("name", newClassName).
+			Add("student_num", newStudentNum),
+		Conditions: raw_sql_tpl.NewConditions().Equal("id", classID),
 	}.Map()
 	if err != nil {
 		t.Fatal(err)
@@ -236,22 +178,12 @@ func TestRawSqlTemplate(t *testing.T) {
 	queryExecuteParams, err := raw_sql_tpl.QueryExecuteParams{
 		TableName:     tableName,
 		SelectColumns: []string{"id", "name", "student_num as student_num_alias", "graduated_time", "created_time", "last_updated_time"},
-		Conditions: []*raw_sql_tpl.Condition{
-			{
-				Column: "id",
-				Value:  classID,
-			},
-			{
-				Column: "name",
-				Value:  className,
-			},
-			{
-				Column: "student_num",
-				Value:  studentNum,
-			},
-		},
-		Limit:  1,
-		Offset: 0,
+		Conditions: raw_sql_tpl.NewConditions().
+			Equal("id", classID).
+			Equal("name", className).
+			Equal("student_num", studentNum),
+		PageNo:   1,
+		PageSize: 1,
 	}.Map()
 	if err != nil {
 		t.Fatal(err)
@@ -260,64 +192,34 @@ func TestRawSqlTemplate(t *testing.T) {
 	newQueryExecuteParams, err := raw_sql_tpl.QueryExecuteParams{
 		TableName:     tableName,
 		SelectColumns: []string{"id", "name", "student_num as student_num_alias", "graduated_time", "created_time", "last_updated_time"},
-		Conditions: []*raw_sql_tpl.Condition{
-			{
-				Column: "id",
-				Value:  classID,
-			},
-			{
-				Column: "name",
-				Value:  newClassName,
-			},
-			{
-				Column: "student_num",
-				Value:  newStudentNum,
-			},
-		},
-		Limit:  0,
-		Offset: 0,
+		Conditions: raw_sql_tpl.NewConditions().
+			Equal("id", classID).
+			Equal("name", newClassName).
+			Equal("student_num", newStudentNum),
+		PageNo:   0,
+		PageSize: 0,
 	}.Map()
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	countExecuteParams, err := raw_sql_tpl.CountExecuteParams{
+	countExecuteParams, err := raw_sql_tpl.ConditionsExecuteParams{
 		TableName: tableName,
-		Conditions: []*raw_sql_tpl.Condition{
-			{
-				Column: "id",
-				Value:  classID,
-			},
-			{
-				Column: "name",
-				Value:  className,
-			},
-			{
-				Column: "student_num",
-				Value:  studentNum,
-			},
-		},
+		Conditions: raw_sql_tpl.NewConditions().
+			Equal("id", classID).
+			Equal("name", className).
+			Equal("student_num", studentNum),
 	}.Map()
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	newCountExecuteParams, err := raw_sql_tpl.CountExecuteParams{
+	newCountExecuteParams, err := raw_sql_tpl.ConditionsExecuteParams{
 		TableName: tableName,
-		Conditions: []*raw_sql_tpl.Condition{
-			{
-				Column: "id",
-				Value:  classID,
-			},
-			{
-				Column: "name",
-				Value:  newClassName,
-			},
-			{
-				Column: "student_num",
-				Value:  newStudentNum,
-			},
-		},
+		Conditions: raw_sql_tpl.NewConditions().
+			Equal("id", classID).
+			Equal("name", newClassName).
+			Equal("student_num", newStudentNum),
 	}.Map()
 	if err != nil {
 		t.Fatal(err)
@@ -431,34 +333,6 @@ func TestSqlMapping(t *testing.T) {
 		if sqlColumn.CanUpdateClear && sqlColumn.Name != "name" {
 			t.Fatal("可清除字段不正确")
 		}
-
-		if !sqlColumn.CanQuery && sqlColumn.Name != "name" {
-			t.Fatal("可清除字段不正确")
-		}
-
-		if sqlColumn.InsertCallback && sqlColumn.Name != "name" {
-			t.Fatal("插入回调不正确")
-		}
-
-		if sqlColumn.UpdateCallback && sqlColumn.Name != "name" {
-			t.Fatal("更新回调不正确")
-		}
-
-		if sqlColumn.QueryCallback && sqlColumn.Name != "student_num" {
-			t.Fatal("查询回调不正确")
-		}
-
-		if sqlColumn.CountCallback && sqlColumn.Name != "student_num" {
-			t.Fatal("计数回调不正确")
-		}
-
-		if sqlColumn.CheckExistCallback && sqlColumn.Name != "student_num" {
-			t.Fatal("检查存在性回调不正确")
-		}
-
-		if sqlColumn.CheckHasOnlyOneCallback && sqlColumn.Name != "student_num" {
-			t.Fatal("检查唯一性回调不正确")
-		}
 	}
 }
 
@@ -472,32 +346,12 @@ func TestSql(t *testing.T) {
 
 	insertExecuteParams, err := raw_sql_tpl.InsertExecuteParams{
 		TableName: tableName,
-		TableRows: []*raw_sql_tpl.TableRow{
-			{
-				Column: "id",
-				Value:  classID,
-			},
-			{
-				Column: "name",
-				Value:  className,
-			},
-			{
-				Column: "student_num",
-				Value:  studentNum,
-			},
-			{
-				Column: "graduated_time",
-				Value:  now,
-			},
-			{
-				Column: "created_time",
-				Value:  now,
-			},
-			{
-				Column: "last_updated_time",
-				Value:  now,
-			},
-		},
+		TableRows: raw_sql_tpl.NewTableRows().Add("id", classID).
+			Add("name", className).
+			Add("student_num", studentNum).
+			Add("graduated_time", now).
+			Add("created_time", now).
+			Add("last_updated_time", now),
 	}.Map()
 	if err != nil {
 		t.Fatal(err)
@@ -548,21 +402,17 @@ func TestSql(t *testing.T) {
 		}
 	}()
 
-	err = sdk.Insert(sdk.GetInstance(), tableName, class, func(e *Class, fieldName string, value any) (retValue any, err error) {
-		return value, nil
-	})
+	err = sdk.InsertEntity(sdk.GetInstance(), tableName, class)
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	err = sdk.Update(sdk.GetInstance(), tableName, newClass, func(e *Class, fieldName string, value any) (retValue any, err error) {
-		return value, nil
-	})
+	err = sdk.UpdateEntity(sdk.GetInstance(), tableName, newClass)
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	err = sdk.Delete(sdk.GetInstance(), tableName, class)
+	err = sdk.DeleteEntity(sdk.GetInstance(), tableName, class)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -578,21 +428,17 @@ func TestSql(t *testing.T) {
 	}
 
 	err = sdk.GetInstance().Transaction(func(tx *sdk.Transaction) error {
-		err = sdk.Insert(tx, tableName, class, func(e *Class, fieldName string, value any) (retValue any, err error) {
-			return value, nil
-		})
+		err = sdk.InsertEntity(tx, tableName, class)
 		if err != nil {
 			t.Fatal(err)
 		}
 
-		err = sdk.Update(tx, tableName, newClass, func(e *Class, fieldName string, value any) (retValue any, err error) {
-			return value, nil
-		})
+		err = sdk.UpdateEntity(tx, tableName, newClass)
 		if err != nil {
 			t.Fatal(err)
 		}
 
-		err = sdk.Delete(tx, tableName, class)
+		err = sdk.DeleteEntity(tx, tableName, class)
 		if err != nil {
 			t.Fatal(err)
 		}