table.php 7.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<?php namespace Laravel\Database\Schema;

use Laravel\Fluent;

class Table {

	/**
	 * The database table name.
	 *
	 * @var string
	 */
	public $name;

	/**
	 * The database connection that should be used.
	 *
	 * @var string
	 */
	public $connection;

	/**
	 * The engine that should be used for the table.
	 *
	 * @var string
	 */
	public $engine;

	/**
	 * The columns that should be added to the table.
	 *
	 * @var array
	 */
	public $columns = array();

	/**
	 * The commands that should be executed on the table.
	 *
	 * @var array
	 */
	public $commands = array();

	/**
	 * Create a new schema table instance.
	 *
	 * @param  string  $name
	 * @return void
	 */
	public function __construct($name)
	{
		$this->name = $name;
	}

	/**
	 * Indicate that the table should be created.
	 *
	 * @return Fluent
	 */
	public function create()
	{
		return $this->command(__FUNCTION__);
	}

	/**
	 * Create a new primary key on the table.
	 *
	 * @param  string|array  $columns
	 * @param  string        $name
	 * @return Fluent
	 */
70
	public function primary($columns, $name = null)
71 72 73 74 75 76 77 78 79 80 81
	{
		return $this->key(__FUNCTION__, $columns, $name);
	}

	/**
	 * Create a new unique index on the table.
	 *
	 * @param  string|array  $columns
	 * @param  string        $name
	 * @return Fluent
	 */
82
	public function unique($columns, $name = null)
83 84 85 86 87 88 89 90 91 92 93
	{
		return $this->key(__FUNCTION__, $columns, $name);
	}

	/**
	 * Create a new full-text index on the table.
	 *
	 * @param  string|array  $columns
	 * @param  string        $name
	 * @return Fluent
	 */
94
	public function fulltext($columns, $name = null)
95 96 97 98 99 100 101
	{
		return $this->key(__FUNCTION__, $columns, $name);
	}

	/**
	 * Create a new index on the table.
	 *
Phill Sparks committed
102 103 104
	 * @param  string|array  $columns
	 * @param  string        $name
	 * @return Fluent
105
	 */
106
	public function index($columns, $name = null)
107 108
	{
		return $this->key(__FUNCTION__, $columns, $name);
109 110 111 112 113 114 115 116 117 118 119
	}

	/**
	 * Add a foreign key constraint to the table.
	 *
	 * @param  string|array  $columns
	 * @param  string        $name
	 */
	public function foreign($columns, $name = null)
	{
		return $this->key(__FUNCTION__, $columns, $name);
120 121 122 123 124 125 126 127 128 129
	}

	/**
	 * Create a command for creating any index.
	 *
	 * @param  string        $type
	 * @param  string|array  $columns
	 * @param  string        $name
	 * @return Fluent
	 */
130
	public function key($type, $columns, $name)
131
	{
132
		$columns = (array) $columns;
133

134 135 136 137 138
		// If no index name was specified, we will concatenate the columns and
		// append the index type to the name to generate a unique name for
		// the index that can be used when dropping indexes.
		if (is_null($name))
		{
139 140 141
			$name = str_replace(array('-', '.'), '_', $this->name);

			$name = $name.'_'.implode('_', $columns).'_'.$type;
142 143 144
		}

		return $this->command($type, compact('name', 'columns'));
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	}

	/**
	 * Drop the database table.
	 *
	 * @return Fluent
	 */
	public function drop()
	{
		return $this->command(__FUNCTION__);
	}

	/**
	 * Drop a column from the table.
	 *
	 * @param  string|array  $columns
	 * @return void
	 */
	public function drop_column($columns)
	{
		return $this->command(__FUNCTION__, array('columns' => (array) $columns));
	}

	/**
	 * Drop a primary key from the table.
	 *
	 * @param  string  $name
	 * @return void
	 */
174
	public function drop_primary($name = null)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	{
		return $this->drop_key(__FUNCTION__, $name);
	}

	/**
	 * Drop a unique index from the table.
	 *
	 * @param  string  $name
	 * @return void
	 */
	public function drop_unique($name)
	{
		return $this->drop_key(__FUNCTION__, $name);
	}

	/**
	 * Drop a full-text index from the table.
	 *
	 * @param  string  $name
	 * @return void
	 */
	public function drop_fulltext($name)
	{
		return $this->drop_key(__FUNCTION__, $name);
	}

