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

codeigniter4 / CodeIgniter4 / 12673986434

08 Jan 2025 03:42PM UTC coverage: 84.455% (+0.001%) from 84.454%
12673986434

Pull #9385

github

web-flow
Merge 06e47f0ee into e475fd8fa
Pull Request #9385: refactor: Fix phpstan expr.resultUnused

20699 of 24509 relevant lines covered (84.45%)

190.57 hits per line

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

84.07
/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 CodeIgniter\Database\TableName;
20
use ErrorException;
21
use PgSql\Connection as PgSqlConnection;
22
use PgSql\Result as PgSqlResult;
23
use stdClass;
24
use Stringable;
25

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

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

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

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

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

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

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

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

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

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

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

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

102
        return $this->connID;
22✔
103
    }
104

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

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

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

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

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

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

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

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

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

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

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

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

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

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

210
            if ($this->DBDebug) {
30✔
211
                throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
13✔
212
            }
213
        }
214

215
        return false;
17✔
216
    }
217

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

226
    /**
227
     * Returns the total number of rows affected by this query.
228
     */
229
    public function affectedRows(): int
230
    {
231
        if ($this->resultID === false) {
47✔
232
            return 0;
1✔
233
        }
234

235
        return pg_affected_rows($this->resultID);
46✔
236
    }
237

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

254
        if ($str instanceof Stringable) {
652✔
255
            if ($str instanceof RawSql) {
3✔
256
                return $str->__toString();
2✔
257
            }
258

259
            $str = (string) $str;
1✔
260
        }
261

262
        if (is_string($str)) {
652✔
263
            return pg_escape_literal($this->connID, $str);
651✔
264
        }
265

266
        if (is_bool($str)) {
633✔
267
            return $str ? 'TRUE' : 'FALSE';
535✔
268
        }
269

270
        return parent::escape($str);
633✔
271
    }
272

273
    /**
274
     * Platform-dependant string escape
275
     */
276
    protected function _escapeString(string $str): string
277
    {
278
        if (! $this->connID) {
4✔
279
            $this->initialize();
×
280
        }
281

282
        return pg_escape_string($this->connID, $str);
4✔
283
    }
284

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

294
        if ($tableName !== null) {
597✔
295
            return $sql . ' AND "table_name" LIKE ' . $this->escape($tableName);
597✔
296
        }
297

298
        if ($prefixLimit && $this->DBPrefix !== '') {
31✔
299
            return $sql . ' AND "table_name" LIKE \''
×
300
                . $this->escapeLikeString($this->DBPrefix) . "%' "
×
301
                . sprintf($this->likeEscapeStr, $this->likeEscapeChar);
×
302
        }
303

304
        return $sql;
31✔
305
    }
306

307
    /**
308
     * Generates a platform-specific query string so that the column names can be fetched.
309
     *
310
     * @param string|TableName $table
311
     */
312
    protected function _listColumns($table = ''): string
313
    {
314
        if ($table instanceof TableName) {
8✔
315
            $tableName = $this->escape($table->getActualTableName());
2✔
316
        } else {
317
            $tableName = $this->escape($this->DBPrefix . strtolower($table));
6✔
318
        }
319

320
        return 'SELECT "column_name"
8✔
321
                        FROM "information_schema"."columns"
322
                        WHERE LOWER("table_name") = ' . $tableName
8✔
323
                . ' ORDER BY "ordinal_position"';
8✔
324
    }
325

326
    /**
327
     * Returns an array of objects with field data
328
     *
329
     * @return list<stdClass>
330
     *
331
     * @throws DatabaseException
332
     */
333
    protected function _fieldData(string $table): array
334
    {
335
        $sql = 'SELECT "column_name", "data_type", "character_maximum_length", "numeric_precision", "column_default",  "is_nullable"
27✔
336
            FROM "information_schema"."columns"
337
            WHERE LOWER("table_name") = '
27✔
338
                . $this->escape(strtolower($table))
27✔
339
                . ' ORDER BY "ordinal_position"';
27✔
340

341
        if (($query = $this->query($sql)) === false) {
27✔
342
            throw new DatabaseException(lang('Database.failGetFieldData'));
×
343
        }
344
        $query = $query->getResultObject();
27✔
345

346
        $retVal = [];
27✔
347

348
        for ($i = 0, $c = count($query); $i < $c; $i++) {
27✔
349
            $retVal[$i] = new stdClass();
27✔
350

351
            $retVal[$i]->name       = $query[$i]->column_name;
27✔
352
            $retVal[$i]->type       = $query[$i]->data_type;
27✔
353
            $retVal[$i]->max_length = $query[$i]->character_maximum_length > 0 ? $query[$i]->character_maximum_length : $query[$i]->numeric_precision;
27✔
354
            $retVal[$i]->nullable   = $query[$i]->is_nullable === 'YES';
27✔
355
            $retVal[$i]->default    = $query[$i]->column_default;
27✔
356
        }
357

358
        return $retVal;
27✔
359
    }
