SqlServerBuilder.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. use InvalidArgumentException;
  4. class SqlServerBuilder extends Builder
  5. {
  6. /**
  7. * Create a database in the schema.
  8. *
  9. * @param string $name
  10. * @return bool
  11. */
  12. public function createDatabase($name)
  13. {
  14. return $this->connection->statement(
  15. $this->grammar->compileCreateDatabase($name, $this->connection)
  16. );
  17. }
  18. /**
  19. * Drop a database from the schema if the database exists.
  20. *
  21. * @param string $name
  22. * @return bool
  23. */
  24. public function dropDatabaseIfExists($name)
  25. {
  26. return $this->connection->statement(
  27. $this->grammar->compileDropDatabaseIfExists($name)
  28. );
  29. }
  30. /**
  31. * Determine if the given table exists.
  32. *
  33. * @param string $table
  34. * @return bool
  35. */
  36. public function hasTable($table)
  37. {
  38. [$schema, $table] = $this->parseSchemaAndTable($table);
  39. $schema ??= $this->getDefaultSchema();
  40. $table = $this->connection->getTablePrefix().$table;
  41. foreach ($this->getTables() as $value) {
  42. if (strtolower($table) === strtolower($value['name'])
  43. && strtolower($schema) === strtolower($value['schema'])) {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. /**
  50. * Determine if the given view exists.
  51. *
  52. * @param string $view
  53. * @return bool
  54. */
  55. public function hasView($view)
  56. {
  57. [$schema, $view] = $this->parseSchemaAndTable($view);
  58. $schema ??= $this->getDefaultSchema();
  59. $view = $this->connection->getTablePrefix().$view;
  60. foreach ($this->getViews() as $value) {
  61. if (strtolower($view) === strtolower($value['name'])
  62. && strtolower($schema) === strtolower($value['schema'])) {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. /**
  69. * Drop all tables from the database.
  70. *
  71. * @return void
  72. */
  73. public function dropAllTables()
  74. {
  75. $this->connection->statement($this->grammar->compileDropAllForeignKeys());
  76. $this->connection->statement($this->grammar->compileDropAllTables());
  77. }
  78. /**
  79. * Drop all views from the database.
  80. *
  81. * @return void
  82. */
  83. public function dropAllViews()
  84. {
  85. $this->connection->statement($this->grammar->compileDropAllViews());
  86. }
  87. /**
  88. * Get the columns for a given table.
  89. *
  90. * @param string $table
  91. * @return array
  92. */
  93. public function getColumns($table)
  94. {
  95. [$schema, $table] = $this->parseSchemaAndTable($table);
  96. $table = $this->connection->getTablePrefix().$table;
  97. $results = $this->connection->selectFromWriteConnection(
  98. $this->grammar->compileColumns($schema, $table)
  99. );
  100. return $this->connection->getPostProcessor()->processColumns($results);
  101. }
  102. /**
  103. * Get the indexes for a given table.
  104. *
  105. * @param string $table
  106. * @return array
  107. */
  108. public function getIndexes($table)
  109. {
  110. [$schema, $table] = $this->parseSchemaAndTable($table);
  111. $table = $this->connection->getTablePrefix().$table;
  112. return $this->connection->getPostProcessor()->processIndexes(
  113. $this->connection->selectFromWriteConnection($this->grammar->compileIndexes($schema, $table))
  114. );
  115. }
  116. /**
  117. * Get the foreign keys for a given table.
  118. *
  119. * @param string $table
  120. * @return array
  121. */
  122. public function getForeignKeys($table)
  123. {
  124. [$schema, $table] = $this->parseSchemaAndTable($table);
  125. $table = $this->connection->getTablePrefix().$table;
  126. return $this->connection->getPostProcessor()->processForeignKeys(
  127. $this->connection->selectFromWriteConnection($this->grammar->compileForeignKeys($schema, $table))
  128. );
  129. }
  130. /**
  131. * Get the default schema for the connection.
  132. *
  133. * @return string
  134. */
  135. protected function getDefaultSchema()
  136. {
  137. return $this->connection->scalar($this->grammar->compileDefaultSchema());
  138. }
  139. /**
  140. * Parse the database object reference and extract the schema and table.
  141. *
  142. * @param string $reference
  143. * @return array
  144. */
  145. protected function parseSchemaAndTable($reference)
  146. {
  147. $parts = array_pad(explode('.', $reference, 2), -2, null);
  148. if (str_contains($parts[1], '.')) {
  149. $database = $parts[0];
  150. throw new InvalidArgumentException("Using three-part reference is not supported, you may use `Schema::connection('$database')` instead.");
  151. }
  152. return $parts;
  153. }
  154. }