Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: return ResultSet as JSON in /sql/execute and implement sql/query in frontend (#251)

tianzhouf891d1dba446
diff --git a/api/sql.go b/api/sql.go
index b3f903727b..cca9a40796 100644
--- a/api/sql.go
+++ b/api/sql.go
@@ -38,6 +38,8 @@ type SQLExecute struct {
// SQLResultSet is the API message for SQL results.
type SQLResultSet struct {
+ // A list of rows marshalled into a JSON.
+ Result string `jsonapi:"attr,result"`
// SQL operation may fail for connection issue and there is no proper http status code for it, so we return error in the response body.
Error string `jsonapi:"attr,error"`
}
diff --git a/frontend/src/store/modules/sql.ts b/frontend/src/store/modules/sql.ts
index 0ee0bf89e8..bcfd26a60c 100644
--- a/frontend/src/store/modules/sql.ts
+++ b/frontend/src/store/modules/sql.ts
@@ -3,12 +3,14 @@ import {
ConnectionInfo,
InstanceId,
INSTANCE_OPERATION_TIMEOUT,
+ QueryInfo,
ResourceObject,
SqlResultSet,
} from "../../types";
function convert(resultSet: ResourceObject): SqlResultSet {
return {
+ result: JSON.parse((resultSet.attributes.result as string) || "{}"),
error: resultSet.attributes.error as string,
};
}
@@ -66,6 +68,28 @@ const actions = {
return resultSet;
},
+ async query({ dispatch }: any, queryInfo: QueryInfo) {
+ const data = (
+ await axios.post(
+ `/api/sql/execute`,
+ {
+ data: {
+ type: "sqlExecute",
+ attributes: {
+ ...queryInfo,
+ readonly: true
+ },
+ },
+ },
+ {
+ timeout: INSTANCE_OPERATION_TIMEOUT,
+ }
+ )
+ ).data.data;
+
+ const resultSet = convert(data);
+ return resultSet;
+ },
};
const mutations = {};
diff --git a/frontend/src/types/sql.ts b/frontend/src/types/sql.ts
index da99d58c59..b46cf82848 100644
--- a/frontend/src/types/sql.ts
+++ b/frontend/src/types/sql.ts
@@ -16,6 +16,14 @@ export type ConnectionInfo = {
instanceId?: InstanceId;
};
+export type QueryInfo = {
+ instanceId: InstanceId;
+ databaseName?: string;
+ statement: string;
+ limit?: number;
+}
+
export type SqlResultSet = {
+ result: string;
error: string;
};
diff --git a/server/sql.go b/server/sql.go
index 06c564b4dc..7eb28e0135 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -12,6 +12,7 @@ import (
"github.com/bytebase/bytebase/plugin/db"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
+ "go.uber.org/zap"
)
func (s *Server) registerSQLRoutes(g *echo.Group) {
@@ -118,28 +119,42 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", exec.InstanceID)).SetInternal(err)
}
- resultSet := func() (resultSet *api.SQLResultSet) {
- resultSet = &api.SQLResultSet{}
+ bytes, err := func() ([]byte, error) {
driver, err := getDatabaseDriver(ctx, instance, exec.DatabaseName, s.l)
if err != nil {
- resultSet.Error = err.Error()
- return
+ return nil, err
}
rowSet, err := driver.Query(ctx, exec.Statement, exec.Limit)
if err != nil {
- resultSet.Error = err.Error()
- return
- }
-
- if _, err = json.Marshal(rowSet); err != nil {
- resultSet.Error = err.Error()
- return
+ return nil, err
}
- return
+ return json.Marshal(rowSet)
}()
+ resultSet := &api.SQLResultSet{}
+ if err == nil {
+ resultSet.Result = string(bytes)
+ s.l.Debug("Query result",
+ zap.String("statement", exec.Statement),
+ zap.String("result", resultSet.Result),
+ )
+ } else {
+ resultSet.Error = err.Error()
+ if s.mode == "dev" {
+ s.l.Error("Failed to execute query",
+ zap.Error(err),
+ zap.String("statement", exec.Statement),
+ )
+ } else {
+ s.l.Debug("Failed to execute query",
+ zap.Error(err),
+ zap.String("statement", exec.Statement),
+ )
+ }
+ }
+
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, resultSet); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal sql result set response").SetInternal(err)