Skip to content

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

chore: add appname in mongodb connection url (#12397)

h3n4l8ddb01fcdfc1
diff --git a/backend/plugin/db/bigquery/bigquery.go b/backend/plugin/db/bigquery/bigquery.go
index e7d6b4143b..e9102e04a6 100644
--- a/backend/plugin/db/bigquery/bigquery.go
+++ b/backend/plugin/db/bigquery/bigquery.go
@@ -111,7 +111,7 @@ func (d *Driver) Execute(ctx context.Context, statement string, _ db.ExecuteOpti
// QueryConn queries a SQL statement in a given connection.
func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
return nil, errors.New("BigQuery does not support EXPLAIN")
}
diff --git a/backend/plugin/db/clickhouse/clickhouse.go b/backend/plugin/db/clickhouse/clickhouse.go
index 9849b52170..0f9d0f249e 100644
--- a/backend/plugin/db/clickhouse/clickhouse.go
+++ b/backend/plugin/db/clickhouse/clickhouse.go
@@ -245,9 +245,9 @@ func getStatementWithResultLimit(statement string, limit int) string {
func (*Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) (*v1pb.QueryResult, error) {
startTime := time.Now()
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
- } else if queryContext.Limit > 0 {
+ } else if queryContext != nil && queryContext.Limit > 0 {
statement = getStatementWithResultLimit(statement, queryContext.Limit)
}
diff --git a/backend/plugin/db/dm/dm.go b/backend/plugin/db/dm/dm.go
index d617471947..87768f0917 100644
--- a/backend/plugin/db/dm/dm.go
+++ b/backend/plugin/db/dm/dm.go
@@ -170,7 +170,9 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
// QueryConn queries a SQL statement in a given connection.
func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
// DM does not support transaction isolation level for read-only queries.(also like Oracle :)
- queryContext.ReadOnly = false
+ if queryContext != nil {
+ queryContext.ReadOnly = false
+ }
singleSQLs, err := plsqlparser.SplitSQL(statement)
if err != nil {
@@ -204,7 +206,7 @@ func (*Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL bas
statement := singleSQL.Text
statement = strings.TrimRight(statement, " \n\t;")
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
startTime := time.Now()
randNum, err := rand.Int(rand.Reader, big.NewInt(999))
if err != nil {
@@ -226,11 +228,11 @@ func (*Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL bas
return result, nil
}
- if queryContext.Limit > 0 {
+ if queryContext != nil && queryContext.Limit > 0 {
statement = getDMStatementWithResultLimit(statement, queryContext.Limit)
}
- if queryContext.SensitiveSchemaInfo != nil {
+ if queryContext != nil && queryContext.SensitiveSchemaInfo != nil {
for _, database := range queryContext.SensitiveSchemaInfo.DatabaseList {
if len(database.SchemaList) == 0 {
continue
diff --git a/backend/plugin/db/dynamodb/dynamodb.go b/backend/plugin/db/dynamodb/dynamodb.go
index da51508c2f..e3f949dbb9 100644
--- a/backend/plugin/db/dynamodb/dynamodb.go
+++ b/backend/plugin/db/dynamodb/dynamodb.go
@@ -154,7 +154,7 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteO
// QueryConn queries a SQL statement in a given connection.
// The result.Rows.Values can be nil in DynamoDB, which means the column is not set in the row.
func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
return nil, errors.New("DynamoDB does not support EXPLAIN")
}
@@ -192,7 +192,7 @@ func (d *Driver) querySinglePartiQL(ctx context.Context, statement base.SingleSQ
input := &dynamodb.ExecuteStatementInput{
Statement: &statement.Text,
}
- if queryContext.Limit > 0 {
+ if queryContext != nil && queryContext.Limit > 0 {
limit := int32(queryContext.Limit)
input.Limit = &limit
}
diff --git a/backend/plugin/db/mongodb/mongodb.go b/backend/plugin/db/mongodb/mongodb.go
index ffbf23973b..6c049469d1 100644
--- a/backend/plugin/db/mongodb/mongodb.go
+++ b/backend/plugin/db/mongodb/mongodb.go
@@ -198,6 +198,7 @@ func getBasicMongoDBConnectionURI(connConfig db.ConnectionConfig) string {
if connConfig.ReplicaSet != "" {
values.Add("replicaSet", connConfig.ReplicaSet)
}
+ values.Add("appName", "bytebase")
// Add SSL options if provided
if connConfig.TLSConfig.SslCA != "" {
values.Add("tlsCAFile", connConfig.TLSConfig.SslCA)
@@ -217,7 +218,7 @@ func getBasicMongoDBConnectionURI(connConfig db.ConnectionConfig) string {
// QueryConn queries a SQL statement in a given connection.
func (driver *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
return nil, errors.New("MongoDB does not support EXPLAIN")
}
diff --git a/backend/plugin/db/mongodb/mongodb_test.go b/backend/plugin/db/mongodb/mongodb_test.go
index 5b437b3553..64dd683260 100644
--- a/backend/plugin/db/mongodb/mongodb_test.go
+++ b/backend/plugin/db/mongodb/mongodb_test.go
@@ -25,7 +25,7 @@ func TestGetMongoDBConnectionURL(t *testing.T) {
Username: "",
Password: "",
},
- want: "mongodb://localhost:27017/?authSource=admin",
+ want: "mongodb://localhost:27017/?appName=bytebase&authSource=admin",
},
{
connConfig: db.ConnectionConfig{
@@ -35,7 +35,7 @@ func TestGetMongoDBConnectionURL(t *testing.T) {
Password: "",
DirectConnection: true,
},
- want: "mongodb://localhost:27017/?authSource=admin&directConnection=true",
+ want: "mongodb://localhost:27017/?appName=bytebase&authSource=admin&directConnection=true",
},
{
connConfig: db.ConnectionConfig{
@@ -45,7 +45,7 @@ func TestGetMongoDBConnectionURL(t *testing.T) {
Password: "",
Database: "sampleDB",
},
- want: "mongodb://localhost:27017/sampleDB?authSource=admin",
+ want: "mongodb://localhost:27017/sampleDB?appName=bytebase&authSource=admin",
},
{
connConfig: db.ConnectionConfig{
@@ -55,7 +55,7 @@ func TestGetMongoDBConnectionURL(t *testing.T) {
Database: "sampleDB",
SRV: true,
},
- want: "mongodb+srv://bytebase:[email protected]/sampleDB?authSource=admin",
+ want: "mongodb+srv://bytebase:[email protected]/sampleDB?appName=bytebase&authSource=admin",
},
{
connConfig: db.ConnectionConfig{
@@ -66,7 +66,7 @@ func TestGetMongoDBConnectionURL(t *testing.T) {
AuthenticationDatabase: "",
SRV: true,
},
- want: "mongodb+srv://bytebase:[email protected]/sampleDB?authSource=admin",
+ want: "mongodb+srv://bytebase:[email protected]/sampleDB?appName=bytebase&authSource=admin",
},
{
connConfig: db.ConnectionConfig{
@@ -77,7 +77,7 @@ func TestGetMongoDBConnectionURL(t *testing.T) {
AuthenticationDatabase: "admin",
SRV: true,
},
- want: "mongodb+srv://bytebase:[email protected]/sampleDB?authSource=admin",
+ want: "mongodb+srv://bytebase:[email protected]/sampleDB?appName=bytebase&authSource=admin",
},
{
connConfig: db.ConnectionConfig{
@@ -94,7 +94,7 @@ func TestGetMongoDBConnectionURL(t *testing.T) {
},
ReplicaSet: "rs0",
},
- want: "mongodb://bytebase:[email protected]:27017,node2.cluster0.sample.mongodb.net:27017,node3.cluster0.sample.mongodb.net:27017/sampleDB?authSource=admin&replicaSet=rs0",
+ want: "mongodb://bytebase:[email protected]:27017,node2.cluster0.sample.mongodb.net:27017,node3.cluster0.sample.mongodb.net:27017/sampleDB?appName=bytebase&authSource=admin&replicaSet=rs0",
},
}
diff --git a/backend/plugin/db/mssql/mssql.go b/backend/plugin/db/mssql/mssql.go
index f716a0d5ec..8fc5e1915c 100644
--- a/backend/plugin/db/mssql/mssql.go
+++ b/backend/plugin/db/mssql/mssql.go
@@ -52,7 +52,7 @@ func newDriver(db.DriverConfig) db.Driver {
// Open opens a MSSQL driver.
func (driver *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) {
query := url.Values{}
- query.Add("app name", "Bytebase")
+ query.Add("app name", "bytebase")
if config.Database != "" {
query.Add("database", config.Database)
}
@@ -223,7 +223,9 @@ func execute(ctx context.Context, tx *sql.Tx, statement string) (int64, error) {
// QueryConn queries a SQL statement in a given connection.
func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
// MSSQL does not support transaction isolation level for read-only queries.
- queryContext.ReadOnly = false
+ if queryContext != nil {
+ queryContext.ReadOnly = false
+ }
singleSQLs, err := tsqlparser.SplitSQL(statement)
if err != nil {
@@ -251,9 +253,9 @@ func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement s
func (*Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL base.SingleSQL, queryContext *db.QueryContext) (*v1pb.QueryResult, error) {
statement := strings.TrimRight(singleSQL.Text, " \n\t;")
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
- } else if queryContext.Limit > 0 {
+ } else if queryContext != nil && queryContext.Limit > 0 {
stmt, err := getMSSQLStatementWithResultLimit(statement, queryContext.Limit)
if err != nil {
slog.Error("fail to add limit clause", "statement", statement, log.BBError(err))
diff --git a/backend/plugin/db/mysql/mysql.go b/backend/plugin/db/mysql/mysql.go
index 796fd99f0e..1f59462b89 100644
--- a/backend/plugin/db/mysql/mysql.go
+++ b/backend/plugin/db/mysql/mysql.go
@@ -448,7 +448,7 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteO
// QueryConn queries a SQL statement in a given connection.
func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
- if queryContext.ReadOnly {
+ if queryContext != nil && queryContext.ReadOnly {
queryContext.ReadOnly = d.getReadOnly()
}
@@ -507,14 +507,14 @@ func (d *Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL b
isSet := variableSetStmtRegexp.MatchString(statement)
isShow := variableShowStmtRegexp.MatchString(statement)
if !isSet && !isShow {
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
- } else if queryContext.Limit > 0 {
+ } else if queryContext != nil && queryContext.Limit > 0 {
statement = getStatementWithResultLimit(statement, queryContext.Limit)
}
}
- if queryContext.SensitiveSchemaInfo != nil {
+ if queryContext != nil && queryContext.SensitiveSchemaInfo != nil {
for _, database := range queryContext.SensitiveSchemaInfo.DatabaseList {
if len(database.SchemaList) == 0 {
continue
diff --git a/backend/plugin/db/obo/obo.go b/backend/plugin/db/obo/obo.go
index dcc355bc63..ed18c5a5ee 100644
--- a/backend/plugin/db/obo/obo.go
+++ b/backend/plugin/db/obo/obo.go
@@ -164,7 +164,9 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
// Oracle does not support transaction isolation level for read-only queries.
- queryContext.ReadOnly = false
+ if queryContext != nil {
+ queryContext.ReadOnly = false
+ }
singleSQLs, err := plsqlparser.SplitSQL(statement)
if err != nil {
@@ -193,7 +195,7 @@ func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement s
func (*Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL base.SingleSQL, queryContext *db.QueryContext) (*v1pb.QueryResult, error) {
statement := strings.TrimRight(singleSQL.Text, " \n\t;")
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
startTime := time.Now()
randNum, err := rand.Int(rand.Reader, big.NewInt(999))
if err != nil {
@@ -215,11 +217,11 @@ func (*Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL bas
return result, nil
}
- if queryContext.Limit > 0 {
+ if queryContext != nil && queryContext.Limit > 0 {
statement = fmt.Sprintf("SELECT * FROM (%s) WHERE ROWNUM <= %d", statement, queryContext.Limit)
}
- if queryContext.SensitiveSchemaInfo != nil {
+ if queryContext != nil && queryContext.SensitiveSchemaInfo != nil {
for _, database := range queryContext.SensitiveSchemaInfo.DatabaseList {
if len(database.SchemaList) == 0 {
continue
diff --git a/backend/plugin/db/oracle/oracle.go b/backend/plugin/db/oracle/oracle.go
index a8f02d3ca0..bb6444c220 100644
--- a/backend/plugin/db/oracle/oracle.go
+++ b/backend/plugin/db/oracle/oracle.go
@@ -191,7 +191,9 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
// QueryConn queries a SQL statement in a given connection.
func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
// Oracle does not support transaction isolation level for read-only queries.
- queryContext.ReadOnly = false
+ if queryContext != nil {
+ queryContext.ReadOnly = false
+ }
singleSQLs, err := plsqlparser.SplitSQL(statement)
if err != nil {
@@ -227,22 +229,26 @@ func (driver *Driver) getOracleStatementWithResultLimit(stmt string, queryContex
if err != nil {
return "", err
}
- switch {
- case versionNumber < dbVersion12:
- return getStatementWithResultLimitFor11g(stmt, queryContext.Limit), nil
- default:
- res, err := getStatementWithResultLimitFor12c(stmt, queryContext.Limit)
- if err != nil {
- return "", err
+ if queryContext != nil {
+ switch {
+ case versionNumber < dbVersion12:
+ return getStatementWithResultLimitFor11g(stmt, queryContext.Limit), nil
+ default:
+ res, err := getStatementWithResultLimitFor12c(stmt, queryContext.Limit)
+ if err != nil {
+ return "", err
+ }
+ return res, nil
}
- return res, nil
}
+
+ return stmt, nil
}
func (driver *Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL base.SingleSQL, queryContext *db.QueryContext) (*v1pb.QueryResult, error) {
statement := strings.TrimRight(singleSQL.Text, " \n\t;")
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
startTime := time.Now()
randNum, err := rand.Int(rand.Reader, big.NewInt(999))
if err != nil {
@@ -264,7 +270,7 @@ func (driver *Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, single
return result, nil
}
- if queryContext.Limit > 0 {
+ if queryContext != nil && queryContext.Limit > 0 {
stmt, err := driver.getOracleStatementWithResultLimit(statement, queryContext)
if err != nil {
slog.Error("fail to add limit clause", "statement", statement, log.BBError(err))
@@ -273,7 +279,7 @@ func (driver *Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, single
statement = stmt
}
- if queryContext.SensitiveSchemaInfo != nil {
+ if queryContext != nil && queryContext.SensitiveSchemaInfo != nil {
for _, database := range queryContext.SensitiveSchemaInfo.DatabaseList {
if len(database.SchemaList) == 0 {
continue
diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.go
index f870a36047..98d056224d 100644
--- a/backend/plugin/db/pg/pg.go
+++ b/backend/plugin/db/pg/pg.go
@@ -715,9 +715,9 @@ func (*Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL bas
isSet := variableSetStmtRegexp.MatchString(statement)
isShow := variableShowStmtRegexp.MatchString(statement)
if !isSet && !isShow {
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
- } else if queryContext.Limit > 0 {
+ } else if queryContext != nil && queryContext.Limit > 0 {
statement = getStatementWithResultLimit(statement, queryContext.Limit)
}
}
diff --git a/backend/plugin/db/redis/redis.go b/backend/plugin/db/redis/redis.go
index a0b77e9777..9e636d153d 100644
--- a/backend/plugin/db/redis/redis.go
+++ b/backend/plugin/db/redis/redis.go
@@ -187,7 +187,7 @@ func (*Driver) Dump(_ context.Context, _ io.Writer, schemaOnly bool) (string, er
// QueryConn queries a SQL statement in a given connection.
func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
return nil, errors.New("MongoDB does not support EXPLAIN")
}
diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.go
index 46b11e4a17..bc981a9778 100644
--- a/backend/plugin/db/redshift/redshift.go
+++ b/backend/plugin/db/redshift/redshift.go
@@ -395,7 +395,7 @@ func (driver *Driver) GetCurrentDatabaseOwner() (string, error) {
// QueryConn queries a SQL statement in a given connection.
func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
// Datashare doesn't support read-only transactions.
- if driver.datashare {
+ if driver.datashare && queryContext != nil {
queryContext.ReadOnly = false
queryContext.ShareDB = true
}
@@ -432,9 +432,9 @@ func (*Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL bas
statement := strings.Trim(singleSQL.Text, " \n\t;")
isSet, _ := regexp.MatchString(`(?i)^SET\s+?`, statement)
if !isSet {
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
- } else if queryContext.Limit > 0 {
+ } else if queryContext != nil && queryContext.Limit > 0 {
statement = getStatementWithResultLimit(statement, queryContext.Limit)
}
}
diff --git a/backend/plugin/db/risingwave/risingwave.go b/backend/plugin/db/risingwave/risingwave.go
index d67e7c604b..39416fa2a5 100644
--- a/backend/plugin/db/risingwave/risingwave.go
+++ b/backend/plugin/db/risingwave/risingwave.go
@@ -434,9 +434,9 @@ func (*Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL bas
statement := strings.Trim(singleSQL.Text, " \n\t;")
isSet, _ := regexp.MatchString(`(?i)^SET\s+?`, statement)
if !isSet {
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
- } else if queryContext.Limit > 0 {
+ } else if queryContext != nil && queryContext.Limit > 0 {
statement = getStatementWithResultLimit(statement, queryContext.Limit)
}
}
diff --git a/backend/plugin/db/snowflake/snowflake.go b/backend/plugin/db/snowflake/snowflake.go
index 5a43429838..ecbb20652f 100644
--- a/backend/plugin/db/snowflake/snowflake.go
+++ b/backend/plugin/db/snowflake/snowflake.go
@@ -279,7 +279,9 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ db.Execut
func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
// Snowflake doesn't support READ ONLY transactions.
// https://github.com/snowflakedb/gosnowflake/blob/0450f0b16a4679b216baecd3fd6cdce739dbb683/connection.go#L166
- queryContext.ReadOnly = false
+ if queryContext != nil {
+ queryContext.ReadOnly = false
+ }
// TODO(rebelice): support multiple queries in a single statement.
var results []*v1pb.QueryResult
@@ -298,9 +300,9 @@ func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement s
func (*Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL base.SingleSQL, queryContext *db.QueryContext) (*v1pb.QueryResult, error) {
statement := strings.TrimRight(singleSQL.Text, " \n\t;")
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
- } else if queryContext.Limit > 0 {
+ } else if queryContext != nil && queryContext.Limit > 0 {
stmt, err := getStatementWithResultLimit(statement, queryContext.Limit)
if err != nil {
slog.Error("fail to add limit clause", "statement", statement, log.BBError(err))
diff --git a/backend/plugin/db/spanner/spanner.go b/backend/plugin/db/spanner/spanner.go
index d7b2cb6cc2..f308820b90 100644
--- a/backend/plugin/db/spanner/spanner.go
+++ b/backend/plugin/db/spanner/spanner.go
@@ -263,7 +263,7 @@ func getDatabaseFromDSN(dsn string) (string, error) {
// QueryConn queries a SQL statement in a given connection.
func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
return nil, errors.New("Spanner does not support EXPLAIN")
}
@@ -274,7 +274,9 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, q
var results []*v1pb.QueryResult
for _, statement := range stmts {
- statement = getStatementWithResultLimit(statement, queryContext.Limit)
+ if queryContext != nil {
+ statement = getStatementWithResultLimit(statement, queryContext.Limit)
+ }
result, err := d.querySingleSQL(ctx, statement)
if err != nil {
results = append(results, &v1pb.QueryResult{
diff --git a/backend/plugin/db/starrocks/starrocks.go b/backend/plugin/db/starrocks/starrocks.go
index e50524439f..c2e17d8139 100644
--- a/backend/plugin/db/starrocks/starrocks.go
+++ b/backend/plugin/db/starrocks/starrocks.go
@@ -216,7 +216,9 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ db.Execut
func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
// Starrocks doesn't support READ ONLY transactions.
// Error: Error 1064 (HY000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'READ' at line 1
- queryContext.ReadOnly = false
+ if queryContext != nil {
+ queryContext.ReadOnly = false
+ }
connectionID, err := getConnectionID(ctx, conn)
if err != nil {
@@ -271,14 +273,14 @@ func (driver *Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, single
statement := strings.TrimLeft(strings.TrimRight(singleSQL.Text, " \n\t;"), " \n\t")
isSet, _ := regexp.MatchString(`(?i)^SET\s+?`, statement)
if !isSet {
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
- } else if queryContext.Limit > 0 {
+ } else if queryContext != nil && queryContext.Limit > 0 {
statement = getStatementWithResultLimit(statement, queryContext.Limit)
}
}
- if queryContext.SensitiveSchemaInfo != nil {
+ if queryContext != nil && queryContext.SensitiveSchemaInfo != nil {
for _, database := range queryContext.SensitiveSchemaInfo.DatabaseList {
if len(database.SchemaList) == 0 {
continue
diff --git a/backend/plugin/db/tidb/tidb.go b/backend/plugin/db/tidb/tidb.go
index 2154f30be0..8481d1ee30 100644
--- a/backend/plugin/db/tidb/tidb.go
+++ b/backend/plugin/db/tidb/tidb.go
@@ -322,7 +322,9 @@ func isNonTransactionStatement(stmt string) bool {
func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
// TiDB doesn't support READ ONLY transactions. We have to skip the flag for it.
// https://github.com/pingcap/tidb/issues/34626
- queryContext.ReadOnly = false
+ if queryContext != nil {
+ queryContext.ReadOnly = false
+ }
connectionID, err := getConnectionID(ctx, conn)
if err != nil {
@@ -379,14 +381,14 @@ func (driver *Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, single
isSet := variableSetStmtRegexp.MatchString(statement)
isShow := variableShowStmtRegexp.MatchString(statement)
if !isSet && !isShow {
- if queryContext.Explain {
+ if queryContext != nil && queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
- } else if queryContext.Limit > 0 {
+ } else if queryContext != nil && queryContext.Limit > 0 {
statement = getStatementWithResultLimit(statement, queryContext.Limit)
}
}
- if queryContext.SensitiveSchemaInfo != nil {
+ if queryContext != nil && queryContext.SensitiveSchemaInfo != nil {
for _, database := range queryContext.SensitiveSchemaInfo.DatabaseList {
if len(database.SchemaList) == 0 {
continue
diff --git a/backend/plugin/db/util/driverutil.go b/backend/plugin/db/util/driverutil.go
index 2d53fbbcac..66584325d1 100644
--- a/backend/plugin/db/util/driverutil.go
+++ b/backend/plugin/db/util/driverutil.go
@@ -35,12 +35,16 @@ func FormatErrorWithQuery(err error, query string) error {
// Query will execute a readonly / SELECT query.
func Query(ctx context.Context, dbType storepb.Engine, conn *sql.Conn, statement string, queryContext *db.QueryContext) (*v1pb.QueryResult, error) {
// TODO(d): use a Redshift extraction for shared database.
- if dbType == storepb.Engine_REDSHIFT && queryContext.ShareDB {
+ if dbType == storepb.Engine_REDSHIFT && queryContext != nil && queryContext.ShareDB {
statement = strings.ReplaceAll(statement, fmt.Sprintf("%s.", queryContext.CurrentDatabase), "")
}
startTime := time.Now()
- tx, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: queryContext.ReadOnly})
+ readOnly := false
+ if queryContext != nil {
+ readOnly = queryContext.ReadOnly
+ }
+ tx, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: readOnly})
if err != nil {
return nil, err
}