MCPcopy Create free account
hub / github.com/bytebase/bytebase / CountAffectedRows

Method CountAffectedRows

backend/plugin/db/mysql/stats.go:16–70  ·  view source on GitHub ↗
(ctx context.Context, statement string)

Source from the content-addressed store, hash-verified

14)
15
16func (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
72func countAffectedRowsForOceanBase(ctx context.Context, sqlDB *sql.DB, dml string) (int64, error) {
73 explainSQL := fmt.Sprintf("EXPLAIN FORMAT=JSON %s", dml)

Callers

nothing calls this directly

Implementers 15

MockDriverbackend/plugin/advisor/utils_for_tests
Driverbackend/plugin/db/bigquery/bigquery.go
Driverbackend/plugin/db/mongodb/mongodb.go
Driverbackend/plugin/db/trino/trino.go
Driverbackend/plugin/db/redshift/redshift.go
Driverbackend/plugin/db/oracle/oracle.go
Driverbackend/plugin/db/dynamodb/dynamodb.go
Driverbackend/plugin/db/cosmosdb/cosmosdb.go
Driverbackend/plugin/db/spanner/spanner.go
Driverbackend/plugin/db/mssql/mssql.go
Driverbackend/plugin/db/mysql/mysql.go
Driverbackend/plugin/db/pg/pg.go

Calls 7

GetColumnIndexFunction · 0.92
QueryContextMethod · 0.80
ColumnsMethod · 0.80
ScanMethod · 0.80
CloseMethod · 0.65
NextMethod · 0.45

Tested by

no test coverage detected