MCPcopy Index your code
hub / github.com/bytebase/bytebase / Execute

Method Execute

backend/plugin/db/spanner/spanner.go:132–185  ·  view source on GitHub ↗

Execute executes a SQL statement.

(ctx context.Context, statement string, opts db.ExecuteOptions)

Source from the content-addressed store, hash-verified

130
131// Execute executes a SQL statement.
132func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {
133 if opts.CreateDatabase {
134 stmts, err := util.SanitizeSQL(statement)
135 if err != nil {
136 return 0, errors.Wrapf(err, "failed to sanitize %v", statement)
137 }
138 if len(stmts) == 0 {
139 return 0, errors.Errorf("expect sanitized SQLs to have at least one entry, original statement: %v", statement)
140 }
141 if !strings.HasPrefix(stmts[0], "CREATE DATABASE") {
142 return 0, errors.Errorf("expect the first entry of the sanitized SQLs to start with 'CREATE DATABASE', sql %v", stmts[0])
143 }
144 if err := d.creataDatabase(ctx, stmts[0], stmts[1:]); err != nil {
145 return 0, errors.Wrap(err, "failed to create database")
146 }
147 return 0, nil
148 }
149
150 // Parse transaction mode from the script
151 config, cleanedStatement := base.ParseTransactionConfig(statement)
152 statement = cleanedStatement
153 transactionMode := config.Mode
154
155 // Apply default when transaction mode is not specified
156 if transactionMode == common.TransactionModeUnspecified {
157 transactionMode = common.GetDefaultTransactionMode()
158 }
159
160 stmts, err := util.SanitizeSQL(statement)
161 if err != nil {
162 return 0, err
163 }
164
165 // Check if any statement is DDL
166 ddl := func() bool {
167 for _, stmt := range stmts {
168 if util.IsDDL(stmt) {
169 return true
170 }
171 }
172 return false
173 }()
174
175 // Spanner DDL is always non-transactional
176 if ddl {
177 return d.executeDDL(ctx, stmts, opts)
178 }
179
180 // Execute based on transaction mode
181 if transactionMode == common.TransactionModeOff {
182 return d.executeInAutoCommitMode(ctx, stmts, opts)
183 }
184 return d.executeInTransactionMode(ctx, stmts, opts)
185}
186
187func (d *Driver) executeDDL(ctx context.Context, stmts []string, _ db.ExecuteOptions) (int64, error) {
188 op, err := d.dbClient.UpdateDatabaseDdl(ctx, &databasepb.UpdateDatabaseDdlRequest{

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 9

creataDatabaseMethod · 0.95
executeDDLMethod · 0.95
SanitizeSQLFunction · 0.92
ParseTransactionConfigFunction · 0.92
IsDDLFunction · 0.92
ErrorfMethod · 0.80

Tested by

no test coverage detected