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