• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 12511167285

27 Dec 2024 03:06AM UTC coverage: 84.405% (+0.001%) from 84.404%
12511167285

Pull #9339

github

web-flow
Merge 638a0d312 into cc1b8f285
Pull Request #9339: refactor: enable instanceof and strictBooleans rector set

55 of 60 new or added lines in 34 files covered. (91.67%)

6 existing lines in 1 file now uncovered.

20405 of 24175 relevant lines covered (84.41%)

189.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.8
/system/Database/Postgre/Connection.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Database\Postgre;
15

16
use CodeIgniter\Database\BaseConnection;
17
use CodeIgniter\Database\Exceptions\DatabaseException;
18
use CodeIgniter\Database\RawSql;
19
use ErrorException;
20
use PgSql\Connection as PgSqlConnection;
21
use PgSql\Result as PgSqlResult;
22
use stdClass;
23
use Stringable;
24

25
/**
26
 * Connection for Postgre
27
 *
28
 * @extends BaseConnection<PgSqlConnection, PgSqlResult>
29
 */
30
class Connection extends BaseConnection
31
{
32
    /**
33
     * Database driver
34
     *
35
     * @var string
36
     */
37
    public $DBDriver = 'Postgre';
38

39
    /**
40
     * Database schema
41
     *
42
     * @var string
43
     */
44
    public $schema = 'public';
45

46
    /**
47
     * Identifier escape character
48
     *
49
     * @var string
50
     */
51
    public $escapeChar = '"';
52

53
    protected $connect_timeout;
54
    protected $options;
55
    protected $sslmode;
56
    protected $service;
57

58
    /**
59
     * Connect to the database.
60
     *
61
     * @return false|PgSqlConnection
62
     */
63
    public function connect(bool $persistent = false)
64
    {
65
        if (empty($this->DSN)) {
20✔
66
            $this->buildDSN();
20✔
67
        }
68

69
        // Convert DSN string
70
        // @TODO This format is for PDO_PGSQL.
71
        //      https://www.php.net/manual/en/ref.pdo-pgsql.connection.php
72
        //      Should deprecate?
73
        if (mb_strpos($this->DSN, 'pgsql:') === 0) {
20✔
74
            $this->convertDSN();
×
75
        }
76

77
        $this->connID = $persistent ? pg_pconnect($this->DSN) : pg_connect($this->DSN);
20✔
78

79
        if ($this->connID !== false) {
20✔
80
            if (
81
                $persistent
20✔
82
                && pg_connection_status($this->connID) === PGSQL_CONNECTION_BAD
20✔
83
                && pg_ping($this->connID) === false
20✔
84
            ) {
85
                $error = pg_last_error($this->connID);
×
86

87
                throw new DatabaseException($error);
×
88
            }
89

90
            if (! empty($this->schema)) {
20✔
91
                $this->simpleQuery("SET search_path TO {$this->schema},public");
20✔
92
            }
93

94
            if ($this->setClientEncoding($this->charset) === false) {
20✔
95
                $error = pg_last_error($this->connID);
1✔
96

97
                throw new DatabaseException($error);
1✔
98
            }
99
        }
100

101
        return $this->connID;
19✔
102
    }
103

104
    /**
105
     * Converts the DSN with semicolon syntax.
106
     *
107
     * @return void
108
     */
109
    private function convertDSN()
110
    {
111
        // Strip pgsql
112
        $this->DSN = mb_substr($this->DSN, 6);
5✔
113

114
        // Convert semicolons to spaces in DSN format like:
115
        // pgsql:host=localhost;port=5432;dbname=database_name
116
        // https://www.php.net/manual/en/function.pg-connect.php
117
        $allowedParams = ['host', 'port', 'dbname', 'user', 'password', 'connect_timeout', 'options', 'sslmode', 'service'];
5✔
118

119
        $parameters = explode(';', $this->DSN);
5✔
120

121
        $output            = '';
5✔
122
        $previousParameter = '';
5✔
123

124
        foreach ($parameters as $parameter) {
5✔
125
            [$key, $value] = explode('=', $parameter, 2);
5✔
126
            if (in_array($key, $allowedParams, true)) {
5✔
127
                if ($previousParameter !== '') {
5✔
128
                    if (array_search($key, $allowedParams, true) < array_search($previousParameter, $allowedParams, true)) {
5✔
129
                        $output .= ';';
1✔
130
                    } else {
131
                        $output .= ' ';
5✔
132
                    }
133
                }
134
                $output .= $parameter;
5✔
135
                $previousParameter = $key;
5✔
136
            } else {
137
                $output .= ';' . $parameter;
1✔
138
            }
139
        }
140

141
        $this->DSN = $output;
5✔
142
    }
143

144
    /**
145
     * Keep or establish the connection if no queries have been sent for
146
     * a length of time exceeding the server's idle timeout.
147
     *
148
     * @return void
149
     */
150
    public function reconnect()
151
    {
152
        if ($this->connID === false || pg_ping($this->connID) === false) {
×
153
            $this->close();
×
154
            $this->initialize();
×
155
        }
156
    }
157

158
    /**
159
     * Close the database connection.
160
     *
161
     * @return void
162
     */
163
    protected function _close()
164
    {
165
        pg_close($this->connID);
1✔
166
    }
167

168
    /**
169
     * Select a specific database table to use.
170
     */
171
    public function setDatabase(string $databaseName): bool
172
    {
173
        return false;
×
174
    }
175

176
    /**
177
     * Returns a string containing the version of the database being used.
178
     */
179
    public function getVersion(): string
180
    {
181
        if (isset($this->dataCache['version'])) {
6✔
182
            return $this->dataCache['version'];
3✔
183
        }
184

185
        if (! $this->connID) {
5✔
186
            $this->initialize();
×
187
        }
188

189
        $pgVersion                  = pg_version($this->connID);
5✔
190
        $this->dataCache['version'] = isset($pgVersion['server']) ?
5✔
191
            (preg_match('/^(\d+\.\d+)/', $pgVersion['server'], $matches) ? $matches[1] : '') :
5✔
192
            '';
×
193

194
        return $this->dataCache['version'];
5✔
195
    }
196

197
    /**
198
     * Executes the query against the database.
199
     *
200
     * @return false|PgSqlResult
201
     */
202
    protected function execute(string $sql)
203
    {
204
        try {
205
            return pg_query($this->connID, $sql);
664✔
206
        } catch (ErrorException $e) {
26✔
207
            log_message('error', (string) $e);
26✔
208

209
            if ($this->DBDebug) {
26✔
210
                throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
10✔
211
            }
212
        }
213

214
        return false;
16✔
215
    }
216

217
    /**
218
     * Get the prefix of the function to access the DB.
219
     */
220
    protected function getDriverFunctionPrefix(): string
221
    {
222
        return 'pg_';
×
223
    }
224

225
    /**
226
     * Returns the total number of rows affected by this query.
227
     */
228
    public function affectedRows(): int
229
    {
230
        return pg_affected_rows($this->resultID);
46✔
231
    }
232

233
    /**
234
     * "Smart" Escape String
235
     *
236
     * Escapes data based on type
237
     *
238
     * @param array|bool|float|int|object|string|null $str
239
     *
240
     * @return         array|float|int|string
241
     * @phpstan-return ($str is array ? array : float|int|string)
242
     */
243
    public function escape($str)
244
    {
245
        if (! $this->connID) {
639✔
246
            $this->initialize();
×
247
        }
248

249
        if ($str instanceof Stringable) {
639✔
250
            if ($str instanceof RawSql) {
3✔
251
                return $str->__toString();
2✔
252
            }
253

254
            $str = (string) $str;
1✔
255
        }
256

257
        if (is_string($str)) {
639✔
258
            return pg_escape_literal($this->connID, $str);
638✔
259
        }
260

261
        if (is_bool($str)) {
620✔
262
            return $str ? 'TRUE' : 'FALSE';
530✔
263
        }
264

265
        return parent::escape($str);
620✔
266
    }
267

268
    /**
269
     * Platform-dependant string escape
270
     */
271
    protected function _escapeString(string $str): string
272
    {
273
        if (! $this->connID) {
4✔
274
            $this->initialize();
×
275
        }
276

277
        return pg_escape_string($this->connID, $str);
4✔
278
    }
279

280
    /**
281
     * Generates the SQL for listing tables in a platform-dependent manner.
282
     *
283
     * @param string|null $tableName If $tableName is provided will return only this table if exists.
284
     */
285
    protected function _listTables(bool $prefixLimit = false, ?string $tableName = null): string
286
    {
287
        $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \'' . $this->schema . "'";
584✔
288

289
        if ($tableName !== null) {
584✔
290
            return $sql . ' AND "table_name" LIKE ' . $this->escape($tableName);
584✔
291
        }
292

293
        if ($prefixLimit && $this->DBPrefix !== '') {
28✔
294
            return $sql . ' AND "table_name" LIKE \''
×
295
                . $this->escapeLikeString($this->DBPrefix) . "%' "
×
296
                . sprintf($this->likeEscapeStr, $this->likeEscapeChar);
×
297
        }
298

299
        return $sql;
28✔
300
    }
301

302
    /**
303
     * Generates a platform-specific query string so that the column names can be fetched.
304
     */
305
    protected function _listColumns(string $table = ''): string
306
    {
307
        return 'SELECT "column_name"
8✔
308
                        FROM "information_schema"."columns"
309
                        WHERE LOWER("table_name") = '
8✔
310
                . $this->escape($this->DBPrefix . strtolower($table))
8✔
311
                . ' ORDER BY "ordinal_position"';
8✔
312
    }
313

314
    /**
315
     * Returns an array of objects with field data
316
     *
317
     * @return list<stdClass>
318
     *
319
     * @throws DatabaseException
320
     */
321
    protected function _fieldData(string $table): array
322
    {
323
        $sql = 'SELECT "column_name", "data_type", "character_maximum_length", "numeric_precision", "column_default",  "is_nullable"
27✔
324
            FROM "information_schema"."columns"
325
            WHERE LOWER("table_name") = '
27✔
326
                . $this->escape(strtolower($table))
27✔
327
                . ' ORDER BY "ordinal_position"';
27✔
328

329
        if (($query = $this->query($sql)) === false) {
27✔
330
            throw new DatabaseException(lang('Database.failGetFieldData'));
×
331
        }
332
        $query = $query->getResultObject();
27✔
333

334
        $retVal = [];
27✔
335

336
        for ($i = 0, $c = count($query); $i < $c; $i++) {
27✔
337
            $retVal[$i] = new stdClass();
27✔
338

339
            $retVal[$i]->name       = $query[$i]->column_name;
27✔
340
            $retVal[$i]->type       = $query[$i]->data_type;
27✔
341
            $retVal[$i]->max_length = $query[$i]->character_maximum_length > 0 ? $query[$i]->character_maximum_length : $query[$i]->numeric_precision;
27✔
342
            $retVal[$i]->nullable   = $query[$i]->is_nullable === 'YES';
27✔
343
            $retVal[$i]->default    = $query[$i]->column_default;
27✔
344
        }
345

346
        return $retVal;
27✔
347
    }
348

349
    /**
350
     * Returns an array of objects with index data
351
     *
352
     * @return array<string, stdClass>
353
     *
354
     * @throws DatabaseException
355
     */
356
    protected function _indexData(string $table): array
357
    {
358
        $sql = 'SELECT "indexname", "indexdef"
19✔
359
                        FROM "pg_indexes"
360
                        WHERE LOWER("tablename") = ' . $this->escape(strtolower($table)) . '
19✔
361
                        AND "schemaname" = ' . $this->escape('public');
19✔
362

363
        if (($query = $this->query($sql)) === false) {
19✔
364
            throw new DatabaseException(lang('Database.failGetIndexData'));
×
365
        }
366
        $query = $query->getResultObject();
19✔
367

368
        $retVal = [];
19✔
369

370
        foreach ($query as $row) {
19✔
371
            $obj         = new stdClass();
18✔
372
            $obj->name   = $row->indexname;
18✔
373
            $_fields     = explode(',', preg_replace('/^.*\((.+?)\)$/', '$1', trim($row->indexdef)));
18✔
374
            $obj->fields = array_map(static fn ($v) => trim($v), $_fields);
18✔
375

376
            if (str_starts_with($row->indexdef, 'CREATE UNIQUE INDEX pk')) {
18✔
377
                $obj->type = 'PRIMARY';
15✔
378
            } else {
379
                $obj->type = (str_starts_with($row->indexdef, 'CREATE UNIQUE')) ? 'UNIQUE' : 'INDEX';
15✔
380
            }
381

382
            $retVal[$obj->name] = $obj;
18✔
383
        }
384

385
        return $retVal;
19✔
386
    }
387

388
    /**
389
     * Returns an array of objects with Foreign key data
390
     *
391
     * @return array<string, stdClass>
392
     *
393
     * @throws DatabaseException
394
     */
395
    protected function _foreignKeyData(string $table): array
396
    {
397
        $sql = 'SELECT c.constraint_name,
5✔
398
                x.table_name,
399
                x.column_name,
400
                y.table_name as foreign_table_name,
401
                y.column_name as foreign_column_name,
402
                c.delete_rule,
403
                c.update_rule,
404
                c.match_option
405
                FROM information_schema.referential_constraints c
406
                JOIN information_schema.key_column_usage x
407
                    on x.constraint_name = c.constraint_name
408
                JOIN information_schema.key_column_usage y
409
                    on y.ordinal_position = x.position_in_unique_constraint
410
                    and y.constraint_name = c.unique_constraint_name
411
                WHERE x.table_name = ' . $this->escape($table) .
5✔
412
                'order by c.constraint_name, x.ordinal_position';
5✔
413

414
        if (($query = $this->query($sql)) === false) {
5✔
415
            throw new DatabaseException(lang('Database.failGetForeignKeyData'));
×
416
        }
417

418
        $query   = $query->getResultObject();
5✔
419
        $indexes = [];
5✔
420

421
        foreach ($query as $row) {
5✔
422
            $indexes[$row->constraint_name]['constraint_name']       = $row->constraint_name;
4✔
423
            $indexes[$row->constraint_name]['table_name']            = $table;
4✔
424
            $indexes[$row->constraint_name]['column_name'][]         = $row->column_name;
4✔
425
            $indexes[$row->constraint_name]['foreign_table_name']    = $row->foreign_table_name;
4✔
426
            $indexes[$row->constraint_name]['foreign_column_name'][] = $row->foreign_column_name;
4✔
427
            $indexes[$row->constraint_name]['on_delete']             = $row->delete_rule;
4✔
428
            $indexes[$row->constraint_name]['on_update']             = $row->update_rule;
4✔
429
            $indexes[$row->constraint_name]['match']                 = $row->match_option;
4✔
430
        }
431

432
        return $this->foreignKeyDataToObjects($indexes);
5✔
433
    }
434

435
    /**
436
     * Returns platform-specific SQL to disable foreign key checks.
437
     *
438
     * @return string
439
     */
440
    protected function _disableForeignKeyChecks()
441
    {
442
        return 'SET CONSTRAINTS ALL DEFERRED';
589✔
443
    }
444

445
    /**
446
     * Returns platform-specific SQL to enable foreign key checks.
447
     *
448
     * @return string
449
     */
450
    protected function _enableForeignKeyChecks()
451
    {
452
        return 'SET CONSTRAINTS ALL IMMEDIATE;';
589✔
453
    }
454

455
    /**
456
     * Returns the last error code and message.
457
     * Must return this format: ['code' => string|int, 'message' => string]
458
     * intval(code) === 0 means "no error".
459
     *
460
     * @return array<string, int|string>
461
     */
462
    public function error(): array
463
    {
464
        $lastError = pg_last_error($this->connID);
2✔
465

466
        return [
2✔
467
            'code'    => '',
2✔
468
            'message' => ! in_array($lastError, ['', '0'], true) ? $lastError : '',
2✔
469
        ];
2✔
470
    }
471

472
    /**
473
     * @return int|string
474
     */
475
    public function insertID()
476
    {
477
        $v = pg_version($this->connID);
78✔
478
        // 'server' key is only available since PostgreSQL 7.4
479
        $v = explode(' ', $v['server'])[0] ?? 0;
78✔
480

481
        $table  = func_num_args() > 0 ? func_get_arg(0) : null;
78✔
482
        $column = func_num_args() > 1 ? func_get_arg(1) : null;
78✔
483

484
        if ($table === null && $v >= '8.1') {
78✔
485
            $sql = 'SELECT LASTVAL() AS ins_id';
78✔
486
        } elseif ($table !== null) {
×
487
            if ($column !== null && $v >= '8.0') {
×
488
                $sql   = "SELECT pg_get_serial_sequence('{$table}', '{$column}') AS seq";
×
489
                $query = $this->query($sql);
×
UNCOV
490
                $query = $query->getRow();
×
UNCOV
491
                $seq   = $query->seq;
×
492
            } else {
493
                // seq_name passed in table parameter
UNCOV
494
                $seq = $table;
×
495
            }
496

497
            $sql = "SELECT CURRVAL('{$seq}') AS ins_id";
×
498
        } else {
UNCOV
499
            return pg_last_oid($this->resultID);
×
500
        }
501

502
        $query = $this->query($sql);
78✔
503
        $query = $query->getRow();
78✔
504

505
        return (int) $query->ins_id;
78✔
506
    }
507

508
    /**
509
     * Build a DSN from the provided parameters
510
     */
511
    protected function buildDSN()
512
    {
513
        if ($this->DSN !== '') {
21✔
UNCOV
514
            $this->DSN = '';
×
515
        }
516

517
        // If UNIX sockets are used, we shouldn't set a port
518
        if (str_contains($this->hostname, '/')) {
21✔
UNCOV
519
            $this->port = '';
×
520
        }
521

522
        if ($this->hostname !== '') {
21✔
523
            $this->DSN = "host={$this->hostname} ";
21✔
524
        }
525

526
        // ctype_digit only accepts strings
527
        $port = (string) $this->port;
21✔
528

529
        if ($port !== '' && ctype_digit($port)) {
21✔
530
            $this->DSN .= "port={$port} ";
21✔
531
        }
532

533
        if ($this->username !== '') {
21✔
534
            $this->DSN .= "user={$this->username} ";
21✔
535

536
            // An empty password is valid!
537
            // password must be set to null to ignore it.
538
            if ($this->password !== null) {
21✔
539
                $this->DSN .= "password='{$this->password}' ";
21✔
540
            }
541
        }
542

543
        if ($this->database !== '') {
21✔
544
            $this->DSN .= "dbname={$this->database} ";
21✔
545
        }
546

547
        // We don't have these options as elements in our standard configuration
548
        // array, but they might be set by parse_url() if the configuration was
549
        // provided via string> Example:
550
        //
551
        // Postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1
552
        foreach (['connect_timeout', 'options', 'sslmode', 'service'] as $key) {
21✔
553
            if (isset($this->{$key}) && is_string($this->{$key}) && $this->{$key} !== '') {
21✔
554
                $this->DSN .= "{$key}='{$this->{$key}}' ";
1✔
555
            }
556
        }
557

558
        $this->DSN = rtrim($this->DSN);
21✔
559
    }
560

561
    /**
562
     * Set client encoding
563
     */
564
    protected function setClientEncoding(string $charset): bool
565
    {
566
        return pg_set_client_encoding($this->connID, $charset) === 0;
20✔
567
    }
568

569
    /**
570
     * Begin Transaction
571
     */
572
    protected function _transBegin(): bool
573
    {
574
        return (bool) pg_query($this->connID, 'BEGIN');
14✔
575
    }
576

577
    /**
578
     * Commit Transaction
579
     */
580
    protected function _transCommit(): bool
581
    {
582
        return (bool) pg_query($this->connID, 'COMMIT');
3✔
583
    }
584

585
    /**
586
     * Rollback Transaction
587
     */
588
    protected function _transRollback(): bool
589
    {
590
        return (bool) pg_query($this->connID, 'ROLLBACK');
14✔
591
    }
592
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc