123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- package sql_tpl
- import "errors"
- const InsertTpl = `
- INSERT INTO
- {{ .table_name }} ({{ .columns | join "," }})
- VALUES
- {{- $valuesClauses := list }}
- {{- range .values_list }}
- {{- $valuesClause := printf "(%s)" ( . | join "," ) }}
- {{- $valuesClauses = append $valuesClauses $valuesClause }}
- {{- end }}
- {{ $valuesClauses | join "," }}
- `
- type InsertExecuteParams struct {
- TableName string
- *TableRow
- }
- func (params InsertExecuteParams) Map() (map[string]any, error) {
- if params.TableRow == nil {
- return nil, nil
- }
- if params.TableRow.err != nil {
- return nil, params.TableRow.err
- }
- columns := make([]string, 0)
- values := make([]any, 0)
- for _, cv := range params.TableRow.columnValues {
- columns = append(columns, cv.column)
- values = append(values, cv.value)
- }
- return map[string]any{
- "table_name": params.TableName,
- "columns": columns,
- "values_list": []any{values},
- }, nil
- }
- type InsertBatchExecuteParams struct {
- TableName string
- TableRowBatch []TableRow
- }
- func (params InsertBatchExecuteParams) Map() (map[string]any, error) {
- if params.TableRowBatch == nil || len(params.TableRowBatch) == 0 {
- return nil, nil
- }
- columns := make([]string, 0)
- for _, cv := range params.TableRowBatch[0].columnValues {
- columns = append(columns, cv.column)
- }
- valuesList := make([]any, 0)
- for _, tableRow := range params.TableRowBatch {
- if tableRow.err != nil {
- return nil, tableRow.err
- }
- if len(columns) != len(tableRow.columnValues) {
- return nil, errors.New("列数不匹配,保证每个TableRow的Add数量一致")
- }
- columnAndValueMap := make(map[string]any, 0)
- for _, cv := range tableRow.columnValues {
- columnAndValueMap[cv.column] = cv.value
- }
- values := make([]any, len(columnAndValueMap))
- for _, column := range columns {
- values = append(values, columnAndValueMap[column])
- }
- valuesList = append(valuesList, values)
- }
- return map[string]any{
- "table_name": params.TableName,
- "columns": columns,
- "values_list": valuesList,
- }, nil
- }
- const DeleteTpl = `
- DELETE FROM
- {{ .table_name }}
- WHERE
- {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
- `
- type DeleteExecuteParams struct {
- TableName string
- *Conditions
- }
- func (params DeleteExecuteParams) Map() (map[string]any, error) {
- if params.Conditions == nil {
- return nil, errors.New("没有传递删除条件")
- }
- if params.Conditions.err != nil {
- return nil, params.Conditions.err
- }
- return map[string]any{
- "table_name": params.TableName,
- "conditions": params.Conditions.Conditions,
- }, nil
- }
- const UpdateTpl = `
- UPDATE
- {{ .table_name }}
- SET
- {{ .set_list | join "," }}
- WHERE
- {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
- `
- type UpdateExecuteParams struct {
- TableName string
- *TableRow
- *Conditions
- }
- func (params UpdateExecuteParams) Map() (map[string]any, error) {
- if params.TableRow == nil {
- return nil, nil
- }
- if params.TableRow.err != nil {
- return nil, params.TableRow.err
- }
- setList := make([]string, 0)
- for _, cv := range params.TableRow.columnValues {
- setList = append(setList, cv.column+" = "+cv.value)
- }
- conditions := make([]string, 0)
- if params.Conditions != nil {
- if params.Conditions.err != nil {
- return nil, params.Conditions.err
- }
- conditions = params.Conditions.Conditions
- }
- return map[string]any{
- "table_name": params.TableName,
- "set_list": setList,
- "conditions": conditions,
- }, nil
- }
- const QueryTpl = `
- SELECT
- {{ if .select_columns }}{{ .select_columns | join "," }}{{ else }}*{{ end }}
- FROM
- {{ .table_name }}
- WHERE
- {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
- {{ if .limit }}LIMIT {{ .limit }}{{ end }}
- {{ if .offset }}OFFSET {{ .offset }}{{ end }}
- `
- type QueryExecuteParams struct {
- TableName string
- SelectColumns []string
- *Conditions
- PageNo int
- PageSize int
- }
- func (params QueryExecuteParams) Map() (map[string]any, error) {
- var limit int
- var offset int
- if params.PageNo != 0 && params.PageSize != 0 {
- limit = params.PageSize
- offset = (params.PageNo - 1) * params.PageSize
- }
- conditions := make([]string, 0)
- if params.Conditions != nil {
- if params.Conditions.err != nil {
- return nil, params.Conditions.err
- }
- conditions = params.Conditions.Conditions
- }
- return map[string]any{
- "table_name": params.TableName,
- "select_columns": params.SelectColumns,
- "conditions": conditions,
- "limit": limit,
- "offset": offset,
- }, nil
- }
- type QueryOneExecuteParams struct {
- TableName string
- SelectColumns []string
- *Conditions
- }
- func (params QueryOneExecuteParams) Map() (map[string]any, error) {
- conditions := make([]string, 0)
- if params.Conditions != nil {
- if params.Conditions.err != nil {
- return nil, params.Conditions.err
- }
- conditions = params.Conditions.Conditions
- }
- return map[string]any{
- "table_name": params.TableName,
- "select_columns": params.SelectColumns,
- "conditions": conditions,
- }, nil
- }
- const CountTpl = `
- SELECT
- COUNT(*)
- FROM
- {{ .table_name }}
- WHERE
- {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
- `
- type CountExecuteParams struct {
- TableName string
- *Conditions
- }
- func (params CountExecuteParams) Map() (map[string]any, error) {
- conditions := make([]string, 0)
- if params.Conditions != nil {
- if params.Conditions.err != nil {
- return nil, params.Conditions.err
- }
- conditions = params.Conditions.Conditions
- }
- return map[string]any{
- "table_name": params.TableName,
- "conditions": conditions,
- }, nil
- }
- type CheckExistExecuteParams struct {
- TableName string
- *Conditions
- }
- func (params CheckExistExecuteParams) Map() (map[string]any, error) {
- conditions := make([]string, 0)
- if params.Conditions != nil {
- if params.Conditions.err != nil {
- return nil, params.Conditions.err
- }
- conditions = params.Conditions.Conditions
- }
- return map[string]any{
- "table_name": params.TableName,
- "conditions": conditions,
- }, nil
- }
- type CheckHasOnlyOneExecuteParams struct {
- TableName string
- *Conditions
- }
- func (params CheckHasOnlyOneExecuteParams) Map() (map[string]any, error) {
- conditions := make([]string, 0)
- if params.Conditions != nil {
- if params.Conditions.err != nil {
- return nil, params.Conditions.err
- }
- conditions = params.Conditions.Conditions
- }
- return map[string]any{
- "table_name": params.TableName,
- "conditions": conditions,
- }, nil
- }
|