Completed
Push — master ( c27d2d...a49ccc )
by Ondřej
03:50
created

Connection::querySingleTuple()   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 2
1
<?php
2
namespace Ivory\Connection;
3
4
use Ivory\Connection\Config\ConnConfig;
5
use Ivory\Connection\Config\IConnConfig;
6
use Ivory\Exception\StatementExceptionFactory;
7
use Ivory\Ivory;
8
use Ivory\Relation\ITuple;
9
use Ivory\Result\ICommandResult;
10
use Ivory\Result\ICopyInResult;
11
use Ivory\Result\IQueryResult;
12
use Ivory\Type\ITypeDictionary;
13
use Ivory\Type\TypeRegister;
14
use Ivory\Utils\NotSerializable;
15
16
// TODO: consider renaming to Database or Session or... - to reflect it is mere a facade, the single entry point
17
class Connection implements IConnection
18
{
19
    use NotSerializable; // TODO: implement connection serialization instead of giving up
20
21
    private $name;
22
    private $connCtl;
23
    private $typeCtl;
24
    private $stmtExec;
25
    private $copyCtl;
26
    private $txCtl;
27
    private $ipcCtl;
28
    private $cacheCtl;
29
30
    private $config;
31
32
33
    /**
34
     * @param string $name name for the connection
35
     * @param ConnectionParameters|array|string $params either a connection parameters object, or an associative array
36
     *                                                    of parameters for {@link ConnectionParameters::__construct()},
37
     *                                                    or a URL for {@link ConnectionParameters::fromUrl()},
38
     *                                                    or a PostgreSQL connection string (see {@link pg_connect()})
39
     */
40
    public function __construct(string $name, $params)
41
    {
42
        $this->name = $name;
43
        $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
44
        $this->typeCtl = new TypeControl($this, $this->connCtl);
45
        $this->stmtExec = new StatementExecution($this->connCtl, $this->typeCtl);
46
        $this->copyCtl = new CopyControl();
47
        $this->txCtl = new TransactionControl($this->connCtl, $this->stmtExec);
48
        $this->ipcCtl = new IPCControl($this->connCtl);
49
        $this->config = new ConnConfig($this->connCtl, $this->stmtExec, $this->txCtl);
50
        $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
51
    }
52
53
    final public function getName(): string
54
    {
55
        return $this->name;
56
    }
57
58
    final public function getConfig(): IConnConfig
59
    {
60
        return $this->config;
61
    }
62
63
64
    //region Connection Control
65
66
    public function getParameters(): ConnectionParameters
67
    {
68
        return $this->connCtl->getParameters();
69
    }
70
71
    public function isConnected()
72
    {
73
        return $this->connCtl->isConnected();
74
    }
75
76
    public function isConnectedWait()
77
    {
78
        return $this->connCtl->isConnectedWait();
79
    }
80
81
    public function connect(\Closure $initProcedure = null): bool
82
    {
83
        return $this->connCtl->connect($initProcedure);
84
    }
85
86
    public function connectWait(): bool
87
    {
88
        return $this->connCtl->connectWait();
89
    }
90
91
    public function disconnect(): bool
92
    {
93
        return $this->connCtl->disconnect();
94
    }
95
96
    public function registerConnectStartHook(\Closure $closure)
97
    {
98
        $this->connCtl->registerConnectStartHook($closure);
99
    }
100
101
    public function registerPreDisconnectHook(\Closure $closure)
102
    {
103
        $this->connCtl->registerPreDisconnectHook($closure);
104
    }
105
106
    public function registerPostDisconnectHook(\Closure $closure)
107
    {
108
        $this->connCtl->registerPostDisconnectHook($closure);
109
    }
110
111
    //endregion
112
113
    //region Type Control
114
115
    public function getTypeRegister(): TypeRegister
116
    {
117
        return $this->typeCtl->getTypeRegister();
118
    }
119
120
    public function getTypeDictionary(): ITypeDictionary
121
    {
122
        return $this->typeCtl->getTypeDictionary();
123
    }
124
125
    public function flushTypeDictionary()
126
    {
127
        $this->typeCtl->flushTypeDictionary();
128
    }
129
130
    //endregion
131
132
    //region Statement Execution
133
134
    public function query($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams): IQueryResult
135
    {
136
        return $this->stmtExec->query($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams);
137
    }
138
139
    public function querySingleTuple($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams): ITuple
140
    {
141
        return $this->stmtExec->querySingleTuple($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams);
142
    }
143
144
    public function querySingleValue($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams)
145
    {
146
        return $this->stmtExec->querySingleValue($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams);
147
    }
148
149
    public function command($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams): ICommandResult
150
    {
151
        return $this->stmtExec->command($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams);
152
    }
153
154
    public function rawQuery(string $sqlQuery): IQueryResult
155
    {
156
        return $this->stmtExec->rawQuery($sqlQuery);
157
    }
158
159
    public function rawCommand(string $sqlCommand): ICommandResult
160
    {
161
        return $this->stmtExec->rawCommand($sqlCommand);
162
    }
163
164
    public function rawMultiStatement($sqlStatements)
165
    {
166
        return $this->stmtExec->rawMultiStatement($sqlStatements);
167
    }
168
169
    public function runScript(string $sqlScript)
170
    {
171
        return $this->stmtExec->runScript($sqlScript);
172
    }
173
174
    public function getStatementExceptionFactory(): StatementExceptionFactory
175
    {
176
        return $this->stmtExec->getStatementExceptionFactory();
177
    }
178
179
    //endregion
180
181
    //region Copy Control
182
183
    public function copyFromFile(string $file, string $table, $columns = null, $options = []): ICommandResult
184
    {
185
        return $this->copyCtl->copyFromFile($file, $table, $columns, $options);
186
    }
187
188
    public function copyFromProgram(string $program, string $table, $columns = null, $options = []): ICommandResult
189
    {
190
        return $this->copyCtl->copyFromProgram($program, $table, $columns, $options);
191
    }
192
193
    public function copyFromInput(string $table, $columns = null, $options = []): ICopyInResult
194
    {
195
        return $this->copyCtl->copyFromInput($table, $columns, $options);
196
    }
197
198
    public function copyToFile(string $file, $tableOrRecipe, $columns = null, $options = []): ICommandResult
199
    {
200
        return $this->copyCtl->copyToFile($file, $tableOrRecipe, $columns, $options);
201
    }
202
203
    public function copyToProgram(string $program, $tableOrRecipe, $columns = null, $options = []): ICommandResult
204
    {
205
        return $this->copyCtl->copyToProgram($program, $tableOrRecipe, $columns, $options);
206
    }
207
208
    public function copyToArray(string $table, $options = [])
209
    {
210
        return $this->copyCtl->copyToArray($table, $options);
211
    }
212
213
    //endregion
214
215
    //region Transaction Control
216
217
    public function inTransaction(): bool
218
    {
219
        return $this->txCtl->inTransaction();
220
    }
221
222
    public function startTransaction($transactionOptions = 0): ITxHandle
223
    {
224
        return $this->txCtl->startTransaction($transactionOptions);
225
    }
226
227
    public function setupSubsequentTransactions($transactionOptions)
228
    {
229
        $this->txCtl->setupSubsequentTransactions($transactionOptions);
230
    }
231
232
    public function commitPreparedTransaction(string $name)
233
    {
234
        $this->txCtl->commitPreparedTransaction($name);
235
    }
236
237
    public function rollbackPreparedTransaction(string $name)
238
    {
239
        $this->txCtl->rollbackPreparedTransaction($name);
240
    }
241
242
    public function listPreparedTransactions(): IQueryResult
243
    {
244
        return $this->txCtl->listPreparedTransactions();
245
    }
246
247
    //endregion
248
249
    //region IPC Control
250
251
    public function getBackendPID(): int
252
    {
253
        return $this->ipcCtl->getBackendPID();
254
    }
255
256
    public function notify(string $channel, string $payload = null)
257
    {
258
        $this->ipcCtl->notify($channel, $payload);
259
    }
260
261
    public function listen(string $channel)
262
    {
263
        $this->ipcCtl->listen($channel);
264
    }
265
266
    public function unlisten(string $channel)
267
    {
268
        $this->ipcCtl->unlisten($channel);
269
    }
270
271
    public function unlistenAll()
272
    {
273
        $this->ipcCtl->unlistenAll();
274
    }
275
276
    public function pollNotification()
277
    {
278
        return $this->ipcCtl->pollNotification();
279
    }
280
281
    //endregion
282
283
    //region Cache Control
284
285
    public function isCacheEnabled(): bool
286
    {
287
        return $this->cacheCtl->isCacheEnabled();
288
    }
289
290
    public function cachePermanently(string $cacheKey, $object): bool
291
    {
292
        return $this->cacheCtl->cachePermanently($cacheKey, $object);
293
    }
294
295
    public function getCached(string $cacheKey)
296
    {
297
        return $this->cacheCtl->getCached($cacheKey);
298
    }
299
300
    public function flushCache(string $cacheKey): bool
301
    {
302
        return $this->cacheCtl->flushCache($cacheKey);
303
    }
304
305
    //endregion
306
}
307