SQLiteConnection.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Illuminate\Database;
  3. use Exception;
  4. use Illuminate\Database\Query\Grammars\SQLiteGrammar as QueryGrammar;
  5. use Illuminate\Database\Query\Processors\SQLiteProcessor;
  6. use Illuminate\Database\Schema\Grammars\SQLiteGrammar as SchemaGrammar;
  7. use Illuminate\Database\Schema\SQLiteBuilder;
  8. use Illuminate\Database\Schema\SqliteSchemaState;
  9. use Illuminate\Filesystem\Filesystem;
  10. class SQLiteConnection extends Connection
  11. {
  12. /**
  13. * Create a new database connection instance.
  14. *
  15. * @param \PDO|\Closure $pdo
  16. * @param string $database
  17. * @param string $tablePrefix
  18. * @param array $config
  19. * @return void
  20. */
  21. public function __construct($pdo, $database = '', $tablePrefix = '', array $config = [])
  22. {
  23. parent::__construct($pdo, $database, $tablePrefix, $config);
  24. $enableForeignKeyConstraints = $this->getForeignKeyConstraintsConfigurationValue();
  25. if ($enableForeignKeyConstraints === null) {
  26. return;
  27. }
  28. $schemaBuilder = $this->getSchemaBuilder();
  29. try {
  30. $enableForeignKeyConstraints
  31. ? $schemaBuilder->enableForeignKeyConstraints()
  32. : $schemaBuilder->disableForeignKeyConstraints();
  33. } catch (QueryException $e) {
  34. if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
  35. throw $e;
  36. }
  37. }
  38. }
  39. /**
  40. * Escape a binary value for safe SQL embedding.
  41. *
  42. * @param string $value
  43. * @return string
  44. */
  45. protected function escapeBinary($value)
  46. {
  47. $hex = bin2hex($value);
  48. return "x'{$hex}'";
  49. }
  50. /**
  51. * Determine if the given database exception was caused by a unique constraint violation.
  52. *
  53. * @param \Exception $exception
  54. * @return bool
  55. */
  56. protected function isUniqueConstraintError(Exception $exception)
  57. {
  58. return boolval(preg_match('#(column(s)? .* (is|are) not unique|UNIQUE constraint failed: .*)#i', $exception->getMessage()));
  59. }
  60. /**
  61. * Get the default query grammar instance.
  62. *
  63. * @return \Illuminate\Database\Query\Grammars\SQLiteGrammar
  64. */
  65. protected function getDefaultQueryGrammar()
  66. {
  67. ($grammar = new QueryGrammar)->setConnection($this);
  68. return $this->withTablePrefix($grammar);
  69. }
  70. /**
  71. * Get a schema builder instance for the connection.
  72. *
  73. * @return \Illuminate\Database\Schema\SQLiteBuilder
  74. */
  75. public function getSchemaBuilder()
  76. {
  77. if (is_null($this->schemaGrammar)) {
  78. $this->useDefaultSchemaGrammar();
  79. }
  80. return new SQLiteBuilder($this);
  81. }
  82. /**
  83. * Get the default schema grammar instance.
  84. *
  85. * @return \Illuminate\Database\Schema\Grammars\SQLiteGrammar
  86. */
  87. protected function getDefaultSchemaGrammar()
  88. {
  89. ($grammar = new SchemaGrammar)->setConnection($this);
  90. return $this->withTablePrefix($grammar);
  91. }
  92. /**
  93. * Get the schema state for the connection.
  94. *
  95. * @param \Illuminate\Filesystem\Filesystem|null $files
  96. * @param callable|null $processFactory
  97. *
  98. * @throws \RuntimeException
  99. */
  100. public function getSchemaState(?Filesystem $files = null, ?callable $processFactory = null)
  101. {
  102. return new SqliteSchemaState($this, $files, $processFactory);
  103. }
  104. /**
  105. * Get the default post processor instance.
  106. *
  107. * @return \Illuminate\Database\Query\Processors\SQLiteProcessor
  108. */
  109. protected function getDefaultPostProcessor()
  110. {
  111. return new SQLiteProcessor;
  112. }
  113. /**
  114. * Get the database connection foreign key constraints configuration option.
  115. *
  116. * @return bool|null
  117. */
  118. protected function getForeignKeyConstraintsConfigurationValue()
  119. {
  120. return $this->getConfig('foreign_key_constraints');
  121. }
  122. }