菜谱项目

PgSqlCaster.php 5.3KB

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