raw_sql_tpl.go 4.1 KB

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