(ctx context.Context, statement string)
| 14 | ) |
| 15 | |
| 16 | func (d *Driver) CountAffectedRows(ctx context.Context, statement string) (int64, error) { |
| 17 | if d.dbType == storepb.Engine_OCEANBASE { |
| 18 | return countAffectedRowsForOceanBase(ctx, d.db, statement) |
| 19 | } |
| 20 | |
| 21 | explainSQL := fmt.Sprintf("EXPLAIN %s", statement) |
| 22 | rows, err := d.db.QueryContext(ctx, explainSQL) |
| 23 | if err != nil { |
| 24 | return 0, err |
| 25 | } |
| 26 | defer rows.Close() |
| 27 | |
| 28 | // mysql> explain delete from td; |
| 29 | // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ |
| 30 | // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | |
| 31 | // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ |
| 32 | // | 1 | DELETE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL | |
| 33 | // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ |
| 34 | // |
| 35 | // mysql> explain insert into td select * from td; |
| 36 | // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+ |
| 37 | // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | |
| 38 | // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+ |
| 39 | // | 1 | INSERT | td | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | |
| 40 | // | 1 | SIMPLE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using temporary | |
| 41 | // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+ |
| 42 | columns, err := rows.Columns() |
| 43 | if err != nil { |
| 44 | return 0, err |
| 45 | } |
| 46 | rowsIndex, ok := util.GetColumnIndex(columns, "rows") |
| 47 | if !ok { |
| 48 | return 0, nil |
| 49 | } |
| 50 | for rows.Next() { |
| 51 | scanArgs := make([]any, len(columns)) |
| 52 | for i := range scanArgs { |
| 53 | var unused any |
| 54 | scanArgs[i] = &unused |
| 55 | } |
| 56 | var rowsColumn sql.NullInt64 |
| 57 | scanArgs[rowsIndex] = &rowsColumn |
| 58 | if err := rows.Scan(scanArgs...); err != nil { |
| 59 | return 0, err |
| 60 | } |
| 61 | |
| 62 | if rowsColumn.Valid { |
| 63 | return rowsColumn.Int64, nil |
| 64 | } |
| 65 | } |
| 66 | if err := rows.Err(); err != nil { |
| 67 | return 0, err |
| 68 | } |
| 69 | return 0, nil |
| 70 | } |
| 71 | |
| 72 | func countAffectedRowsForOceanBase(ctx context.Context, sqlDB *sql.DB, dml string) (int64, error) { |
| 73 | explainSQL := fmt.Sprintf("EXPLAIN FORMAT=JSON %s", dml) |
nothing calls this directly
no test coverage detected