PageRenderTime 46ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/database/schema/table.php

https://bitbucket.org/Maron1/taqman
PHP | 462 lines | 163 code | 50 blank | 249 comment | 3 complexity | 8f3f01e39df6dd222e43a099940f8f6f MD5 | raw file
  1. <?php namespace Laravel\Database\Schema;
  2. use Laravel\Fluent;
  3. class Table {
  4. /**
  5. * The database table name.
  6. *
  7. * @var string
  8. */
  9. public $name;
  10. /**
  11. * The database connection that should be used.
  12. *
  13. * @var string
  14. */
  15. public $connection;
  16. /**
  17. * The engine that should be used for the table.
  18. *
  19. * @var string
  20. */
  21. public $engine;
  22. /**
  23. * The columns that should be added to the table.
  24. *
  25. * @var array
  26. */
  27. public $columns = array();
  28. /**
  29. * The commands that should be executed on the table.
  30. *
  31. * @var array
  32. */
  33. public $commands = array();
  34. /**
  35. * The registered custom macros.
  36. *
  37. * @var array
  38. */
  39. public static $macros = array();
  40. /**
  41. * Registers a custom macro.
  42. *
  43. * @param string $name
  44. * @param Closure $macro
  45. * @return void
  46. */
  47. public static function macro($name, $macro)
  48. {
  49. static::$macros[$name] = $macro;
  50. }
  51. /**
  52. * Create a new schema table instance.
  53. *
  54. * @param string $name
  55. * @return void
  56. */
  57. public function __construct($name)
  58. {
  59. $this->name = $name;
  60. }
  61. /**
  62. * Indicate that the table should be created.
  63. *
  64. * @return Fluent
  65. */
  66. public function create()
  67. {
  68. return $this->command(__FUNCTION__);
  69. }
  70. /**
  71. * Create a new primary key on the table.
  72. *
  73. * @param string|array $columns
  74. * @param string $name
  75. * @return Fluent
  76. */
  77. public function primary($columns, $name = null)
  78. {
  79. return $this->key(__FUNCTION__, $columns, $name);
  80. }
  81. /**
  82. * Create a new unique index on the table.
  83. *
  84. * @param string|array $columns
  85. * @param string $name
  86. * @return Fluent
  87. */
  88. public function unique($columns, $name = null)
  89. {
  90. return $this->key(__FUNCTION__, $columns, $name);
  91. }
  92. /**
  93. * Create a new full-text index on the table.
  94. *
  95. * @param string|array $columns
  96. * @param string $name
  97. * @return Fluent
  98. */
  99. public function fulltext($columns, $name = null)
  100. {
  101. return $this->key(__FUNCTION__, $columns, $name);
  102. }
  103. /**
  104. * Create a new index on the table.
  105. *
  106. * @param string|array $columns
  107. * @param string $name
  108. * @return Fluent
  109. */
  110. public function index($columns, $name = null)
  111. {
  112. return $this->key(__FUNCTION__, $columns, $name);
  113. }
  114. /**
  115. * Add a foreign key constraint to the table.
  116. *
  117. * @param string|array $columns
  118. * @param string $name
  119. * @return Fluent
  120. */
  121. public function foreign($columns, $name = null)
  122. {
  123. return $this->key(__FUNCTION__, $columns, $name);
  124. }
  125. /**
  126. * Create a command for creating any index.
  127. *
  128. * @param string $type
  129. * @param string|array $columns
  130. * @param string $name
  131. * @return Fluent
  132. */
  133. public function key($type, $columns, $name)
  134. {
  135. $columns = (array) $columns;
  136. // If no index name was specified, we will concatenate the columns and
  137. // append the index type to the name to generate a unique name for
  138. // the index that can be used when dropping indexes.
  139. if (is_null($name))
  140. {
  141. $name = str_replace(array('-', '.'), '_', $this->name);
  142. $name = $name.'_'.implode('_', $columns).'_'.$type;
  143. }
  144. return $this->command($type, compact('name', 'columns'));
  145. }
  146. /**
  147. * Rename the database table.
  148. *
  149. * @param string $name
  150. * @return Fluent
  151. */
  152. public function rename($name)
  153. {
  154. return $this->command(__FUNCTION__, compact('name'));
  155. }
  156. /**
  157. * Drop the database table.
  158. *
  159. * @return Fluent
  160. */
  161. public function drop()
  162. {
  163. return $this->command(__FUNCTION__);
  164. }
  165. /**
  166. * Drop a column from the table.
  167. *
  168. * @param string|array $columns
  169. * @return void
  170. */
  171. public function drop_column($columns)
  172. {
  173. return $this->command(__FUNCTION__, array('columns' => (array) $columns));
  174. }
  175. /**
  176. * Drop a primary key from the table.
  177. *
  178. * @param string $name
  179. * @return void
  180. */
  181. public function drop_primary($name = null)
  182. {
  183. return $this->drop_key(__FUNCTION__, $name);
  184. }
  185. /**
  186. * Drop a unique index from the table.
  187. *
  188. * @param string $name
  189. * @return void
  190. */
  191. public function drop_unique($name)
  192. {
  193. return $this->drop_key(__FUNCTION__, $name);
  194. }
  195. /**
  196. * Drop a full-text index from the table.
  197. *
  198. * @param string $name
  199. * @return void
  200. */
  201. public function drop_fulltext($name)
  202. {
  203. return $this->drop_key(__FUNCTION__, $name);
  204. }
  205. /**
  206. * Drop an index from the table.
  207. *
  208. * @param string $name
  209. * @return void
  210. */
  211. public function drop_index($name)
  212. {
  213. return $this->drop_key(__FUNCTION__, $name);
  214. }
  215. /**
  216. * Drop a foreign key constraint from the table.
  217. *
  218. * @param string $name
  219. * @return void
  220. */
  221. public function drop_foreign($name)
  222. {
  223. return $this->drop_key(__FUNCTION__, $name);
  224. }
  225. /**
  226. * Create a command to drop any type of index.
  227. *
  228. * @param string $type
  229. * @param string $name
  230. * @return Fluent
  231. */
  232. protected function drop_key($type, $name)
  233. {
  234. return $this->command($type, compact('name'));
  235. }
  236. /**
  237. * Add an auto-incrementing integer to the table.
  238. *
  239. * @param string $name
  240. * @return Fluent
  241. */
  242. public function increments($name)
  243. {
  244. return $this->integer($name, true);
  245. }
  246. /**
  247. * Add a string column to the table.
  248. *
  249. * @param string $name
  250. * @param int $length
  251. * @return Fluent
  252. */
  253. public function string($name, $length = 200)
  254. {
  255. return $this->column(__FUNCTION__, compact('name', 'length'));
  256. }
  257. /**
  258. * Add an integer column to the table.
  259. *
  260. * @param string $name
  261. * @param bool $increment
  262. * @return Fluent
  263. */
  264. public function integer($name, $increment = false)
  265. {
  266. return $this->column(__FUNCTION__, compact('name', 'increment'));
  267. }
  268. /**
  269. * Add a float column to the table.
  270. *
  271. * @param string $name
  272. * @return Fluent
  273. */
  274. public function float($name)
  275. {
  276. return $this->column(__FUNCTION__, compact('name'));
  277. }
  278. /**
  279. * Add a decimal column to the table.
  280. *
  281. * @param string $name
  282. * @param int $precision
  283. * @param int $scale
  284. * @return Fluent
  285. */
  286. public function decimal($name, $precision, $scale)
  287. {
  288. return $this->column(__FUNCTION__, compact('name', 'precision', 'scale'));
  289. }
  290. /**
  291. * Add a boolean column to the table.
  292. *
  293. * @param string $name
  294. * @return Fluent
  295. */
  296. public function boolean($name)
  297. {
  298. return $this->column(__FUNCTION__, compact('name'));
  299. }
  300. /**
  301. * Create date-time columns for creation and update timestamps.
  302. *
  303. * @return void
  304. */
  305. public function timestamps()
  306. {
  307. $this->date('created_at');
  308. $this->date('updated_at');
  309. }
  310. /**
  311. * Add a date-time column to the table.
  312. *
  313. * @param string $name
  314. * @return Fluent
  315. */
  316. public function date($name)
  317. {
  318. return $this->column(__FUNCTION__, compact('name'));
  319. }
  320. /**
  321. * Add a timestamp column to the table.
  322. *
  323. * @param string $name
  324. * @return Fluent
  325. */
  326. public function timestamp($name)
  327. {
  328. return $this->column(__FUNCTION__, compact('name'));
  329. }
  330. /**
  331. * Add a text column to the table.
  332. *
  333. * @param string $name
  334. * @return Fluent
  335. */
  336. public function text($name)
  337. {
  338. return $this->column(__FUNCTION__, compact('name'));
  339. }
  340. /**
  341. * Add a blob column to the table.
  342. *
  343. * @param string $name
  344. * @return Fluent
  345. */
  346. public function blob($name)
  347. {
  348. return $this->column(__FUNCTION__, compact('name'));
  349. }
  350. /**
  351. * Set the database connection for the table operation.
  352. *
  353. * @param string $connection
  354. * @return void
  355. */
  356. public function on($connection)
  357. {
  358. $this->connection = $connection;
  359. }
  360. /**
  361. * Determine if the schema table has a creation command.
  362. *
  363. * @return bool
  364. */
  365. public function creating()
  366. {
  367. return ! is_null(array_first($this->commands, function($key, $value)
  368. {
  369. return $value->type == 'create';
  370. }));
  371. }
  372. /**
  373. * Create a new fluent command instance.
  374. *
  375. * @param string $type
  376. * @param array $parameters
  377. * @return Fluent
  378. */
  379. protected function command($type, $parameters = array())
  380. {
  381. $parameters = array_merge(compact('type'), $parameters);
  382. return $this->commands[] = new Fluent($parameters);
  383. }
  384. /**
  385. * Create a new fluent column instance.
  386. *
  387. * @param string $type
  388. * @param array $parameters
  389. * @return Fluent
  390. */
  391. protected function column($type, $parameters = array())
  392. {
  393. $parameters = array_merge(compact('type'), $parameters);
  394. return $this->columns[] = new Fluent($parameters);
  395. }
  396. /**
  397. * Dynamically handle calls to custom macros.
  398. *
  399. * @param string $method
  400. * @param array $parameters
  401. * @return mixed
  402. */
  403. public function __call($method, $parameters)
  404. {
  405. if (isset(static::$macros[$method]))
  406. {
  407. array_unshift($parameters, $this);
  408. return call_user_func_array(static::$macros[$method], $parameters);
  409. }
  410. throw new \Exception("Method [$method] does not exist.");
  411. }
  412. }