sql_tpl.go 4.2 KB

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