sql_tpl.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package sql_tpl
  2. import "errors"
  3. const InsertTpl = `
  4. INSERT INTO
  5. {{ .table_name }} ({{ .columns | join "," }})
  6. VALUES
  7. {{- $valuesClauses := list }}
  8. {{- range .values_list }}
  9. {{- $valuesClause := printf "(%s)" ( . | join "," ) }}
  10. {{- $valuesClauses = append $valuesClauses $valuesClause }}
  11. {{- end }}
  12. {{ $valuesClauses | join "," }}
  13. `
  14. type InsertExecuteParams struct {
  15. TableName string
  16. *TableRow
  17. }
  18. func (params InsertExecuteParams) Map() (map[string]any, error) {
  19. if params.TableRow == nil {
  20. return nil, nil
  21. }
  22. if params.TableRow.err != nil {
  23. return nil, params.TableRow.err
  24. }
  25. columns := make([]string, 0)
  26. values := make([]any, 0)
  27. for _, cv := range params.TableRow.columnValues {
  28. columns = append(columns, cv.column)
  29. values = append(values, cv.value)
  30. }
  31. return map[string]any{
  32. "table_name": params.TableName,
  33. "columns": columns,
  34. "values_list": []any{values},
  35. }, nil
  36. }
  37. type InsertBatchExecuteParams struct {
  38. TableName string
  39. TableRowBatch []TableRow
  40. }
  41. func (params InsertBatchExecuteParams) Map() (map[string]any, error) {
  42. if params.TableRowBatch == nil || len(params.TableRowBatch) == 0 {
  43. return nil, nil
  44. }
  45. columns := make([]string, 0)
  46. for _, cv := range params.TableRowBatch[0].columnValues {
  47. columns = append(columns, cv.column)
  48. }
  49. valuesList := make([]any, 0)
  50. for _, tableRow := range params.TableRowBatch {
  51. if tableRow.err != nil {
  52. return nil, tableRow.err
  53. }
  54. if len(columns) != len(tableRow.columnValues) {
  55. return nil, errors.New("列数不匹配,保证每个TableRow的Add数量一致")
  56. }
  57. columnAndValueMap := make(map[string]any, 0)
  58. for _, cv := range tableRow.columnValues {
  59. columnAndValueMap[cv.column] = cv.value
  60. }
  61. values := make([]any, len(columnAndValueMap))
  62. for _, column := range columns {
  63. values = append(values, columnAndValueMap[column])
  64. }
  65. valuesList = append(valuesList, values)
  66. }
  67. return map[string]any{
  68. "table_name": params.TableName,
  69. "columns": columns,
  70. "values_list": valuesList,
  71. }, nil
  72. }
  73. const DeleteTpl = `
  74. DELETE FROM
  75. {{ .table_name }}
  76. WHERE
  77. {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
  78. `
  79. type DeleteExecuteParams struct {
  80. TableName string
  81. *Conditions
  82. }
  83. func (params DeleteExecuteParams) Map() (map[string]any, error) {
  84. if params.Conditions == nil {
  85. return nil, errors.New("没有传递删除条件")
  86. }
  87. if params.Conditions.err != nil {
  88. return nil, params.Conditions.err
  89. }
  90. return map[string]any{
  91. "table_name": params.TableName,
  92. "conditions": params.Conditions.Conditions,
  93. }, nil
  94. }
  95. const UpdateTpl = `
  96. UPDATE
  97. {{ .table_name }}
  98. SET
  99. {{ .set_list | join "," }}
  100. WHERE
  101. {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
  102. `
  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 _, cv := range params.TableRow.columnValues {
  117. setList = append(setList, cv.column+" = "+cv.value)
  118. }
  119. conditions := make([]string, 0)
  120. if params.Conditions != nil {
  121. if params.Conditions.err != nil {
  122. return nil, params.Conditions.err
  123. }
  124. conditions = params.Conditions.Conditions
  125. }
  126. return map[string]any{
  127. "table_name": params.TableName,
  128. "set_list": setList,
  129. "conditions": conditions,
  130. }, nil
  131. }
  132. const QueryTpl = `
  133. SELECT
  134. {{ if .select_columns }}{{ .select_columns | join "," }}{{ else }}*{{ end }}
  135. FROM
  136. {{ .table_name }}
  137. WHERE
  138. {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
  139. {{ if .limit }}LIMIT {{ .limit }}{{ end }}
  140. {{ if .offset }}OFFSET {{ .offset }}{{ end }}
  141. `
  142. type QueryExecuteParams struct {
  143. TableName string
  144. SelectColumns []string
  145. *Conditions
  146. PageNo int
  147. PageSize int
  148. }
  149. func (params QueryExecuteParams) Map() (map[string]any, error) {
  150. var limit int
  151. var offset int
  152. if params.PageNo != 0 && params.PageSize != 0 {
  153. limit = params.PageSize
  154. offset = (params.PageNo - 1) * params.PageSize
  155. }
  156. conditions := make([]string, 0)
  157. if params.Conditions != nil {
  158. if params.Conditions.err != nil {
  159. return nil, params.Conditions.err
  160. }
  161. conditions = params.Conditions.Conditions
  162. }
  163. return map[string]any{
  164. "table_name": params.TableName,
  165. "select_columns": params.SelectColumns,
  166. "conditions": conditions,
  167. "limit": limit,
  168. "offset": offset,
  169. }, nil
  170. }
  171. type QueryOneExecuteParams struct {
  172. TableName string
  173. SelectColumns []string
  174. *Conditions
  175. }
  176. func (params QueryOneExecuteParams) Map() (map[string]any, error) {
  177. conditions := make([]string, 0)
  178. if params.Conditions != nil {
  179. if params.Conditions.err != nil {
  180. return nil, params.Conditions.err
  181. }
  182. conditions = params.Conditions.Conditions
  183. }
  184. return map[string]any{
  185. "table_name": params.TableName,
  186. "select_columns": params.SelectColumns,
  187. "conditions": conditions,
  188. }, nil
  189. }
  190. const CountTpl = `
  191. SELECT
  192. COUNT(*)
  193. FROM
  194. {{ .table_name }}
  195. WHERE
  196. {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
  197. `
  198. type CountExecuteParams struct {
  199. TableName string
  200. *Conditions
  201. }
  202. func (params CountExecuteParams) Map() (map[string]any, error) {
  203. conditions := make([]string, 0)
  204. if params.Conditions != nil {
  205. if params.Conditions.err != nil {
  206. return nil, params.Conditions.err
  207. }
  208. conditions = params.Conditions.Conditions
  209. }
  210. return map[string]any{
  211. "table_name": params.TableName,
  212. "conditions": conditions,
  213. }, nil
  214. }
  215. type CheckExistExecuteParams struct {
  216. TableName string
  217. *Conditions
  218. }
  219. func (params CheckExistExecuteParams) Map() (map[string]any, error) {
  220. conditions := make([]string, 0)
  221. if params.Conditions != nil {
  222. if params.Conditions.err != nil {
  223. return nil, params.Conditions.err
  224. }
  225. conditions = params.Conditions.Conditions
  226. }
  227. return map[string]any{
  228. "table_name": params.TableName,
  229. "conditions": conditions,
  230. }, nil
  231. }
  232. type CheckHasOnlyOneExecuteParams struct {
  233. TableName string
  234. *Conditions
  235. }
  236. func (params CheckHasOnlyOneExecuteParams) Map() (map[string]any, error) {
  237. conditions := make([]string, 0)
  238. if params.Conditions != nil {
  239. if params.Conditions.err != nil {
  240. return nil, params.Conditions.err
  241. }
  242. conditions = params.Conditions.Conditions
  243. }
  244. return map[string]any{
  245. "table_name": params.TableName,
  246. "conditions": conditions,
  247. }, nil
  248. }