SQLiteBuilder.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. use Illuminate\Database\QueryException;
  4. use Illuminate\Support\Facades\File;
  5. class SQLiteBuilder extends Builder
  6. {
  7. /**
  8. * Create a database in the schema.
  9. *
  10. * @param string $name
  11. * @return bool
  12. */
  13. public function createDatabase($name)
  14. {
  15. return File::put($name, '') !== false;
  16. }
  17. /**
  18. * Drop a database from the schema if the database exists.
  19. *
  20. * @param string $name
  21. * @return bool
  22. */
  23. public function dropDatabaseIfExists($name)
  24. {
  25. return File::exists($name)
  26. ? File::delete($name)
  27. : true;
  28. }
  29. /**
  30. * Get the tables for the database.
  31. *
  32. * @param bool $withSize
  33. * @return array
  34. */
  35. public function getTables($withSize = true)
  36. {
  37. if ($withSize) {
  38. try {
  39. $withSize = $this->connection->scalar($this->grammar->compileDbstatExists());
  40. } catch (QueryException $e) {
  41. $withSize = false;
  42. }
  43. }
  44. return $this->connection->getPostProcessor()->processTables(
  45. $this->connection->selectFromWriteConnection($this->grammar->compileTables($withSize))
  46. );
  47. }
  48. /**
  49. * Get the columns for a given table.
  50. *
  51. * @param string $table
  52. * @return array
  53. */
  54. public function getColumns($table)
  55. {
  56. $table = $this->connection->getTablePrefix().$table;
  57. return $this->connection->getPostProcessor()->processColumns(
  58. $this->connection->selectFromWriteConnection($this->grammar->compileColumns($table)),
  59. $this->connection->scalar($this->grammar->compileSqlCreateStatement($table))
  60. );
  61. }
  62. /**
  63. * Drop all tables from the database.
  64. *
  65. * @return void
  66. */
  67. public function dropAllTables()
  68. {
  69. if ($this->connection->getDatabaseName() !== ':memory:') {
  70. return $this->refreshDatabaseFile();
  71. }
  72. $this->connection->select($this->grammar->compileEnableWriteableSchema());
  73. $this->connection->select($this->grammar->compileDropAllTables());
  74. $this->connection->select($this->grammar->compileDisableWriteableSchema());
  75. $this->connection->select($this->grammar->compileRebuild());
  76. }
  77. /**
  78. * Drop all views from the database.
  79. *
  80. * @return void
  81. */
  82. public function dropAllViews()
  83. {
  84. $this->connection->select($this->grammar->compileEnableWriteableSchema());
  85. $this->connection->select($this->grammar->compileDropAllViews());
  86. $this->connection->select($this->grammar->compileDisableWriteableSchema());
  87. $this->connection->select($this->grammar->compileRebuild());
  88. }
  89. /**
  90. * Empty the database file.
  91. *
  92. * @return void
  93. */
  94. public function refreshDatabaseFile()
  95. {
  96. file_put_contents($this->connection->getDatabaseName(), '');
  97. }
  98. }