Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\VarDumper\Caster; Chris@0: Chris@0: use Symfony\Component\VarDumper\Cloner\Stub; Chris@0: Chris@0: /** Chris@0: * Casts pqsql resources to array representation. Chris@0: * Chris@0: * @author Nicolas Grekas
Chris@0: */ Chris@0: class PgSqlCaster Chris@0: { Chris@17: private static $paramCodes = [ Chris@0: 'server_encoding', Chris@0: 'client_encoding', Chris@0: 'is_superuser', Chris@0: 'session_authorization', Chris@0: 'DateStyle', Chris@0: 'TimeZone', Chris@0: 'IntervalStyle', Chris@0: 'integer_datetimes', Chris@0: 'application_name', Chris@0: 'standard_conforming_strings', Chris@17: ]; Chris@0: Chris@17: private static $transactionStatus = [ Chris@0: PGSQL_TRANSACTION_IDLE => 'PGSQL_TRANSACTION_IDLE', Chris@0: PGSQL_TRANSACTION_ACTIVE => 'PGSQL_TRANSACTION_ACTIVE', Chris@0: PGSQL_TRANSACTION_INTRANS => 'PGSQL_TRANSACTION_INTRANS', Chris@0: PGSQL_TRANSACTION_INERROR => 'PGSQL_TRANSACTION_INERROR', Chris@0: PGSQL_TRANSACTION_UNKNOWN => 'PGSQL_TRANSACTION_UNKNOWN', Chris@17: ]; Chris@0: Chris@17: private static $resultStatus = [ Chris@0: PGSQL_EMPTY_QUERY => 'PGSQL_EMPTY_QUERY', Chris@0: PGSQL_COMMAND_OK => 'PGSQL_COMMAND_OK', Chris@0: PGSQL_TUPLES_OK => 'PGSQL_TUPLES_OK', Chris@0: PGSQL_COPY_OUT => 'PGSQL_COPY_OUT', Chris@0: PGSQL_COPY_IN => 'PGSQL_COPY_IN', Chris@0: PGSQL_BAD_RESPONSE => 'PGSQL_BAD_RESPONSE', Chris@0: PGSQL_NONFATAL_ERROR => 'PGSQL_NONFATAL_ERROR', Chris@0: PGSQL_FATAL_ERROR => 'PGSQL_FATAL_ERROR', Chris@17: ]; Chris@0: Chris@17: private static $diagCodes = [ Chris@0: 'severity' => PGSQL_DIAG_SEVERITY, Chris@0: 'sqlstate' => PGSQL_DIAG_SQLSTATE, Chris@0: 'message' => PGSQL_DIAG_MESSAGE_PRIMARY, Chris@0: 'detail' => PGSQL_DIAG_MESSAGE_DETAIL, Chris@0: 'hint' => PGSQL_DIAG_MESSAGE_HINT, Chris@0: 'statement position' => PGSQL_DIAG_STATEMENT_POSITION, Chris@0: 'internal position' => PGSQL_DIAG_INTERNAL_POSITION, Chris@0: 'internal query' => PGSQL_DIAG_INTERNAL_QUERY, Chris@0: 'context' => PGSQL_DIAG_CONTEXT, Chris@0: 'file' => PGSQL_DIAG_SOURCE_FILE, Chris@0: 'line' => PGSQL_DIAG_SOURCE_LINE, Chris@0: 'function' => PGSQL_DIAG_SOURCE_FUNCTION, Chris@17: ]; Chris@0: Chris@0: public static function castLargeObject($lo, array $a, Stub $stub, $isNested) Chris@0: { Chris@0: $a['seek position'] = pg_lo_tell($lo); Chris@0: Chris@0: return $a; Chris@0: } Chris@0: Chris@0: public static function castLink($link, array $a, Stub $stub, $isNested) Chris@0: { Chris@0: $a['status'] = pg_connection_status($link); Chris@0: $a['status'] = new ConstStub(PGSQL_CONNECTION_OK === $a['status'] ? 'PGSQL_CONNECTION_OK' : 'PGSQL_CONNECTION_BAD', $a['status']); Chris@0: $a['busy'] = pg_connection_busy($link); Chris@0: Chris@0: $a['transaction'] = pg_transaction_status($link); Chris@0: if (isset(self::$transactionStatus[$a['transaction']])) { Chris@0: $a['transaction'] = new ConstStub(self::$transactionStatus[$a['transaction']], $a['transaction']); Chris@0: } Chris@0: Chris@0: $a['pid'] = pg_get_pid($link); Chris@0: $a['last error'] = pg_last_error($link); Chris@0: $a['last notice'] = pg_last_notice($link); Chris@0: $a['host'] = pg_host($link); Chris@0: $a['port'] = pg_port($link); Chris@0: $a['dbname'] = pg_dbname($link); Chris@0: $a['options'] = pg_options($link); Chris@0: $a['version'] = pg_version($link); Chris@0: Chris@0: foreach (self::$paramCodes as $v) { Chris@0: if (false !== $s = pg_parameter_status($link, $v)) { Chris@0: $a['param'][$v] = $s; Chris@0: } Chris@0: } Chris@0: Chris@0: $a['param']['client_encoding'] = pg_client_encoding($link); Chris@0: $a['param'] = new EnumStub($a['param']); Chris@0: Chris@0: return $a; Chris@0: } Chris@0: Chris@0: public static function castResult($result, array $a, Stub $stub, $isNested) Chris@0: { Chris@0: $a['num rows'] = pg_num_rows($result); Chris@0: $a['status'] = pg_result_status($result); Chris@0: if (isset(self::$resultStatus[$a['status']])) { Chris@0: $a['status'] = new ConstStub(self::$resultStatus[$a['status']], $a['status']); Chris@0: } Chris@0: $a['command-completion tag'] = pg_result_status($result, PGSQL_STATUS_STRING); Chris@0: Chris@0: if (-1 === $a['num rows']) { Chris@0: foreach (self::$diagCodes as $k => $v) { Chris@0: $a['error'][$k] = pg_result_error_field($result, $v); Chris@0: } Chris@0: } Chris@0: Chris@0: $a['affected rows'] = pg_affected_rows($result); Chris@0: $a['last OID'] = pg_last_oid($result); Chris@0: Chris@0: $fields = pg_num_fields($result); Chris@0: Chris@0: for ($i = 0; $i < $fields; ++$i) { Chris@17: $field = [ Chris@0: 'name' => pg_field_name($result, $i), Chris@0: 'table' => sprintf('%s (OID: %s)', pg_field_table($result, $i), pg_field_table($result, $i, true)), Chris@0: 'type' => sprintf('%s (OID: %s)', pg_field_type($result, $i), pg_field_type_oid($result, $i)), Chris@0: 'nullable' => (bool) pg_field_is_null($result, $i), Chris@0: 'storage' => pg_field_size($result, $i).' bytes', Chris@0: 'display' => pg_field_prtlen($result, $i).' chars', Chris@17: ]; Chris@0: if (' (OID: )' === $field['table']) { Chris@0: $field['table'] = null; Chris@0: } Chris@0: if ('-1 bytes' === $field['storage']) { Chris@0: $field['storage'] = 'variable size'; Chris@0: } elseif ('1 bytes' === $field['storage']) { Chris@0: $field['storage'] = '1 byte'; Chris@0: } Chris@0: if ('1 chars' === $field['display']) { Chris@0: $field['display'] = '1 char'; Chris@0: } Chris@0: $a['fields'][] = new EnumStub($field); Chris@0: } Chris@0: Chris@0: return $a; Chris@0: } Chris@0: }