PostgresBuilder.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. list($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. * Get all of the table names for the database.
  44. *
  45. * @return array
  46. */
  47. protected function getAllTables()
  48. {
  49. return $this->connection->select(
  50. $this->grammar->compileGetAllTables($this->connection->getConfig('schema'))
  51. );
  52. }
  53. /**
  54. * Get the column listing for a given table.
  55. *
  56. * @param string $table
  57. * @return array
  58. */
  59. public function getColumnListing($table)
  60. {
  61. list($schema, $table) = $this->parseSchemaAndTable($table);
  62. $table = $this->connection->getTablePrefix().$table;
  63. $results = $this->connection->select(
  64. $this->grammar->compileColumnListing(), [$schema, $table]
  65. );
  66. return $this->connection->getPostProcessor()->processColumnListing($results);
  67. }
  68. /**
  69. * Parse the table name and extract the schema and table.
  70. *
  71. * @param string $table
  72. * @return array
  73. */
  74. protected function parseSchemaAndTable($table)
  75. {
  76. $table = explode('.', $table);
  77. if (is_array($schema = $this->connection->getConfig('schema'))) {
  78. if (in_array($table[0], $schema)) {
  79. return [array_shift($table), implode('.', $table)];
  80. }
  81. $schema = head($schema);
  82. }
  83. return [$schema ?: 'public', implode('.', $table)];
  84. }
  85. }