QueryConn queries a SQL statement in a given connection.
(ctx context.Context, conn *sql.Conn, statement string, queryContext db.QueryContext)
| 509 | |
| 510 | // QueryConn queries a SQL statement in a given connection. |
| 511 | func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext db.QueryContext) ([]*v1pb.QueryResult, error) { |
| 512 | singleSQLs, err := base.SplitMultiSQL(storepb.Engine_MYSQL, statement) |
| 513 | if err != nil { |
| 514 | return nil, err |
| 515 | } |
| 516 | singleSQLs = base.FilterEmptyStatements(singleSQLs) |
| 517 | if len(singleSQLs) == 0 { |
| 518 | return nil, nil |
| 519 | } |
| 520 | |
| 521 | connectionID, err := getConnectionID(ctx, conn) |
| 522 | if err != nil { |
| 523 | return nil, err |
| 524 | } |
| 525 | slog.Debug("connectionID", slog.String("connectionID", connectionID)) |
| 526 | |
| 527 | var results []*v1pb.QueryResult |
| 528 | for _, singleSQL := range singleSQLs { |
| 529 | statement := singleSQL.Text |
| 530 | if queryContext.Explain { |
| 531 | statement = fmt.Sprintf("EXPLAIN %s", statement) |
| 532 | } else if queryContext.Limit > 0 { |
| 533 | statement = getStatementWithResultLimit(statement, queryContext.Limit) |
| 534 | } |
| 535 | sqlWithBytebaseAppComment := util.MySQLPrependBytebaseAppComment(statement) |
| 536 | |
| 537 | _, allQuery, err := base.ValidateSQLForEditor(storepb.Engine_MYSQL, statement) |
| 538 | if err != nil { |
| 539 | slog.Error("failed to validate sql", slog.String("statement", statement), log.BBError(err)) |
| 540 | allQuery = true |
| 541 | } |
| 542 | startTime := time.Now() |
| 543 | queryResult, err := func() (*v1pb.QueryResult, error) { |
| 544 | if allQuery { |
| 545 | rows, err := conn.QueryContext(ctx, sqlWithBytebaseAppComment) |
| 546 | if err != nil { |
| 547 | return nil, err |
| 548 | } |
| 549 | defer rows.Close() |
| 550 | r, err := util.RowsToQueryResult(rows, makeValueByTypeName, convertValue, queryContext.MaximumSQLResultSize) |
| 551 | if err != nil { |
| 552 | return nil, err |
| 553 | } |
| 554 | if err := rows.Err(); err != nil { |
| 555 | return nil, err |
| 556 | } |
| 557 | return r, nil |
| 558 | } |
| 559 | |
| 560 | sqlResult, err := conn.ExecContext(ctx, statement) |
| 561 | if err != nil { |
| 562 | return nil, err |
| 563 | } |
| 564 | affectedRows, err := sqlResult.RowsAffected() |
| 565 | if err != nil { |
| 566 | slog.Info("rowsAffected returns error", log.BBError(err)) |
| 567 | } |
| 568 | return util.BuildAffectedRowsResult(affectedRows, nil), nil |
nothing calls this directly
no test coverage detected