MySqlBuilder.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Database\Schema;
  12. use Hyperf\Database\Query\Processors\MySqlProcessor;
  13. class MySqlBuilder extends Builder
  14. {
  15. /**
  16. * Determine if the given table exists.
  17. *
  18. * @param string $table
  19. */
  20. public function hasTable($table): bool
  21. {
  22. $table = $this->connection->getTablePrefix() . $table;
  23. return count($this->connection->select(
  24. $this->grammar->compileTableExists(),
  25. [$this->connection->getDatabaseName(), $table]
  26. )) > 0;
  27. }
  28. /**
  29. * Get the column listing for a given table.
  30. *
  31. * @param string $table
  32. */
  33. public function getColumnListing($table): array
  34. {
  35. $table = $this->connection->getTablePrefix() . $table;
  36. $results = $this->connection->select(
  37. $this->grammar->compileColumnListing(),
  38. [$this->connection->getDatabaseName(), $table]
  39. );
  40. return $this->connection->getPostProcessor()->processColumnListing($results);
  41. }
  42. /**
  43. * Get the columns.
  44. */
  45. public function getColumns(): array
  46. {
  47. $results = $this->connection->select(
  48. $this->grammar->compileColumns(),
  49. [
  50. $this->connection->getDatabaseName(),
  51. ]
  52. );
  53. return $this->connection->getPostProcessor()->processColumns($results);
  54. }
  55. /**
  56. * Get the column type listing for a given table.
  57. *
  58. * @param string $table
  59. * @return array
  60. */
  61. public function getColumnTypeListing($table)
  62. {
  63. $table = $this->connection->getTablePrefix() . $table;
  64. $results = $this->connection->select(
  65. $this->grammar->compileColumnListing(),
  66. [$this->connection->getDatabaseName(), $table]
  67. );
  68. /** @var MySqlProcessor $processor */
  69. $processor = $this->connection->getPostProcessor();
  70. return $processor->processListing($results);
  71. }
  72. /**
  73. * Drop all tables from the database.
  74. */
  75. public function dropAllTables(): void
  76. {
  77. $tables = [];
  78. foreach ($this->getAllTables() as $row) {
  79. $row = (array) $row;
  80. $tables[] = reset($row);
  81. }
  82. if (empty($tables)) {
  83. return;
  84. }
  85. $this->disableForeignKeyConstraints();
  86. $this->connection->statement(
  87. $this->grammar->compileDropAllTables($tables)
  88. );
  89. $this->enableForeignKeyConstraints();
  90. }
  91. /**
  92. * Drop all views from the database.
  93. */
  94. public function dropAllViews(): void
  95. {
  96. $views = [];
  97. foreach ($this->getAllViews() as $row) {
  98. $row = (array) $row;
  99. $views[] = reset($row);
  100. }
  101. if (empty($views)) {
  102. return;
  103. }
  104. $this->connection->statement(
  105. $this->grammar->compileDropAllViews($views)
  106. );
  107. }
  108. /**
  109. * Get all the table names for the database.
  110. *
  111. * @return array
  112. */
  113. public function getAllTables()
  114. {
  115. return $this->connection->select(
  116. $this->grammar->compileGetAllTables()
  117. );
  118. }
  119. /**
  120. * Get all the view names for the database.
  121. *
  122. * @return array
  123. */
  124. protected function getAllViews()
  125. {
  126. return $this->connection->select(
  127. $this->grammar->compileGetAllViews()
  128. );
  129. }
  130. }