1
|
|
|
<?php |
2
|
|
|
namespace Dazzle\PgSQL\Transaction; |
3
|
|
|
|
4
|
|
|
use Dazzle\Event\BaseEventEmitter; |
5
|
|
|
use Dazzle\Event\EventEmitterInterface; |
6
|
|
|
use Dazzle\PgSQL\Result\CommandResultStatement; |
7
|
|
|
use Dazzle\Promise\Promise; |
8
|
|
|
use Dazzle\PgSQL\Connection\ConnectorInterface; |
9
|
|
|
|
10
|
|
|
class Transaction extends BaseEventEmitter implements TransactionInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ConnectorInterface |
14
|
|
|
*/ |
15
|
|
|
protected $connector; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $open; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param ConnectorInterface $connector |
24
|
|
|
* @param EventEmitterInterface $emitter |
25
|
|
|
*/ |
26
|
|
|
public function __construct(ConnectorInterface $connector, EventEmitterInterface $emitter) |
27
|
|
|
{ |
28
|
|
|
$this->connector = $connector; |
29
|
|
|
$this->emitter = $emitter; |
|
|
|
|
30
|
|
|
$this->open = false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @override |
35
|
|
|
* @inheritDoc |
36
|
|
|
*/ |
37
|
|
|
public function isOpen() |
38
|
|
|
{ |
39
|
|
|
return $this->open; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @override |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
|
|
public function query($sql, array $sqlParams = []) |
47
|
|
|
{ |
48
|
|
|
return $this->connector->query($sql, $sqlParams); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @override |
53
|
|
|
* @inheritDoc |
54
|
|
|
*/ |
55
|
|
|
public function execute($sql, array $sqlParams = []) |
56
|
|
|
{ |
57
|
|
|
return $this->connector->execute($sql, $sqlParams); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function begin() |
61
|
|
|
{ |
62
|
|
|
if ($this->isOpen()) { |
63
|
|
|
return Promise::doResolve($this); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->connector->execute('BEGIN')->success(function (CommandResultStatement $result) { |
67
|
|
|
if ($result) { |
68
|
|
|
$this->emitter->emit('transaction:begin'); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @override |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function commit() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
return $this->connector->execute('COMMIT')->success(function (CommandResultStatement $result) { |
82
|
|
|
if ($result) { |
83
|
|
|
$this->emitter->emit('transaction:end'); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $result; |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @override |
92
|
|
|
* @inheritDoc |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function rollback() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
return $this->connector->execute('ROLLBACK')->success(function (CommandResultStatement $result) { |
97
|
|
|
if ($result) { |
98
|
|
|
$this->emitter->emit('transaction:end'); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $result; |
102
|
|
|
}); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.