| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- 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) {
- 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("没有传递删除条件")
- }
- 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) {
- 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 {
- 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 {
- 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 {
- 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 {
- 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 {
- 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 {
- conditions = params.Conditions.Conditions
- }
- return map[string]any{
- "table_name": params.TableName,
- "conditions": conditions,
- }, nil
- }
|