/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php

https://gitlab.com/ealexis.t/trends · PHP · 281 lines · 112 code · 42 blank · 127 comment · 2 complexity · d66cd42831fe0c2ccfbc4df8acab22e9 MD5 · raw file

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