1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by Gorlum 31.07.2016 12:08 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace DBAL; |
8
|
|
|
|
9
|
|
|
use \classSupernova; |
10
|
|
|
use Common\GlobalContainer; |
11
|
|
|
use \db_mysql; |
12
|
|
|
|
13
|
|
|
class DbTransaction { |
14
|
|
|
|
15
|
|
|
protected $db_in_transaction = false; |
16
|
|
|
protected $transaction_id = 0; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var db_mysql $db |
20
|
|
|
*/ |
21
|
|
|
protected $db; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \SnCache $snCache |
25
|
|
|
*/ |
26
|
|
|
protected $snCache; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* DbTransaction constructor. |
30
|
|
|
* |
31
|
|
|
* @param GlobalContainer $gc |
32
|
|
|
* @param db_mysql $db |
33
|
|
|
*/ |
34
|
|
|
public function __construct($gc, $db) { |
|
|
|
|
35
|
|
|
$this->db = $db; |
36
|
|
|
$this->snCache = $db->snCache; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Эта функция проверяет статус транзакции |
41
|
|
|
* |
42
|
|
|
* Это - низкоуровневая функция. В нормальном состоянии движка её сообщения никогда не будут видны |
43
|
|
|
* |
44
|
|
|
* @param null|bool $status Должна ли быть запущена транзакция в момент проверки |
45
|
|
|
* <p>null - транзакция НЕ должна быть запущена</p> |
46
|
|
|
* <p>true - транзакция должна быть запущена - для совместимости с $for_update</p> |
47
|
|
|
* <p>false - всё равно - для совместимости с $for_update</p> |
48
|
|
|
* |
49
|
|
|
* @return bool Текущий статус транзакции |
50
|
|
|
*/ |
51
|
|
|
public function check($status = null) { |
52
|
|
|
$error_msg = false; |
53
|
|
|
if ($status && !$this->db_in_transaction) { |
54
|
|
|
$error_msg = 'No transaction started for current operation'; |
55
|
|
|
} elseif ($status === null && $this->db_in_transaction) { |
56
|
|
|
$error_msg = 'Transaction is already started'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!empty($error_msg)) { |
60
|
|
|
// TODO - Убрать позже |
61
|
|
|
print('<h1>СООБЩИТЕ ЭТО АДМИНУ: sn_db_transaction_check() - ' . $error_msg . '</h1>'); |
62
|
|
|
$backtrace = debug_backtrace(); |
63
|
|
|
array_shift($backtrace); |
64
|
|
|
pdump($backtrace); |
65
|
|
|
die($error_msg); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->db_in_transaction; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function start($level = '') { |
72
|
|
|
$this->check(null); |
73
|
|
|
|
74
|
|
|
$level ? $this->db->doSql('SET TRANSACTION ISOLATION LEVEL ' . $level) : false; |
75
|
|
|
|
76
|
|
|
$this->transaction_id++; |
77
|
|
|
$this->db->doSql('START TRANSACTION'); |
78
|
|
|
//pdump(debug_backtrace());pdie(); |
|
|
|
|
79
|
|
|
|
80
|
|
|
if (classSupernova::$gc->config->db_manual_lock_enabled) { |
81
|
|
|
classSupernova::$gc->config->db_loadItem('var_db_manually_locked'); |
|
|
|
|
82
|
|
|
classSupernova::$gc->config->db_saveItem('var_db_manually_locked', SN_TIME_SQL); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->db_in_transaction = true; |
86
|
|
|
$this->db->snCache->locatorReset(); |
87
|
|
|
$this->db->snCache->queriesReset(); |
88
|
|
|
|
89
|
|
|
return $this->transaction_id; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// TODO - move changeset data and methods somewhere |
93
|
|
|
public function commit() { |
94
|
|
|
$this->check(true); |
95
|
|
|
|
96
|
|
|
$this->db->doSql('COMMIT'); |
97
|
|
|
|
98
|
|
|
return $this->db_transaction_clear(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function rollback() { |
102
|
|
|
// TODO - вообще-то тут тоже надо проверять есть ли транзакция |
103
|
|
|
|
104
|
|
|
$this->db->doSql('ROLLBACK'); |
105
|
|
|
|
106
|
|
|
return $this->db_transaction_clear(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function db_transaction_clear() { |
110
|
|
|
$this->db->snCache->cache_lock_unset_all(); |
111
|
|
|
|
112
|
|
|
$this->db_in_transaction = false; |
113
|
|
|
$this->transaction_id++; |
114
|
|
|
|
115
|
|
|
return $this->transaction_id; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get Transaction ID for next query |
120
|
|
|
* |
121
|
|
|
* If transaction is in progress - next ID would be equal to current |
122
|
|
|
* Otherwise we should increase ID and return this value |
123
|
|
|
* |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
|
|
public function getNextQueryTransactionId() { |
127
|
|
|
return $this->check(false) ? $this->transaction_id : $this->transaction_id++; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.