package sql_tpl import "errors" const InsertTpl = ` INSERT INTO {{ .table_name }} ({{ .columns | join "," }}) VALUES ({{ .values | join "," }}) ` type InsertExecuteParams struct { TableName string *TableRows } func (params InsertExecuteParams) Map() (map[string]any, error) { if params.TableRows == nil { return nil, nil } if params.TableRows.err != nil { return nil, params.TableRows.err } columns := make([]string, 0) values := make([]any, 0) for _, row := range params.TableRows.Rows { columns = append(columns, row.Column) values = append(values, row.Value) } return map[string]any{ "table_name": params.TableName, "columns": columns, "values": values, }, 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 *TableRows *Conditions } func (params UpdateExecuteParams) Map() (map[string]any, error) { if params.TableRows == nil { return nil, nil } if params.TableRows.err != nil { return nil, params.TableRows.err } setList := make([]string, 0) for _, row := range params.TableRows.Rows { setList = append(setList, row.Column+" = "+row.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 }