| 1 | #include "duckdb/execution/operator/helper/physical_transaction.hpp" | 
|---|---|
| 2 | #include "duckdb/main/client_context.hpp" | 
| 3 | |
| 4 | using namespace duckdb; | 
| 5 | using namespace std; | 
| 6 | |
| 7 | void PhysicalTransaction::GetChunkInternal(ClientContext &context, DataChunk &chunk, PhysicalOperatorState *state) { | 
| 8 | switch (info->type) { | 
| 9 | case TransactionType::BEGIN_TRANSACTION: { | 
| 10 | if (context.transaction.IsAutoCommit()) { | 
| 11 | // start the active transaction | 
| 12 | // if autocommit is active, we have already called | 
| 13 | // BeginTransaction by setting autocommit to false we | 
| 14 | // prevent it from being closed after this query, hence | 
| 15 | // preserving the transaction context for the next query | 
| 16 | context.transaction.SetAutoCommit(false); | 
| 17 | } else { | 
| 18 | throw TransactionException( "cannot start a transaction within a transaction"); | 
| 19 | } | 
| 20 | break; | 
| 21 | } | 
| 22 | case TransactionType::COMMIT: { | 
| 23 | if (context.transaction.IsAutoCommit()) { | 
| 24 | throw TransactionException( "cannot commit - no transaction is active"); | 
| 25 | } else { | 
| 26 | // explicitly commit the current transaction | 
| 27 | context.transaction.Commit(); | 
| 28 | } | 
| 29 | break; | 
| 30 | } | 
| 31 | case TransactionType::ROLLBACK: { | 
| 32 | if (context.transaction.IsAutoCommit()) { | 
| 33 | throw TransactionException( "cannot rollback - no transaction is active"); | 
| 34 | } else { | 
| 35 | // explicitly rollback the current transaction | 
| 36 | context.transaction.Rollback(); | 
| 37 | } | 
| 38 | break; | 
| 39 | } | 
| 40 | default: | 
| 41 | throw NotImplementedException( "Unrecognized transaction type!"); | 
| 42 | } | 
| 43 | state->finished = true; | 
| 44 | } | 
| 45 | 
