SQLiteConnection.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Illuminate\Database;
  3. use Doctrine\DBAL\Driver\PDOSqlite\Driver as DoctrineDriver;
  4. use Doctrine\DBAL\Version;
  5. use Illuminate\Database\PDO\SQLiteDriver;
  6. use Illuminate\Database\Query\Grammars\SQLiteGrammar as QueryGrammar;
  7. use Illuminate\Database\Query\Processors\SQLiteProcessor;
  8. use Illuminate\Database\Schema\Grammars\SQLiteGrammar as SchemaGrammar;
  9. use Illuminate\Database\Schema\SQLiteBuilder;
  10. use Illuminate\Database\Schema\SqliteSchemaState;
  11. use Illuminate\Filesystem\Filesystem;
  12. class SQLiteConnection extends Connection
  13. {
  14. /**
  15. * Create a new database connection instance.
  16. *
  17. * @param \PDO|\Closure $pdo
  18. * @param string $database
  19. * @param string $tablePrefix
  20. * @param array $config
  21. * @return void
  22. */
  23. public function __construct($pdo, $database = '', $tablePrefix = '', array $config = [])
  24. {
  25. parent::__construct($pdo, $database, $tablePrefix, $config);
  26. $enableForeignKeyConstraints = $this->getForeignKeyConstraintsConfigurationValue();
  27. if ($enableForeignKeyConstraints === null) {
  28. return;
  29. }
  30. $enableForeignKeyConstraints
  31. ? $this->getSchemaBuilder()->enableForeignKeyConstraints()
  32. : $this->getSchemaBuilder()->disableForeignKeyConstraints();
  33. }
  34. /**
  35. * Get the default query grammar instance.
  36. *
  37. * @return \Illuminate\Database\Query\Grammars\SQLiteGrammar
  38. */
  39. protected function getDefaultQueryGrammar()
  40. {
  41. return $this->withTablePrefix(new QueryGrammar);
  42. }
  43. /**
  44. * Get a schema builder instance for the connection.
  45. *
  46. * @return \Illuminate\Database\Schema\SQLiteBuilder
  47. */
  48. public function getSchemaBuilder()
  49. {
  50. if (is_null($this->schemaGrammar)) {
  51. $this->useDefaultSchemaGrammar();
  52. }
  53. return new SQLiteBuilder($this);
  54. }
  55. /**
  56. * Get the default schema grammar instance.
  57. *
  58. * @return \Illuminate\Database\Schema\Grammars\SQLiteGrammar
  59. */
  60. protected function getDefaultSchemaGrammar()
  61. {
  62. return $this->withTablePrefix(new SchemaGrammar);
  63. }
  64. /**
  65. * Get the schema state for the connection.
  66. *
  67. * @param \Illuminate\Filesystem\Filesystem|null $files
  68. * @param callable|null $processFactory
  69. *
  70. * @throws \RuntimeException
  71. */
  72. public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
  73. {
  74. return new SqliteSchemaState($this, $files, $processFactory);
  75. }
  76. /**
  77. * Get the default post processor instance.
  78. *
  79. * @return \Illuminate\Database\Query\Processors\SQLiteProcessor
  80. */
  81. protected function getDefaultPostProcessor()
  82. {
  83. return new SQLiteProcessor;
  84. }
  85. /**
  86. * Get the Doctrine DBAL driver.
  87. *
  88. * @return \Doctrine\DBAL\Driver\PDOSqlite\Driver|\Illuminate\Database\PDO\SQLiteDriver
  89. */
  90. protected function getDoctrineDriver()
  91. {
  92. return class_exists(Version::class) ? new DoctrineDriver : new SQLiteDriver;
  93. }
  94. /**
  95. * Get the database connection foreign key constraints configuration option.
  96. *
  97. * @return bool|null
  98. */
  99. protected function getForeignKeyConstraintsConfigurationValue()
  100. {
  101. return $this->getConfig('foreign_key_constraints');
  102. }
  103. }