1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Db |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Db\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Db\Exception\RuntimeException; |
18
|
|
|
use Phossa2\Db\Interfaces\TransactionInterface; |
19
|
|
|
use Phossa2\Db\Message\Message; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* TransactionTrait |
23
|
|
|
* |
24
|
|
|
* Implementation of TransactionInterface |
25
|
|
|
* |
26
|
|
|
* @package Phossa2\Db |
27
|
|
|
* @author Hong Zhang <[email protected]> |
28
|
|
|
* @see TransactionInterface |
29
|
|
|
* @version 2.0.0 |
30
|
|
|
* @since 2.0.0 added |
31
|
|
|
*/ |
32
|
|
|
trait TransactionTrait |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* In transaction or not |
36
|
|
|
* |
37
|
|
|
* @var bool |
38
|
|
|
* @access protected |
39
|
|
|
*/ |
40
|
|
|
protected $transaction = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
public function inTransaction()/*# : bool */ |
46
|
|
|
{ |
47
|
|
|
return $this->transaction; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
|
|
public function begin() |
54
|
|
|
{ |
55
|
|
|
$this->connect(); |
56
|
|
|
$this->transaction = true; |
57
|
|
|
$this->realBegin(); |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function commit() |
65
|
|
|
{ |
66
|
|
|
if ($this->isConnected()) { |
67
|
|
|
$this->realCommit(); |
68
|
|
|
} |
69
|
|
|
$this->transaction = false; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
|
|
public function rollback() |
77
|
|
|
{ |
78
|
|
|
if ($this->isConnected()) { |
79
|
|
|
if (!$this->inTransaction()) { |
80
|
|
|
throw new RuntimeException( |
81
|
|
|
Message::get(Message::DB_TRANSACTION_NOTIN), |
82
|
|
|
Message::DB_TRANSACTION_NOTIN |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
$this->realRollback(); |
86
|
|
|
} |
87
|
|
|
$this->transaction = false; |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Driver specific begin transaction |
93
|
|
|
* |
94
|
|
|
* @access protected |
95
|
|
|
*/ |
96
|
|
|
abstract protected function realBegin(); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Driver specific commit |
100
|
|
|
* |
101
|
|
|
* @access protected |
102
|
|
|
*/ |
103
|
|
|
abstract protected function realCommit(); |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Driver specific rollback |
107
|
|
|
* |
108
|
|
|
* @access protected |
109
|
|
|
*/ |
110
|
|
|
abstract protected function realRollback(); |
111
|
|
|
|
112
|
|
|
/* from other traits */ |
113
|
|
|
abstract public function connect(); |
114
|
|
|
abstract public function isConnected()/*# : bool */; |
115
|
|
|
} |
116
|
|
|
|