PageRenderTime 35ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/jjpa2018/dashboard
PHP | 433 lines | 172 code | 46 blank | 215 comment | 3 complexity | 6ac449a1c209af0b15da93845bf14edc MD5 | raw file
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. use Closure;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Database\Connection;
  6. use InvalidArgumentException;
  7. use LogicException;
  8. class Builder
  9. {
  10. /**
  11. * The database connection instance.
  12. *
  13. * @var \Illuminate\Database\Connection
  14. */
  15. protected $connection;
  16. /**
  17. * The schema grammar instance.
  18. *
  19. * @var \Illuminate\Database\Schema\Grammars\Grammar
  20. */
  21. protected $grammar;
  22. /**
  23. * The Blueprint resolver callback.
  24. *
  25. * @var \Closure
  26. */
  27. protected $resolver;
  28. /**
  29. * The default string length for migrations.
  30. *
  31. * @var int
  32. */
  33. public static $defaultStringLength = 255;
  34. /**
  35. * The default relationship morph key type.
  36. *
  37. * @var string
  38. */
  39. public static $defaultMorphKeyType = 'int';
  40. /**
  41. * Create a new database Schema manager.
  42. *
  43. * @param \Illuminate\Database\Connection $connection
  44. * @return void
  45. */
  46. public function __construct(Connection $connection)
  47. {
  48. $this->connection = $connection;
  49. $this->grammar = $connection->getSchemaGrammar();
  50. }
  51. /**
  52. * Set the default string length for migrations.
  53. *
  54. * @param int $length
  55. * @return void
  56. */
  57. public static function defaultStringLength($length)
  58. {
  59. static::$defaultStringLength = $length;
  60. }
  61. /**
  62. * Set the default morph key type for migrations.
  63. *
  64. * @param string $type
  65. * @return void
  66. *
  67. * @throws \InvalidArgumentException
  68. */
  69. public static function defaultMorphKeyType(string $type)
  70. {
  71. if (! in_array($type, ['int', 'uuid'])) {
  72. throw new InvalidArgumentException("Morph key type must be 'int' or 'uuid'.");
  73. }
  74. static::$defaultMorphKeyType = $type;
  75. }
  76. /**
  77. * Set the default morph key type for migrations to UUIDs.
  78. *
  79. * @return void
  80. */
  81. public static function morphUsingUuids()
  82. {
  83. return static::defaultMorphKeyType('uuid');
  84. }
  85. /**
  86. * Create a database in the schema.
  87. *
  88. * @param string $name
  89. * @return bool
  90. *
  91. * @throws \LogicException
  92. */
  93. public function createDatabase($name)
  94. {
  95. throw new LogicException('This database driver does not support creating databases.');
  96. }
  97. /**
  98. * Drop a database from the schema if the database exists.
  99. *
  100. * @param string $name
  101. * @return bool
  102. *
  103. * @throws \LogicException
  104. */
  105. public function dropDatabaseIfExists($name)
  106. {
  107. throw new LogicException('This database driver does not support dropping databases.');
  108. }
  109. /**
  110. * Determine if the given table exists.
  111. *
  112. * @param string $table
  113. * @return bool
  114. */
  115. public function hasTable($table)
  116. {
  117. $table = $this->connection->getTablePrefix().$table;
  118. return count($this->connection->selectFromWriteConnection(
  119. $this->grammar->compileTableExists(), [$table]
  120. )) > 0;
  121. }
  122. /**
  123. * Determine if the given table has a given column.
  124. *
  125. * @param string $table
  126. * @param string $column
  127. * @return bool
  128. */
  129. public function hasColumn($table, $column)
  130. {
  131. return in_array(
  132. strtolower($column), array_map('strtolower', $this->getColumnListing($table))
  133. );
  134. }
  135. /**
  136. * Determine if the given table has given columns.
  137. *
  138. * @param string $table
  139. * @param array $columns
  140. * @return bool
  141. */
  142. public function hasColumns($table, array $columns)
  143. {
  144. $tableColumns = array_map('strtolower', $this->getColumnListing($table));
  145. foreach ($columns as $column) {
  146. if (! in_array(strtolower($column), $tableColumns)) {
  147. return false;
  148. }
  149. }
  150. return true;
  151. }
  152. /**
  153. * Get the data type for the given column name.
  154. *
  155. * @param string $table
  156. * @param string $column
  157. * @return string
  158. */
  159. public function getColumnType($table, $column)
  160. {
  161. $table = $this->connection->getTablePrefix().$table;
  162. return $this->connection->getDoctrineColumn($table, $column)->getType()->getName();
  163. }
  164. /**
  165. * Get the column listing for a given table.
  166. *
  167. * @param string $table
  168. * @return array
  169. */
  170. public function getColumnListing($table)
  171. {
  172. $results = $this->connection->selectFromWriteConnection($this->grammar->compileColumnListing(
  173. $this->connection->getTablePrefix().$table
  174. ));
  175. return $this->connection->getPostProcessor()->processColumnListing($results);
  176. }
  177. /**
  178. * Modify a table on the schema.
  179. *
  180. * @param string $table
  181. * @param \Closure $callback
  182. * @return void
  183. */
  184. public function table($table, Closure $callback)
  185. {
  186. $this->build($this->createBlueprint($table, $callback));
  187. }
  188. /**
  189. * Create a new table on the schema.
  190. *
  191. * @param string $table
  192. * @param \Closure $callback
  193. * @return void
  194. */
  195. public function create($table, Closure $callback)
  196. {
  197. $this->build(tap($this->createBlueprint($table), function ($blueprint) use ($callback) {
  198. $blueprint->create();
  199. $callback($blueprint);
  200. }));
  201. }
  202. /**
  203. * Drop a table from the schema.
  204. *
  205. * @param string $table
  206. * @return void
  207. */
  208. public function drop($table)
  209. {
  210. $this->build(tap($this->createBlueprint($table), function ($blueprint) {
  211. $blueprint->drop();
  212. }));
  213. }
  214. /**
  215. * Drop a table from the schema if it exists.
  216. *
  217. * @param string $table
  218. * @return void
  219. */
  220. public function dropIfExists($table)
  221. {
  222. $this->build(tap($this->createBlueprint($table), function ($blueprint) {
  223. $blueprint->dropIfExists();
  224. }));
  225. }
  226. /**
  227. * Drop columns from a table schema.
  228. *
  229. * @param string $table
  230. * @param string|array $columns
  231. * @return void
  232. */
  233. public function dropColumns($table, $columns)
  234. {
  235. $this->table($table, function (Blueprint $blueprint) use ($columns) {
  236. $blueprint->dropColumn($columns);
  237. });
  238. }
  239. /**
  240. * Drop all tables from the database.
  241. *
  242. * @return void
  243. *
  244. * @throws \LogicException
  245. */
  246. public function dropAllTables()
  247. {
  248. throw new LogicException('This database driver does not support dropping all tables.');
  249. }
  250. /**
  251. * Drop all views from the database.
  252. *
  253. * @return void
  254. *
  255. * @throws \LogicException
  256. */
  257. public function dropAllViews()
  258. {
  259. throw new LogicException('This database driver does not support dropping all views.');
  260. }
  261. /**
  262. * Drop all types from the database.
  263. *
  264. * @return void
  265. *
  266. * @throws \LogicException
  267. */
  268. public function dropAllTypes()
  269. {
  270. throw new LogicException('This database driver does not support dropping all types.');
  271. }
  272. /**
  273. * Get all of the table names for the database.
  274. *
  275. * @return void
  276. *
  277. * @throws \LogicException
  278. */
  279. public function getAllTables()
  280. {
  281. throw new LogicException('This database driver does not support getting all tables.');
  282. }
  283. /**
  284. * Rename a table on the schema.
  285. *
  286. * @param string $from
  287. * @param string $to
  288. * @return void
  289. */
  290. public function rename($from, $to)
  291. {
  292. $this->build(tap($this->createBlueprint($from), function ($blueprint) use ($to) {
  293. $blueprint->rename($to);
  294. }));
  295. }
  296. /**
  297. * Enable foreign key constraints.
  298. *
  299. * @return bool
  300. */
  301. public function enableForeignKeyConstraints()
  302. {
  303. return $this->connection->statement(
  304. $this->grammar->compileEnableForeignKeyConstraints()
  305. );
  306. }
  307. /**
  308. * Disable foreign key constraints.
  309. *
  310. * @return bool
  311. */
  312. public function disableForeignKeyConstraints()
  313. {
  314. return $this->connection->statement(
  315. $this->grammar->compileDisableForeignKeyConstraints()
  316. );
  317. }
  318. /**
  319. * Execute the blueprint to build / modify the table.
  320. *
  321. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  322. * @return void
  323. */
  324. protected function build(Blueprint $blueprint)
  325. {
  326. $blueprint->build($this->connection, $this->grammar);
  327. }
  328. /**
  329. * Create a new command set with a Closure.
  330. *
  331. * @param string $table
  332. * @param \Closure|null $callback
  333. * @return \Illuminate\Database\Schema\Blueprint
  334. */
  335. protected function createBlueprint($table, Closure $callback = null)
  336. {
  337. $prefix = $this->connection->getConfig('prefix_indexes')
  338. ? $this->connection->getConfig('prefix')
  339. : '';
  340. if (isset($this->resolver)) {
  341. return call_user_func($this->resolver, $table, $callback, $prefix);
  342. }
  343. return Container::getInstance()->make(Blueprint::class, compact('table', 'callback', 'prefix'));
  344. }
  345. /**
  346. * Register a custom Doctrine mapping type.
  347. *
  348. * @param string $class
  349. * @param string $name
  350. * @param string $type
  351. * @return void
  352. */
  353. public function registerCustomDoctrineType($class, $name, $type)
  354. {
  355. $this->connection->registerDoctrineType($class, $name, $type);
  356. }
  357. /**
  358. * Get the database connection instance.
  359. *
  360. * @return \Illuminate\Database\Connection
  361. */
  362. public function getConnection()
  363. {
  364. return $this->connection;
  365. }
  366. /**
  367. * Set the database connection instance.
  368. *
  369. * @param \Illuminate\Database\Connection $connection
  370. * @return $this
  371. */
  372. public function setConnection(Connection $connection)
  373. {
  374. $this->connection = $connection;
  375. return $this;
  376. }
  377. /**
  378. * Set the Schema Blueprint resolver callback.
  379. *
  380. * @param \Closure $resolver
  381. * @return void
  382. */
  383. public function blueprintResolver(Closure $resolver)
  384. {
  385. $this->resolver = $resolver;
  386. }
  387. }