sql_tpl.go 6.3 KB

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