	/**
	 * Drop an index from the table.
	 *
	 * @param  string  $name
	 * @return void
	 */
	public function drop_index($name)
	{
		return $this->drop_key(__FUNCTION__, $name);
	}

	/**
213 214 215 216 217 218 219 220 221 222 223
	 * Drop a foreign key constraint from the table.
	 *
	 * @param  string  $name
	 * @return void
	 */
	public function drop_foreign($name)
	{
		return $this->drop_key(__FUNCTION__, $name);
	}

	/**
224 225 226 227 228 229 230 231
	 * Create a command to drop any type of index.
	 *
	 * @param  string  $type
	 * @param  string  $name
	 * @return Fluent
	 */
	protected function drop_key($type, $name)
	{
232
		return $this->command($type, compact('name'));
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
	}

	/**
	 * Add an auto-incrementing integer to the table.
	 *
	 * @param  string  $name
	 * @return Fluent
	 */
	public function increments($name)
	{
		return $this->integer($name, true);
	}

	/**
	 * Add a string column to the table.
	 *
	 * @param  string  $name
	 * @param  int     $length
	 * @return Fluent
	 */
	public function string($name, $length = 200)
	{
		return $this->column(__FUNCTION__, compact('name', 'length'));
	}

	/**
	 * Add an integer column to the table.
	 *
	 * @param  string  $name
	 * @param  bool    $increment
	 * @return Fluent
	 */
	public function integer($name, $increment = false)
	{
		return $this->column(__FUNCTION__, compact('name', 'increment'));
	}

	/**
	 * Add a float column to the table.
	 *
	 * @param  string  $name
	 * @return Fluent
	 */
	public function float($name)
	{
		return $this->column(__FUNCTION__, compact('name'));
	}

	/**
282 283 284 285 286 287 288 289 290 291 292 293 294
	 * Add a decimal column to the table.
	 *
	 * @param  string  $name
	 * @param  int     $precision
	 * @param  int     $scale
	 * @return Fluent
	 */
	public function decimal($name, $precision, $scale)
	{
		return $this->column(__FUNCTION__, compact('name', 'precision', 'scale'));
	}

	/**
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	 * Add a boolean column to the table.
	 *
	 * @param  string  $name
	 * @return Fluent
	 */
	public function boolean($name)
	{
		return $this->column(__FUNCTION__, compact('name'));
	}

	/**
	 * Create date-time columns for creation and update timestamps.
	 *
	 * @return void
	 */
	public function timestamps()
	{
		$this->date('created_at');

		$this->date('updated_at');
	}

	/**
	 * Add a date-time column to the table.
	 *
	 * @param  string  $name
	 * @return Fluent
	 */
	public function date($name)
	{
		return $this->column(__FUNCTION__, compact('name'));
	}

	/**
	 * Add a timestamp column to the table.
	 *
	 * @param  string  $name
	 * @return Fluent
	 */
	public function timestamp($name)
	{
		return $this->column(__FUNCTION__, compact('name'));
	}

	/**
	 * Add a text column to the table.
	 *
	 * @param  string  $name
	 * @return Fluent
	 */
	public function text($name)
	{
		return $this->column(__FUNCTION__, compact('name'));
	}

	/**
	 * Add a blob column to the table.
	 *
	 * @param  string  $name
	 * @return Fluent
	 */
	public function blob($name)
	{
		return $this->column(__FUNCTION__, compact('name'));
	}

	/**
	 * Set the database connection for the table operation.
	 *
	 * @param  string  $connection
	 * @return void
	 */
	public function on($connection)
	{
		$this->connection = $connection;
	}

	/**
	 * Determine if the schema table has a creation command.
	 *
	 * @return bool
	 */
	public function creating()
	{
		return ! is_null(array_first($this->commands, function($key, $value)
		{
			return $value->type == 'create';
		}));
	}

	/**
	 * Create a new fluent command instance.
	 *
	 * @param  string  $type
	 * @param  array   $parameters
	 * @return Fluent
	 */
	protected function command($type, $parameters = array())
	{
		$parameters = array_merge(compact('type'), $parameters);

		$this->commands[] = new Fluent($parameters);

		return end($this->commands);
	}

	/**
	 * Create a new fluent column instance.
	 *
	 * @param  string  $type
	 * @param  array   $parameters
	 * @return Fluent
	 */
	protected function column($type, $parameters = array())
	{
		$parameters = array_merge(compact('type'), $parameters);

		$this->columns[] = new Fluent($parameters);

		return end($this->columns);
	}

}