raw_sql_tpl.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package raw_sql_tpl
  2. import (
  3. "errors"
  4. "git.sxidc.com/go-tools/utils/strutils"
  5. "github.com/fatih/structs"
  6. )
  7. type TableRow struct {
  8. Column string `structs:"column"`
  9. Value string `structs:"value"`
  10. }
  11. func (tableRow TableRow) check() error {
  12. if strutils.IsStringEmpty(tableRow.Column) {
  13. return errors.New("没有传递列名")
  14. }
  15. if strutils.IsStringEmpty(tableRow.Value) {
  16. return errors.New("没有传递列值")
  17. }
  18. return nil
  19. }
  20. type Condition struct {
  21. Column string `structs:"column"`
  22. Operator string `structs:"operator"`
  23. Value string `structs:"value"`
  24. }
  25. func (cond Condition) check() error {
  26. if strutils.IsStringEmpty(cond.Column) {
  27. return errors.New("没有传递列名")
  28. }
  29. if strutils.IsStringEmpty(cond.Value) {
  30. return errors.New("没有传递列值")
  31. }
  32. return nil
  33. }
  34. const InsertTpl = `
  35. INSERT INTO
  36. {{ .table_name }} ({{ .columns | join "," }})
  37. VALUES
  38. ({{ .values | join "," }})
  39. `
  40. type InsertExecuteParams struct {
  41. TableName string
  42. TableRows []TableRow
  43. }
  44. func (params InsertExecuteParams) check() error {
  45. if strutils.IsStringEmpty(params.TableName) {
  46. return errors.New("没有传递表名")
  47. }
  48. if params.TableRows == nil || len(params.TableRows) == 0 {
  49. return errors.New("没有传递行数据")
  50. }
  51. for _, tableRow := range params.TableRows {
  52. if err := tableRow.check(); err != nil {
  53. return err
  54. }
  55. }
  56. return nil
  57. }
  58. func (params InsertExecuteParams) Map() (map[string]any, error) {
  59. if err := params.check(); err != nil {
  60. return nil, err
  61. }
  62. columns := make([]string, 0)
  63. values := make([]string, 0)
  64. for _, tableRow := range params.TableRows {
  65. columns = append(columns, tableRow.Column)
  66. values = append(values, tableRow.Value)
  67. }
  68. return map[string]any{
  69. "table_name": params.TableName,
  70. "columns": columns,
  71. "values": values,
  72. }, nil
  73. }
  74. const DeleteTpl = `
  75. DELETE FROM
  76. {{ .table_name }}
  77. WHERE
  78. {{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1
  79. `
  80. type DeleteExecuteParams struct {
  81. TableName string `structs:"table_name"`
  82. Conditions []Condition `structs:"conditions"`
  83. }
  84. func (params DeleteExecuteParams) check() error {
  85. if strutils.IsStringEmpty(params.TableName) {
  86. return errors.New("没有传递表名")
  87. }
  88. if params.Conditions == nil || len(params.Conditions) == 0 {
  89. return errors.New("没有传递条件")
  90. }
  91. for _, condition := range params.Conditions {
  92. if err := condition.check(); err != nil {
  93. return err
  94. }
  95. }
  96. return nil
  97. }
  98. func (params DeleteExecuteParams) Map() (map[string]any, error) {
  99. if err := params.check(); err != nil {
  100. return nil, err
  101. }
  102. return structs.Map(params), nil
  103. }
  104. const UpdateTpl = `
  105. {{- $setList := list -}}
  106. {{- range .table_rows -}}
  107. {{- $set := printf "%s = %s" .column .value -}}
  108. {{- $setList = append $setList $set -}}
  109. {{- end }}
  110. UPDATE
  111. {{ .table_name }}
  112. SET
  113. {{ $setList | join "," }}
  114. WHERE
  115. {{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1
  116. `
  117. type UpdateExecuteParams struct {
  118. TableName string `structs:"table_name"`
  119. TableRows []TableRow `structs:"table_rows"`
  120. Conditions []Condition `structs:"conditions"`
  121. }
  122. func (params UpdateExecuteParams) check() error {
  123. if strutils.IsStringEmpty(params.TableName) {
  124. return errors.New("没有传递表名")
  125. }
  126. if params.TableRows == nil || len(params.TableRows) == 0 {
  127. return errors.New("没有传递表行")
  128. }
  129. for _, tableRow := range params.TableRows {
  130. if err := tableRow.check(); err != nil {
  131. return err
  132. }
  133. }
  134. if params.Conditions == nil || len(params.Conditions) == 0 {
  135. return errors.New("没有传递条件")
  136. }
  137. for _, condition := range params.Conditions {
  138. if err := condition.check(); err != nil {
  139. return err
  140. }
  141. }
  142. return nil
  143. }
  144. func (params UpdateExecuteParams) Map() (map[string]any, error) {
  145. if err := params.check(); err != nil {
  146. return nil, err
  147. }
  148. return structs.Map(params), nil
  149. }
  150. const QueryTpl = `
  151. SELECT
  152. {{ if .select_columns }}{{ .select_columns | join "," }}{{ else }}*{{ end }}
  153. FROM
  154. {{ .table_name }}
  155. WHERE
  156. {{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1
  157. {{ if .limit }}LIMIT {{ .limit }}{{ end }}
  158. {{ if .offset }}OFFSET {{ .offset }}{{ end }}
  159. `
  160. type QueryExecuteParams struct {
  161. TableName string `structs:"table_name"`
  162. SelectColumns []string `structs:"select_columns"`
  163. Conditions []Condition `structs:"conditions"`
  164. Limit int `structs:"limit"`
  165. Offset int `structs:"offset"`
  166. }
  167. func (params QueryExecuteParams) check() error {
  168. if strutils.IsStringEmpty(params.TableName) {
  169. return errors.New("没有传递表名")
  170. }
  171. return nil
  172. }
  173. func (params QueryExecuteParams) Map() (map[string]any, error) {
  174. if err := params.check(); err != nil {
  175. return nil, err
  176. }
  177. return structs.Map(params), nil
  178. }
  179. const CountTpl = `
  180. SELECT
  181. COUNT(*)
  182. FROM
  183. {{ .table_name }}
  184. WHERE
  185. {{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1`
  186. type CountExecuteParams struct {
  187. TableName string `structs:"table_name"`
  188. Conditions []Condition `structs:"conditions"`
  189. }
  190. func (params CountExecuteParams) check() error {
  191. if strutils.IsStringEmpty(params.TableName) {
  192. return errors.New("没有传递表名")
  193. }
  194. return nil
  195. }
  196. func (params CountExecuteParams) Map() (map[string]any, error) {
  197. if err := params.check(); err != nil {
  198. return nil, err
  199. }
  200. return structs.Map(params), nil
  201. }