Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a30b3c

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

File tree

2 files changed

+152
-0
lines changed

2 files changed

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

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)