Skip to content

Commit a672efb

Browse files
[VarDumper] Add caster for pgsql resources
1 parent 7f745d7 commit a672efb

File tree

2 files changed

+157
-0
lines changed

2 files changed

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

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ abstract class AbstractCloner implements ClonerInterface
9898
':dba persistent' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
9999
':gd' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castGd',
100100
':mysql link' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castMysqlLink',
101+
':pgsql large object' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castLargeObject',
102+
':pgsql link' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castLink',
103+
':pgsql link persistent' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castLink',
104+
':pgsql result' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castResult',
101105
':process' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castProcess',
102106
':stream' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStream',
103107
':stream-context' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStreamContext',

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy