raw_sql_tpl.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package raw_sql_tpl
  2. import (
  3. "errors"
  4. "git.sxidc.com/go-tools/utils/strutils"
  5. "github.com/fatih/structs"
  6. "reflect"
  7. "strconv"
  8. "time"
  9. )
  10. type TableRow struct {
  11. Column string `structs:"column"`
  12. Value any `structs:"-"`
  13. ParsedValue string `structs:"value"`
  14. }
  15. func (tableRow *TableRow) parse() error {
  16. if strutils.IsStringEmpty(tableRow.Column) {
  17. return errors.New("没有传递列名")
  18. }
  19. if tableRow.Value == nil {
  20. return errors.New("没有传递列值")
  21. }
  22. parsedValue, err := parseValue(tableRow.Value)
  23. if err != nil {
  24. return err
  25. }
  26. tableRow.ParsedValue = parsedValue
  27. return nil
  28. }
  29. type Condition struct {
  30. Column string `structs:"column"`
  31. Operator string `structs:"operator"`
  32. Value any `structs:"-"`
  33. ParsedValue string `structs:"value"`
  34. }
  35. func (cond *Condition) parse() error {
  36. if strutils.IsStringEmpty(cond.Column) {
  37. return errors.New("没有传递列名")
  38. }
  39. if cond.Value == nil {
  40. return errors.New("没有传递条件值")
  41. }
  42. parsedValue, err := parseValue(cond.Value)
  43. if err != nil {
  44. return err
  45. }
  46. cond.ParsedValue = parsedValue
  47. return nil
  48. }
  49. const InsertTpl = `
  50. INSERT INTO
  51. {{ .table_name }} ({{ .columns | join "," }})
  52. VALUES
  53. ({{ .values | join "," }})
  54. `
  55. type InsertExecuteParams struct {
  56. TableName string
  57. TableRows []*TableRow
  58. }
  59. func (params InsertExecuteParams) check() error {
  60. if strutils.IsStringEmpty(params.TableName) {
  61. return errors.New("没有传递表名")
  62. }
  63. if params.TableRows == nil || len(params.TableRows) == 0 {
  64. return errors.New("没有传递行数据")
  65. }
  66. for _, tableRow := range params.TableRows {
  67. if err := tableRow.parse(); err != nil {
  68. return err
  69. }
  70. }
  71. return nil
  72. }
  73. func (params InsertExecuteParams) Map() (map[string]any, error) {
  74. if err := params.check(); err != nil {
  75. return nil, err
  76. }
  77. columns := make([]string, 0)
  78. values := make([]any, 0)
  79. for _, tableRow := range params.TableRows {
  80. columns = append(columns, tableRow.Column)
  81. values = append(values, tableRow.ParsedValue)
  82. }
  83. return map[string]any{
  84. "table_name": params.TableName,
  85. "columns": columns,
  86. "values": values,
  87. }, nil
  88. }
  89. const DeleteTpl = `
  90. DELETE FROM
  91. {{ .table_name }}
  92. WHERE
  93. {{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1
  94. `
  95. type DeleteExecuteParams struct {
  96. TableName string `structs:"table_name"`
  97. Conditions []*Condition `structs:"conditions"`
  98. }
  99. func (params DeleteExecuteParams) check() error {
  100. if strutils.IsStringEmpty(params.TableName) {
  101. return errors.New("没有传递表名")
  102. }
  103. if params.Conditions == nil || len(params.Conditions) == 0 {
  104. return errors.New("没有传递条件")
  105. }
  106. for _, condition := range params.Conditions {
  107. if err := condition.parse(); err != nil {
  108. return err
  109. }
  110. }
  111. return nil
  112. }
  113. func (params DeleteExecuteParams) Map() (map[string]any, error) {
  114. if err := params.check(); err != nil {
  115. return nil, err
  116. }
  117. return structs.Map(params), nil
  118. }
  119. const UpdateTpl = `
  120. {{- $setList := list -}}
  121. {{- range .table_rows -}}
  122. {{- $set := printf "%s = %s" .column .value -}}
  123. {{- $setList = append $setList $set -}}
  124. {{- end }}
  125. UPDATE
  126. {{ .table_name }}
  127. SET
  128. {{ $setList | join "," }}
  129. WHERE
  130. {{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1
  131. `
  132. type UpdateExecuteParams struct {
  133. TableName string `structs:"table_name"`
  134. TableRows []*TableRow `structs:"table_rows"`
  135. Conditions []*Condition `structs:"conditions"`
  136. }
  137. func (params UpdateExecuteParams) check() error {
  138. if strutils.IsStringEmpty(params.TableName) {
  139. return errors.New("没有传递表名")
  140. }
  141. if params.TableRows == nil || len(params.TableRows) == 0 {
  142. return errors.New("没有传递表行")
  143. }
  144. for _, tableRow := range params.TableRows {
  145. if err := tableRow.parse(); err != nil {
  146. return err
  147. }
  148. }
  149. if params.Conditions == nil || len(params.Conditions) == 0 {
  150. return errors.New("没有传递条件")
  151. }
  152. for _, condition := range params.Conditions {
  153. if err := condition.parse(); err != nil {
  154. return err
  155. }
  156. }
  157. return nil
  158. }
  159. func (params UpdateExecuteParams) Map() (map[string]any, error) {
  160. if err := params.check(); err != nil {
  161. return nil, err
  162. }
  163. return structs.Map(params), nil
  164. }
  165. const QueryTpl = `
  166. SELECT
  167. {{ if .select_columns }}{{ .select_columns | join "," }}{{ else }}*{{ end }}
  168. FROM
  169. {{ .table_name }}
  170. WHERE
  171. {{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1
  172. {{ if .limit }}LIMIT {{ .limit }}{{ end }}
  173. {{ if .offset }}OFFSET {{ .offset }}{{ end }}
  174. `
  175. type QueryExecuteParams struct {
  176. TableName string `structs:"table_name"`
  177. SelectColumns []string `structs:"select_columns"`
  178. Conditions []*Condition `structs:"conditions"`
  179. Limit int `structs:"limit"`
  180. Offset int `structs:"offset"`
  181. }
  182. func (params QueryExecuteParams) check() error {
  183. if strutils.IsStringEmpty(params.TableName) {
  184. return errors.New("没有传递表名")
  185. }
  186. if params.Conditions != nil && len(params.Conditions) != 0 {
  187. for _, condition := range params.Conditions {
  188. if err := condition.parse(); err != nil {
  189. return err
  190. }
  191. }
  192. }
  193. return nil
  194. }
  195. func (params QueryExecuteParams) Map() (map[string]any, error) {
  196. if err := params.check(); err != nil {
  197. return nil, err
  198. }
  199. return structs.Map(params), nil
  200. }
  201. const CountTpl = `
  202. SELECT
  203. COUNT(*)
  204. FROM
  205. {{ .table_name }}
  206. WHERE
  207. {{ range .conditions }} {{ .column }} {{ if .operator }}{{ .operator }}{{ else }}={{ end }} {{ .value }} AND {{ end }} 1 = 1`
  208. type CountExecuteParams struct {
  209. TableName string `structs:"table_name"`
  210. Conditions []*Condition `structs:"conditions"`
  211. }
  212. func (params CountExecuteParams) check() error {
  213. if strutils.IsStringEmpty(params.TableName) {
  214. return errors.New("没有传递表名")
  215. }
  216. if params.Conditions != nil && len(params.Conditions) != 0 {
  217. for _, condition := range params.Conditions {
  218. if err := condition.parse(); err != nil {
  219. return err
  220. }
  221. }
  222. }
  223. return nil
  224. }
  225. func (params CountExecuteParams) Map() (map[string]any, error) {
  226. if err := params.check(); err != nil {
  227. return nil, err
  228. }
  229. return structs.Map(params), nil
  230. }
  231. const (
  232. timeWriteFormat = time.DateTime + ".000000 +08:00"
  233. )
  234. func parseValue(value any) (string, error) {
  235. valueValue := reflect.ValueOf(value)
  236. if !valueValue.IsValid() {
  237. return "", errors.New("无效值")
  238. }
  239. if valueValue.Kind() == reflect.Ptr && valueValue.IsNil() {
  240. return "", errors.New("空值")
  241. }
  242. if valueValue.Kind() == reflect.Ptr {
  243. valueValue = valueValue.Elem()
  244. }
  245. switch v := valueValue.Interface().(type) {
  246. case string:
  247. return "'" + v + "'", nil
  248. case bool:
  249. return strconv.FormatBool(v), nil
  250. case time.Time:
  251. return "'" + v.Format(timeWriteFormat) + "'", nil
  252. case int:
  253. return strconv.Itoa(v), nil
  254. case int8:
  255. return strconv.FormatInt(int64(v), 10), nil
  256. case int16:
  257. return strconv.FormatInt(int64(v), 10), nil
  258. case int32:
  259. return strconv.FormatInt(int64(v), 10), nil
  260. case int64:
  261. return strconv.FormatInt(v, 10), nil
  262. case uint:
  263. return strconv.FormatUint(uint64(v), 10), nil
  264. case uint8:
  265. return strconv.FormatUint(uint64(v), 10), nil
  266. case uint16:
  267. return strconv.FormatUint(uint64(v), 10), nil
  268. case uint32:
  269. return strconv.FormatUint(uint64(v), 10), nil
  270. case uint64:
  271. return strconv.FormatUint(v, 10), nil
  272. default:
  273. return "", errors.New("不支持的类型")
  274. }
  275. }