| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- 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
- }
- const InsertTpl = `
- INSERT INTO
- {{ .table_name }} ({{ .columns | join "," }})
- VALUES
- ({{ .values | join "," }})
- `
- 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
- }
- 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)
- }
- return map[string]any{
- "table_name": params.TableName,
- "columns": columns,
- "values": values,
- }, nil
- }
- const DeleteTpl = `
- DELETE FROM
- {{ .table_name }}
- WHERE
- {{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} 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
- }
- func (params DeleteExecuteParams) Map() (map[string]any, error) {
- if err := params.check(); err != nil {
- return nil, err
- }
- return structs.Map(params), 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 "," }}
- WHERE
- {{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1
- `
- type UpdateExecuteParams struct {
- TableName string `structs:"table_name"`
- TableRows []*TableRow `structs:"table_rows"`
- Conditions []*Condition `structs:"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
- }
- }
- return nil
- }
- func (params UpdateExecuteParams) Map() (map[string]any, error) {
- if err := params.check(); err != nil {
- return nil, err
- }
- return structs.Map(params), nil
- }
- const QueryTpl = `
- SELECT
- {{ if .select_columns }}{{ .select_columns | join "," }}{{ else }}*{{ end }}
- FROM
- {{ .table_name }}
- WHERE
- {{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} 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"`
- }
- func (params QueryExecuteParams) 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 QueryExecuteParams) Map() (map[string]any, error) {
- if err := params.check(); err != nil {
- return nil, err
- }
- return structs.Map(params), nil
- }
- const CountTpl = `
- SELECT
- COUNT(*)
- 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
- }
- return structs.Map(params), nil
- }
- 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("不支持的类型")
- }
- }
|