Execute will execute the statement. For CREATE DATABASE statement, some types of databases such as Postgres will not use transactions to execute the statement but will still use transactions to execute the rest of statements.
(ctx context.Context, statement string, opts db.ExecuteOptions)
| 156 | // Execute will execute the statement. For CREATE DATABASE statement, some types of databases such as Postgres |
| 157 | // will not use transactions to execute the statement but will still use transactions to execute the rest of statements. |
| 158 | func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) { |
| 159 | if d.datashare { |
| 160 | return 0, errors.Errorf("datashare database cannot be updated") |
| 161 | } |
| 162 | if opts.CreateDatabase { |
| 163 | if err := d.createDatabaseExecute(ctx, statement); err != nil { |
| 164 | return 0, err |
| 165 | } |
| 166 | return 0, nil |
| 167 | } |
| 168 | |
| 169 | // Parse transaction mode from the script |
| 170 | config, cleanedStatement := base.ParseTransactionConfig(statement) |
| 171 | statement = cleanedStatement |
| 172 | transactionMode := config.Mode |
| 173 | |
| 174 | // Apply default when transaction mode is not specified |
| 175 | if transactionMode == common.TransactionModeUnspecified { |
| 176 | transactionMode = common.GetDefaultTransactionMode() |
| 177 | } |
| 178 | |
| 179 | var commands []base.Statement |
| 180 | oneshot := true |
| 181 | if len(statement) <= common.MaxSheetCheckSize { |
| 182 | singleSQLs, err := redshiftparser.SplitSQL(statement) |
| 183 | if err != nil { |
| 184 | return 0, err |
| 185 | } |
| 186 | commands = base.FilterEmptyStatements(singleSQLs) |
| 187 | if len(commands) <= common.MaximumCommands { |
| 188 | oneshot = false |
| 189 | } |
| 190 | } |
| 191 | if oneshot { |
| 192 | commands = []base.Statement{ |
| 193 | { |
| 194 | Text: statement, |
| 195 | }, |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // Execute based on transaction mode |
| 200 | if transactionMode == common.TransactionModeOff { |
| 201 | return d.executeInAutoCommitMode(ctx, commands, opts) |
| 202 | } |
| 203 | return d.executeInTransactionMode(ctx, commands, opts) |
| 204 | } |
| 205 | |
| 206 | func (d *Driver) createDatabaseExecute(ctx context.Context, statement string) error { |
| 207 | databaseName, err := getDatabaseInCreateDatabaseStatement(statement) |
nothing calls this directly
no test coverage detected