360

361
    /**
362
     * Returns an array of objects with index data
363
     *
364
     * @return array<string, stdClass>
365
     *
366
     * @throws DatabaseException
367
     */
368
    protected function _indexData(string $table): array
369
    {
370
        $sql = 'SELECT "indexname", "indexdef"
19✔
371
                        FROM "pg_indexes"
372
                        WHERE LOWER("tablename") = ' . $this->escape(strtolower($table)) . '
19✔
373
                        AND "schemaname" = ' . $this->escape('public');
19✔
374

375
        if (($query = $this->query($sql)) === false) {
19✔
376
            throw new DatabaseException(lang('Database.failGetIndexData'));
×
377
        }
378
        $query = $query->getResultObject();
19✔
379

380
        $retVal = [];
19✔
381

382
        foreach ($query as $row) {
19✔
383
            $obj         = new stdClass();
18✔
384
            $obj->name   = $row->indexname;
18✔
385
            $_fields     = explode(',', preg_replace('/^.*\((.+?)\)$/', '$1', trim($row->indexdef)));
18✔
386
            $obj->fields = array_map(static fn ($v): string => trim($v), $_fields);
18✔
387

388
            if (str_starts_with($row->indexdef, 'CREATE UNIQUE INDEX pk')) {
18✔
389
                $obj->type = 'PRIMARY';
15✔
390
            } else {
391
                $obj->type = (str_starts_with($row->indexdef, 'CREATE UNIQUE')) ? 'UNIQUE' : 'INDEX';
15✔
392
            }
393

394
            $retVal[$obj->name] = $obj;
18✔
395
        }
396

397
        return $retVal;
19✔
398
    }
399

400
    /**
401
     * Returns an array of objects with Foreign key data
402
     *
403
     * @return array<string, stdClass>
404
     *
405
     * @throws DatabaseException
406
     */
407
    protected function _foreignKeyData(string $table): array
408
    {
409
        $sql = 'SELECT c.constraint_name,
5✔
410
                x.table_name,
411
                x.column_name,
412
                y.table_name as foreign_table_name,
413
                y.column_name as foreign_column_name,
414
                c.delete_rule,
415
                c.update_rule,
416
                c.match_option
417
                FROM information_schema.referential_constraints c
418
                JOIN information_schema.key_column_usage x
419
                    on x.constraint_name = c.constraint_name
420
                JOIN information_schema.key_column_usage y
421
                    on y.ordinal_position = x.position_in_unique_constraint
422
                    and y.constraint_name = c.unique_constraint_name
423
                WHERE x.table_name = ' . $this->escape($table) .
5✔
424
                'order by c.constraint_name, x.ordinal_position';
5✔
425

426
        if (($query = $this->query($sql)) === false) {
5✔
427
            throw new DatabaseException(lang('Database.failGetForeignKeyData'));
×
428
        }
429

430
        $query   = $query->getResultObject();
5✔
431
        $indexes = [];
5✔
432

433
        foreach ($query as $row) {
5✔
434
            $indexes[$row->constraint_name]['constraint_name']       = $row->constraint_name;
4✔
435
            $indexes[$row->constraint_name]['table_name']            = $table;
4✔
436
            $indexes[$row->constraint_name]['column_name'][]         = $row->column_name;
4✔
437
            $indexes[$row->constraint_name]['foreign_table_name']    = $row->foreign_table_name;
4✔
438
            $indexes[$row->constraint_name]['foreign_column_name'][] = $row->foreign_column_name;
4✔
439
            $indexes[$row->constraint_name]['on_delete']             = $row->delete_rule;
4✔
440
            $indexes[$row->constraint_name]['on_update']             = $row->update_rule;
4✔
441
            $indexes[$row->constraint_name]['match']                 = $row->match_option;
4✔
442
        }
443

444
        return $this->foreignKeyDataToObjects($indexes);
5✔
445
    }
446

447
    /**
448
     * Returns platform-specific SQL to disable foreign key checks.
449
     *
450
     * @return string
451
     */
452
    protected function _disableForeignKeyChecks()
453
    {
454
        return 'SET CONSTRAINTS ALL DEFERRED';
602✔
455
    }
456

457
    /**
458
     * Returns platform-specific SQL to enable foreign key checks.
459
     *
460
     * @return string
461
     */
462
    protected function _enableForeignKeyChecks()
