1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\PgSQL; |
4
|
|
|
|
5
|
|
|
use Dazzle\Event\BaseEventEmitter; |
6
|
|
|
use Dazzle\Promise\Promise; |
7
|
|
|
use Dazzle\Promise\PromiseInterface; |
8
|
|
|
use Dazzle\Throwable\Exception\Runtime\ExecutionException; |
9
|
|
|
|
10
|
|
|
class Transaction extends BaseEventEmitter implements TransactionInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var DatabaseInterface |
14
|
|
|
*/ |
15
|
|
|
protected $database; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var mixed[] |
19
|
|
|
*/ |
20
|
|
|
protected $queue; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $open; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param DatabaseInterface $database |
29
|
|
|
*/ |
30
|
|
|
public function __construct(DatabaseInterface $database) |
31
|
|
|
{ |
32
|
|
|
$this->database = $database; |
33
|
|
|
$this->queue = []; |
34
|
|
|
$this->open = true; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @override |
39
|
|
|
* @inheritDoc |
40
|
|
|
*/ |
41
|
|
|
public function isOpen() |
42
|
|
|
{ |
43
|
|
|
return $this->open; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @override |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
|
|
public function query($sql, $sqlParams = []) |
51
|
|
|
{ |
52
|
|
|
if (!$this->isOpen()) |
53
|
|
|
{ |
54
|
|
|
return Promise::doReject(new ExecutionException('This transaction is no longer open.')); |
55
|
|
|
} |
56
|
|
|
// TODO |
57
|
|
|
return Promise::doReject(new ExecutionException('Not yet implemented.')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @override |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
|
|
public function execute($sql, $sqlParams = []) |
65
|
|
|
{ |
66
|
|
|
// TODO |
67
|
|
|
return $this->query($sql, $sqlParams)->then(function($command) { |
68
|
|
|
return $command->affectedRows; |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @override |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
|
|
public function commit() |
77
|
|
|
{ |
78
|
|
|
if (!$this->isOpen()) |
79
|
|
|
{ |
80
|
|
|
return Promise::doReject(new ExecutionException('This transaction is no longer open.')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$promise = new Promise(); |
84
|
|
|
|
85
|
|
|
$this->on('error', function ($trans, $err) use ($promise) { |
86
|
|
|
return $promise->reject($err); |
87
|
|
|
}); |
88
|
|
|
$this->on('success', function ($trans) use ($promise) { |
|
|
|
|
89
|
|
|
return $promise->resolve(); |
90
|
|
|
}); |
91
|
|
|
|
92
|
|
|
$this->open = false; |
93
|
|
|
$this->emit('commit', [ $this, $this->queue ]); |
94
|
|
|
$this->queue = []; |
95
|
|
|
|
96
|
|
|
return $promise; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @override |
101
|
|
|
* @inheritDoc |
102
|
|
|
*/ |
103
|
|
|
public function rollback() |
104
|
|
|
{ |
105
|
|
|
if (!$this->isOpen()) |
106
|
|
|
{ |
107
|
|
|
return Promise::doReject(new ExecutionException('This transaction is no longer open.')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->open = false; |
111
|
|
|
$this->emit('rollback', [ $this ]); |
112
|
|
|
$this->queue = []; |
113
|
|
|
|
114
|
|
|
return Promise::doResolve(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.