table.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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)
  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)
  83. {
  84. return $this->key(__FUNCTION__, $columns, $name);
  85. }
  86. /**
  87. * Create a new index on the table.
  88. *
  89. * @param string|array
  90. */
  91. public function index($columns, $name)
  92. {
  93. return $this->key(__FUNCTION__, $columns, $name);
  94. }
  95. /**
  96. * Create a command for creating any index.
  97. *
  98. * @param string $type
  99. * @param string|array $columns
  100. * @param string $name
  101. * @return Fluent
  102. */
  103. public function key($type, $columns, $name)
  104. {
  105. $parameters = array('name' => $name, 'columns' => (array) $columns);
  106. return $this->command($type, $parameters);
  107. }
  108. /**
  109. * Drop the database table.
  110. *
  111. * @return Fluent
  112. */
  113. public function drop()
  114. {
  115. return $this->command(__FUNCTION__);
  116. }
  117. /**
  118. * Drop a column from the table.
  119. *
  120. * @param string|array $columns
  121. * @return void
  122. */
  123. public function drop_column($columns)
  124. {
  125. return $this->command(__FUNCTION__, array('columns' => (array) $columns));
  126. }
  127. /**
  128. * Drop a primary key from the table.
  129. *
  130. * @param string $name
  131. * @return void
  132. */
  133. public function drop_primary($name = null)
  134. {
  135. return $this->drop_key(__FUNCTION__, $name);
  136. }
  137. /**
  138. * Drop a unique index from the table.
  139. *
  140. * @param string $name
  141. * @return void
  142. */
  143. public function drop_unique($name)
  144. {
  145. return $this->drop_key(__FUNCTION__, $name);
  146. }
  147. /**
  148. * Drop a full-text index from the table.
  149. *
  150. * @param string $name
  151. * @return void
  152. */
  153. public function drop_fulltext($name)
  154. {
  155. return $this->drop_key(__FUNCTION__, $name);
  156. }
  157. /**
  158. * Drop an index from the table.
  159. *
  160. * @param string $name
  161. * @return void
  162. */
  163. public function drop_index($name)
  164. {
  165. return $this->drop_key(__FUNCTION__, $name);
  166. }
  167. /**
  168. * Create a command to drop any type of index.
  169. *
  170. * @param string $type
  171. * @param string $name
  172. * @return Fluent
  173. */
  174. protected function drop_key($type, $name)
  175. {
  176. return $this->command($type, array('name' => $name));
  177. }
  178. /**
  179. * Add an auto-incrementing integer to the table.
  180. *
  181. * @param string $name
  182. * @return Fluent
  183. */
  184. public function increments($name)
  185. {
  186. return $this->integer($name, true);
  187. }
  188. /**
  189. * Add a string column to the table.
  190. *
  191. * @param string $name
  192. * @param int $length
  193. * @return Fluent
  194. */
  195. public function string($name, $length = 200)
  196. {
  197. return $this->column(__FUNCTION__, compact('name', 'length'));
  198. }
  199. /**
  200. * Add an integer column to the table.
  201. *
  202. * @param string $name
  203. * @param bool $increment
  204. * @return Fluent
  205. */
  206. public function integer($name, $increment = false)
  207. {
  208. return $this->column(__FUNCTION__, compact('name', 'increment'));
  209. }
  210. /**
  211. * Add a float column to the table.
  212. *
  213. * @param string $name
  214. * @param bool $increment
  215. * @return Fluent
  216. */
  217. public function float($name)
  218. {
  219. return $this->column(__FUNCTION__, compact('name'));
  220. }
  221. /**
  222. * Add a boolean column to the table.
  223. *
  224. * @param string $name
  225. * @return Fluent
  226. */
  227. public function boolean($name)
  228. {
  229. return $this->column(__FUNCTION__, compact('name'));
  230. }
  231. /**
  232. * Create date-time columns for creation and update timestamps.
  233. *
  234. * @return void
  235. */
  236. public function timestamps()
  237. {
  238. $this->date('created_at');
  239. $this->date('updated_at');
  240. }
  241. /**
  242. * Add a date-time column to the table.
  243. *
  244. * @param string $name
  245. * @return Fluent
  246. */
  247. public function date($name)
  248. {
  249. return $this->column(__FUNCTION__, compact('name'));
  250. }
  251. /**
  252. * Add a timestamp column to the table.
  253. *
  254. * @param string $name
  255. * @return Fluent
  256. */
  257. public function timestamp($name)
  258. {
  259. return $this->column(__FUNCTION__, compact('name'));
  260. }
  261. /**
  262. * Add a text column to the table.
  263. *
  264. * @param string $name
  265. * @return Fluent
  266. */
  267. public function text($name)
  268. {
  269. return $this->column(__FUNCTION__, compact('name'));
  270. }
  271. /**
  272. * Add a blob column to the table.
  273. *
  274. * @param string $name
  275. * @return Fluent
  276. */
  277. public function blob($name)
  278. {
  279. return $this->column(__FUNCTION__, compact('name'));
  280. }
  281. /**
  282. * Set the database connection for the table operation.
  283. *
  284. * @param string $connection
  285. * @return void
  286. */
  287. public function on($connection)
  288. {
  289. $this->connection = $connection;
  290. }
  291. /**
  292. * Determine if the schema table has a creation command.
  293. *
  294. * @return bool
  295. */
  296. public function creating()
  297. {
  298. return ! is_null(array_first($this->commands, function($key, $value)
  299. {
  300. return $value->type == 'create';
  301. }));
  302. }
  303. /**
  304. * Create a new fluent command instance.
  305. *
  306. * @param string $type
  307. * @param array $parameters
  308. * @return Fluent
  309. */
  310. protected function command($type, $parameters = array())
  311. {
  312. $parameters = array_merge(compact('type'), $parameters);
  313. $this->commands[] = new Fluent($parameters);
  314. return end($this->commands);
  315. }
  316. /**
  317. * Create a new fluent column instance.
  318. *
  319. * @param string $type
  320. * @param array $parameters
  321. * @return Fluent
  322. */
  323. protected function column($type, $parameters = array())
  324. {
  325. $parameters = array_merge(compact('type'), $parameters);
  326. $this->columns[] = new Fluent($parameters);
  327. return end($this->columns);
  328. }
  329. }