|
@@ -61,10 +61,10 @@ func ApiV1(binding *http_binding.Binding, dpsAddress string, operatorIDFunc Oper
|
|
|
},
|
|
|
})
|
|
|
|
|
|
- http_binding.PostBind(binding, &http_binding.SimpleBindItem[OperateRequest, any]{
|
|
|
+ http_binding.PostBind(binding, &http_binding.SimpleBindItem[OperateRequest, map[string]any]{
|
|
|
Path: "/dpsv1/database/operate",
|
|
|
- ResponseFunc: response.SendMsgResponse,
|
|
|
- BusinessFunc: func(c *binding_context.Context, inputModel OperateRequest) (any, error) {
|
|
|
+ ResponseFunc: response.SendMapResponse,
|
|
|
+ BusinessFunc: func(c *binding_context.Context, inputModel OperateRequest) (map[string]any, error) {
|
|
|
version := "v1"
|
|
|
if utils.IsStringNotEmpty(inputModel.Version) {
|
|
|
version = inputModel.Version
|
|
@@ -77,19 +77,21 @@ func ApiV1(binding *http_binding.Binding, dpsAddress string, operatorIDFunc Oper
|
|
|
|
|
|
operatorID, err := operatorIDFunc(c)
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return map[string]any{"table_rows": make([]map[string]any, 0)}, err
|
|
|
}
|
|
|
|
|
|
parsedClauses, err := parseSql(inputModel.SQL)
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return map[string]any{"table_rows": make([]map[string]any, 0)}, err
|
|
|
}
|
|
|
|
|
|
dpsClient, err := dps.NewClient(dpsAddress, "v1", inputModel.DatabaseID)
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return map[string]any{"table_rows": make([]map[string]any, 0)}, err
|
|
|
}
|
|
|
|
|
|
+ result := make([]map[string]any, 0)
|
|
|
+
|
|
|
err = dpsClient.Transaction(func(tx client.Transaction) error {
|
|
|
for _, parsedClause := range parsedClauses {
|
|
|
switch clause := parsedClause.(type) {
|
|
@@ -100,7 +102,13 @@ func ApiV1(binding *http_binding.Binding, dpsAddress string, operatorIDFunc Oper
|
|
|
case *updateClause:
|
|
|
return doUpdate(tx, version, keyColumns, clause, operatorID)
|
|
|
case *selectClause:
|
|
|
- return doSelect(dpsClient, version, clause)
|
|
|
+ r, err := doSelect(dpsClient, version, clause)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ result = r
|
|
|
+ return nil
|
|
|
default:
|
|
|
return errors.New("不支持的SQL语句")
|
|
|
}
|
|
@@ -109,17 +117,17 @@ func ApiV1(binding *http_binding.Binding, dpsAddress string, operatorIDFunc Oper
|
|
|
return nil
|
|
|
})
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return map[string]any{"table_rows": make([]map[string]any, 0)}, err
|
|
|
}
|
|
|
|
|
|
- return nil, nil
|
|
|
+ return map[string]any{"table_rows": result}, nil
|
|
|
},
|
|
|
})
|
|
|
}
|
|
|
|
|
|
func insertMap(clause *insertClause) map[string]any {
|
|
|
tableRows := make(map[string]any)
|
|
|
- for columnName, value := range clause.tableRows {
|
|
|
+ for columnName, value := range clause.tableRow {
|
|
|
tableRows[columnName] = value.value
|
|
|
}
|
|
|
|
|
@@ -145,22 +153,9 @@ func selectMap(clause *selectClause) map[string]any {
|
|
|
}
|
|
|
|
|
|
func doInsert(tx client.Transaction, version string, keyColumns []string, clause *insertClause, operatorID string) error {
|
|
|
- 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("不支持的值类型")
|
|
|
- }
|
|
|
+ tableRow, err := clauseTableRowsToDPSTableRow(clause.tableRow)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
statement, err := tx.InsertTx(&client.InsertRequest{
|
|
@@ -195,9 +190,74 @@ func doDelete(tx client.Transaction, version string, keyColumns []string, clause
|
|
|
}
|
|
|
|
|
|
func doUpdate(tx client.Transaction, version string, keyColumns []string, clause *updateClause, operatorID string) error {
|
|
|
+ newTableRow, err := clauseTableRowsToDPSTableRow(clause.newTableRow)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ statement, err := tx.UpdateWhereTx(&client.UpdateWhereRequest{
|
|
|
+ TablePrefixWithSchema: clause.table,
|
|
|
+ Version: version,
|
|
|
+ KeyColumns: keyColumns,
|
|
|
+ Where: client.NewClause().Common(clause.where),
|
|
|
+ NewTableRow: newTableRow,
|
|
|
+ UserID: operatorID,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(statement)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func doSelect(dpsClient client.Client, version string, clause *selectClause) error {
|
|
|
- return nil
|
|
|
+func doSelect(dpsClient client.Client, version string, clause *selectClause) ([]map[string]any, error) {
|
|
|
+ statement, tableRows, err := dpsClient.CommonQueryOnly(&client.CommonQueryRequest{
|
|
|
+ TablePrefixWithSchema: "",
|
|
|
+ Table: "",
|
|
|
+ Version: version,
|
|
|
+ Select: nil,
|
|
|
+ Where: nil,
|
|
|
+ OrderBy: nil,
|
|
|
+ Or: nil,
|
|
|
+ GroupBy: nil,
|
|
|
+ Joins: nil,
|
|
|
+ Having: nil,
|
|
|
+ PageNo: 0,
|
|
|
+ PageSize: 0,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(statement)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ results := make([]map[string]any, 0)
|
|
|
+ for _, tableRow := range tableRows {
|
|
|
+ results = append(results, tableRow.ToMap())
|
|
|
+ }
|
|
|
+
|
|
|
+ return results, nil
|
|
|
+}
|
|
|
+
|
|
|
+func clauseTableRowsToDPSTableRow(clauseTableRow map[string]clauseTableRowValue) (*client.TableRow, error) {
|
|
|
+ tableRow := client.NewTableRow()
|
|
|
+
|
|
|
+ for columnName, value := range clauseTableRow {
|
|
|
+ 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 nil, errors.New("不支持的值类型")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return tableRow, nil
|
|
|
}
|