PostgresBuilder.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. use Illuminate\Database\Concerns\ParsesSearchPath;
  4. use InvalidArgumentException;
  5. class PostgresBuilder extends Builder
  6. {
  7. use ParsesSearchPath {
  8. parseSearchPath as baseParseSearchPath;
  9. }
  10. /**
  11. * Create a database in the schema.
  12. *
  13. * @param string $name
  14. * @return bool
  15. */
  16. public function createDatabase($name)
  17. {
  18. return $this->connection->statement(
  19. $this->grammar->compileCreateDatabase($name, $this->connection)
  20. );
  21. }
  22. /**
  23. * Drop a database from the schema if the database exists.
  24. *
  25. * @param string $name
  26. * @return bool
  27. */
  28. public function dropDatabaseIfExists($name)
  29. {
  30. return $this->connection->statement(
  31. $this->grammar->compileDropDatabaseIfExists($name)
  32. );
  33. }
  34. /**
  35. * Determine if the given table exists.
  36. *
  37. * @param string $table
  38. * @return bool
  39. */
  40. public function hasTable($table)
  41. {
  42. [$schema, $table] = $this->parseSchemaAndTable($table);
  43. $table = $this->connection->getTablePrefix().$table;
  44. foreach ($this->getTables() as $value) {
  45. if (strtolower($table) === strtolower($value['name'])
  46. && strtolower($schema) === strtolower($value['schema'])) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. /**
  53. * Determine if the given view exists.
  54. *
  55. * @param string $view
  56. * @return bool
  57. */
  58. public function hasView($view)
  59. {
  60. [$schema, $view] = $this->parseSchemaAndTable($view);
  61. $view = $this->connection->getTablePrefix().$view;
  62. foreach ($this->getViews() as $value) {
  63. if (strtolower($view) === strtolower($value['name'])
  64. && strtolower($schema) === strtolower($value['schema'])) {
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. /**
  71. * Get the user-defined types that belong to the database.
  72. *
  73. * @return array
  74. */
  75. public function getTypes()
  76. {
  77. return $this->connection->getPostProcessor()->processTypes(
  78. $this->connection->selectFromWriteConnection($this->grammar->compileTypes())
  79. );
  80. }
  81. /**
  82. * Drop all tables from the database.
  83. *
  84. * @return void
  85. */
  86. public function dropAllTables()
  87. {
  88. $tables = [];
  89. $excludedTables = $this->grammar->escapeNames(
  90. $this->connection->getConfig('dont_drop') ?? ['spatial_ref_sys']
  91. );
  92. $schemas = $this->grammar->escapeNames($this->getSchemas());
  93. foreach ($this->getTables() as $table) {
  94. $qualifiedName = $table['schema'].'.'.$table['name'];
  95. if (empty(array_intersect($this->grammar->escapeNames([$table['name'], $qualifiedName]), $excludedTables))
  96. && in_array($this->grammar->escapeNames([$table['schema']])[0], $schemas)) {
  97. $tables[] = $qualifiedName;
  98. }
  99. }
  100. if (empty($tables)) {
  101. return;
  102. }
  103. $this->connection->statement(
  104. $this->grammar->compileDropAllTables($tables)
  105. );
  106. }
  107. /**
  108. * Drop all views from the database.
  109. *
  110. * @return void
  111. */
  112. public function dropAllViews()
  113. {
  114. $views = [];
  115. $schemas = $this->grammar->escapeNames($this->getSchemas());
  116. foreach ($this->getViews() as $view) {
  117. if (in_array($this->grammar->escapeNames([$view['schema']])[0], $schemas)) {
  118. $views[] = $view['schema'].'.'.$view['name'];
  119. }
  120. }
  121. if (empty($views)) {
  122. return;
  123. }
  124. $this->connection->statement(
  125. $this->grammar->compileDropAllViews($views)
  126. );
  127. }
  128. /**
  129. * Drop all types from the database.
  130. *
  131. * @return void
  132. */
  133. public function dropAllTypes()
  134. {
  135. $types = [];
  136. $domains = [];
  137. $schemas = $this->grammar->escapeNames($this->getSchemas());
  138. foreach ($this->getTypes() as $type) {
  139. if (! $type['implicit'] && in_array($this->grammar->escapeNames([$type['schema']])[0], $schemas)) {
  140. if ($type['type'] === 'domain') {
  141. $domains[] = $type['schema'].'.'.$type['name'];
  142. } else {
  143. $types[] = $type['schema'].'.'.$type['name'];
  144. }
  145. }
  146. }
  147. if (! empty($types)) {
  148. $this->connection->statement($this->grammar->compileDropAllTypes($types));
  149. }
  150. if (! empty($domains)) {
  151. $this->connection->statement($this->grammar->compileDropAllDomains($domains));
  152. }
  153. }
  154. /**
  155. * Get the columns for a given table.
  156. *
  157. * @param string $table
  158. * @return array
  159. */
  160. public function getColumns($table)
  161. {
  162. [$schema, $table] = $this->parseSchemaAndTable($table);
  163. $table = $this->connection->getTablePrefix().$table;
  164. $results = $this->connection->selectFromWriteConnection(
  165. $this->grammar->compileColumns($schema, $table)
  166. );
  167. return $this->connection->getPostProcessor()->processColumns($results);
  168. }
  169. /**
  170. * Get the indexes for a given table.
  171. *
  172. * @param string $table
  173. * @return array
  174. */
  175. public function getIndexes($table)
  176. {
  177. [$schema, $table] = $this->parseSchemaAndTable($table);
  178. $table = $this->connection->getTablePrefix().$table;
  179. return $this->connection->getPostProcessor()->processIndexes(
  180. $this->connection->selectFromWriteConnection($this->grammar->compileIndexes($schema, $table))
  181. );
  182. }
  183. /**
  184. * Get the foreign keys for a given table.
  185. *
  186. * @param string $table
  187. * @return array
  188. */
  189. public function getForeignKeys($table)
  190. {
  191. [$schema, $table] = $this->parseSchemaAndTable($table);
  192. $table = $this->connection->getTablePrefix().$table;
  193. return $this->connection->getPostProcessor()->processForeignKeys(
  194. $this->connection->selectFromWriteConnection($this->grammar->compileForeignKeys($schema, $table))
  195. );
  196. }
  197. /**
  198. * Get the schemas for the connection.
  199. *
  200. * @return array
  201. */
  202. protected function getSchemas()
  203. {
  204. return $this->parseSearchPath(
  205. $this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema') ?: 'public'
  206. );
  207. }
  208. /**
  209. * Parse the database object reference and extract the schema and table.
  210. *
  211. * @param string $reference
  212. * @return array
  213. */
  214. protected function parseSchemaAndTable($reference)
  215. {
  216. $parts = explode('.', $reference);
  217. if (count($parts) > 2) {
  218. $database = $parts[0];
  219. throw new InvalidArgumentException("Using three-part reference is not supported, you may use `Schema::connection('$database')` instead.");
  220. }
  221. // We will use the default schema unless the schema has been specified in the
  222. // query. If the schema has been specified in the query then we can use it
  223. // instead of a default schema configured in the connection search path.
  224. $schema = $this->getSchemas()[0];
  225. if (count($parts) === 2) {
  226. $schema = $parts[0];
  227. array_shift($parts);
  228. }
  229. return [$schema, $parts[0]];
  230. }
  231. /**
  232. * Parse the "search_path" configuration value into an array.
  233. *
  234. * @param string|array|null $searchPath
  235. * @return array
  236. */
  237. protected function parseSearchPath($searchPath)
  238. {
  239. return array_map(function ($schema) {
  240. return $schema === '$user'
  241. ? $this->connection->getConfig('username')
  242. : $schema;
  243. }, $this->baseParseSearchPath($searchPath));
  244. }
  245. }