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