PostgresConnection.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Illuminate\Database;
  3. use Exception;
  4. use Illuminate\Database\Query\Grammars\PostgresGrammar as QueryGrammar;
  5. use Illuminate\Database\Query\Processors\PostgresProcessor;
  6. use Illuminate\Database\Schema\Grammars\PostgresGrammar as SchemaGrammar;
  7. use Illuminate\Database\Schema\PostgresBuilder;
  8. use Illuminate\Database\Schema\PostgresSchemaState;
  9. use Illuminate\Filesystem\Filesystem;
  10. class PostgresConnection extends Connection
  11. {
  12. /**
  13. * Escape a binary value for safe SQL embedding.
  14. *
  15. * @param string $value
  16. * @return string
  17. */
  18. protected function escapeBinary($value)
  19. {
  20. $hex = bin2hex($value);
  21. return "'\x{$hex}'::bytea";
  22. }
  23. /**
  24. * Escape a bool value for safe SQL embedding.
  25. *
  26. * @param bool $value
  27. * @return string
  28. */
  29. protected function escapeBool($value)
  30. {
  31. return $value ? 'true' : 'false';
  32. }
  33. /**
  34. * Determine if the given database exception was caused by a unique constraint violation.
  35. *
  36. * @param \Exception $exception
  37. * @return bool
  38. */
  39. protected function isUniqueConstraintError(Exception $exception)
  40. {
  41. return '23505' === $exception->getCode();
  42. }
  43. /**
  44. * Get the default query grammar instance.
  45. *
  46. * @return \Illuminate\Database\Query\Grammars\PostgresGrammar
  47. */
  48. protected function getDefaultQueryGrammar()
  49. {
  50. ($grammar = new QueryGrammar)->setConnection($this);
  51. return $this->withTablePrefix($grammar);
  52. }
  53. /**
  54. * Get a schema builder instance for the connection.
  55. *
  56. * @return \Illuminate\Database\Schema\PostgresBuilder
  57. */
  58. public function getSchemaBuilder()
  59. {
  60. if (is_null($this->schemaGrammar)) {
  61. $this->useDefaultSchemaGrammar();
  62. }
  63. return new PostgresBuilder($this);
  64. }
  65. /**
  66. * Get the default schema grammar instance.
  67. *
  68. * @return \Illuminate\Database\Schema\Grammars\PostgresGrammar
  69. */
  70. protected function getDefaultSchemaGrammar()
  71. {
  72. ($grammar = new SchemaGrammar)->setConnection($this);
  73. return $this->withTablePrefix($grammar);
  74. }
  75. /**
  76. * Get the schema state for the connection.
  77. *
  78. * @param \Illuminate\Filesystem\Filesystem|null $files
  79. * @param callable|null $processFactory
  80. * @return \Illuminate\Database\Schema\PostgresSchemaState
  81. */
  82. public function getSchemaState(?Filesystem $files = null, ?callable $processFactory = null)
  83. {
  84. return new PostgresSchemaState($this, $files, $processFactory);
  85. }
  86. /**
  87. * Get the default post processor instance.
  88. *
  89. * @return \Illuminate\Database\Query\Processors\PostgresProcessor
  90. */
  91. protected function getDefaultPostProcessor()
  92. {
  93. return new PostgresProcessor;
  94. }
  95. }