MySqlBuilder.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. class MySqlBuilder extends Builder
  4. {
  5. /**
  6. * Create a database in the schema.
  7. *
  8. * @param string $name
  9. * @return bool
  10. */
  11. public function createDatabase($name)
  12. {
  13. return $this->connection->statement(
  14. $this->grammar->compileCreateDatabase($name, $this->connection)
  15. );
  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 $this->connection->statement(
  26. $this->grammar->compileDropDatabaseIfExists($name)
  27. );
  28. }
  29. /**
  30. * Get the tables for the database.
  31. *
  32. * @return array
  33. */
  34. public function getTables()
  35. {
  36. return $this->connection->getPostProcessor()->processTables(
  37. $this->connection->selectFromWriteConnection(
  38. $this->grammar->compileTables($this->connection->getDatabaseName())
  39. )
  40. );
  41. }
  42. /**
  43. * Get the views for the database.
  44. *
  45. * @return array
  46. */
  47. public function getViews()
  48. {
  49. return $this->connection->getPostProcessor()->processViews(
  50. $this->connection->selectFromWriteConnection(
  51. $this->grammar->compileViews($this->connection->getDatabaseName())
  52. )
  53. );
  54. }
  55. /**
  56. * Get the columns for a given table.
  57. *
  58. * @param string $table
  59. * @return array
  60. */
  61. public function getColumns($table)
  62. {
  63. $table = $this->connection->getTablePrefix().$table;
  64. $results = $this->connection->selectFromWriteConnection(
  65. $this->grammar->compileColumns($this->connection->getDatabaseName(), $table)
  66. );
  67. return $this->connection->getPostProcessor()->processColumns($results);
  68. }
  69. /**
  70. * Get the indexes for a given table.
  71. *
  72. * @param string $table
  73. * @return array
  74. */
  75. public function getIndexes($table)
  76. {
  77. $table = $this->connection->getTablePrefix().$table;
  78. return $this->connection->getPostProcessor()->processIndexes(
  79. $this->connection->selectFromWriteConnection(
  80. $this->grammar->compileIndexes($this->connection->getDatabaseName(), $table)
  81. )
  82. );
  83. }
  84. /**
  85. * Get the foreign keys for a given table.
  86. *
  87. * @param string $table
  88. * @return array
  89. */
  90. public function getForeignKeys($table)
  91. {
  92. $table = $this->connection->getTablePrefix().$table;
  93. return $this->connection->getPostProcessor()->processForeignKeys(
  94. $this->connection->selectFromWriteConnection(
  95. $this->grammar->compileForeignKeys($this->connection->getDatabaseName(), $table)
  96. )
  97. );
  98. }
  99. /**
  100. * Drop all tables from the database.
  101. *
  102. * @return void
  103. */
  104. public function dropAllTables()
  105. {
  106. $tables = array_column($this->getTables(), 'name');
  107. if (empty($tables)) {
  108. return;
  109. }
  110. $this->disableForeignKeyConstraints();
  111. $this->connection->statement(
  112. $this->grammar->compileDropAllTables($tables)
  113. );
  114. $this->enableForeignKeyConstraints();
  115. }
  116. /**
  117. * Drop all views from the database.
  118. *
  119. * @return void
  120. */
  121. public function dropAllViews()
  122. {
  123. $views = array_column($this->getViews(), 'name');
  124. if (empty($views)) {
  125. return;
  126. }
  127. $this->connection->statement(
  128. $this->grammar->compileDropAllViews($views)
  129. );
  130. }
  131. }