PostgresBuilder.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. class PostgresBuilder extends Builder
  4. {
  5. /**
  6. * Determine if the given table exists.
  7. *
  8. * @param string $table
  9. * @return bool
  10. */
  11. public function hasTable($table)
  12. {
  13. [$schema, $table] = $this->parseSchemaAndTable($table);
  14. $table = $this->connection->getTablePrefix().$table;
  15. return count($this->connection->select(
  16. $this->grammar->compileTableExists(), [$schema, $table]
  17. )) > 0;
  18. }
  19. /**
  20. * Drop all tables from the database.
  21. *
  22. * @return void
  23. */
  24. public function dropAllTables()
  25. {
  26. $tables = [];
  27. $excludedTables = ['spatial_ref_sys'];
  28. foreach ($this->getAllTables() as $row) {
  29. $row = (array) $row;
  30. $table = reset($row);
  31. if (! in_array($table, $excludedTables)) {
  32. $tables[] = $table;
  33. }
  34. }
  35. if (empty($tables)) {
  36. return;
  37. }
  38. $this->connection->statement(
  39. $this->grammar->compileDropAllTables($tables)
  40. );
  41. }
  42. /**
  43. * Drop all views from the database.
  44. *
  45. * @return void
  46. */
  47. public function dropAllViews()
  48. {
  49. $views = [];
  50. foreach ($this->getAllViews() as $row) {
  51. $row = (array) $row;
  52. $views[] = reset($row);
  53. }
  54. if (empty($views)) {
  55. return;
  56. }
  57. $this->connection->statement(
  58. $this->grammar->compileDropAllViews($views)
  59. );
  60. }
  61. /**
  62. * Drop all types from the database.
  63. */
  64. public function dropAllTypes()
  65. {
  66. $types = [];
  67. foreach ($this->getAllTypes() as $row) {
  68. $row = (array) $row;
  69. $types[] = reset($row);
  70. }
  71. if (empty($types)) {
  72. return;
  73. }
  74. $this->connection->statement(
  75. $this->grammar->compileDropAllTypes($types)
  76. );
  77. }
  78. /**
  79. * Get all of the table names for the database.
  80. *
  81. * @return array
  82. */
  83. protected function getAllTables()
  84. {
  85. return $this->connection->select(
  86. $this->grammar->compileGetAllTables($this->connection->getConfig('schema'))
  87. );
  88. }
  89. /**
  90. * Get all of the view names for the database.
  91. *
  92. * @return array
  93. */
  94. protected function getAllViews()
  95. {
  96. return $this->connection->select(
  97. $this->grammar->compileGetAllViews($this->connection->getConfig('schema'))
  98. );
  99. }
  100. /**
  101. * Get all of the type names for the database.
  102. *
  103. * @return array
  104. */
  105. protected function getAllTypes()
  106. {
  107. return $this->connection->select(
  108. $this->grammar->compileGetAllTypes()
  109. );
  110. }
  111. /**
  112. * Get the column listing for a given table.
  113. *
  114. * @param string $table
  115. * @return array
  116. */
  117. public function getColumnListing($table)
  118. {
  119. [$schema, $table] = $this->parseSchemaAndTable($table);
  120. $table = $this->connection->getTablePrefix().$table;
  121. $results = $this->connection->select(
  122. $this->grammar->compileColumnListing(), [$schema, $table]
  123. );
  124. return $this->connection->getPostProcessor()->processColumnListing($results);
  125. }
  126. /**
  127. * Parse the table name and extract the schema and table.
  128. *
  129. * @param string $table
  130. * @return array
  131. */
  132. protected function parseSchemaAndTable($table)
  133. {
  134. $table = explode('.', $table);
  135. if (is_array($schema = $this->connection->getConfig('schema'))) {
  136. if (in_array($table[0], $schema)) {
  137. return [array_shift($table), implode('.', $table)];
  138. }
  139. $schema = head($schema);
  140. }
  141. return [$schema ?: 'public', implode('.', $table)];
  142. }
  143. }