Completed
Push — master ( 1763ff...752297 )
by Ondřej
03:04
created

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