sql_tpl.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package sql_tpl
  2. import "errors"
  3. const InsertTpl = `
  4. INSERT INTO
  5. {{ .table_name }} ({{ .columns | join "," }})
  6. VALUES
  7. ({{ .values | join "," }})
  8. `
  9. type InsertExecuteParams struct {
  10. TableName string
  11. *TableRows
  12. }
  13. func (params InsertExecuteParams) Map() (map[string]any, error) {
  14. if params.TableRows == nil {
  15. return nil, nil
  16. }
  17. if params.TableRows.err != nil {
  18. return nil, params.TableRows.err
  19. }
  20. columns := make([]string, 0)
  21. values := make([]any, 0)
  22. for _, row := range params.TableRows.Rows {
  23. columns = append(columns, row.Column)
  24. values = append(values, row.Value)
  25. }
  26. return map[string]any{
  27. "table_name": params.TableName,
  28. "columns": columns,
  29. "values": values,
  30. }, nil
  31. }
  32. const DeleteTpl = `
  33. DELETE FROM
  34. {{ .table_name }}
  35. WHERE
  36. {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
  37. `
  38. type DeleteExecuteParams struct {
  39. TableName string
  40. *Conditions
  41. }
  42. func (params DeleteExecuteParams) Map() (map[string]any, error) {
  43. if params.Conditions == nil {
  44. return nil, errors.New("没有传递删除条件")
  45. }
  46. if params.Conditions.err != nil {
  47. return nil, params.Conditions.err
  48. }
  49. return map[string]any{
  50. "table_name": params.TableName,
  51. "conditions": params.Conditions.Conditions,
  52. }, nil
  53. }
  54. const UpdateTpl = `
  55. UPDATE
  56. {{ .table_name }}
  57. SET
  58. {{ .set_list | join "," }}
  59. WHERE
  60. {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
  61. `
  62. type UpdateExecuteParams struct {
  63. TableName string
  64. *TableRows
  65. *Conditions
  66. }
  67. func (params UpdateExecuteParams) Map() (map[string]any, error) {
  68. if params.TableRows == nil {
  69. return nil, nil
  70. }
  71. if params.TableRows.err != nil {
  72. return nil, params.TableRows.err
  73. }
  74. setList := make([]string, 0)
  75. for _, row := range params.TableRows.Rows {
  76. setList = append(setList, row.Column+" = "+row.Value)
  77. }
  78. conditions := make([]string, 0)
  79. if params.Conditions != nil {
  80. if params.Conditions.err != nil {
  81. return nil, params.Conditions.err
  82. }
  83. conditions = params.Conditions.Conditions
  84. }
  85. return map[string]any{
  86. "table_name": params.TableName,
  87. "set_list": setList,
  88. "conditions": conditions,
  89. }, nil
  90. }
  91. const QueryTpl = `
  92. SELECT
  93. {{ if .select_columns }}{{ .select_columns | join "," }}{{ else }}*{{ end }}
  94. FROM
  95. {{ .table_name }}
  96. WHERE
  97. {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
  98. {{ if .limit }}LIMIT {{ .limit }}{{ end }}
  99. {{ if .offset }}OFFSET {{ .offset }}{{ end }}
  100. `
  101. type QueryExecuteParams struct {
  102. TableName string
  103. SelectColumns []string
  104. *Conditions
  105. PageNo int
  106. PageSize int
  107. }
  108. func (params QueryExecuteParams) Map() (map[string]any, error) {
  109. var limit int
  110. var offset int
  111. if params.PageNo != 0 && params.PageSize != 0 {
  112. limit = params.PageSize
  113. offset = (params.PageNo - 1) * params.PageSize
  114. }
  115. conditions := make([]string, 0)
  116. if params.Conditions != nil {
  117. if params.Conditions.err != nil {
  118. return nil, params.Conditions.err
  119. }
  120. conditions = params.Conditions.Conditions
  121. }
  122. return map[string]any{
  123. "table_name": params.TableName,
  124. "select_columns": params.SelectColumns,
  125. "conditions": conditions,
  126. "limit": limit,
  127. "offset": offset,
  128. }, nil
  129. }
  130. type QueryOneExecuteParams struct {
  131. TableName string
  132. SelectColumns []string
  133. *Conditions
  134. }
  135. func (params QueryOneExecuteParams) Map() (map[string]any, error) {
  136. conditions := make([]string, 0)
  137. if params.Conditions != nil {
  138. if params.Conditions.err != nil {
  139. return nil, params.Conditions.err
  140. }
  141. conditions = params.Conditions.Conditions
  142. }
  143. return map[string]any{
  144. "table_name": params.TableName,
  145. "select_columns": params.SelectColumns,
  146. "conditions": conditions,
  147. }, nil
  148. }
  149. const CountTpl = `
  150. SELECT
  151. COUNT(*)
  152. FROM
  153. {{ .table_name }}
  154. WHERE
  155. {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
  156. `
  157. type CountExecuteParams struct {
  158. TableName string
  159. *Conditions
  160. }
  161. func (params CountExecuteParams) Map() (map[string]any, error) {
  162. conditions := make([]string, 0)
  163. if params.Conditions != nil {
  164. if params.Conditions.err != nil {
  165. return nil, params.Conditions.err
  166. }
  167. conditions = params.Conditions.Conditions
  168. }
  169. return map[string]any{
  170. "table_name": params.TableName,
  171. "conditions": conditions,
  172. }, nil
  173. }
  174. type CheckExistExecuteParams struct {
  175. TableName string
  176. *Conditions
  177. }
  178. func (params CheckExistExecuteParams) 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. "conditions": conditions,
  189. }, nil
  190. }
  191. type CheckHasOnlyOneExecuteParams struct {
  192. TableName string
  193. *Conditions
  194. }
  195. func (params CheckHasOnlyOneExecuteParams) Map() (map[string]any, error) {
  196. conditions := make([]string, 0)
  197. if params.Conditions != nil {
  198. if params.Conditions.err != nil {
  199. return nil, params.Conditions.err
  200. }
  201. conditions = params.Conditions.Conditions
  202. }
  203. return map[string]any{
  204. "table_name": params.TableName,
  205. "conditions": conditions,
  206. }, nil
  207. }