88414e4ccff265d256f8695565fc636771e5f134
[yaffs-website] / var-dumper / Caster / PgSqlCaster.php
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 !== $s = pg_parameter_status($link, $v)) {
99                 $a['param'][$v] = $s;
100             }
101         }
102
103         $a['param']['client_encoding'] = pg_client_encoding($link);
104         $a['param'] = new EnumStub($a['param']);
105
106         return $a;
107     }
108
109     public static function castResult($result, array $a, Stub $stub, $isNested)
110     {
111         $a['num rows'] = pg_num_rows($result);
112         $a['status'] = pg_result_status($result);
113         if (isset(self::$resultStatus[$a['status']])) {
114             $a['status'] = new ConstStub(self::$resultStatus[$a['status']], $a['status']);
115         }
116         $a['command-completion tag'] = pg_result_status($result, PGSQL_STATUS_STRING);
117
118         if (-1 === $a['num rows']) {
119             foreach (self::$diagCodes as $k => $v) {
120                 $a['error'][$k] = pg_result_error_field($result, $v);
121             }
122         }
123
124         $a['affected rows'] = pg_affected_rows($result);
125         $a['last OID'] = pg_last_oid($result);
126
127         $fields = pg_num_fields($result);
128
129         for ($i = 0; $i < $fields; ++$i) {
130             $field = array(
131                 'name' => pg_field_name($result, $i),
132                 'table' => sprintf('%s (OID: %s)', pg_field_table($result, $i), pg_field_table($result, $i, true)),
133                 'type' => sprintf('%s (OID: %s)', pg_field_type($result, $i), pg_field_type_oid($result, $i)),
134                 'nullable' => (bool) pg_field_is_null($result, $i),
135                 'storage' => pg_field_size($result, $i).' bytes',
136                 'display' => pg_field_prtlen($result, $i).' chars',
137             );
138             if (' (OID: )' === $field['table']) {
139                 $field['table'] = null;
140             }
141             if ('-1 bytes' === $field['storage']) {
142                 $field['storage'] = 'variable size';
143             } elseif ('1 bytes' === $field['storage']) {
144                 $field['storage'] = '1 byte';
145             }
146             if ('1 chars' === $field['display']) {
147                 $field['display'] = '1 char';
148             }
149             $a['fields'][] = new EnumStub($field);
150         }
151
152         return $a;
153     }
154 }