PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 243 lines | 95 code | 38 blank | 110 comment | 2 complexity | 987586c469aeb0416c6c253e16731663 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 column listing for a given table.
  79. *
  80. * @param string $table
  81. * @return array
  82. */
  83. public function getColumnListing($table)
  84. {
  85. $table = $this->connection->getTablePrefix().$table;
  86. $results = $this->connection->select($this->grammar->compileColumnExists($table));
  87. return $this->connection->getPostProcessor()->processColumnListing($results);
  88. }
  89. /**
  90. * Modify a table on the schema.
  91. *
  92. * @param string $table
  93. * @param \Closure $callback
  94. * @return \Illuminate\Database\Schema\Blueprint
  95. */
  96. public function table($table, Closure $callback)
  97. {
  98. $this->build($this->createBlueprint($table, $callback));
  99. }
  100. /**
  101. * Create a new table on the schema.
  102. *
  103. * @param string $table
  104. * @param \Closure $callback
  105. * @return \Illuminate\Database\Schema\Blueprint
  106. */
  107. public function create($table, Closure $callback)
  108. {
  109. $blueprint = $this->createBlueprint($table);
  110. $blueprint->create();
  111. $callback($blueprint);
  112. $this->build($blueprint);
  113. }
  114. /**
  115. * Drop a table from the schema.
  116. *
  117. * @param string $table
  118. * @return \Illuminate\Database\Schema\Blueprint
  119. */
  120. public function drop($table)
  121. {
  122. $blueprint = $this->createBlueprint($table);
  123. $blueprint->drop();
  124. $this->build($blueprint);
  125. }
  126. /**
  127. * Drop a table from the schema if it exists.
  128. *
  129. * @param string $table
  130. * @return \Illuminate\Database\Schema\Blueprint
  131. */
  132. public function dropIfExists($table)
  133. {
  134. $blueprint = $this->createBlueprint($table);
  135. $blueprint->dropIfExists();
  136. $this->build($blueprint);
  137. }
  138. /**
  139. * Rename a table on the schema.
  140. *
  141. * @param string $from
  142. * @param string $to
  143. * @return \Illuminate\Database\Schema\Blueprint
  144. */
  145. public function rename($from, $to)
  146. {
  147. $blueprint = $this->createBlueprint($from);
  148. $blueprint->rename($to);
  149. $this->build($blueprint);
  150. }
  151. /**
  152. * Execute the blueprint to build / modify the table.
  153. *
  154. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  155. * @return void
  156. */
  157. protected function build(Blueprint $blueprint)
  158. {
  159. $blueprint->build($this->connection, $this->grammar);
  160. }
  161. /**
  162. * Create a new command set with a Closure.
  163. *
  164. * @param string $table
  165. * @param \Closure|null $callback
  166. * @return \Illuminate\Database\Schema\Blueprint
  167. */
  168. protected function createBlueprint($table, Closure $callback = null)
  169. {
  170. if (isset($this->resolver)) {
  171. return call_user_func($this->resolver, $table, $callback);
  172. }
  173. return new Blueprint($table, $callback);
  174. }
  175. /**
  176. * Get the database connection instance.
  177. *
  178. * @return \Illuminate\Database\Connection
  179. */
  180. public function getConnection()
  181. {
  182. return $this->connection;
  183. }
  184. /**
  185. * Set the database connection instance.
  186. *
  187. * @param \Illuminate\Database\Connection $connection
  188. * @return $this
  189. */
  190. public function setConnection(Connection $connection)
  191. {
  192. $this->connection = $connection;
  193. return $this;
  194. }
  195. /**
  196. * Set the Schema Blueprint resolver callback.
  197. *
  198. * @param \Closure $resolver
  199. * @return void
  200. */
  201. public function blueprintResolver(Closure $resolver)
  202. {
  203. $this->resolver = $resolver;
  204. }
  205. }