v1.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package api
  2. import (
  3. "errors"
  4. "fmt"
  5. "git.sxidc.com/go-tools/api_binding/http_binding"
  6. "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
  7. "git.sxidc.com/go-tools/api_binding/http_binding/response"
  8. "git.sxidc.com/go-tools/api_binding/utils"
  9. "git.sxidc.com/service-supports/dps-sdk"
  10. "git.sxidc.com/service-supports/dps-sdk/client"
  11. "time"
  12. )
  13. func ApiV1(binding *http_binding.Binding, dpsAddress string, operatorIDFunc OperatorIDFunc) {
  14. if binding == nil {
  15. panic("没有传递http_binding")
  16. }
  17. if utils.IsStringEmpty(dpsAddress) {
  18. panic("没有指定dps地址")
  19. }
  20. if operatorIDFunc == nil {
  21. panic("没有传递获取operatorID的回调函数")
  22. }
  23. http_binding.PostBind(binding, &http_binding.SimpleBindItem[OperateParseRequest, map[string]any]{
  24. Path: "/dpsv1/database/operate/parse",
  25. ResponseFunc: response.SendMapResponse,
  26. BusinessFunc: func(c *binding_context.Context, inputModel OperateParseRequest) (map[string]any, error) {
  27. parsedClauses, err := parseSql(inputModel.SQL)
  28. if err != nil {
  29. return nil, err
  30. }
  31. for _, parsedClause := range parsedClauses {
  32. switch clause := parsedClause.(type) {
  33. case *insertClause:
  34. return map[string]any{
  35. "parsed": insertMap(clause),
  36. }, nil
  37. case *deleteClause:
  38. return map[string]any{
  39. "parsed": deleteMap(clause),
  40. }, nil
  41. case *updateClause:
  42. return map[string]any{
  43. "parsed": updateMap(clause),
  44. }, nil
  45. case *selectClause:
  46. return map[string]any{
  47. "parsed": selectMap(clause),
  48. }, nil
  49. default:
  50. return nil, errors.New("不支持的SQL语句")
  51. }
  52. }
  53. return nil, nil
  54. },
  55. })
  56. http_binding.PostBind(binding, &http_binding.SimpleBindItem[OperateRequest, map[string]any]{
  57. Path: "/dpsv1/database/operate",
  58. ResponseFunc: response.SendMapResponse,
  59. BusinessFunc: func(c *binding_context.Context, inputModel OperateRequest) (map[string]any, error) {
  60. version := "v1"
  61. if utils.IsStringNotEmpty(inputModel.Version) {
  62. version = inputModel.Version
  63. }
  64. keyColumns := []string{"id"}
  65. if inputModel.KeyColumns != nil && len(inputModel.KeyColumns) != 0 {
  66. keyColumns = inputModel.KeyColumns
  67. }
  68. operatorID, err := operatorIDFunc(c)
  69. if err != nil {
  70. return map[string]any{"tableRows": make([]map[string]any, 0)}, err
  71. }
  72. parsedClauses, err := parseSql(inputModel.SQL)
  73. if err != nil {
  74. return map[string]any{"tableRows": make([]map[string]any, 0)}, err
  75. }
  76. dpsClient, err := dps.NewClient(dpsAddress, "v1", inputModel.DatabaseID)
  77. if err != nil {
  78. return map[string]any{"tableRows": make([]map[string]any, 0)}, err
  79. }
  80. result := make([]map[string]any, 0)
  81. err = dpsClient.Transaction(func(tx client.Transaction) error {
  82. for _, parsedClause := range parsedClauses {
  83. switch clause := parsedClause.(type) {
  84. case *insertClause:
  85. return doInsert(tx, version, keyColumns, clause, operatorID)
  86. case *deleteClause:
  87. return doDelete(tx, version, keyColumns, clause, operatorID)
  88. case *updateClause:
  89. return doUpdate(tx, version, keyColumns, clause, operatorID)
  90. case *selectClause:
  91. r, err := doSelect(dpsClient, version, clause)
  92. if err != nil {
  93. return err
  94. }
  95. result = r
  96. return nil
  97. default:
  98. return errors.New("不支持的SQL语句")
  99. }
  100. }
  101. return nil
  102. })
  103. if err != nil {
  104. return map[string]any{"tableRows": make([]map[string]any, 0)}, err
  105. }
  106. return map[string]any{"tableRows": result}, nil
  107. },
  108. })
  109. }
  110. func insertMap(clause *insertClause) map[string]any {
  111. tableRows := make(map[string]any)
  112. for columnName, value := range clause.tableRow {
  113. tableRows[columnName] = value.value
  114. }
  115. return map[string]any{
  116. "table": clause.table,
  117. "tableRow": tableRows,
  118. }
  119. }
  120. func deleteMap(clause *deleteClause) map[string]any {
  121. return map[string]any{
  122. "table": clause.table,
  123. "where": clause.where,
  124. }
  125. }
  126. func updateMap(clause *updateClause) map[string]any {
  127. newTableRows := make(map[string]any)
  128. for columnName, value := range clause.newTableRow {
  129. newTableRows[columnName] = value.value
  130. }
  131. return map[string]any{
  132. "table": clause.table,
  133. "where": clause.where,
  134. "newTableRow": newTableRows,
  135. }
  136. }
  137. func selectMap(clause *selectClause) map[string]any {
  138. return map[string]any{
  139. "table": clause.table,
  140. "fromSubQuery": clause.fromSubQuery,
  141. "selectClause": clause.selectClause,
  142. "where": clause.where,
  143. "orderBy": clause.orderBy,
  144. "groupBy": clause.groupBy,
  145. "having": clause.having,
  146. "pageNo": clause.pageNo,
  147. "pageSize": clause.pageSize,
  148. }
  149. }
  150. func doInsert(tx client.Transaction, version string, keyColumns []string, clause *insertClause, operatorID string) error {
  151. tableRow, err := clauseTableRowsToDPSTableRow(clause.tableRow)
  152. if err != nil {
  153. return err
  154. }
  155. statement, err := tx.InsertTx(&client.InsertRequest{
  156. TablePrefixWithSchema: clause.table,
  157. Version: version,
  158. KeyColumns: keyColumns,
  159. TableRow: tableRow,
  160. UserID: operatorID,
  161. })
  162. if err != nil {
  163. fmt.Println(statement)
  164. return err
  165. }
  166. return nil
  167. }
  168. func doDelete(tx client.Transaction, version string, keyColumns []string, clause *deleteClause, operatorID string) error {
  169. statement, err := tx.DeleteWhereTx(&client.DeleteWhereRequest{
  170. TablePrefixWithSchema: clause.table,
  171. Version: version,
  172. KeyColumns: keyColumns,
  173. Where: client.NewClause().Common(clause.where),
  174. UserID: operatorID,
  175. })
  176. if err != nil {
  177. fmt.Println(statement)
  178. return err
  179. }
  180. return nil
  181. }
  182. func doUpdate(tx client.Transaction, version string, keyColumns []string, clause *updateClause, operatorID string) error {
  183. newTableRow, err := clauseTableRowsToDPSTableRow(clause.newTableRow)
  184. if err != nil {
  185. return err
  186. }
  187. statement, err := tx.UpdateWhereTx(&client.UpdateWhereRequest{
  188. TablePrefixWithSchema: clause.table,
  189. Version: version,
  190. KeyColumns: keyColumns,
  191. Where: client.NewClause().Common(clause.where),
  192. NewTableRow: newTableRow,
  193. UserID: operatorID,
  194. })
  195. if err != nil {
  196. fmt.Println(statement)
  197. return err
  198. }
  199. return nil
  200. }
  201. func doSelect(dpsClient client.Client, version string, clause *selectClause) ([]map[string]any, error) {
  202. statement, tableRows, err := dpsClient.CommonQueryOnly(&client.CommonQueryRequest{
  203. TablePrefixWithSchema: clause.table,
  204. Table: clause.fromSubQuery,
  205. Version: version,
  206. Select: client.NewClause().Common(clause.selectClause),
  207. Where: client.NewClause().Common(clause.where),
  208. OrderBy: clause.orderBy,
  209. GroupBy: clause.groupBy,
  210. Having: client.NewClause().Common(clause.having),
  211. PageNo: clause.pageNo,
  212. PageSize: clause.pageSize,
  213. })
  214. if err != nil {
  215. fmt.Println(statement)
  216. return nil, err
  217. }
  218. results := make([]map[string]any, 0)
  219. for _, tableRow := range tableRows {
  220. results = append(results, tableRow.ToMap())
  221. }
  222. return results, nil
  223. }
  224. func clauseTableRowsToDPSTableRow(clauseTableRow map[string]clauseTableRowValue) (*client.TableRow, error) {
  225. tableRow := client.NewTableRow()
  226. for columnName, value := range clauseTableRow {
  227. switch value.kind {
  228. case clauseTableRowValueKindTime:
  229. tableRow.AddColumnValueTime(columnName, value.value.(time.Time))
  230. case clauseTableRowValueKindBool:
  231. tableRow.AddColumnValueBool(columnName, value.value.(bool))
  232. case clauseTableRowValueKindString:
  233. tableRow.AddColumnValueString(columnName, value.value.(string))
  234. case clauseTableRowValueKindUint64:
  235. tableRow.AddColumnValueUint64(columnName, value.value.(uint64))
  236. case clauseTableRowValueKindFloat64:
  237. tableRow.AddColumnValueFloat64(columnName, value.value.(float64))
  238. default:
  239. return nil, errors.New("不支持的值类型")
  240. }
  241. }
  242. return tableRow, nil
  243. }