1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\PgSQL; |
4
|
|
|
|
5
|
|
|
use Dazzle\Event\BaseEventEmitter; |
6
|
|
|
use Dazzle\Loop\LoopAwareTrait; |
7
|
|
|
use Dazzle\Loop\LoopInterface; |
8
|
|
|
use Dazzle\PgSQL\Support\Transaction\TransactionBox; |
9
|
|
|
use Dazzle\PgSQL\Support\Transaction\TransactionBoxInterface; |
10
|
|
|
use Dazzle\Promise\Promise; |
11
|
|
|
use Dazzle\Promise\PromiseInterface; |
12
|
|
|
use Dazzle\Throwable\Exception\Runtime\ExecutionException; |
13
|
|
|
|
14
|
|
|
class Database extends BaseEventEmitter implements DatabaseInterface |
15
|
|
|
{ |
16
|
|
|
use LoopAwareTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
const STATE_INIT = 0; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
const STATE_CONNECT_PENDING = 4; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
const STATE_CONNECT_FAILED = 2; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
const STATE_CONNECT_SUCCEEDED = 6; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
const STATE_AUTH_PENDING = 5; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
const STATE_AUTH_FAILED = 3; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
const STATE_AUTH_SUCCEEDED = 7; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
const STATE_DISCONNECT_PENDING = 8; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var int |
60
|
|
|
*/ |
61
|
|
|
const STATE_DISCONNECT_SUCCEEDED = 1; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var mixed[] |
65
|
|
|
*/ |
66
|
|
|
protected $config; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var mixed[] |
70
|
|
|
*/ |
71
|
|
|
protected $serverInfo; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var int |
75
|
|
|
*/ |
76
|
|
|
protected $state; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var |
80
|
|
|
*/ |
81
|
|
|
protected $queue; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var TransactionBoxInterface |
85
|
|
|
*/ |
86
|
|
|
protected $transBox; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param LoopInterface $loop |
90
|
|
|
* @param mixed[] $config |
91
|
|
|
*/ |
92
|
|
|
public function __construct(LoopInterface $loop, $config = []) |
93
|
|
|
{ |
94
|
|
|
$this->loop = $loop; |
95
|
|
|
$this->config = $this->createConfig($config); |
96
|
|
|
$this->serverInfo = []; |
97
|
|
|
$this->state = self::STATE_INIT; |
98
|
|
|
$this->transBox = $this->createTransactionBox(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @override |
103
|
|
|
* @inheritDoc |
104
|
|
|
*/ |
105
|
|
|
public function isPaused() |
106
|
|
|
{ |
107
|
|
|
// TODO |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @override |
113
|
|
|
* @inheritDoc |
114
|
|
|
*/ |
115
|
|
|
public function pause() |
116
|
|
|
{ |
117
|
|
|
// TODO |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @override |
122
|
|
|
* @inheritDoc |
123
|
|
|
*/ |
124
|
|
|
public function resume() |
125
|
|
|
{ |
126
|
|
|
// TODO |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @override |
131
|
|
|
* @inheritDoc |
132
|
|
|
*/ |
133
|
|
|
public function isStarted() |
134
|
|
|
{ |
135
|
|
|
return $this->state >= self::STATE_CONNECT_PENDING; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @override |
140
|
|
|
* @inheritDoc |
141
|
|
|
*/ |
142
|
|
|
public function start() |
143
|
|
|
{ |
144
|
|
|
if ($this->isStarted()) |
145
|
|
|
{ |
146
|
|
|
return Promise::doResolve($this); |
147
|
|
|
} |
148
|
|
|
// TODO |
149
|
|
|
return Promise::doReject(new ExecutionException('Not yet implemented.')); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @override |
154
|
|
|
* @inheritDoc |
155
|
|
|
*/ |
156
|
|
|
public function stop() |
157
|
|
|
{ |
158
|
|
|
if (!$this->isStarted()) |
159
|
|
|
{ |
160
|
|
|
return Promise::doResolve($this); |
161
|
|
|
} |
162
|
|
|
// TODO |
163
|
|
|
return Promise::doReject(new ExecutionException('Not yet implemented.')); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @override |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
170
|
|
|
public function getState() |
171
|
|
|
{ |
172
|
|
|
return $this->state; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @override |
177
|
|
|
* @inheritDoc |
178
|
|
|
*/ |
179
|
|
|
public function getInfo() |
180
|
|
|
{ |
181
|
|
|
return $this->serverInfo; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @override |
186
|
|
|
* @inheritDoc |
187
|
|
|
*/ |
188
|
|
|
public function setDatabase($dbname) |
189
|
|
|
{ |
190
|
|
|
// TODO |
191
|
|
|
return $this->query(sprintf('USE `%s`', $dbname)); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @override |
196
|
|
|
* @inheritDoc |
197
|
|
|
*/ |
198
|
|
|
public function getDatabase() |
199
|
|
|
{ |
200
|
|
|
// TODO |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @override |
205
|
|
|
* @inheritDoc |
206
|
|
|
*/ |
207
|
|
|
public function query($sql, $sqlParams = []) |
208
|
|
|
{ |
209
|
|
|
// TODO |
210
|
|
|
return Promise::doReject(new ExecutionException('Not yet implemented.')); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @override |
215
|
|
|
* @inheritDoc |
216
|
|
|
*/ |
217
|
|
|
public function execute($sql, $sqlParams = []) |
218
|
|
|
{ |
219
|
|
|
// TODO |
220
|
|
|
return $this->query($sql, $sqlParams)->then(function($command) { |
221
|
|
|
return $command->affectedRows; |
222
|
|
|
}); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @override |
227
|
|
|
* @inheritDoc |
228
|
|
|
*/ |
229
|
|
|
public function ping() |
230
|
|
|
{ |
231
|
|
|
// TODO |
232
|
|
|
return Promise::doReject(new ExecutionException('Not yet implemented.')); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @override |
237
|
|
|
* @inheritDoc |
238
|
|
|
*/ |
239
|
|
|
public function beginTransaction() |
240
|
|
|
{ |
241
|
|
|
$trans = new Transaction($this); |
242
|
|
|
|
243
|
|
|
$trans->on('commit', function(TransactionInterface $trans, array $queue) { |
244
|
|
|
$this->commitTransaction($queue)->then( |
245
|
|
|
function() use($trans) { |
246
|
|
|
return $trans->emit('success', [ $trans ]); |
247
|
|
|
}, |
248
|
|
|
function($ex) use($trans) { |
249
|
|
|
return $trans->emit('error', [ $trans, $ex ]); |
250
|
|
|
} |
251
|
|
|
); |
252
|
|
|
$this->transBox->remove($trans); |
253
|
|
|
}); |
254
|
|
|
$trans->on('rollback', function(TransactionInterface $trans) { |
255
|
|
|
$this->transBox->remove($trans); |
256
|
|
|
}); |
257
|
|
|
|
258
|
|
|
return $this->transBox->add($trans); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @override |
263
|
|
|
* @inheritDoc |
264
|
|
|
*/ |
265
|
|
|
public function endTransaction(TransactionInterface $trans) |
266
|
|
|
{ |
267
|
|
|
return $trans->rollback(); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Try to commit a transaction. |
272
|
|
|
* |
273
|
|
|
* @param mixed[] $queue |
274
|
|
|
* @return PromiseInterface |
275
|
|
|
*/ |
276
|
|
|
protected function commitTransaction($queue) |
277
|
|
|
{ |
278
|
|
|
// TODO |
279
|
|
|
return Promise::doReject(new ExecutionException('Not yet implemented.')); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @override |
284
|
|
|
* @inheritDoc |
285
|
|
|
*/ |
286
|
|
|
public function inTransaction() |
287
|
|
|
{ |
288
|
|
|
return !$this->transBox->isEmpty(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Create transaction box. |
293
|
|
|
* |
294
|
|
|
* @return TransactionBoxInterface |
295
|
|
|
*/ |
296
|
|
|
protected function createTransactionBox() |
297
|
|
|
{ |
298
|
|
|
return new TransactionBox(); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Create configuration file. |
303
|
|
|
* |
304
|
|
|
* @param mixed[] $config |
305
|
|
|
* @return mixed[] |
306
|
|
|
*/ |
307
|
|
|
protected function createConfig($config = []) |
308
|
|
|
{ |
309
|
|
|
$default = [ |
310
|
|
|
'endpoint' => 'tcp://127.0.0.1:5432', |
311
|
|
|
'user' => 'root', |
312
|
|
|
'pass' => '', |
313
|
|
|
'dbname' => '', |
314
|
|
|
]; |
315
|
|
|
return array_merge($default, $config); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|