Execute executes the migration, `beforeCommitTxFunc` will be called before transaction commit and after executing `statement`. Callers can use `beforeCommitTx` to do some extra work before transaction commit, like get the transaction id. Any error returned by `beforeCommitTx` will rollback the tran
(ctx context.Context, statement string, opts db.ExecuteOptions)
| 103 | // Callers can use `beforeCommitTx` to do some extra work before transaction commit, like get the transaction id. |
| 104 | // Any error returned by `beforeCommitTx` will rollback the transaction, so it is the callers' responsibility to return nil if the error occurs in `beforeCommitTx` is not fatal. |
| 105 | func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) { |
| 106 | if opts.CreateDatabase { |
| 107 | return 0, errors.New("create database is not supported for Oracle") |
| 108 | } |
| 109 | |
| 110 | // Parse transaction mode from the script |
| 111 | config, cleanedStatement := base.ParseTransactionConfig(statement) |
| 112 | statement = cleanedStatement |
| 113 | transactionMode := config.Mode |
| 114 | |
| 115 | // Apply default when transaction mode is not specified |
| 116 | if transactionMode == common.TransactionModeUnspecified { |
| 117 | transactionMode = common.GetDefaultTransactionMode() |
| 118 | } |
| 119 | |
| 120 | var commands []base.Statement |
| 121 | if len(statement) <= common.MaxSheetCheckSize { |
| 122 | // Use Oracle sql parser. |
| 123 | singleSQLs, err := plsqlparser.SplitSQL(statement) |
| 124 | if err != nil { |
| 125 | return 0, errors.Wrapf(err, "failed to split sql") |
| 126 | } |
| 127 | singleSQLs = base.FilterEmptyStatements(singleSQLs) |
| 128 | if len(singleSQLs) == 0 { |
| 129 | return 0, nil |
| 130 | } |
| 131 | commands = singleSQLs |
| 132 | } else { |
| 133 | commands = []base.Statement{ |
| 134 | { |
| 135 | Text: statement, |
| 136 | }, |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | conn, err := d.db.Conn(ctx) |
| 141 | if err != nil { |
| 142 | return 0, errors.Wrapf(err, "failed to get connection") |
| 143 | } |
| 144 | defer conn.Close() |
| 145 | |
| 146 | // Execute based on transaction mode |
| 147 | if transactionMode == common.TransactionModeOff { |
| 148 | return d.executeInAutoCommitMode(ctx, conn, commands, opts) |
| 149 | } |
| 150 | return d.executeInTransactionMode(ctx, conn, commands, opts) |
| 151 | } |
| 152 | |
| 153 | // executeInTransactionMode executes statements within a single transaction |
| 154 | func (*Driver) executeInTransactionMode(ctx context.Context, conn *sql.Conn, commands []base.Statement, opts db.ExecuteOptions) (int64, error) { |
nothing calls this directly
no test coverage detected