Completed
Push — master ( 4b2e55...537f36 )
by Edgard
16:31
created

Transaction   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 70.49%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 6
dl 0
loc 156
ccs 43
cts 61
cp 0.7049
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getIsActive() 0 4 3
A setIsolationLevel() 0 8 2
A getLevel() 0 4 1
B begin() 0 31 6
B commit() 0 22 4
B rollBack() 0 26 4
1
<?php
2
3
/**
4
 * @link http://www.yiiframework.com/
5
 * @copyright Copyright (c) 2008 Yii Software LLC
6
 * @license http://www.yiiframework.com/license/
7
 */
8
9
namespace edgardmessias\db\firebird;
10
11
use Yii;
12
use yii\base\InvalidConfigException;
13
use yii\db\Exception;
14
15
/**
16
 * Transaction represents a DB transaction.
17
 *
18
 * It is usually created by calling [[Connection::beginTransaction()]].
19
 *
20
 * The following code is a typical example of using transactions (note that some
21
 * DBMS may not support transactions):
22
 *
23
 * ~~~
24
 * $transaction = $connection->beginTransaction();
25
 * try {
26
 *     $connection->createCommand($sql1)->execute();
27
 *     $connection->createCommand($sql2)->execute();
28
 *     //.... other SQL executions
29
 *     $transaction->commit();
30
 * } catch (Exception $e) {
31
 *     $transaction->rollBack();
32
 * }
33
 * ~~~
34
 *
35
 * @property boolean $isActive Whether this transaction is active. Only an active transaction can [[commit()]]
36
 * or [[rollBack()]]. This property is read-only.
37
 * @property string $isolationLevel The transaction isolation level to use for this transaction. This can be
38
 * one of [[READ_UNCOMMITTED]], [[READ_COMMITTED]], [[REPEATABLE_READ]] and [[SERIALIZABLE]] but also a string
39
 * containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`. This property is
40
 * write-only.
41
 *
42
 * @author Edgard Lorraine Messias <[email protected]>
43
 * @since 1.0
44
 */
