|
@@ -9,9 +9,22 @@ import (
|
|
|
"git.sxidc.com/go-tools/api_binding/utils"
|
|
|
"git.sxidc.com/service-supports/dps-sdk"
|
|
|
"git.sxidc.com/service-supports/dps-sdk/client"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
-func ApiV1(binding *http_binding.Binding, dpsAddress string) {
|
|
|
+func ApiV1(binding *http_binding.Binding, dpsAddress string, operatorIDFunc OperatorIDFunc) {
|
|
|
+ if binding == nil {
|
|
|
+ panic("没有传递http_binding")
|
|
|
+ }
|
|
|
+
|
|
|
+ if utils.IsStringEmpty(dpsAddress) {
|
|
|
+ panic("没有指定dps地址")
|
|
|
+ }
|
|
|
+
|
|
|
+ if operatorIDFunc == nil {
|
|
|
+ panic("没有传递获取operatorID的回调函数")
|
|
|
+ }
|
|
|
+
|
|
|
http_binding.PostBind(binding, &http_binding.SimpleBindItem[OperateParseRequest, map[string]any]{
|
|
|
Path: "/dpsv1/database/operate/parse",
|
|
|
ResponseFunc: response.SendMapResponse,
|
|
@@ -52,6 +65,11 @@ func ApiV1(binding *http_binding.Binding, dpsAddress string) {
|
|
|
Path: "/dpsv1/database/operate",
|
|
|
ResponseFunc: response.SendMsgResponse,
|
|
|
BusinessFunc: func(c *binding_context.Context, inputModel OperateRequest) (any, error) {
|
|
|
+ operatorID, err := operatorIDFunc(c)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
parsedClauses, err := parseSql(inputModel.SQL)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
@@ -66,13 +84,13 @@ func ApiV1(binding *http_binding.Binding, dpsAddress string) {
|
|
|
for _, parsedClause := range parsedClauses {
|
|
|
switch clause := parsedClause.(type) {
|
|
|
case *insertClause:
|
|
|
- return doInsert(inputModel, clause)
|
|
|
+ return doInsert(tx, inputModel, clause, operatorID)
|
|
|
case *deleteClause:
|
|
|
- return doDelete(inputModel, clause)
|
|
|
+ return doDelete(tx, inputModel, clause, operatorID)
|
|
|
case *updateClause:
|
|
|
- return doUpdate(inputModel, clause)
|
|
|
+ return doUpdate(tx, inputModel, clause, operatorID)
|
|
|
case *selectClause:
|
|
|
- return doSelect(inputModel, clause)
|
|
|
+ return doSelect(dpsClient, inputModel, clause)
|
|
|
default:
|
|
|
return errors.New("不支持的SQL语句")
|
|
|
}
|
|
@@ -90,10 +108,15 @@ func ApiV1(binding *http_binding.Binding, dpsAddress string) {
|
|
|
}
|
|
|
|
|
|
func insertMap(clause *insertClause) map[string]any {
|
|
|
+ tableRows := make(map[string]any)
|
|
|
+ for columnName, value := range clause.tableRows {
|
|
|
+ tableRows[columnName] = value.value
|
|
|
+ }
|
|
|
+
|
|
|
return map[string]any{
|
|
|
"table": clause.table,
|
|
|
"key_columns": clause.keyColumns,
|
|
|
- "table_rows": clause.tableRows,
|
|
|
+ "table_rows": tableRows,
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -109,18 +132,46 @@ func selectMap(clause *selectClause) map[string]any {
|
|
|
return map[string]any{}
|
|
|
}
|
|
|
|
|
|
-func doInsert(inputModel OperateRequest, clause *insertClause) error {
|
|
|
+func doInsert(tx client.Transaction, inputModel OperateRequest, clause *insertClause, operatorID string) error {
|
|
|
version := inputModel.Version
|
|
|
if utils.IsStringEmpty(version) {
|
|
|
version = "v1"
|
|
|
}
|
|
|
|
|
|
- fmt.Printf("%+#v\n", clause)
|
|
|
+ tableRow := client.NewTableRow()
|
|
|
+ for columnName, value := range clause.tableRows {
|
|
|
+ switch value.kind {
|
|
|
+ case clauseTableRowValueKindTime:
|
|
|
+ tableRow.AddColumnValueTime(columnName, value.value.(time.Time))
|
|
|
+ case clauseTableRowValueKindBool:
|
|
|
+ tableRow.AddColumnValueBool(columnName, value.value.(bool))
|
|
|
+ case clauseTableRowValueKindString:
|
|
|
+ tableRow.AddColumnValueString(columnName, value.value.(string))
|
|
|
+ case clauseTableRowValueKindUint64:
|
|
|
+ tableRow.AddColumnValueUint64(columnName, value.value.(uint64))
|
|
|
+ case clauseTableRowValueKindFloat64:
|
|
|
+ tableRow.AddColumnValueFloat64(columnName, value.value.(float64))
|
|
|
+ default:
|
|
|
+ return errors.New("不支持的值类型")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ statement, err := tx.InsertTx(&client.InsertRequest{
|
|
|
+ TablePrefixWithSchema: clause.table,
|
|
|
+ Version: inputModel.Version,
|
|
|
+ KeyColumns: clause.keyColumns,
|
|
|
+ TableRow: tableRow,
|
|
|
+ UserID: operatorID,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(statement)
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func doDelete(inputModel OperateRequest, clause *deleteClause) error {
|
|
|
+func doDelete(tx client.Transaction, inputModel OperateRequest, clause *deleteClause, operatorID string) error {
|
|
|
version := inputModel.Version
|
|
|
if utils.IsStringEmpty(version) {
|
|
|
version = "v1"
|
|
@@ -129,7 +180,7 @@ func doDelete(inputModel OperateRequest, clause *deleteClause) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func doUpdate(inputModel OperateRequest, clause *updateClause) error {
|
|
|
+func doUpdate(tx client.Transaction, inputModel OperateRequest, clause *updateClause, operatorID string) error {
|
|
|
version := inputModel.Version
|
|
|
if utils.IsStringEmpty(version) {
|
|
|
version = "v1"
|
|
@@ -138,7 +189,7 @@ func doUpdate(inputModel OperateRequest, clause *updateClause) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func doSelect(inputModel OperateRequest, clause *selectClause) error {
|
|
|
+func doSelect(dpsClient client.Client, inputModel OperateRequest, clause *selectClause) error {
|
|
|
version := inputModel.Version
|
|
|
if utils.IsStringEmpty(version) {
|
|
|
version = "v1"
|