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