45
class Transaction extends \yii\db\Transaction
46
{
47
48
    /**
49
     * @var integer the nesting level of the transaction. 0 means the outermost level.
50
     */
51
    private $_level = 0;
52
53
    /**
54
     * Returns a value indicating whether this transaction is active.
55
     * @return boolean whether this transaction is active. Only an active transaction
56
     * can [[commit()]] or [[rollBack()]].
57
     */
58 6
    public function getIsActive()
59
    {
60 6
        return $this->_level > 0 && $this->db && $this->db->isActive;
61
    }
62
63
    /**
64
     * Begins a transaction.
65
     * @param string|null $isolationLevel The [isolation level][] to use for this transaction.
66
     * This can be one of [[READ_UNCOMMITTED]], [[READ_COMMITTED]], [[REPEATABLE_READ]] and [[SERIALIZABLE]] but
67
     * also a string containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`.
68
     * If not specified (`null`) the isolation level will not be set explicitly and the DBMS default will be used.
69
     *
70
     * > Note: This setting does not work for PostgreSQL, where setting the isolation level before the transaction
71
     * has no effect. You have to call [[setIsolationLevel()]] in this case after the transaction has started.
72
     *
73
     * > Note: Some DBMS allow setting of the isolation level only for the whole connection so subsequent transactions
74
     * may get the same isolation level even if you did not specify any. When using this feature
75
     * you may need to set the isolation level for all transactions explicitly to avoid conflicting settings.
76
     * At the time of this writing affected DBMS are MSSQL and SQLite.
77
     *
78
     * [isolation level]: http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels
79
     * @throws InvalidConfigException if [[db]] is `null`.
80
     */
81 6
    public function begin($isolationLevel = null)
82
    {
83 6
        if ($this->db === null) {
84
            throw new InvalidConfigException('Transaction::db must be set.');
85
        }
86 6
        $this->db->open();
87
88 6
        if ($this->_level == 0) {
89 6
            Yii::trace('Begin transaction' . ($isolationLevel ? ' with isolation level ' . $isolationLevel : ''), __METHOD__);
90
91 6
            $this->db->trigger(Connection::EVENT_BEGIN_TRANSACTION);
92 6
            if ($isolationLevel !== null) {
93 2
                $this->db->pdo->beginTransaction(false);
94 2
                $this->db->getSchema()->setTransactionIsolationLevel($isolationLevel);
95
            } else {
96 4
                $this->db->pdo->beginTransaction();
97
            }
98 6
            $this->_level = 1;
99
100 6
            return;
101
        }
102
103 1
        $schema = $this->db->getSchema();
104 1
        if ($schema->supportsSavepoint()) {
105 1
            Yii::trace('Set savepoint ' . $this->_level, __METHOD__);
106 1
            $schema->createSavepoint('LEVEL' . $this->_level);
107
        } else {
108
            Yii::info('Transaction not started: nested transaction not supported', __METHOD__);
109
        }
110 1
        $this->_level++;
111 1
    }
112
113
    /**
114
     * Commits a transaction.
115
     * @throws Exception if the transaction is not active
116
     */
117 5
    public function commit()
118
    {
119 5
        if (!$this->getIsActive()) {
120
            throw new Exception('Failed to commit transaction: transaction was inactive.');
121
        }
122
123 5
        $this->_level--;
124 5
        if ($this->_level == 0) {
125 5
            Yii::trace('Commit transaction', __METHOD__);
126 5
            $this->db->pdo->commit();
127 5
            $this->db->trigger(Connection::EVENT_COMMIT_TRANSACTION);
128 5
            return;
129
        }
130
131
        $schema = $this->db->getSchema();
132
        if ($schema->supportsSavepoint()) {
133
            Yii::trace('Release savepoint ' . $this->_level, __METHOD__);
134
            $schema->releaseSavepoint('LEVEL' . $this->_level);
135
        } else {
136
            Yii::info('Transaction not committed: nested transaction not supported', __METHOD__);
137
        }
138
    }
139
140
    /**
141
     * Rolls back a transaction.
142
     * @throws Exception if the transaction is not active
143
     */
144 3
    public function rollBack()
145
    {
146 3
        if (!$this->getIsActive()) {
147
            // do nothing if transaction is not active: this could be the transaction is committed
148
            // but the event handler to "commitTransaction" throw an exception
149
            return;
150
        }
151
152 3
        $this->_level--;
153 3
        if ($this->_level == 0) {
154 2
            Yii::trace('Roll back transaction', __METHOD__);
155 2
            $this->db->pdo->rollBack();
156 2
            $this->db->trigger(Connection::EVENT_ROLLBACK_TRANSACTION);
157 2
            return;
158
        }
159
160 1
        $schema = $this->db->getSchema();
161 1
        if ($schema->supportsSavepoint()) {
162 1
            Yii::trace('Roll back to savepoint ' . $this->_level, __METHOD__);
163 1
            $schema->rollBackSavepoint('LEVEL' . $this->_level);
164
        } else {
165
            Yii::info('Transaction not rolled back: nested transaction not supported', __METHOD__);
166
            // throw an exception to fail the outer transaction
167
            throw new Exception('Roll back failed: nested transaction not supported.');
168
        }
169 1
    }
170
171
    /**
172
     * Sets the transaction isolation level for this transaction.
173
     *
174
     * This method can be used to set the isolation level while the transaction is already active.
175
     * However this is not supported by all DBMS so you might rather specify the isolation level directly
176
     * when calling [[begin()]].
177
     * @param string $level The transaction isolation level to use for this transaction.
178
     * This can be one of [[READ_UNCOMMITTED]], [[READ_COMMITTED]], [[REPEATABLE_READ]] and [[SERIALIZABLE]] but
179
     * also a string containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`.
180
     * @throws Exception if the transaction is not active
181
     * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels
182
     */
183
    public function setIsolationLevel($level)
184
    {
185
        if (!$this->getIsActive()) {
186
            throw new Exception('Failed to set isolation level: transaction was inactive.');
187
        }
188
        Yii::trace('Setting transaction isolation level to ' . $level, __METHOD__);
189
        $this->db->getSchema()->setTransactionIsolationLevel($level);
190
    }
191
192
    /**
193
     * @return integer The current nesting level of the transaction.
194
     * @since 2.0.8
195
     */
196 4
    public function getLevel()
197
    {
198 4
        return $this->_level;
199
    }
200
}
201