Passed
Push — programming-is-horrible ( 2233ca...483908 )
by Sam
08:19
created

MySQLTransactionManager::transactionEnd()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Connect;
4
5
use SilverStripe\Dev\Deprecation;
6
7
/**
8
 * TransactionManager that executes MySQL-compatible transaction control queries
9
 */
10
class MySQLTransactionManager implements TransactionManager
11
{
12
    protected $dbConn;
13
14
    protected $inTransaction = false;
15
16
    public function __construct(Database $dbConn)
17
    {
18
        $this->dbConn = $dbConn;
19
    }
20
21
    public function transactionStart($transactionMode = false, $sessionCharacteristics = false)
22
    {
23
        if ($transactionMode || $sessionCharacteristics) {
24
            Deprecation::notice(
25
                '4.4',
26
                '$transactionMode and $sessionCharacteristics are deprecated and will be removed in SS5'
27
            );
28
        }
29
30
        if ($this->inTransaction) {
31
            throw new DatabaseException(
32
                "Already in transaction, can't start another. Consider decorating with NestedTransactionManager."
33
            );
34
        }
35
36
        // This sets the isolation level for the NEXT transaction, not the current one.
37
        if ($transactionMode) {
38
            $this->dbConn->query('SET TRANSACTION ' . $transactionMode);
39
        }
40
41
        $this->dbConn->query('START TRANSACTION');
42
43
        if ($sessionCharacteristics) {
44
            $this->dbConn->query('SET SESSION TRANSACTION ' . $sessionCharacteristics);
45
        }
46
47
        $this->inTransaction = true;
48
        return true;
49
    }
50
51
    public function transactionEnd($chain = false)
52
    {
53
        if (!$this->inTransaction) {
54
            throw new DatabaseException("Not in transaction, can't end.");
55
        }
56
57
        if ($chain) {
58
            user_error(
59
                "transactionEnd() chain argument no longer implemented. Use NestedTransactionManager",
60
                E_USER_WARNING
61
            );
62
        }
63
64
        $this->dbConn->query('COMMIT');
65
66
        $this->inTransaction = false;
67
        return true;
68
    }
69
70
    public function transactionRollback($savepoint = null)
71
    {
72
        if (!$this->inTransaction) {
73
            throw new DatabaseException("Not in transaction, can't roll back.");
74
        }
75
76
        if ($savepoint) {
77
            $this->dbConn->query("ROLLBACK TO SAVEPOINT $savepoint");
78
        } else {
79
            $this->dbConn->query('ROLLBACK');
80
            $this->inTransaction = false;
81
        }
82
83
        return true;
84
    }
85
86
    public function transactionSavepoint($savepoint)
87
    {
88
        $this->dbConn->query("SAVEPOINT $savepoint");
89
    }
90
91
    public function transactionDepth()
92
    {
93
        return (int)$this->inTransaction;
94
    }
95
96
    public function supportsSavepoints()
97
    {
98
        return true;
99
    }
100
}
101