Query sends a query to the server and returns a Rows to read the results. Only errors encountered sending the query and initializing Rows will be returned. Err() on the returned Rows must be checked after the Rows is closed to determine if the query executed successfully. The returned Rows must be
(ctx context.Context, sql string, args ...any)
| 749 | // QueryResultFormatsByOID may be used as the first args to control exactly how the query is executed. This is rarely |
| 750 | // needed. See the documentation for those types for details. |
| 751 | func (c *Conn) Query(ctx context.Context, sql string, args ...any) (Rows, error) { |
| 752 | if c.queryTracer != nil { |
| 753 | ctx = c.queryTracer.TraceQueryStart(ctx, c, TraceQueryStartData{SQL: sql, Args: args}) |
| 754 | } |
| 755 | |
| 756 | if err := c.deallocateInvalidatedCachedStatements(ctx); err != nil { |
| 757 | if c.queryTracer != nil { |
| 758 | c.queryTracer.TraceQueryEnd(ctx, c, TraceQueryEndData{Err: err}) |
| 759 | } |
| 760 | return &baseRows{err: err, closed: true}, err |
| 761 | } |
| 762 | |
| 763 | var resultFormats QueryResultFormats |
| 764 | var resultFormatsByOID QueryResultFormatsByOID |
| 765 | mode := c.config.DefaultQueryExecMode |
| 766 | var queryRewriter QueryRewriter |
| 767 | |
| 768 | optionLoop: |
| 769 | for len(args) > 0 { |
| 770 | switch arg := args[0].(type) { |
| 771 | case QueryResultFormats: |
| 772 | resultFormats = arg |
| 773 | args = args[1:] |
| 774 | case QueryResultFormatsByOID: |
| 775 | resultFormatsByOID = arg |
| 776 | args = args[1:] |
| 777 | case QueryExecMode: |
| 778 | mode = arg |
| 779 | args = args[1:] |
| 780 | case QueryRewriter: |
| 781 | queryRewriter = arg |
| 782 | args = args[1:] |
| 783 | default: |
| 784 | break optionLoop |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | if queryRewriter != nil { |
| 789 | var err error |
| 790 | originalSQL := sql |
| 791 | originalArgs := args |
| 792 | sql, args, err = queryRewriter.RewriteQuery(ctx, c, sql, args) |
| 793 | if err != nil { |
| 794 | rows := c.getRows(ctx, originalSQL, originalArgs) |
| 795 | err = fmt.Errorf("rewrite query failed: %w", err) |
| 796 | rows.fatal(err) |
| 797 | return rows, err |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | // Bypass any statement caching. |
| 802 | if sql == "" { |
| 803 | mode = QueryExecModeSimpleProtocol |
| 804 | } |
| 805 | |
| 806 | c.eqb.reset() |
| 807 | rows := c.getRows(ctx, sql, args) |
| 808 |
no test coverage detected