|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license http://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yii\db; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* ConstraintFinderTrait provides methods for getting a table constraint information. |
|
12
|
|
|
* |
|
13
|
|
|
* @property CheckConstraint[][] $schemaChecks Check constraints for all tables in the database. |
|
14
|
|
|
* Each array element is an array of [[CheckConstraint]] or its child classes. This property is read-only. |
|
15
|
|
|
* @property DefaultValueConstraint[] $schemaDefaultValues Default value constraints for all tables in the database. |
|
16
|
|
|
* Each array element is an array of [[DefaultValueConstraint]] or its child classes. This property is read-only. |
|
17
|
|
|
* @property ForeignKeyConstraint[][] $schemaForeignKeys Foreign keys for all tables in the database. Each |
|
18
|
|
|
* array element is an array of [[ForeignKeyConstraint]] or its child classes. This property is read-only. |
|
19
|
|
|
* @property IndexConstraint[][] $schemaIndexes Indexes for all tables in the database. Each array element is |
|
20
|
|
|
* an array of [[IndexConstraint]] or its child classes. This property is read-only. |
|
21
|
|
|
* @property Constraint[] $schemaPrimaryKeys Primary keys for all tables in the database. Each array element |
|
22
|
|
|
* is an instance of [[Constraint]] or its child class. This property is read-only. |
|
23
|
|
|
* @property IndexConstraint[][] $schemaUniques Unique constraints for all tables in the database. |
|
24
|
|
|
* Each array element is an array of [[IndexConstraint]] or its child classes. This property is read-only. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Sergey Makinen <[email protected]> |
|
27
|
|
|
* @since 2.0.13 |
|
28
|
|
|
*/ |
|
29
|
|
|
trait ConstraintFinderTrait |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Returns the metadata of the given type for the given table. |
|
33
|
|
|
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
|
34
|
|
|
* @param string $type metadata type. |
|
35
|
|
|
* @param bool $refresh whether to reload the table metadata even if it is found in the cache. |
|
36
|
|
|
* @return mixed metadata. |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract protected function getTableMetadata($name, $type, $refresh); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns the metadata of the given type for all tables in the given schema. |
|
42
|
|
|
* @param string $schema the schema of the metadata. Defaults to empty string, meaning the current or default schema name. |
|
43
|
|
|
* @param string $type metadata type. |
|
44
|
|
|
* @param bool $refresh whether to fetch the latest available table metadata. If this is `false`, |
|
45
|
|
|
* cached data may be returned if available. |
|
46
|
|
|
* @return array array of metadata. |
|
47
|
|
|
*/ |
|
48
|
|
|
abstract protected function getSchemaMetadata($schema, $type, $refresh); |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Loads a primary key for the given table. |
|
52
|
|
|
* @param string $tableName table name. |
|
53
|
|
|
* @return Constraint|null primary key for the given table, `null` if the table has no primary key. |
|
54
|
|
|
*/ |
|
55
|
|
|
abstract protected function loadTablePrimaryKey($tableName); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Loads all foreign keys for the given table. |
|
59
|
|
|
* @param string $tableName table name. |
|
60
|
|
|
* @return ForeignKeyConstraint[] foreign keys for the given table. |
|
61
|
|
|
*/ |
|
62
|
|
|
abstract protected function loadTableForeignKeys($tableName); |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Loads all indexes for the given table. |
|
66
|
|
|
* @param string $tableName table name. |
|
67
|
|
|
* @return IndexConstraint[] indexes for the given table. |
|
68
|
|
|
*/ |
|
69
|
|
|
abstract protected function loadTableIndexes($tableName); |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Loads all unique constraints for the given table. |
|
73
|
|
|
* @param string $tableName table name. |
|
74
|
|
|
* @return Constraint[] unique constraints for the given table. |
|
75
|
|
|
*/ |
|
76
|
|
|
abstract protected function loadTableUniques($tableName); |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Loads all check constraints for the given table. |
|
80
|
|
|
* @param string $tableName table name. |
|
81
|
|
|
* @return CheckConstraint[] check constraints for the given table. |
|
82
|
|
|
*/ |
|
83
|
|
|
abstract protected function loadTableChecks($tableName); |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Loads all default value constraints for the given table. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $tableName table name. |
|
89
|
|
|
* @return DefaultValueConstraint[] default value constraints for the given table. |
|
90
|
|
|
*/ |
|
91
|
|
|
abstract protected function loadTableDefaultValues($tableName); |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Obtains the primary key for the named table. |
|
95
|
|
|
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
|
96
|
|
|
* @param bool $refresh whether to reload the information even if it is found in the cache. |
|
97
|
|
|
* @return Constraint|null table primary key, `null` if the table has no primary key. |
|
98
|
|
|
*/ |
|
99
|
38 |
|
public function getTablePrimaryKey($name, $refresh = false) |
|
100
|
|
|
{ |
|
101
|
38 |
|
return $this->getTableMetadata($name, 'primaryKey', $refresh); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns primary keys for all tables in the database. |
|
106
|
|
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
|
107
|
|
|
* @param bool $refresh whether to fetch the latest available table schemas. If this is `false`, |
|
108
|
|
|
* cached data may be returned if available. |
|
109
|
|
|
* @return Constraint[] primary keys for all tables in the database. |
|
110
|
|
|
* Each array element is an instance of [[Constraint]] or its child class. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getSchemaPrimaryKeys($schema = '', $refresh = false) |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->getSchemaMetadata($schema, 'primaryKey', $refresh); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Obtains the foreign keys information for the named table. |
|
119
|
|
|
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
|
120
|
|
|
* @param bool $refresh whether to reload the information even if it is found in the cache. |
|
121
|
|
|
* @return ForeignKeyConstraint[] table foreign keys. |
|
122
|
|
|
*/ |
|
123
|
11 |
|
public function getTableForeignKeys($name, $refresh = false) |
|
124
|
|
|
{ |
|
125
|
11 |
|
return $this->getTableMetadata($name, 'foreignKeys', $refresh); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns foreign keys for all tables in the database. |
|
130
|
|
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
|
131
|
|
|
* @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
|
132
|
|
|
* cached data may be returned if available. |
|
133
|
|
|
* @return ForeignKeyConstraint[][] foreign keys for all tables in the database. |
|
134
|
|
|
* Each array element is an array of [[ForeignKeyConstraint]] or its child classes. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getSchemaForeignKeys($schema = '', $refresh = false) |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->getSchemaMetadata($schema, 'foreignKeys', $refresh); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Obtains the indexes information for the named table. |
|
143
|
|
|
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
|
144
|
|
|
* @param bool $refresh whether to reload the information even if it is found in the cache. |
|
145
|
|
|
* @return IndexConstraint[] table indexes. |
|
146
|
|
|
*/ |
|
147
|
30 |
|
public function getTableIndexes($name, $refresh = false) |
|
148
|
|
|
{ |
|
149
|
30 |
|
return $this->getTableMetadata($name, 'indexes', $refresh); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Returns indexes for all tables in the database. |
|
154
|
|
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
|
155
|
|
|
* @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
|
156
|
|
|
* cached data may be returned if available. |
|
157
|
|
|
* @return IndexConstraint[][] indexes for all tables in the database. |
|
158
|
|
|
* Each array element is an array of [[IndexConstraint]] or its child classes. |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getSchemaIndexes($schema = '', $refresh = false) |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->getSchemaMetadata($schema, 'indexes', $refresh); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Obtains the unique constraints information for the named table. |
|
167
|
|
|
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
|
168
|
|
|
* @param bool $refresh whether to reload the information even if it is found in the cache. |
|
169
|
|
|
* @return Constraint[] table unique constraints. |
|
170
|
|
|
*/ |
|
171
|
38 |
|
public function getTableUniques($name, $refresh = false) |
|
172
|
|
|
{ |
|
173
|
38 |
|
return $this->getTableMetadata($name, 'uniques', $refresh); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Returns unique constraints for all tables in the database. |
|
178
|
|
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
|
179
|
|
|
* @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
|
180
|
|
|
* cached data may be returned if available. |
|
181
|
|
|
* @return Constraint[][] unique constraints for all tables in the database. |
|
182
|
|
|
* Each array element is an array of [[Constraint]] or its child classes. |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getSchemaUniques($schema = '', $refresh = false) |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->getSchemaMetadata($schema, 'uniques', $refresh); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Obtains the check constraints information for the named table. |
|
191
|
|
|
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
|
192
|
|
|
* @param bool $refresh whether to reload the information even if it is found in the cache. |
|
193
|
|
|
* @return CheckConstraint[] table check constraints. |
|
194
|
|
|
*/ |
|
195
|
37 |
|
public function getTableChecks($name, $refresh = false) |
|
196
|
|
|
{ |
|
197
|
37 |
|
return $this->getTableMetadata($name, 'checks', $refresh); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Returns check constraints for all tables in the database. |
|
202
|
|
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
|
203
|
|
|
* @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
|
204
|
|
|
* cached data may be returned if available. |
|
205
|
|
|
* @return CheckConstraint[][] check constraints for all tables in the database. |
|
206
|
|
|
* Each array element is an array of [[CheckConstraint]] or its child classes. |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getSchemaChecks($schema = '', $refresh = false) |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->getSchemaMetadata($schema, 'checks', $refresh); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Obtains the default value constraints information for the named table. |
|
215
|
|
|
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
|
216
|
|
|
* @param bool $refresh whether to reload the information even if it is found in the cache. |
|
217
|
|
|
* @return DefaultValueConstraint[] table default value constraints. |
|
218
|
|
|
*/ |
|
219
|
36 |
|
public function getTableDefaultValues($name, $refresh = false) |
|
220
|
|
|
{ |
|
221
|
36 |
|
return $this->getTableMetadata($name, 'defaultValues', $refresh); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Returns default value constraints for all tables in the database. |
|
226
|
|
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
|
227
|
|
|
* @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
|
228
|
|
|
* cached data may be returned if available. |
|
229
|
|
|
* @return DefaultValueConstraint[] default value constraints for all tables in the database. |
|
230
|
|
|
* Each array element is an array of [[DefaultValueConstraint]] or its child classes. |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getSchemaDefaultValues($schema = '', $refresh = false) |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->getSchemaMetadata($schema, 'defaultValues', $refresh); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|