Passed
Push — int-types ( e9891a...c34c73 )
by Sam
06:45
created

MySQLTransactionManager::transactionStart()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 10
nop 2
dl 0
loc 28
rs 9.2222
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
    /**
22
     * @inherit
23
     */
24
    public function transactionStart($transactionMode = false, $sessionCharacteristics = false)
25
    {
26
        if ($transactionMode || $sessionCharacteristics) {
27
            Deprecation::notice(
28
                '4.3',
29
                '$transactionMode and $sessionCharacteristics are deprecated and will be removed in SS5'
30
            );
31
        }
32
33
        if ($this->inTransaction) {
34
            throw new DatabaseException(
35
                "Already in transaction, can't start another. Consider decorating with NestedTransactionManager."
36
            );
37
        }
38
39
        // This sets the isolation level for the NEXT transaction, not the current one.
40
        if ($transactionMode) {
41
            $this->dbConn->query('SET TRANSACTION ' . $transactionMode);
42
        }
43
44
        $this->dbConn->query('START TRANSACTION');
45
46
        if ($sessionCharacteristics) {
47
            $this->dbConn->query('SET SESSION TRANSACTION ' . $sessionCharacteristics);
48
        }
49
50
        $this->inTransaction = true;
51
        return true;
52
    }
53
54
    /**
55
     * @inherit
56
     */
57
    public function transactionEnd($chain = false)
58
    {
59
        if (!$this->inTransaction) {
60
            throw new DatabaseException("Not in transaction, can't end.");
61
        }
62
63
        if ($chain) {
64
            user_error(
65
                "transactionEnd() chain argument no longer implemented. Use NestedTransactionManager",
66
                E_USER_WARNING
67
            );
68
        }
69
70
        $this->dbConn->query('COMMIT');
71
72
        $this->inTransaction = false;
73
        return true;
74
    }
75
76
    /**
77
     * @inherit
78
     */
79
    public function transactionRollback($savepoint = null)
80
    {
81
        if (!$this->inTransaction) {
82
            throw new DatabaseException("Not in transaction, can't roll back.");
83
        }
84
85
        if ($savepoint) {
86
            $this->dbConn->query("ROLLBACK TO SAVEPOINT $savepoint");
87
        } else {
88
            $this->dbConn->query('ROLLBACK');
89
            $this->inTransaction = false;
90
        }
91
92
        return true;
93
    }
94
95
    /**
96
     * @inherit
97
     */
98
    public function transactionSavepoint($savepoint)
99
    {
100
        $this->dbConn->query("SAVEPOINT $savepoint");
101
    }
102
103
    /**
104
     * @inherit
105
     */
106
    public function transactionDepth()
107
    {
108
        return (int)$this->inTransaction;
109
    }
110
111
    public function supportsSavepoints()
112
    {
113
        return true;
114
    }
115
}
116