Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 45
PgSqlCaster
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
240
0.00% covered (danger)
0.00%
0 / 45
 castLargeObject
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 castLink
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 20
 castResult
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 23
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\VarDumper\Caster;
13
14use Symfony\Component\VarDumper\Cloner\Stub;
15
16/**
17 * Casts pqsql resources to array representation.
18 *
19 * @author Nicolas Grekas <p@tchwork.com>
20 *
21 * @final
22 */
23class PgSqlCaster
24{
25    private static $paramCodes = [
26        'server_encoding',
27        'client_encoding',
28        'is_superuser',
29        'session_authorization',
30        'DateStyle',
31        'TimeZone',
32        'IntervalStyle',
33        'integer_datetimes',
34        'application_name',
35        'standard_conforming_strings',
36    ];
37
38    private static $transactionStatus = [
39        \PGSQL_TRANSACTION_IDLE => 'PGSQL_TRANSACTION_IDLE',
40        \PGSQL_TRANSACTION_ACTIVE => 'PGSQL_TRANSACTION_ACTIVE',
41        \PGSQL_TRANSACTION_INTRANS => 'PGSQL_TRANSACTION_INTRANS',
42        \PGSQL_TRANSACTION_INERROR => 'PGSQL_TRANSACTION_INERROR',
43        \PGSQL_TRANSACTION_UNKNOWN => 'PGSQL_TRANSACTION_UNKNOWN',
44    ];
45
46    private static $resultStatus = [
47        \PGSQL_EMPTY_QUERY => 'PGSQL_EMPTY_QUERY',
48        \PGSQL_COMMAND_OK => 'PGSQL_COMMAND_OK',
49        \PGSQL_TUPLES_OK => 'PGSQL_TUPLES_OK',
50        \PGSQL_COPY_OUT => 'PGSQL_COPY_OUT',
51        \PGSQL_COPY_IN => 'PGSQL_COPY_IN',
52        \PGSQL_BAD_RESPONSE => 'PGSQL_BAD_RESPONSE',
53        \PGSQL_NONFATAL_ERROR => 'PGSQL_NONFATAL_ERROR',
54        \PGSQL_FATAL_ERROR => 'PGSQL_FATAL_ERROR',
55    ];
56
57    private static $diagCodes = [
58        'severity' => \PGSQL_DIAG_SEVERITY,
59        'sqlstate' => \PGSQL_DIAG_SQLSTATE,
60        'message' => \PGSQL_DIAG_MESSAGE_PRIMARY,
61        'detail' => \PGSQL_DIAG_MESSAGE_DETAIL,
62        'hint' => \PGSQL_DIAG_MESSAGE_HINT,
63        'statement position' => \PGSQL_DIAG_STATEMENT_POSITION,
64        'internal position' => \PGSQL_DIAG_INTERNAL_POSITION,
65        'internal query' => \PGSQL_DIAG_INTERNAL_QUERY,
66        'context' => \PGSQL_DIAG_CONTEXT,
67        'file' => \PGSQL_DIAG_SOURCE_FILE,
68        'line' => \PGSQL_DIAG_SOURCE_LINE,
69        'function' => \PGSQL_DIAG_SOURCE_FUNCTION,
70    ];
71
72    public static function castLargeObject($lo, array $a, Stub $stub, bool $isNested)
73    {
74        $a['seek position'] = pg_lo_tell($lo);
75
76        return $a;
77    }
78
79    public static function castLink($link, array $a, Stub $stub, bool $isNested)
80    {
81        $a['status'] = pg_connection_status($link);
82        $a['status'] = new ConstStub(\PGSQL_CONNECTION_OK === $a['status'] ? 'PGSQL_CONNECTION_OK' : 'PGSQL_CONNECTION_BAD', $a['status']);
83        $a['busy'] = pg_connection_busy($link);
84
85        $a['transaction'] = pg_transaction_status($link);
86        if (isset(self::$transactionStatus[$a['transaction']])) {
87            $a['transaction'] = new ConstStub(self::$transactionStatus[$a['transaction']], $a['transaction']);
88        }
89
90        $a['pid'] = pg_get_pid($link);
91        $a['last error'] = pg_last_error($link);
92        $a['last notice'] = pg_last_notice($link);
93        $a['host'] = pg_host($link);
94        $a['port'] = pg_port($link);
95        $a['dbname'] = pg_dbname($link);
96        $a['options'] = pg_options($link);
97        $a['version'] = pg_version($link);
98
99        foreach (self::$paramCodes as $v) {
100            if (false !== $s = pg_parameter_status($link, $v)) {
101                $a['param'][$v] = $s;
102            }
103        }
104
105        $a['param']['client_encoding'] = pg_client_encoding($link);
106        $a['param'] = new EnumStub($a['param']);
107
108        return $a;
109    }
110
111    public static function castResult($result, array $a, Stub $stub, bool $isNested)
112    {
113        $a['num rows'] = pg_num_rows($result);
114        $a['status'] = pg_result_status($result);
115        if (isset(self::$resultStatus[$a['status']])) {
116            $a['status'] = new ConstStub(self::$resultStatus[$a['status']], $a['status']);
117        }
118        $a['command-completion tag'] = pg_result_status($result, \PGSQL_STATUS_STRING);
119
120        if (-1 === $a['num rows']) {
121            foreach (self::$diagCodes as $k => $v) {
122                $a['error'][$k] = pg_result_error_field($result, $v);
123            }
124        }
125
126        $a['affected rows'] = pg_affected_rows($result);
127        $a['last OID'] = pg_last_oid($result);
128
129        $fields = pg_num_fields($result);
130
131        for ($i = 0; $i < $fields; ++$i) {
132            $field = [
133                'name' => pg_field_name($result, $i),
134                'table' => sprintf('%s (OID: %s)', pg_field_table($result, $i), pg_field_table($result, $i, true)),
135                'type' => sprintf('%s (OID: %s)', pg_field_type($result, $i), pg_field_type_oid($result, $i)),
136                'nullable' => (bool) pg_field_is_null($result, $i),
137                'storage' => pg_field_size($result, $i).' bytes',
138                'display' => pg_field_prtlen($result, $i).' chars',
139            ];
140            if (' (OID: )' === $field['table']) {
141                $field['table'] = null;
142            }
143            if ('-1 bytes' === $field['storage']) {
144                $field['storage'] = 'variable size';
145            } elseif ('1 bytes' === $field['storage']) {
146                $field['storage'] = '1 byte';
147            }
148            if ('1 chars' === $field['display']) {
149                $field['display'] = '1 char';
150            }
151            $a['fields'][] = new EnumStub($field);
152        }
153
154        return $a;
155    }
156}