PageRenderTime 23ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/database/schema/table.php

https://github.com/jasonlewis/laravel
PHP | 415 lines | 146 code | 46 blank | 223 comment | 2 complexity | 330098e4477b72d351ecfdc48c66b141 MD5 | raw file
Possible License(s): MIT
  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. * Create a new schema table instance.
  36. *
  37. * @param string $name
  38. * @return void
  39. */
  40. public function __construct($name)
  41. {
  42. $this->name = $name;
  43. }
  44. /**
  45. * Indicate that the table should be created.
  46. *
  47. * @return Fluent
  48. */
  49. public function create()
  50. {
  51. return $this->command(__FUNCTION__);
  52. }
  53. /**
  54. * Create a new primary key on the table.
  55. *
  56. * @param string|array $columns
  57. * @param string $name
  58. * @return Fluent
  59. */
  60. public function primary($columns, $name = null)
  61. {
  62. return $this->key(__FUNCTION__, $columns, $name);
  63. }
  64. /**
  65. * Create a new unique index on the table.
  66. *
  67. * @param string|array $columns
  68. * @param string $name
  69. * @return Fluent
  70. */
  71. public function unique($columns, $name = null)
  72. {
  73. return $this->key(__FUNCTION__, $columns, $name);
  74. }
  75. /**
  76. * Create a new full-text index on the table.
  77. *
  78. * @param string|array $columns
  79. * @param string $name
  80. * @return Fluent
  81. */
  82. public function fulltext($columns, $name = null)
  83. {
  84. return $this->key(__FUNCTION__, $columns, $name);
  85. }
  86. /**
  87. * Create a new index on the table.
  88. *
  89. * @param string|array $columns
  90. * @param string $name
  91. * @return Fluent
  92. */
  93. public function index($columns, $name = null)
  94. {
  95. return $this->key(__FUNCTION__, $columns, $name);
  96. }
  97. /**
  98. * Add a foreign key constraint to the table.
  99. *
  100. * @param string|array $columns
  101. * @param string $name
  102. */
  103. public function foreign($columns, $name = null)
  104. {
  105. return $this->key(__FUNCTION__, $columns, $name);
  106. }
  107. /**
  108. * Create a command for creating any index.
  109. *
  110. * @param string $type
  111. * @param string|array $columns
  112. * @param string $name
  113. * @return Fluent
  114. */
  115. public function key($type, $columns, $name)
  116. {
  117. $columns = (array) $columns;
  118. // If no index name was specified, we will concatenate the columns and
  119. // append the index type to the name to generate a unique name for
  120. // the index that can be used when dropping indexes.
  121. if (is_null($name))
  122. {
  123. $name = $this->name.'_'.implode('_', $columns).'_'.$type;
  124. }
  125. return $this->command($type, compact('name', 'columns'));
  126. }
  127. /**
  128. * Drop the database table.
  129. *
  130. * @return Fluent
  131. */
  132. public function drop()
  133. {
  134. return $this->command(__FUNCTION__);
  135. }
  136. /**
  137. * Drop a column from the table.
  138. *
  139. * @param string|array $columns
  140. * @return void
  141. */
  142. public function drop_column($columns)
  143. {
  144. return $this->command(__FUNCTION__, array('columns' => (array) $columns));
  145. }
  146. /**
  147. * Drop a primary key from the table.
  148. *
  149. * @param string $name
  150. * @return void
  151. */
  152. public function drop_primary($name = null)
  153. {
  154. return $this->drop_key(__FUNCTION__, $name);
  155. }
  156. /**
  157. * Drop a unique index from the table.
  158. *
  159. * @param string $name
  160. * @return void
  161. */
  162. public function drop_unique($name)
  163. {
  164. return $this->drop_key(__FUNCTION__, $name);
  165. }
  166. /**
  167. * Drop a full-text index from the table.
  168. *
  169. * @param string $name
  170. * @return void
  171. */
  172. public function drop_fulltext($name)
  173. {
  174. return $this->drop_key(__FUNCTION__, $name);
  175. }
  176. /**
  177. * Drop an index from the table.
  178. *
  179. * @param string $name
  180. * @return void
  181. */
  182. public function drop_index($name)
  183. {
  184. return $this->drop_key(__FUNCTION__, $name);
  185. }
  186. /**
  187. * Drop a foreign key constraint from the table.
  188. *
  189. * @param string $name
  190. * @return void
  191. */
  192. public function drop_foreign($name)
  193. {
  194. return $this->drop_key(__FUNCTION__, $name);
  195. }
  196. /**
  197. * Create a command to drop any type of index.
  198. *
  199. * @param string $type
  200. * @param string $name
  201. * @return Fluent
  202. */
  203. protected function drop_key($type, $name)
  204. {
  205. return $this->command($type, compact('name'));
  206. }
  207. /**
  208. * Add an auto-incrementing integer to the table.
  209. *
  210. * @param string $name
  211. * @return Fluent
  212. */
  213. public function increments($name)
  214. {
  215. return $this->integer($name, true);
  216. }
  217. /**
  218. * Add a string column to the table.
  219. *
  220. * @param string $name
  221. * @param int $length
  222. * @return Fluent
  223. */
  224. public function string($name, $length = 200)
  225. {
  226. return $this->column(__FUNCTION__, compact('name', 'length'));
  227. }
  228. /**
  229. * Add an integer column to the table.
  230. *
  231. * @param string $name
  232. * @param bool $increment
  233. * @return Fluent
  234. */
  235. public function integer($name, $increment = false)
  236. {
  237. return $this->column(__FUNCTION__, compact('name', 'increment'));
  238. }
  239. /**
  240. * Add a float column to the table.
  241. *
  242. * @param string $name
  243. * @return Fluent
  244. */
  245. public function float($name)
  246. {
  247. return $this->column(__FUNCTION__, compact('name'));
  248. }
  249. /**
  250. * Add a decimal column to the table.
  251. *
  252. * @param string $name
  253. * @param int $precision
  254. * @param int $scale
  255. * @return Fluent
  256. */
  257. public function decimal($name, $precision, $scale)
  258. {
  259. return $this->column(__FUNCTION__, compact('name', 'precision', 'scale'));
  260. }
  261. /**
  262. * Add a boolean column to the table.
  263. *
  264. * @param string $name
  265. * @return Fluent
  266. */
  267. public function boolean($name)
  268. {
  269. return $this->column(__FUNCTION__, compact('name'));
  270. }
  271. /**
  272. * Create date-time columns for creation and update timestamps.
  273. *
  274. * @return void
  275. */
  276. public function timestamps()
  277. {
  278. $this->date('created_at');
  279. $this->date('updated_at');
  280. }
  281. /**
  282. * Add a date-time column to the table.
  283. *
  284. * @param string $name
  285. * @return Fluent
  286. */
  287. public function date($name)
  288. {
  289. return $this->column(__FUNCTION__, compact('name'));
  290. }
  291. /**
  292. * Add a timestamp column to the table.
  293. *
  294. * @param string $name
  295. * @return Fluent
  296. */
  297. public function timestamp($name)
  298. {
  299. return $this->column(__FUNCTION__, compact('name'));
  300. }
  301. /**
  302. * Add a text column to the table.
  303. *
  304. * @param string $name
  305. * @return Fluent
  306. */
  307. public function text($name)
  308. {
  309. return $this->column(__FUNCTION__, compact('name'));
  310. }
  311. /**
  312. * Add a blob column to the table.
  313. *
  314. * @param string $name
  315. * @return Fluent
  316. */
  317. public function blob($name)
  318. {
  319. return $this->column(__FUNCTION__, compact('name'));
  320. }
  321. /**
  322. * Set the database connection for the table operation.
  323. *
  324. * @param string $connection
  325. * @return void
  326. */
  327. public function on($connection)
  328. {
  329. $this->connection = $connection;
  330. }
  331. /**
  332. * Determine if the schema table has a creation command.
  333. *
  334. * @return bool
  335. */
  336. public function creating()
  337. {
  338. return ! is_null(array_first($this->commands, function($key, $value)
  339. {
  340. return $value->type == 'create';
  341. }));
  342. }
  343. /**
  344. * Create a new fluent command instance.
  345. *
  346. * @param string $type
  347. * @param array $parameters
  348. * @return Fluent
  349. */
  350. protected function command($type, $parameters = array())
  351. {
  352. $parameters = array_merge(compact('type'), $parameters);
  353. $this->commands[] = new Fluent($parameters);
  354. return end($this->commands);
  355. }
  356. /**
  357. * Create a new fluent column instance.
  358. *
  359. * @param string $type
  360. * @param array $parameters
  361. * @return Fluent
  362. */
  363. protected function column($type, $parameters = array())
  364. {
  365. $parameters = array_merge(compact('type'), $parameters);
  366. $this->columns[] = new Fluent($parameters);
  367. return end($this->columns);
  368. }
  369. }