table.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. * @return Fluent
  103. */
  104. public function foreign($columns, $name = null)
  105. {
  106. return $this->key(__FUNCTION__, $columns, $name);
  107. }
  108. /**
  109. * Create a command for creating any index.
  110. *
  111. * @param string $type
  112. * @param string|array $columns
  113. * @param string $name
  114. * @return Fluent
  115. */
  116. public function key($type, $columns, $name)
  117. {
  118. $columns = (array) $columns;
  119. // If no index name was specified, we will concatenate the columns and
  120. // append the index type to the name to generate a unique name for
  121. // the index that can be used when dropping indexes.
  122. if (is_null($name))
  123. {
  124. $name = str_replace(array('-', '.'), '_', $this->name);
  125. $name = $name.'_'.implode('_', $columns).'_'.$type;
  126. }
  127. return $this->command($type, compact('name', 'columns'));
  128. }
  129. /**
  130. * Rename the database table.
  131. *
  132. * @param string $name
  133. * @return Fluent
  134. */
  135. public function rename($name)
  136. {
  137. return $this->command(__FUNCTION__, compact('name'));
  138. }
  139. /**
  140. * Drop the database table.
  141. *
  142. * @return Fluent
  143. */
  144. public function drop()
  145. {
  146. return $this->command(__FUNCTION__);
  147. }
  148. /**
  149. * Drop a column from the table.
  150. *
  151. * @param string|array $columns
  152. * @return void
  153. */
  154. public function drop_column($columns)
  155. {
  156. return $this->command(__FUNCTION__, array('columns' => (array) $columns));
  157. }
  158. /**
  159. * Drop a primary key from the table.
  160. *
  161. * @param string $name
  162. * @return void
  163. */
  164. public function drop_primary($name = null)
  165. {
  166. return $this->drop_key(__FUNCTION__, $name);
  167. }
  168. /**
  169. * Drop a unique index from the table.
  170. *
  171. * @param string $name
  172. * @return void
  173. */
  174. public function drop_unique($name)
  175. {
  176. return $this->drop_key(__FUNCTION__, $name);
  177. }
  178. /**
  179. * Drop a full-text index from the table.
  180. *
  181. * @param string $name
  182. * @return void
  183. */
  184. public function drop_fulltext($name)
  185. {
  186. return $this->drop_key(__FUNCTION__, $name);
  187. }
  188. /**
  189. * Drop an index from the table.
  190. *
  191. * @param string $name
  192. * @return void
  193. */
  194. public function drop_index($name)
  195. {
  196. return $this->drop_key(__FUNCTION__, $name);
  197. }
  198. /**
  199. * Drop a foreign key constraint from the table.
  200. *
  201. * @param string $name
  202. * @return void
  203. */
  204. public function drop_foreign($name)
  205. {
  206. return $this->drop_key(__FUNCTION__, $name);
  207. }
  208. /**
  209. * Create a command to drop any type of index.
  210. *
  211. * @param string $type
  212. * @param string $name
  213. * @return Fluent
  214. */
  215. protected function drop_key($type, $name)
  216. {
  217. return $this->command($type, compact('name'));
  218. }
  219. /**
  220. * Add an auto-incrementing integer to the table.
  221. *
  222. * @param string $name
  223. * @return Fluent
  224. */
  225. public function increments($name)
  226. {
  227. return $this->integer($name, true);
  228. }
  229. /**
  230. * Add a string column to the table.
  231. *
  232. * @param string $name
  233. * @param int $length
  234. * @return Fluent
  235. */
  236. public function string($name, $length = 200)
  237. {
  238. return $this->column(__FUNCTION__, compact('name', 'length'));
  239. }
  240. /**
  241. * Add an integer column to the table.
  242. *
  243. * @param string $name
  244. * @param bool $increment
  245. * @return Fluent
  246. */
  247. public function integer($name, $increment = false)
  248. {
  249. return $this->column(__FUNCTION__, compact('name', 'increment'));
  250. }
  251. /**
  252. * Add a float column to the table.
  253. *
  254. * @param string $name
  255. * @return Fluent
  256. */
  257. public function float($name)
  258. {
  259. return $this->column(__FUNCTION__, compact('name'));
  260. }
  261. /**
  262. * Add a decimal column to the table.
  263. *
  264. * @param string $name
  265. * @param int $precision
  266. * @param int $scale
  267. * @return Fluent
  268. */
  269. public function decimal($name, $precision, $scale)
  270. {
  271. return $this->column(__FUNCTION__, compact('name', 'precision', 'scale'));
  272. }
  273. /**
  274. * Add a boolean column to the table.
  275. *
  276. * @param string $name
  277. * @return Fluent
  278. */
  279. public function boolean($name)
  280. {
  281. return $this->column(__FUNCTION__, compact('name'));
  282. }
  283. /**
  284. * Create date-time columns for creation and update timestamps.
  285. *
  286. * @return void
  287. */
  288. public function timestamps()
  289. {
  290. $this->date('created_at');
  291. $this->date('updated_at');
  292. }
  293. /**
  294. * Add a date-time column to the table.
  295. *
  296. * @param string $name
  297. * @return Fluent
  298. */
  299. public function date($name)
  300. {
  301. return $this->column(__FUNCTION__, compact('name'));
  302. }
  303. /**
  304. * Add a timestamp column to the table.
  305. *
  306. * @param string $name
  307. * @return Fluent
  308. */
  309. public function timestamp($name)
  310. {
  311. return $this->column(__FUNCTION__, compact('name'));
  312. }
  313. /**
  314. * Add a text column to the table.
  315. *
  316. * @param string $name
  317. * @return Fluent
  318. */
  319. public function text($name)
  320. {
  321. return $this->column(__FUNCTION__, compact('name'));
  322. }
  323. /**
  324. * Add a blob column to the table.
  325. *
  326. * @param string $name
  327. * @return Fluent
  328. */
  329. public function blob($name)
  330. {
  331. return $this->column(__FUNCTION__, compact('name'));
  332. }
  333. /**
  334. * Set the database connection for the table operation.
  335. *
  336. * @param string $connection
  337. * @return void
  338. */
  339. public function on($connection)
  340. {
  341. $this->connection = $connection;
  342. }
  343. /**
  344. * Determine if the schema table has a creation command.
  345. *
  346. * @return bool
  347. */
  348. public function creating()
  349. {
  350. return ! is_null(array_first($this->commands, function($key, $value)
  351. {
  352. return $value->type == 'create';
  353. }));
  354. }
  355. /**
  356. * Create a new fluent command instance.
  357. *
  358. * @param string $type
  359. * @param array $parameters
  360. * @return Fluent
  361. */
  362. protected function command($type, $parameters = array())
  363. {
  364. $parameters = array_merge(compact('type'), $parameters);
  365. return $this->commands[] = new Fluent($parameters);
  366. }
  367. /**
  368. * Create a new fluent column instance.
  369. *
  370. * @param string $type
  371. * @param array $parameters
  372. * @return Fluent
  373. */
  374. protected function column($type, $parameters = array())
  375. {
  376. $parameters = array_merge(compact('type'), $parameters);
  377. return $this->columns[] = new Fluent($parameters);
  378. }
  379. }