You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.4 KiB

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