sql_template.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package sql
  2. import (
  3. "github.com/pkg/errors"
  4. )
  5. const InsertTpl = `
  6. INSERT INTO
  7. {{ .table_name }} ({{ .columns | join "," }})
  8. VALUES
  9. {{- $valuesClauses := list }}
  10. {{- range .values_list }}
  11. {{- $valuesClause := printf "(%s)" ( . | join ", " ) }}
  12. {{- $valuesClauses = append $valuesClauses $valuesClause }}
  13. {{- end }}
  14. {{ $valuesClauses | join "," }}
  15. `
  16. // InsertExecuteParams 插入参数
  17. type InsertExecuteParams struct {
  18. TableName string
  19. *TableRow
  20. }
  21. func (params InsertExecuteParams) Map() (map[string]any, error) {
  22. if params.TableRow == nil {
  23. return nil, nil
  24. }
  25. if params.TableRow.err != nil {
  26. return nil, params.TableRow.err
  27. }
  28. if (params.TableRow.columns == nil || len(params.TableRow.columns) == 0) ||
  29. (params.TableRow.values == nil || len(params.TableRow.values) == 0) {
  30. return nil, errors.New("没有传递列和值")
  31. }
  32. values := make([]any, 0)
  33. for i := 0; i < len(params.TableRow.values); i++ {
  34. values = append(values, "?")
  35. }
  36. return map[string]any{
  37. "table_name": params.TableName,
  38. "columns": params.TableRow.columns,
  39. "values_list": []any{values},
  40. }, nil
  41. }
  42. // InsertBatchExecuteParams 批量插入参数
  43. type InsertBatchExecuteParams struct {
  44. TableName string
  45. TableRowBatch []TableRow
  46. }
  47. func (params InsertBatchExecuteParams) Map() (map[string]any, error) {
  48. if params.TableRowBatch == nil || len(params.TableRowBatch) == 0 {
  49. return nil, nil
  50. }
  51. values := make([]any, 0)
  52. for i := 0; i < len(params.TableRowBatch[0].values); i++ {
  53. values = append(values, "?")
  54. }
  55. valuesList := make([]any, 0)
  56. for _, tableRow := range params.TableRowBatch {
  57. if tableRow.err != nil {
  58. return nil, tableRow.err
  59. }
  60. if len(values) != len(tableRow.values) {
  61. return nil, errors.New("列数不匹配,保证每个TableRow的Add数量一致")
  62. }
  63. valuesList = append(valuesList, values)
  64. }
  65. return map[string]any{
  66. "table_name": params.TableName,
  67. "columns": params.TableRowBatch[0].columns,
  68. "values_list": valuesList,
  69. }, nil
  70. }
  71. const DeleteTpl = `
  72. DELETE FROM
  73. {{ .table_name }}
  74. WHERE
  75. {{ range .queries }}{{ . }} AND {{ end }}1 = 1
  76. `
  77. // DeleteExecuteParams 删除参数
  78. type DeleteExecuteParams struct {
  79. TableName string
  80. *Conditions
  81. }
  82. func (params DeleteExecuteParams) Map() (map[string]any, error) {
  83. if params.Conditions == nil {
  84. return nil, errors.New("没有传递删除条件")
  85. }
  86. if params.Conditions.err != nil {
  87. return nil, params.Conditions.err
  88. }
  89. return map[string]any{
  90. "table_name": params.TableName,
  91. "queries": params.queries,
  92. }, nil
  93. }
  94. const UpdateTpl = `
  95. UPDATE
  96. {{ .table_name }}
  97. SET
  98. {{ .set_list | join ", " }}
  99. WHERE
  100. {{ range .queries }}{{ . }} AND {{ end }}1 = 1
  101. `
  102. // UpdateExecuteParams 更新参数
  103. type UpdateExecuteParams struct {
  104. TableName string
  105. *TableRow
  106. *Conditions
  107. }
  108. func (params UpdateExecuteParams) Map() (map[string]any, error) {
  109. if params.TableRow == nil {
  110. return nil, nil
  111. }
  112. if params.TableRow.err != nil {
  113. return nil, params.TableRow.err
  114. }
  115. setList := make([]string, 0)
  116. for _, column := range params.TableRow.columns {
  117. setList = append(setList, column+" = ?")
  118. }
  119. return map[string]any{
  120. "table_name": params.TableName,
  121. "set_list": setList,
  122. "queries": params.Conditions.queries,
  123. }, nil
  124. }
  125. const QueryTpl = `
  126. SELECT
  127. {{ if .select_columns }}{{ .select_columns | join ", " }}{{ else }}*{{ end }}
  128. FROM
  129. {{ .table_name }}
  130. WHERE
  131. {{ range .queries -}}{{ . }} AND {{ end -}}1 = 1
  132. {{- if .order_by }}
  133. ORDER BY {{ .order_by }}
  134. {{- end }}
  135. {{- if not (eq .limit -1) }}
  136. LIMIT {{ .limit }}
  137. {{- end }}
  138. {{- if not (eq .offset -1) }}
  139. OFFSET {{ .offset }}
  140. {{- end }}
  141. `
  142. // QueryExecuteParams 查询参数
  143. type QueryExecuteParams struct {
  144. TableName string
  145. SelectClauses []string
  146. *Conditions
  147. OrderBy string
  148. PageNo int
  149. PageSize int
  150. }
  151. func (params QueryExecuteParams) Map() (map[string]any, error) {
  152. limit := -1
  153. offset := -1
  154. if params.PageNo != 0 && params.PageSize != 0 {
  155. limit = params.PageSize
  156. offset = (params.PageNo - 1) * params.PageSize
  157. }
  158. return map[string]any{
  159. "table_name": params.TableName,
  160. "select_columns": params.SelectClauses,
  161. "queries": params.Conditions.queries,
  162. "limit": limit,
  163. "offset": offset,
  164. "order_by": params.OrderBy,
  165. }, nil
  166. }
  167. // QueryOneExecuteParams 单查询参数
  168. type QueryOneExecuteParams struct {
  169. TableName string
  170. SelectClauses []string
  171. *Conditions
  172. }
  173. func (params QueryOneExecuteParams) Map() (map[string]any, error) {
  174. return map[string]any{
  175. "table_name": params.TableName,
  176. "select_columns": params.SelectClauses,
  177. "queries": params.Conditions.queries,
  178. "limit": -1,
  179. "offset": -1,
  180. }, nil
  181. }
  182. const CountTpl = `
  183. SELECT
  184. COUNT(*)
  185. FROM
  186. {{ .table_name }}
  187. WHERE
  188. {{ range .queries }}{{ . }} AND {{ end }}1 = 1
  189. `
  190. // CountExecuteParams 计数参数
  191. type CountExecuteParams struct {
  192. TableName string
  193. *Conditions
  194. }
  195. func (params CountExecuteParams) Map() (map[string]any, error) {
  196. return map[string]any{
  197. "table_name": params.TableName,
  198. "queries": params.Conditions.queries,
  199. }, nil
  200. }
  201. // CheckExistExecuteParams 存在性校验参数
  202. type CheckExistExecuteParams struct {
  203. TableName string
  204. *Conditions
  205. }
  206. func (params CheckExistExecuteParams) Map() (map[string]any, error) {
  207. return map[string]any{
  208. "table_name": params.TableName,
  209. "queries": params.Conditions.queries,
  210. }, nil
  211. }
  212. // CheckHasOnlyOneExecuteParams 唯一性校验参数
  213. type CheckHasOnlyOneExecuteParams struct {
  214. TableName string
  215. *Conditions
  216. }
  217. func (params CheckHasOnlyOneExecuteParams) Map() (map[string]any, error) {
  218. return map[string]any{
  219. "table_name": params.TableName,
  220. "queries": params.Conditions.queries,
  221. }, nil
  222. }