Builder.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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->select(
  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->select($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. * Rename a table on the schema.
  183. *
  184. * @param string $from
  185. * @param string $to
  186. * @return void
  187. */
  188. public function rename($from, $to)
  189. {
  190. $this->build(tap($this->createBlueprint($from), function ($blueprint) use ($to) {
  191. $blueprint->rename($to);
  192. }));
  193. }
  194. /**
  195. * Enable foreign key constraints.
  196. *
  197. * @return bool
  198. */
  199. public function enableForeignKeyConstraints()
  200. {
  201. return $this->connection->statement(
  202. $this->grammar->compileEnableForeignKeyConstraints()
  203. );
  204. }
  205. /**
  206. * Disable foreign key constraints.
  207. *
  208. * @return bool
  209. */
  210. public function disableForeignKeyConstraints()
  211. {
  212. return $this->connection->statement(
  213. $this->grammar->compileDisableForeignKeyConstraints()
  214. );
  215. }
  216. /**
  217. * Execute the blueprint to build / modify the table.
  218. *
  219. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  220. * @return void
  221. */
  222. protected function build(Blueprint $blueprint)
  223. {
  224. $blueprint->build($this->connection, $this->grammar);
  225. }
  226. /**
  227. * Create a new command set with a Closure.
  228. *
  229. * @param string $table
  230. * @param \Closure|null $callback
  231. * @return \Illuminate\Database\Schema\Blueprint
  232. */
  233. protected function createBlueprint($table, Closure $callback = null)
  234. {
  235. if (isset($this->resolver)) {
  236. return call_user_func($this->resolver, $table, $callback);
  237. }
  238. return new Blueprint($table, $callback);
  239. }
  240. /**
  241. * Get the database connection instance.
  242. *
  243. * @return \Illuminate\Database\Connection
  244. */
  245. public function getConnection()
  246. {
  247. return $this->connection;
  248. }
  249. /**
  250. * Set the database connection instance.
  251. *
  252. * @param \Illuminate\Database\Connection $connection
  253. * @return $this
  254. */
  255. public function setConnection(Connection $connection)
  256. {
  257. $this->connection = $connection;
  258. return $this;
  259. }
  260. /**
  261. * Set the Schema Blueprint resolver callback.
  262. *
  263. * @param \Closure $resolver
  264. * @return void
  265. */
  266. public function blueprintResolver(Closure $resolver)
  267. {
  268. $this->resolver = $resolver;
  269. }
  270. }