463
    {
464
        return 'SET CONSTRAINTS ALL IMMEDIATE;';
602✔
465
    }
466

467
    /**
468
     * Returns the last error code and message.
469
     * Must return this format: ['code' => string|int, 'message' => string]
470
     * intval(code) === 0 means "no error".
471
     *
472
     * @return array<string, int|string>
473
     */
474
    public function error(): array
475
    {
476
        return [
2✔
477
            'code'    => '',
2✔
478
            'message' => pg_last_error($this->connID),
2✔
479
        ];
2✔
480
    }
481

482
    /**
483
     * @return int|string
484
     */
485
    public function insertID()
486
    {
487
        $v = pg_version($this->connID);
79✔
488
        // 'server' key is only available since PostgreSQL 7.4
489
        $v = explode(' ', $v['server'])[0] ?? 0;
79✔
490

491
        $table  = func_num_args() > 0 ? func_get_arg(0) : null;
79✔
492
        $column = func_num_args() > 1 ? func_get_arg(1) : null;
79✔
493

494
        if ($table === null && $v >= '8.1') {
79✔
495
            $sql = 'SELECT LASTVAL() AS ins_id';
79✔
496
        } elseif ($table !== null) {
×
497
            if ($column !== null && $v >= '8.0') {
×
498
                $sql   = "SELECT pg_get_serial_sequence('{$table}', '{$column}') AS seq";
×
499
                $query = $this->query($sql);
×
500
                $query = $query->getRow();
×
501
                $seq   = $query->seq;
×
502
            } else {
503
                // seq_name passed in table parameter
504
                $seq = $table;
×
505
            }
506

507
            $sql = "SELECT CURRVAL('{$seq}') AS ins_id";
×
508
        } else {
509
            return pg_last_oid($this->resultID);
×
510
        }
511

512
        $query = $this->query($sql);
79✔
513
        $query = $query->getRow();
79✔
514

515
        return (int) $query->ins_id;
79✔
516
    }
517

518
    /**
519
     * Build a DSN from the provided parameters
520
     */
521
    protected function buildDSN()
522
    {
523
        if ($this->DSN !== '') {
24✔
524
            $this->DSN = '';
×
525
        }
526

527
        // If UNIX sockets are used, we shouldn't set a port
528
        if (str_contains($this->hostname, '/')) {
24✔
529
            $this->port = '';
×
530
        }
531

532
        if ($this->hostname !== '') {
24✔
533
            $this->DSN = "host={$this->hostname} ";
24✔
534
        }
535

536
        // ctype_digit only accepts strings
537
        $port = (string) $this->port;
24✔
538

539
        if ($port !== '' && ctype_digit($port)) {
24✔
540
            $this->DSN .= "port={$port} ";
24✔
541
        }
542

543
        if ($this->username !== '') {
24✔
544
            $this->DSN .= "user={$this->username} ";
24✔
545

546
            // An empty password is valid!
547
            // password must be set to null to ignore it.
548
            if ($this->password !== null) {
24✔
549
                $this->DSN .= "password='{$this->password}' ";
24✔
550
            }
551
        }
552

553
        if ($this->database !== '') {
24✔
554
            $this->DSN .= "dbname={$this->database} ";
24✔
555
        }
556

557
        // We don't have these options as elements in our standard configuration
558
        // array, but they might be set by parse_url() if the configuration was
559
        // provided via string> Example:
560
        //
561
        // Postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1
562
        foreach (['connect_timeout', 'options', 'sslmode', 'service'] as $key) {
24✔
563
            if (isset($this->{$key}) && is_string($this->{$key}) && $this->{$key} !== '') {
24✔
564
                $this->DSN .= "{$key}='{$this->{$key}}' ";
1✔
565
            }
566
        }
567

568
        $this->DSN = rtrim($this->DSN);
24✔
569
    }
570

571
    /**
572
     * Set client encoding
573
     */
574
    protected function setClientEncoding(string $charset): bool
575
    {
576
        return pg_set_client_encoding($this->connID, $charset) === 0;
23✔
577
    }
578

579
    /**
580
     * Begin Transaction
581
     */
582
    protected function _transBegin(): bool
583
    {
584
        return (bool) pg_query($this->connID, 'BEGIN');
17✔
585
    }
586

587
    /**
588
     * Commit Transaction
589
     */
590
    protected function _transCommit(): bool
591
    {
592
        return (bool) pg_query($this->connID, 'COMMIT');
5✔
593
    }
594

595
    /**
596
     * Rollback Transaction
597
     */
598
    protected function _transRollback(): bool
599
    {
600
        return (bool) pg_query($this->connID, 'ROLLBACK');
17✔
601
    }
602
}
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

© 2026 Coveralls, Inc