table.php 6.8 KB

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