Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: cursor (#13232)
Danny Xubf91870549a1
diff --git a/backend/plugin/db/hive/dump.go b/backend/plugin/db/hive/dump.goindex c9f8b96bae..b630123031 100644--- a/backend/plugin/db/hive/dump.go+++ b/backend/plugin/db/hive/dump.go@@ -67,9 +67,8 @@ func (d *Driver) Dump(ctx context.Context, out io.Writer) (string, error) {schema := database.Schemas[0]cursor := d.conn.Cursor()- cursor.Exec(ctx, fmt.Sprintf("use %s", databaseName))- if cursor.Err != nil {- return "", cursor.Err+ if err := executeCursor(ctx, cursor, fmt.Sprintf("use %s", databaseName)); err != nil {+ return "", err}// dump managed tables.diff --git a/backend/plugin/db/hive/hive.go b/backend/plugin/db/hive/hive.goindex 2478fd8b48..2d93fc8b86 100644--- a/backend/plugin/db/hive/hive.go+++ b/backend/plugin/db/hive/hive.go@@ -44,7 +44,7 @@ var (_ db.Driver = (*Driver)(nil))-func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) {+func (d *Driver) Open(ctx context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) {if config.Host == "" {return nil, errors.Errorf("hostname not set")}@@ -82,9 +82,8 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionCif config.Database != "" {cursor := d.conn.Cursor()- cursor.Exec(context.Background(), fmt.Sprintf("use %s", config.Database))- if cursor.Err != nil {- return nil, multierr.Combine(d.conn.Close(), cursor.Err)+ if err := executeCursor(ctx, cursor, fmt.Sprintf("use %s", config.Database)); err != nil {+ return nil, multierr.Combine(d.conn.Close(), err)}}return d, nil@@ -98,9 +97,8 @@ func (d *Driver) Ping(ctx context.Context) error {cursor := d.conn.Cursor()defer cursor.Close()- cursor.Exec(ctx, "SELECT 1")- if cursor.Err != nil {- return errors.Errorf("bad connection")+ if err := executeCursor(ctx, cursor, "SELECT 1"); err != nil {+ return errors.Wrapf(err, "bad connection")}return nil}@@ -124,9 +122,9 @@ func (d *Driver) Execute(ctx context.Context, statementsStr string, _ db.Execute}for _, statement := range statements {- cursor.Execute(ctx, strings.TrimRight(statement.Text, ";"), false)- if cursor.Err != nil {- return 0, errors.Wrap(cursor.Err, "failed to execute statement")+ query := strings.TrimRight(statement.Text, ";")+ if err := executeCursor(ctx, cursor, query); err != nil {+ return 0, err}operationStatus := cursor.Poll(false)affectedRows += operationStatus.GetNumModifiedRows()@@ -192,9 +190,8 @@ func runSingleStatement(ctx context.Context, conn *gohive.Connection, statementdefer cursor.Close()// run query.- cursor.Execute(ctx, statement, false)- if cursor.Err != nil {- return nil, errors.Wrap(cursor.Err, "failed to execute statement")+ if err := executeCursor(ctx, cursor, statement); err != nil {+ return nil, err}result := &v1pb.QueryResult{@@ -234,3 +231,11 @@ func runSingleStatement(ctx context.Context, conn *gohive.Connection, statementresult.Latency = durationpb.New(time.Since(startTime))return result, nil}++func executeCursor(ctx context.Context, cursor *gohive.Cursor, statement string) error {+ cursor.Exec(ctx, statement)+ if cursor.Err != nil {+ return errors.Wrap(cursor.Err, "failed to execute statement")+ }+ return nil+}diff --git a/backend/plugin/db/hive/role.go b/backend/plugin/db/hive/role.godeleted file mode 100644index 652ce6433c..0000000000--- a/backend/plugin/db/hive/role.go+++ /dev/null@@ -1,72 +0,0 @@-package hive--import (- "context"- "fmt"- "strings"-- "github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/plugin/db"-)--func (d *Driver) CreateRole(ctx context.Context, roleUpsertMessage *db.DatabaseRoleUpsertMessage) (*db.DatabaseRoleMessage, error) {- _, err := d.Execute(ctx, fmt.Sprintf("CREATE ROLE %s", roleUpsertMessage.Name), db.ExecuteOptions{})- if err != nil {- return nil, errors.Wrapf(err, "failed to create role: %s", roleUpsertMessage.Name)- }- return &db.DatabaseRoleMessage{- Name: roleUpsertMessage.Name,- }, nil-}--func (*Driver) UpdateRole(_ context.Context, _ string, _ *db.DatabaseRoleUpsertMessage) (*db.DatabaseRoleMessage, error) {- return nil, errors.Errorf("UpdateRole() is not applicable to Hive")-}--func (*Driver) FindRole(_ context.Context, _ string) (*db.DatabaseRoleMessage, error) {- return nil, errors.Errorf("FindRole() is not applicable to Hive")-}--func (d *Driver) ListRole(ctx context.Context) ([]*db.DatabaseRoleMessage, error) {- roleResult, err := runSingleStatement(ctx, d.conn, "SHOW ROLES")- if err != nil {- return nil, errors.Wrapf(err, "failed to get roles")- }-- var roleMessages []*db.DatabaseRoleMessage- for _, row := range roleResult.Rows {- roleMessages = append(roleMessages, &db.DatabaseRoleMessage{- Name: row.Values[0].GetStringValue(),- })- }- return roleMessages, nil-}--func (d *Driver) DeleteRole(ctx context.Context, roleName string) error {- _, err := d.Execute(ctx, fmt.Sprintf("DROP ROLE %s", roleName), db.ExecuteOptions{})- if err != nil {- return errors.Wrapf(err, "failed to drop role %s", roleName)- }- return nil-}--func (d *Driver) GetRoleGrant(ctx context.Context, roleName string) (string, error) {- grantResult, err := runSingleStatement(ctx, d.conn, fmt.Sprintf("SHOW GRANT ROLE %s", roleName))- if err != nil {- return "", errors.Wrapf(err, "failed to get grant from %s", roleName)- }-- var grantStrings []string- // get grant-info rows from a certain role and combine them into a string.- for _, row := range grantResult.Rows {- databaseName := row.Values[0].GetStringValue()- tableName := row.Values[1].GetStringValue()- privilege := row.Values[6].GetStringValue()- // TODO(tommy): the format of this string should be carefully considered.- grantStrings = append(grantStrings, fmt.Sprintf("%s/%s: %s", databaseName, tableName, privilege))- }- grantString := strings.Join(grantStrings, "\n")-- return grantString, nil-}diff --git a/backend/plugin/db/hive/sync.go b/backend/plugin/db/hive/sync.goindex e161959a36..ce680cc1fd 100644--- a/backend/plugin/db/hive/sync.go+++ b/backend/plugin/db/hive/sync.go@@ -235,7 +235,7 @@ type TableInfo struct {comment string}-func (d *Driver) getTableInfo(ctx context.Context, tabName string, databaseName string) (+func (d *Driver) getTableInfo(ctx context.Context, tableName string, databaseName string) (*TableInfo,error,) {@@ -249,9 +249,9 @@ func (d *Driver) getTableInfo(ctx context.Context, tabName string, databaseName)cursor := d.conn.Cursor()- cursor.Exec(ctx, fmt.Sprintf("DESCRIBE FORMATTED `%s`.`%s`", databaseName, tabName))- if cursor.Err != nil {- return nil, errors.Wrapf(cursor.Err, "failed to describe table %s", tabName)+ query := fmt.Sprintf("DESCRIBE FORMATTED `%s`.`%s`", databaseName, tableName)+ if err := executeCursor(ctx, cursor, query); err != nil {+ return nil, errors.Wrapf(err, "failed to describe table %s", tableName)}cnt := 0for cursor.HasMore(ctx) {