Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PostgresBuilder.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * Get all of the table names for the database.
  63. *
  64. * @return array
  65. */
  66. protected function getAllTables()
  67. {
  68. return $this->connection->select(
  69. $this->grammar->compileGetAllTables($this->connection->getConfig('schema'))
  70. );
  71. }
  72. /**
  73. * Get all of the view names for the database.
  74. *
  75. * @return array
  76. */
  77. protected function getAllViews()
  78. {
  79. return $this->connection->select(
  80. $this->grammar->compileGetAllViews($this->connection->getConfig('schema'))
  81. );
  82. }
  83. /**
  84. * Get the column listing for a given table.
  85. *
  86. * @param string $table
  87. * @return array
  88. */
  89. public function getColumnListing($table)
  90. {
  91. [$schema, $table] = $this->parseSchemaAndTable($table);
  92. $table = $this->connection->getTablePrefix().$table;
  93. $results = $this->connection->select(
  94. $this->grammar->compileColumnListing(), [$schema, $table]
  95. );
  96. return $this->connection->getPostProcessor()->processColumnListing($results);
  97. }
  98. /**
  99. * Parse the table name and extract the schema and table.
  100. *
  101. * @param string $table
  102. * @return array
  103. */
  104. protected function parseSchemaAndTable($table)
  105. {
  106. $table = explode('.', $table);
  107. if (is_array($schema = $this->connection->getConfig('schema'))) {
  108. if (in_array($table[0], $schema)) {
  109. return [array_shift($table), implode('.', $table)];
  110. }
  111. $schema = head($schema);
  112. }
  113. return [$schema ?: 'public', implode('.', $table)];
  114. }
  115. }