PgSqlCaster.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 pgsql resources to array representation.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. *
  17. * @final
  18. *
  19. * @internal since Symfony 7.3
  20. */
  21. class PgSqlCaster
  22. {
  23. private const PARAM_CODES = [
  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. private const TRANSACTION_STATUS = [
  36. \PGSQL_TRANSACTION_IDLE => 'PGSQL_TRANSACTION_IDLE',
  37. \PGSQL_TRANSACTION_ACTIVE => 'PGSQL_TRANSACTION_ACTIVE',
  38. \PGSQL_TRANSACTION_INTRANS => 'PGSQL_TRANSACTION_INTRANS',
  39. \PGSQL_TRANSACTION_INERROR => 'PGSQL_TRANSACTION_INERROR',
  40. \PGSQL_TRANSACTION_UNKNOWN => 'PGSQL_TRANSACTION_UNKNOWN',
  41. ];
  42. private const RESULT_STATUS = [
  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. private const DIAG_CODES = [
  53. 'severity' => \PGSQL_DIAG_SEVERITY,
  54. 'sqlstate' => \PGSQL_DIAG_SQLSTATE,
  55. 'message' => \PGSQL_DIAG_MESSAGE_PRIMARY,
  56. 'detail' => \PGSQL_DIAG_MESSAGE_DETAIL,
  57. 'hint' => \PGSQL_DIAG_MESSAGE_HINT,
  58. 'statement position' => \PGSQL_DIAG_STATEMENT_POSITION,
  59. 'internal position' => \PGSQL_DIAG_INTERNAL_POSITION,
  60. 'internal query' => \PGSQL_DIAG_INTERNAL_QUERY,
  61. 'context' => \PGSQL_DIAG_CONTEXT,
  62. 'file' => \PGSQL_DIAG_SOURCE_FILE,
  63. 'line' => \PGSQL_DIAG_SOURCE_LINE,
  64. 'function' => \PGSQL_DIAG_SOURCE_FUNCTION,
  65. ];
  66. public static function castLargeObject($lo, array $a, Stub $stub, bool $isNested): array
  67. {
  68. $a['seek position'] = pg_lo_tell($lo);
  69. return $a;
  70. }
  71. public static function castLink($link, array $a, Stub $stub, bool $isNested): array
  72. {
  73. $a['status'] = pg_connection_status($link);
  74. $a['status'] = new ConstStub(\PGSQL_CONNECTION_OK === $a['status'] ? 'PGSQL_CONNECTION_OK' : 'PGSQL_CONNECTION_BAD', $a['status']);
  75. $a['busy'] = pg_connection_busy($link);
  76. $a['transaction'] = pg_transaction_status($link);
  77. if (isset(self::TRANSACTION_STATUS[$a['transaction']])) {
  78. $a['transaction'] = new ConstStub(self::TRANSACTION_STATUS[$a['transaction']], $a['transaction']);
  79. }
  80. $a['pid'] = pg_get_pid($link);
  81. $a['last error'] = pg_last_error($link);
  82. $a['last notice'] = pg_last_notice($link);
  83. $a['host'] = pg_host($link);
  84. $a['port'] = pg_port($link);
  85. $a['dbname'] = pg_dbname($link);
  86. $a['options'] = pg_options($link);
  87. $a['version'] = pg_version($link);
  88. foreach (self::PARAM_CODES as $v) {
  89. if (false !== $s = pg_parameter_status($link, $v)) {
  90. $a['param'][$v] = $s;
  91. }
  92. }
  93. $a['param']['client_encoding'] = pg_client_encoding($link);
  94. $a['param'] = new EnumStub($a['param']);
  95. return $a;
  96. }
  97. public static function castResult($result, array $a, Stub $stub, bool $isNested): array
  98. {
  99. $a['num rows'] = pg_num_rows($result);
  100. $a['status'] = pg_result_status($result);
  101. if (isset(self::RESULT_STATUS[$a['status']])) {
  102. $a['status'] = new ConstStub(self::RESULT_STATUS[$a['status']], $a['status']);
  103. }
  104. $a['command-completion tag'] = pg_result_status($result, \PGSQL_STATUS_STRING);
  105. if (-1 === $a['num rows']) {
  106. foreach (self::DIAG_CODES as $k => $v) {
  107. $a['error'][$k] = pg_result_error_field($result, $v);
  108. }
  109. }
  110. $a['affected rows'] = pg_affected_rows($result);
  111. $a['last OID'] = pg_last_oid($result);
  112. $fields = pg_num_fields($result);
  113. for ($i = 0; $i < $fields; ++$i) {
  114. $field = [
  115. 'name' => pg_field_name($result, $i),
  116. 'table' => \sprintf('%s (OID: %s)', pg_field_table($result, $i), pg_field_table($result, $i, true)),
  117. 'type' => \sprintf('%s (OID: %s)', pg_field_type($result, $i), pg_field_type_oid($result, $i)),
  118. 'nullable' => (bool) (\PHP_VERSION_ID >= 80300 ? pg_field_is_null($result, null, $i) : pg_field_is_null($result, $i)),
  119. 'storage' => pg_field_size($result, $i).' bytes',
  120. 'display' => (\PHP_VERSION_ID >= 80300 ? pg_field_prtlen($result, null, $i) : pg_field_prtlen($result, $i)).' chars',
  121. ];
  122. if (' (OID: )' === $field['table']) {
  123. $field['table'] = null;
  124. }
  125. if ('-1 bytes' === $field['storage']) {
  126. $field['storage'] = 'variable size';
  127. } elseif ('1 bytes' === $field['storage']) {
  128. $field['storage'] = '1 byte';
  129. }
  130. if ('1 chars' === $field['display']) {
  131. $field['display'] = '1 char';
  132. }
  133. $a['fields'][] = new EnumStub($field);
  134. }
  135. return $a;
  136. }
  137. }