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