1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Database; |
4
|
|
|
|
5
|
|
|
class TableSchema |
6
|
|
|
{ |
7
|
|
|
/** @var Doctrine\DBAL\Schema\Table|Backpack\CRUD\app\Library\Database\Table */ |
|
|
|
|
8
|
|
|
public $schema; |
9
|
|
|
|
10
|
|
|
public function __construct(string $connection, string $table) |
11
|
|
|
{ |
12
|
|
|
$this->schema = app('DatabaseSchema')->getForTable($connection, $table); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Return an array of column names in database. |
17
|
|
|
* |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
public function getColumnsNames() |
21
|
|
|
{ |
22
|
|
|
return array_values( |
23
|
|
|
array_map(function ($item) { |
24
|
|
|
return $item->getName(); |
25
|
|
|
}, $this->getColumns()) |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return the column type in database. |
31
|
|
|
* |
32
|
|
|
* @param string $columnName |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function getColumnType(string $columnName) |
36
|
|
|
{ |
37
|
|
|
if (! $this->schemaExists() || ! $this->schema->hasColumn($columnName)) { |
38
|
|
|
return 'varchar'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$column = $this->schema->getColumn($columnName); |
42
|
|
|
|
43
|
|
|
return $column->getType()->getName(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check if the column exists in the database. |
48
|
|
|
* |
49
|
|
|
* @param string $columnName |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function hasColumn($columnName) |
53
|
|
|
{ |
54
|
|
|
if (! $this->schemaExists()) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->schema->hasColumn($columnName); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check if the column is nullable in database. |
63
|
|
|
* |
64
|
|
|
* @param string $columnName |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function columnIsNullable($columnName) |
68
|
|
|
{ |
69
|
|
|
if (! $this->columnExists($columnName)) { |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$column = $this->schema->getColumn($columnName); |
74
|
|
|
|
75
|
|
|
return $column->getNotnull() ? false : true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Check if the column has default value set on database. |
80
|
|
|
* |
81
|
|
|
* @param string $columnName |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function columnHasDefault($columnName) |
85
|
|
|
{ |
86
|
|
|
if (! $this->columnExists($columnName)) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$column = $this->schema->getColumn($columnName); |
91
|
|
|
|
92
|
|
|
return $column->getDefault() !== null ? true : false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the default value for a column on database. |
97
|
|
|
* |
98
|
|
|
* @param string $columnName |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function getColumnDefault($columnName) |
102
|
|
|
{ |
103
|
|
|
if (! $this->columnExists($columnName)) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$column = $this->schema->getColumn($columnName); |
108
|
|
|
|
109
|
|
|
return $column->getDefault(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the table schema columns. |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getColumns() |
118
|
|
|
{ |
119
|
|
|
if (! $this->schemaExists()) { |
120
|
|
|
return []; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->schema->getColumns(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Make sure column exists or throw an exception. |
128
|
|
|
* |
129
|
|
|
* @param string $columnName |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
private function columnExists($columnName) |
133
|
|
|
{ |
134
|
|
|
if (! $this->schemaExists()) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->schema->hasColumn($columnName); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Make sure the schema for the connection is initialized. |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
private function schemaExists() |
147
|
|
|
{ |
148
|
|
|
if (! empty($this->schema)) { |
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|