QueryConn queries a SQL statement in a given connection.
(ctx context.Context, conn *sql.Conn, statement string, queryContext db.QueryContext)
| 609 | |
| 610 | // QueryConn queries a SQL statement in a given connection. |
| 611 | func (*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 |
nothing calls this directly
no test coverage detected