Completed
Push — master ( 5f3c84...0c19f1 )
by Ondřej
03:04
created

Connection::getDefaultTxConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Ivory\Connection;
5
6
use Ivory\Connection\Config\ConnConfig;
7
use Ivory\Connection\Config\IConnConfig;
8
use Ivory\Exception\StatementExceptionFactory;
9
use Ivory\Ivory;
10
use Ivory\Relation\IColumn;
11
use Ivory\Relation\ITuple;
12
use Ivory\Result\ICommandResult;
13
use Ivory\Result\ICopyInResult;
14
use Ivory\Result\IQueryResult;
15
use Ivory\Result\IResult;
16
use Ivory\Type\ITypeDictionary;
17
use Ivory\Type\TypeRegister;
18
use Ivory\Utils\NotSerializable;
19
20
// TODO: consider renaming to Database or Session or... - to reflect it is mere a facade, the single entry point
21
class Connection implements IConnection
22
{
23
    use NotSerializable; // TODO: implement connection serialization instead of giving up
24
25
    private $name;
26
    private $connCtl;
27
    private $typeCtl;
28
    private $stmtExec;
29
    private $copyCtl;
30
    private $txCtl;
31
    private $ipcCtl;
32
    private $cacheCtl;
33
34
    private $config;
35
36
37
    /**
38
     * @param string $name name for the connection
39
     * @param ConnectionParameters|array|string $params either a connection parameters object, or an associative array
40
     *                                                    of parameters for {@link ConnectionParameters::__construct()},
41
     *                                                    or a URL for {@link ConnectionParameters::fromUrl()},
42
     *                                                    or a PostgreSQL connection string (see {@link pg_connect()})
43
     */
44
    public function __construct(string $name, $params)
45
    {
46
        $this->name = $name;
47
        $this->connCtl = new ConnectionControl($this, $params); // TODO: extract all usages of ConnectionControl::requireConnection() - consider introducing an interface specifying the method, named like PGSQLDriver or ConnectionManager or ConnectionPool
48
        $this->typeCtl = new TypeControl($this, $this->connCtl);
49
        $this->stmtExec = new StatementExecution($this->connCtl, $this->typeCtl);
50
        $this->copyCtl = new CopyControl();
51
        $this->txCtl = new TransactionControl($this->connCtl, $this->stmtExec, $this);
52
        $this->ipcCtl = new IPCControl($this->connCtl, $this->stmtExec);
53
        $this->config = new ConnConfig($this->connCtl, $this->stmtExec, $this->txCtl);
54
        $this->cacheCtl = Ivory::getCoreFactory()->createCacheControl($this); // TODO: consider moving cacheCtl initialization out of Connection itself (let the core factory set it up), or do not hold cache control here but rather besides the connection register at Ivory
55
    }
56
57
    final public function getName(): string
58
    {
59
        return $this->name;
60
    }
61
62
    final public function getConfig(): IConnConfig
63
    {
64
        return $this->config;
65
    }
66
67
68
    //region Connection Control
69
70
    public function getParameters(): ConnectionParameters
71
    {
72
        return $this->connCtl->getParameters();
73
    }
74
75
    public function isConnected(): ?bool
76
    {
77
        return $this->connCtl->isConnected();
78
    }
79
80
    public function isConnectedWait(): ?bool
81
    {
82
        return $this->connCtl->isConnectedWait();
83
    }
84
85
    public function connect(?\Closure $initProcedure = null): bool
86
    {
87
        return $this->connCtl->connect($initProcedure);
88
    }
89
90
    public function connectWait(): bool
91
    {
92
        return $this->connCtl->connectWait();
93
    }
94
95
    public function disconnect(): bool
96
    {
97
        return $this->connCtl->disconnect();
98
    }
99
100
    public function registerConnectStartHook(\Closure $closure): void
101
    {
102
        $this->connCtl->registerConnectStartHook($closure);
103
    }
104
105
    public function registerPreDisconnectHook(\Closure $closure): void
106
    {
107
        $this->connCtl->registerPreDisconnectHook($closure);
108
    }
109
110
    public function registerPostDisconnectHook(\Closure $closure): void
111
    {
112
        $this->connCtl->registerPostDisconnectHook($closure);
113
    }
114
115
    //endregion
116
117
    //region Type Control
118
119
    public function getTypeRegister(): TypeRegister
120
    {
121
        return $this->typeCtl->getTypeRegister();
122
    }
123
124
    public function getTypeDictionary(): ITypeDictionary
125
    {
126
        return $this->typeCtl->getTypeDictionary();
127
    }
128
129
    public function flushTypeDictionary(): void
130
    {
131
        $this->typeCtl->flushTypeDictionary();
132
    }
133
134
    public function setTypeControlOption(int $option): void
135
    {
136
        $this->typeCtl->setTypeControlOption($option);
137
    }
138
139
    public function unsetTypeControlOption(int $option): void
140
    {
141
        $this->typeCtl->unsetTypeControlOption($option);
142
    }
143
144
    //endregion
145
146
    //region Statement Execution
147
148
    public function query($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams): IQueryResult
149
    {
150
        return $this->stmtExec->query($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams);
151
    }
152
153
    public function querySingleTuple($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams): ITuple
154
    {
155
        return $this->stmtExec->querySingleTuple($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams);
156
    }
157
158
    public function querySingleColumn($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams): IColumn
159
    {
160
        return $this->stmtExec->querySingleColumn($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams);
161
    }
162
163
    public function querySingleValue($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams)
164
    {
165
        return $this->stmtExec->querySingleValue($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams);
166
    }
167
168
    public function command($sqlFragmentPatternOrCommand, ...$fragmentsAndParams): ICommandResult
169
    {
170
        return $this->stmtExec->command($sqlFragmentPatternOrCommand, ...$fragmentsAndParams);
171
    }
172
173
    public function rawQuery(string $sqlQuery): IQueryResult
174
    {
175
        return $this->stmtExec->rawQuery($sqlQuery);
176
    }
177
178
    public function rawCommand(string $sqlCommand): ICommandResult
179
    {
180
        return $this->stmtExec->rawCommand($sqlCommand);
181
    }
182
183
    public function executeStatement($sqlStatement): IResult
184
    {
185
        return $this->stmtExec->executeStatement($sqlStatement);
186
    }
187
188
    public function runScript(string $sqlScript): array
189
    {
190
        return $this->stmtExec->runScript($sqlScript);
191
    }
192
193
    public function getStatementExceptionFactory(): StatementExceptionFactory
194
    {
195
        return $this->stmtExec->getStatementExceptionFactory();
196
    }
197
198
    //endregion
199
200
    //region Copy Control
201
202
    public function copyFromFile(string $file, string $table, ?array $columns = null, array $options = []): ICommandResult
203
    {
204
        return $this->copyCtl->copyFromFile($file, $table, $columns, $options);
205
    }
206
207
    public function copyFromProgram(string $program, string $table, ?array $columns = null, array $options = []): ICommandResult
208
    {
209
        return $this->copyCtl->copyFromProgram($program, $table, $columns, $options);
210
    }
211
212
    public function copyFromInput(string $table, ?array $columns = null, array $options = []): ICopyInResult
213
    {
214
        return $this->copyCtl->copyFromInput($table, $columns, $options);
215
    }
216
217
    public function copyToFile(string $file, $tableOrRelationDefinition, ?array $columns = null, array $options = []): ICommandResult
218
    {
219
        return $this->copyCtl->copyToFile($file, $tableOrRelationDefinition, $columns, $options);
220
    }
221
222
    public function copyToProgram(string $program, $tableOrRelationDefinition, ?array $columns = null, array $options = []): ICommandResult
223
    {
224
        return $this->copyCtl->copyToProgram($program, $tableOrRelationDefinition, $columns, $options);
225
    }
226
227
    public function copyToArray(string $table, array $options = []): array
228
    {
229
        return $this->copyCtl->copyToArray($table, $options);
230
    }
231
232
    //endregion
233
234
    //region Transaction Control
235
236
    public function inTransaction(): bool
237
    {
238
        return $this->txCtl->inTransaction();
239
    }
240
241
    public function startTransaction($transactionOptions = 0): ITxHandle
242
    {
243
        return $this->txCtl->startTransaction($transactionOptions);
244
    }
245
246
    public function setupSubsequentTransactions($transactionOptions): void
247
    {
248
        $this->txCtl->setupSubsequentTransactions($transactionOptions);
249
    }
250
251
    public function getDefaultTxConfig(): TxConfig
252
    {
253
        return $this->txCtl->getDefaultTxConfig();
254
    }
255
256
    public function commitPreparedTransaction(string $name): void
257
    {
258
        $this->txCtl->commitPreparedTransaction($name);
259
    }
260
261
    public function rollbackPreparedTransaction(string $name): void
262
    {
263
        $this->txCtl->rollbackPreparedTransaction($name);
264
    }
265
266
    public function listPreparedTransactions(): IQueryResult
267
    {
268
        return $this->txCtl->listPreparedTransactions();
269
    }
270
271
    //endregion
272
273
    //region Transaction observing
274
275
    public function addTransactionControlObserver(ITransactionControlObserver $observer): void
276
    {
277
        $this->txCtl->addObserver($observer);
278
    }
279
280
    public function removeTransactionControlObserver(ITransactionControlObserver $observer): void
281
    {
282
        $this->txCtl->removeObserver($observer);
283
    }
284
285
    public function removeAllTransactionControlObservers(): void
286
    {
287
        $this->txCtl->removeAllObservers();
288
    }
289
290
    //endregion
291
292
    //region IPC Control
293
294
    public function getBackendPID(): int
295
    {
296
        return $this->ipcCtl->getBackendPID();
297
    }
298
299
    public function notify(string $channel, ?string $payload = null): void
300
    {
301
        $this->ipcCtl->notify($channel, $payload);
302
    }
303
304
    public function listen(string $channel): void
305
    {
306
        $this->ipcCtl->listen($channel);
307
    }
308
309
    public function unlisten(string $channel): void
310
    {
311
        $this->ipcCtl->unlisten($channel);
312
    }
313
314
    public function unlistenAll(): void
315
    {
316
        $this->ipcCtl->unlistenAll();
317
    }
318
319
    public function pollNotification(): ?Notification
320
    {
321
        return $this->ipcCtl->pollNotification();
322
    }
323
324
    //endregion
325
326
    //region Cache Control
327
328
    public function isCacheEnabled(): bool
329
    {
330
        return $this->cacheCtl->isCacheEnabled();
331
    }
332
333
    public function cachePermanently(string $cacheKey, $object): bool
334
    {
335
        return $this->cacheCtl->cachePermanently($cacheKey, $object);
336
    }
337
338
    public function getCached(string $cacheKey)
339
    {
340
        return $this->cacheCtl->getCached($cacheKey);
341
    }
342
343
    public function flushCache(string $cacheKey): bool
344
    {
345
        return $this->cacheCtl->flushCache($cacheKey);
346
    }
347
348
    //endregion
349
}
350