MCPcopy Index your code
hub / github.com/bytebase/bytebase / QueryConn

Method QueryConn

backend/plugin/db/redshift/redshift.go:363–445  ·  view source on GitHub ↗

QueryConn queries a SQL statement in a given connection.

(ctx context.Context, conn *sql.Conn, statement string, queryContext db.QueryContext)

Source from the content-addressed store, hash-verified

361
362// QueryConn queries a SQL statement in a given connection.
363func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext db.QueryContext) ([]*v1pb.QueryResult, error) {
364 singleSQLs, err := redshiftparser.SplitSQL(statement)
365 if err != nil {
366 return nil, err
367 }
368 singleSQLs = base.FilterEmptyStatements(singleSQLs)
369 if len(singleSQLs) == 0 {
370 return nil, nil
371 }
372
373 // Sanitize the schema name by escaping any quotes.
374 safeSchemeName := strings.ReplaceAll(queryContext.Schema, "\"", "\"\"")
375
376 // If the queryContext.Schema is not empty, set the search path for the database connection to the specified schema.
377 // Reference: https://docs.aws.amazon.com/redshift/latest/dg/r_search_path.html
378 if queryContext.Schema != "" {
379 if _, err := conn.ExecContext(ctx, fmt.Sprintf(`set search_path to "%s";`, safeSchemeName)); err != nil { // NOSONAR(go:S2077) safeSchemeName is sanitized by escaping double quotes above
380 return nil, err
381 }
382 }
383
384 var results []*v1pb.QueryResult
385 for _, singleSQL := range singleSQLs {
386 statement := singleSQL.Text
387 if d.datashare {
388 statement = strings.ReplaceAll(statement, fmt.Sprintf("%s.", d.databaseName), "")
389 }
390 if queryContext.Explain {
391 statement = fmt.Sprintf("EXPLAIN %s", statement)
392 } else if queryContext.Limit > 0 {
393 statement = getStatementWithResultLimit(statement, queryContext.Limit)
394 }
395
396 _, allQuery, err := base.ValidateSQLForEditor(storepb.Engine_REDSHIFT, statement)
397 if err != nil {
398 return nil, err
399 }
400 startTime := time.Now()
401 queryResult, err := func() (*v1pb.QueryResult, error) {
402 if allQuery {
403 rows, err := conn.QueryContext(ctx, statement)
404 if err != nil {
405 return nil, err
406 }
407 defer rows.Close()
408 r, err := util.RowsToQueryResult(rows, makeValueByTypeName, convertValue, queryContext.MaximumSQLResultSize)
409 if err != nil {
410 return nil, err
411 }
412 if err := rows.Err(); err != nil {
413 return nil, err
414 }
415 return r, nil
416 }
417
418 sqlResult, err := conn.ExecContext(ctx, statement)
419 if err != nil {
420 return nil, err

Callers

nothing calls this directly

Implementers 15

MockDriverbackend/plugin/advisor/utils_for_tests
Driverbackend/plugin/db/bigquery/bigquery.go
Driverbackend/plugin/db/mongodb/mongodb.go
Driverbackend/plugin/db/trino/trino.go
Driverbackend/plugin/db/redshift/redshift.go
Driverbackend/plugin/db/oracle/oracle.go
Driverbackend/plugin/db/dynamodb/dynamodb.go
Driverbackend/plugin/db/cosmosdb/cosmosdb.go
Driverbackend/plugin/db/spanner/spanner.go
Driverbackend/plugin/db/mssql/mssql.go
Driverbackend/plugin/db/mysql/mysql.go
Driverbackend/plugin/db/pg/pg.go

Calls 10

FilterEmptyStatementsFunction · 0.92
ValidateSQLForEditorFunction · 0.92
RowsToQueryResultFunction · 0.92
BBErrorFunction · 0.92
BuildAffectedRowsResultFunction · 0.92
QueryContextMethod · 0.80
InfoMethod · 0.80
CloseMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected