You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. use Closure;
  4. use LogicException;
  5. use Illuminate\Database\Connection;
  6. class Builder
  7. {
  8. /**
  9. * The database connection instance.
  10. *
  11. * @var \Illuminate\Database\Connection
  12. */
  13. protected $connection;
  14. /**
  15. * The schema grammar instance.
  16. *
  17. * @var \Illuminate\Database\Schema\Grammars\Grammar
  18. */
  19. protected $grammar;
  20. /**
  21. * The Blueprint resolver callback.
  22. *
  23. * @var \Closure
  24. */
  25. protected $resolver;
  26. /**
  27. * The default string length for migrations.
  28. *
  29. * @var int
  30. */
  31. public static $defaultStringLength = 255;
  32. /**
  33. * Create a new database Schema manager.
  34. *
  35. * @param \Illuminate\Database\Connection $connection
  36. * @return void
  37. */
  38. public function __construct(Connection $connection)
  39. {
  40. $this->connection = $connection;
  41. $this->grammar = $connection->getSchemaGrammar();
  42. }
  43. /**
  44. * Set the default string length for migrations.
  45. *
  46. * @param int $length
  47. * @return void
  48. */
  49. public static function defaultStringLength($length)
  50. {
  51. static::$defaultStringLength = $length;
  52. }
  53. /**
  54. * Determine if the given table exists.
  55. *
  56. * @param string $table
  57. * @return bool
  58. */
  59. public function hasTable($table)
  60. {
  61. $table = $this->connection->getTablePrefix().$table;
  62. return count($this->connection->selectFromWriteConnection(
  63. $this->grammar->compileTableExists(), [$table]
  64. )) > 0;
  65. }
  66. /**
  67. * Determine if the given table has a given column.
  68. *
  69. * @param string $table
  70. * @param string $column
  71. * @return bool
  72. */
  73. public function hasColumn($table, $column)
  74. {
  75. return in_array(
  76. strtolower($column), array_map('strtolower', $this->getColumnListing($table))
  77. );
  78. }
  79. /**
  80. * Determine if the given table has given columns.
  81. *
  82. * @param string $table
  83. * @param array $columns
  84. * @return bool
  85. */
  86. public function hasColumns($table, array $columns)
  87. {
  88. $tableColumns = array_map('strtolower', $this->getColumnListing($table));
  89. foreach ($columns as $column) {
  90. if (! in_array(strtolower($column), $tableColumns)) {
  91. return false;
  92. }
  93. }
  94. return true;
  95. }
  96. /**
  97. * Get the data type for the given column name.
  98. *
  99. * @param string $table
  100. * @param string $column
  101. * @return string
  102. */
  103. public function getColumnType($table, $column)
  104. {
  105. $table = $this->connection->getTablePrefix().$table;
  106. return $this->connection->getDoctrineColumn($table, $column)->getType()->getName();
  107. }
  108. /**
  109. * Get the column listing for a given table.
  110. *
  111. * @param string $table
  112. * @return array
  113. */
  114. public function getColumnListing($table)
  115. {
  116. $results = $this->connection->selectFromWriteConnection($this->grammar->compileColumnListing(
  117. $this->connection->getTablePrefix().$table
  118. ));
  119. return $this->connection->getPostProcessor()->processColumnListing($results);
  120. }
  121. /**
  122. * Modify a table on the schema.
  123. *
  124. * @param string $table
  125. * @param \Closure $callback
  126. * @return void
  127. */
  128. public function table($table, Closure $callback)
  129. {
  130. $this->build($this->createBlueprint($table, $callback));
  131. }
  132. /**
  133. * Create a new table on the schema.
  134. *
  135. * @param string $table
  136. * @param \Closure $callback
  137. * @return void
  138. */
  139. public function create($table, Closure $callback)
  140. {
  141. $this->build(tap($this->createBlueprint($table), function ($blueprint) use ($callback) {
  142. $blueprint->create();
  143. $callback($blueprint);
  144. }));
  145. }
  146. /**
  147. * Drop a table from the schema.
  148. *
  149. * @param string $table
  150. * @return void
  151. */
  152. public function drop($table)
  153. {
  154. $this->build(tap($this->createBlueprint($table), function ($blueprint) {
  155. $blueprint->drop();
  156. }));
  157. }
  158. /**
  159. * Drop a table from the schema if it exists.
  160. *
  161. * @param string $table
  162. * @return void
  163. */
  164. public function dropIfExists($table)
  165. {
  166. $this->build(tap($this->createBlueprint($table), function ($blueprint) {
  167. $blueprint->dropIfExists();
  168. }));
  169. }
  170. /**
  171. * Drop all tables from the database.
  172. *
  173. * @return void
  174. *
  175. * @throws \LogicException
  176. */
  177. public function dropAllTables()
  178. {
  179. throw new LogicException('This database driver does not support dropping all tables.');
  180. }
  181. /**
  182. * Drop all views from the database.
  183. *
  184. * @return void
  185. *
  186. * @throws \LogicException
  187. */
  188. public function dropAllViews()
  189. {
  190. throw new LogicException('This database driver does not support dropping all views.');
  191. }
  192. /**
  193. * Rename a table on the schema.
  194. *
  195. * @param string $from
  196. * @param string $to
  197. * @return void
  198. */
  199. public function rename($from, $to)
  200. {
  201. $this->build(tap($this->createBlueprint($from), function ($blueprint) use ($to) {
  202. $blueprint->rename($to);
  203. }));
  204. }
  205. /**
  206. * Enable foreign key constraints.
  207. *
  208. * @return bool
  209. */
  210. public function enableForeignKeyConstraints()
  211. {
  212. return $this->connection->statement(
  213. $this->grammar->compileEnableForeignKeyConstraints()
  214. );
  215. }
  216. /**
  217. * Disable foreign key constraints.
  218. *
  219. * @return bool
  220. */
  221. public function disableForeignKeyConstraints()
  222. {
  223. return $this->connection->statement(
  224. $this->grammar->compileDisableForeignKeyConstraints()
  225. );
  226. }
  227. /**
  228. * Execute the blueprint to build / modify the table.
  229. *
  230. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  231. * @return void
  232. */
  233. protected function build(Blueprint $blueprint)
  234. {
  235. $blueprint->build($this->connection, $this->grammar);
  236. }
  237. /**
  238. * Create a new command set with a Closure.
  239. *
  240. * @param string $table
  241. * @param \Closure|null $callback
  242. * @return \Illuminate\Database\Schema\Blueprint
  243. */
  244. protected function createBlueprint($table, Closure $callback = null)
  245. {
  246. $prefix = $this->connection->getConfig('prefix_indexes')
  247. ? $this->connection->getConfig('prefix')
  248. : '';
  249. if (isset($this->resolver)) {
  250. return call_user_func($this->resolver, $table, $callback, $prefix);
  251. }
  252. return new Blueprint($table, $callback, $prefix);
  253. }
  254. /**
  255. * Get the database connection instance.
  256. *
  257. * @return \Illuminate\Database\Connection
  258. */
  259. public function getConnection()
  260. {
  261. return $this->connection;
  262. }
  263. /**
  264. * Set the database connection instance.
  265. *
  266. * @param \Illuminate\Database\Connection $connection
  267. * @return $this
  268. */
  269. public function setConnection(Connection $connection)
  270. {
  271. $this->connection = $connection;
  272. return $this;
  273. }
  274. /**
  275. * Set the Schema Blueprint resolver callback.
  276. *
  277. * @param \Closure $resolver
  278. * @return void
  279. */
  280. public function blueprintResolver(Closure $resolver)
  281. {
  282. $this->resolver = $resolver;
  283. }
  284. }