|
1
|
|
|
<?php namespace jlourenco\base\Database; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Database\Schema\Blueprint as IlluminateBlueprint; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Extended version of Blueprint with |
|
7
|
|
|
* support of 'set' data type |
|
8
|
|
|
*/ |
|
9
|
|
|
class Blueprint extends IlluminateBlueprint { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Create a new binary column on the table. |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $column |
|
15
|
|
|
* @param int $length |
|
16
|
|
|
* @return \Illuminate\Support\Fluent |
|
17
|
|
|
*/ |
|
18
|
|
|
public function binary($column, $length = 255) |
|
19
|
|
|
{ |
|
20
|
|
|
return $this->addColumn('binary', $column, compact('length')); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Add creation and update user information to the table. |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function creation() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->integer('created_by')->unsigned(); |
|
31
|
|
|
$this->integer('modified_by')->unsigned(); |
|
32
|
|
|
$this->integer('deleted_by')->unsigned(); |
|
33
|
|
|
|
|
34
|
|
|
$this->foreign('created_by')->references('id')->on('User'); |
|
35
|
|
|
$this->foreign('modified_by')->references('id')->on('User'); |
|
36
|
|
|
$this->foreign('deleted_by')->references('id')->on('User'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a new 'set' column on the table. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $column |
|
43
|
|
|
* @param array $allowed |
|
44
|
|
|
* @return \Illuminate\Support\Fluent |
|
45
|
|
|
*/ |
|
46
|
|
|
public function set($column, array $allowed) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->addColumn('set', $column, compact('allowed')); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Specify a unique index for the table. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string|array $columns |
|
55
|
|
|
* @param string $name |
|
56
|
|
|
* @param int $length |
|
57
|
|
|
* @return \Illuminate\Support\Fluent |
|
58
|
|
|
*/ |
|
59
|
|
|
public function unique($columns, $name = null, $length = null) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->indexCommand('unique', $columns, $name, $length); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Specify an index for the table. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string|array $columns |
|
68
|
|
|
* @param string $name |
|
69
|
|
|
* @param int $length |
|
70
|
|
|
* @return \Illuminate\Support\Fluent |
|
71
|
|
|
*/ |
|
72
|
|
|
public function index($columns, $name = null, $length = null) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->indexCommand('index', $columns, $name, $length); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Determine if the given table exists. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $table |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public function hasForeign($table, $foreign) |
|
85
|
|
|
{ |
|
86
|
|
|
$sql = $this->grammar->compileHasForeign(); |
|
87
|
|
|
$table = $this->connection->getTablePrefix() . $table; |
|
88
|
|
|
return count($this->connection->select($sql, [$table, $foreign])) > 0; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Add a new index command to the blueprint. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $type |
|
95
|
|
|
* @param string|array $columns |
|
96
|
|
|
* @param string $index |
|
97
|
|
|
* @param int $length |
|
98
|
|
|
* @return \Illuminate\Support\Fluent |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function indexCommand($type, $columns, $index, $length = null) |
|
101
|
|
|
{ |
|
102
|
|
|
$columns = (array) $columns; |
|
103
|
|
|
// If no name was specified for this index, we will create one using a basic |
|
104
|
|
|
// convention of the table name, followed by the columns, followed by an |
|
105
|
|
|
// index type, such as primary or index, which makes the index unique. |
|
106
|
|
|
if (is_null($index)) |
|
107
|
|
|
$index = $this->createIndexName($type, $columns); |
|
108
|
|
|
return $this->addCommand($type, compact('index', 'columns', 'length')); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |