MCPcopy Create free account
hub / github.com/bytebase/bytebase / QueryConn

Method QueryConn

backend/plugin/db/cockroachdb/cockroachdb.go:611–700  ·  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

609
610// QueryConn queries a SQL statement in a given connection.
611func (*Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext db.QueryContext) ([]*v1pb.QueryResult, error) {
612 singleSQLs, err := crdbparser.SplitSQLStatement(statement)
613 if err != nil {
614 return nil, err
615 }
616 if len(singleSQLs) == 0 {
617 return nil, nil
618 }
619
620 var results []*v1pb.QueryResult
621 for _, singleSQL := range singleSQLs {
622 statement := singleSQL
623 if queryContext.Explain {
624 statement = fmt.Sprintf("EXPLAIN %s", statement)
625 } else if queryContext.Limit > 0 {
626 statement = getStatementWithResultLimit(statement, queryContext.Limit)
627 }
628
629 _, allQuery, err := base.ValidateSQLForEditor(storepb.Engine_POSTGRES, statement)
630 if err != nil {
631 return nil, err
632 }
633
634 // Sanitize the schema name by escaping any quotes.
635 safeSchemeName := strings.ReplaceAll(queryContext.Schema, "\"", "\"\"")
636
637 // If the queryContext.Schema is not empty, set the search path for the database connection to the specified schema.
638 if queryContext.Schema != "" {
639 if err := crdb.Execute(func() error {
640 _, err := conn.ExecContext(ctx, fmt.Sprintf(`SET search_path TO "%s";`, safeSchemeName)) // NOSONAR(go:S2077) safeSchemeName is sanitized by escaping double quotes above
641 return err
642 }); err != nil {
643 return nil, err
644 }
645 }
646
647 startTime := time.Now()
648 queryResult, err := func() (*v1pb.QueryResult, error) {
649 if allQuery {
650 var r *v1pb.QueryResult
651 if err := crdb.Execute(func() error {
652 rows, err := conn.QueryContext(ctx, statement)
653 if err != nil {
654 return err
655 }
656 defer rows.Close()
657 r, err = util.RowsToQueryResult(rows, makeValueByTypeName, convertValue, queryContext.MaximumSQLResultSize)
658 if err != nil {
659 return err
660 }
661 err = rows.Err()
662 return err
663 }); err != nil {
664 return nil, err
665 }
666 return r, nil
667 }
668

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

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

Tested by

no test coverage detected