PostgresProcessor.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Illuminate\Database\Query\Processors;
  3. use Illuminate\Database\Query\Builder;
  4. class PostgresProcessor extends Processor
  5. {
  6. /**
  7. * Process an "insert get ID" query.
  8. *
  9. * @param \Illuminate\Database\Query\Builder $query
  10. * @param string $sql
  11. * @param array $values
  12. * @param string|null $sequence
  13. * @return int
  14. */
  15. public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
  16. {
  17. $connection = $query->getConnection();
  18. $connection->recordsHaveBeenModified();
  19. $result = $connection->selectFromWriteConnection($sql, $values)[0];
  20. $sequence = $sequence ?: 'id';
  21. $id = is_object($result) ? $result->{$sequence} : $result[$sequence];
  22. return is_numeric($id) ? (int) $id : $id;
  23. }
  24. /**
  25. * Process the results of a types query.
  26. *
  27. * @param array $results
  28. * @return array
  29. */
  30. public function processTypes($results)
  31. {
  32. return array_map(function ($result) {
  33. $result = (object) $result;
  34. return [
  35. 'name' => $result->name,
  36. 'schema' => $result->schema,
  37. 'implicit' => (bool) $result->implicit,
  38. 'type' => match (strtolower($result->type)) {
  39. 'b' => 'base',
  40. 'c' => 'composite',
  41. 'd' => 'domain',
  42. 'e' => 'enum',
  43. 'p' => 'pseudo',
  44. 'r' => 'range',
  45. 'm' => 'multirange',
  46. default => null,
  47. },
  48. 'category' => match (strtolower($result->category)) {
  49. 'a' => 'array',
  50. 'b' => 'boolean',
  51. 'c' => 'composite',
  52. 'd' => 'date_time',
  53. 'e' => 'enum',
  54. 'g' => 'geometric',
  55. 'i' => 'network_address',
  56. 'n' => 'numeric',
  57. 'p' => 'pseudo',
  58. 'r' => 'range',
  59. 's' => 'string',
  60. 't' => 'timespan',
  61. 'u' => 'user_defined',
  62. 'v' => 'bit_string',
  63. 'x' => 'unknown',
  64. 'z' => 'internal_use',
  65. default => null,
  66. },
  67. ];
  68. }, $results);
  69. }
  70. /**
  71. * Process the results of a columns query.
  72. *
  73. * @param array $results
  74. * @return array
  75. */
  76. public function processColumns($results)
  77. {
  78. return array_map(function ($result) {
  79. $result = (object) $result;
  80. $autoincrement = $result->default !== null && str_starts_with($result->default, 'nextval(');
  81. return [
  82. 'name' => $result->name,
  83. 'type_name' => $result->type_name,
  84. 'type' => $result->type,
  85. 'collation' => $result->collation,
  86. 'nullable' => (bool) $result->nullable,
  87. 'default' => $result->generated ? null : $result->default,
  88. 'auto_increment' => $autoincrement,
  89. 'comment' => $result->comment,
  90. 'generation' => $result->generated ? [
  91. 'type' => match ($result->generated) {
  92. 's' => 'stored',
  93. default => null,
  94. },
  95. 'expression' => $result->default,
  96. ] : null,
  97. ];
  98. }, $results);
  99. }
  100. /**
  101. * Process the results of an indexes query.
  102. *
  103. * @param array $results
  104. * @return array
  105. */
  106. public function processIndexes($results)
  107. {
  108. return array_map(function ($result) {
  109. $result = (object) $result;
  110. return [
  111. 'name' => strtolower($result->name),
  112. 'columns' => explode(',', $result->columns),
  113. 'type' => strtolower($result->type),
  114. 'unique' => (bool) $result->unique,
  115. 'primary' => (bool) $result->primary,
  116. ];
  117. }, $results);
  118. }
  119. /**
  120. * Process the results of a foreign keys query.
  121. *
  122. * @param array $results
  123. * @return array
  124. */
  125. public function processForeignKeys($results)
  126. {
  127. return array_map(function ($result) {
  128. $result = (object) $result;
  129. return [
  130. 'name' => $result->name,
  131. 'columns' => explode(',', $result->columns),
  132. 'foreign_schema' => $result->foreign_schema,
  133. 'foreign_table' => $result->foreign_table,
  134. 'foreign_columns' => explode(',', $result->foreign_columns),
  135. 'on_update' => match (strtolower($result->on_update)) {
  136. 'a' => 'no action',
  137. 'r' => 'restrict',
  138. 'c' => 'cascade',
  139. 'n' => 'set null',
  140. 'd' => 'set default',
  141. default => null,
  142. },
  143. 'on_delete' => match (strtolower($result->on_delete)) {
  144. 'a' => 'no action',
  145. 'r' => 'restrict',
  146. 'c' => 'cascade',
  147. 'n' => 'set null',
  148. 'd' => 'set default',
  149. default => null,
  150. },
  151. ];
  152. }, $results);
  153. }
  154. }