Execute executes a SQL statement.
(ctx context.Context, statement string, opts db.ExecuteOptions)
| 130 | |
| 131 | // Execute executes a SQL statement. |
| 132 | func (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 | |
| 187 | func (d *Driver) executeDDL(ctx context.Context, stmts []string, _ db.ExecuteOptions) (int64, error) { |
| 188 | op, err := d.dbClient.UpdateDatabaseDdl(ctx, &databasepb.UpdateDatabaseDdlRequest{ |
nothing calls this directly
no test coverage detected