1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Adds transactions in DAO class. |
7
|
|
|
* Ubiquity\orm\traits$DAOTransactionsTrait |
8
|
|
|
* This class is part of Ubiquity |
9
|
|
|
* |
10
|
|
|
* @author jcheron <[email protected]> |
11
|
|
|
* @version 1.0.1 |
12
|
|
|
* @property \Ubiquity\db\Database $db |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
trait DAOTransactionsTrait { |
16
|
|
|
|
17
|
|
|
abstract public static function getDatabase($offset = 'default'); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Initiates a transaction |
21
|
|
|
* |
22
|
|
|
* @param string $offset the database offset to use |
23
|
|
|
* @return boolean true on success or false on failure |
24
|
|
|
*/ |
25
|
16 |
|
public static function beginTransaction($offset = 'default') { |
26
|
16 |
|
self::getDatabase ( $offset )->beginTransaction (); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Commits a transaction |
31
|
|
|
* |
32
|
|
|
* @param string $offset the database offset to use |
33
|
|
|
* @return boolean true on success or false on failure |
34
|
|
|
*/ |
35
|
8 |
|
public static function commit($offset = 'default') { |
36
|
8 |
|
return self::getDatabase ( $offset )->commit (); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Commits nested transactions up to level $transactionLevel |
41
|
|
|
* |
42
|
|
|
* @param int $transactionLevel |
43
|
|
|
* @param string $offset the database offset to use |
44
|
|
|
* @return boolean true on success or false on failure |
45
|
|
|
*/ |
46
|
|
|
public static function commitToLevel($transactionLevel, $offset = 'default') { |
47
|
|
|
return self::getDatabase ( $offset )->commitToLevel ( $transactionLevel ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Commits all nested transactions (up to level 0) |
52
|
|
|
* |
53
|
|
|
* @param string $offset the database offset to use |
54
|
|
|
* |
55
|
|
|
* @return boolean true on success or false on failure |
56
|
|
|
*/ |
57
|
2 |
|
public static function commitAll($offset = 'default') { |
58
|
2 |
|
return self::getDatabase ( $offset )->commitAll (); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Rolls back a transaction |
63
|
|
|
* |
64
|
|
|
* @param string $offset the database offset to use |
65
|
|
|
* @return boolean true on success or false on failure |
66
|
|
|
*/ |
67
|
4 |
|
public static function rollBack($offset = 'default') { |
68
|
4 |
|
return self::getDatabase ( $offset )->rollBack (); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Rolls back nested transactions up to level $transactionLevel |
73
|
|
|
* |
74
|
|
|
* @param int $transactionLevel |
75
|
|
|
* @param string $offset the database offset to use |
76
|
|
|
* @return boolean true on success or false on failure |
77
|
|
|
*/ |
78
|
|
|
public static function rollBackToLevel($transactionLevel, $offset = 'default') { |
79
|
|
|
return self::getDatabase ( $offset )->rollBackToLevel ( $transactionLevel ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Rolls back all nested transactions (up to level 0) |
84
|
|
|
* |
85
|
|
|
* @param string $offset the database offset to use |
86
|
|
|
* @return boolean true on success or false on failure |
87
|
|
|
*/ |
88
|
2 |
|
public static function rollBackAll($offset = 'default') { |
89
|
2 |
|
return self::getDatabase ( $offset )->rollBackAll (); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Call a callback with an array of parameters in a transaction |
94
|
|
|
* |
95
|
|
|
* @param callable $callback |
96
|
|
|
* @param string $offset the database offset to use |
97
|
|
|
* @param mixed ...$parameters |
98
|
|
|
* @throws \Exception |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public static function callInTransaction($callback, $offset, ...$parameters) { |
102
|
|
|
return self::getDatabase ( $offset )->callInTransaction ( $callback, $offset, ...$parameters ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Sets the isolation level for transactions. |
107
|
|
|
* @param string $offset |
108
|
|
|
* @param string $isolationLevel |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public static function setIsolationLevel(string $offset='default',$isolationLevel='READ COMMITTED') { |
112
|
|
|
return self::getDatabase($offset)->setIsolationLevel($isolationLevel); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|