-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathConstraint.php
More file actions
176 lines (150 loc) · 4.15 KB
/
Constraint.php
File metadata and controls
176 lines (150 loc) · 4.15 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
174
175
176
<?php
namespace Exodus4D\Pathfinder\Db\Sql\Mysql;
use DB\SQL;
class Constraint
{
// available actions
const ACTIONS_DELETE = ['RESTRICT', 'CASCADE', 'SET NULL', 'NO ACTION'];
const ACTIONS_UPDATE = ['RESTRICT', 'CASCADE', 'SET NULL', 'NO ACTION'];
// default actions
const ACTION_DELETE = 'RESTRICT';
const ACTION_UPDATE = 'RESTRICT';
const TEXT_ActionNotSupported = 'Constraint action `%s` is not supported.';
protected $table;
protected $keys = [];
protected $referencedTable = '';
protected $referencedCols = [];
protected $onDelete = self::ACTION_DELETE;
protected $onUpdate = self::ACTION_UPDATE;
/**
* Constraint constructor.
* @param SQL\TableBuilder $table
* @param array $keys
* @param string $referencedTable
* @param array $referencedCols
*/
public function __construct(SQL\TableBuilder $table, $keys = [], $referencedTable = '', $referencedCols = ['id'])
{
$this->table = &$table;
$this->setKeys($keys);
$this->setReferencedTable($referencedTable);
$this->setReferencedCols($referencedCols);
}
/**
* @param mixed $keys
*/
public function setKeys($keys)
{
$this->keys = (array)$keys;
}
/**
* @param mixed $referencedTable
*/
public function setReferencedTable($referencedTable)
{
$this->referencedTable = $referencedTable;
}
/**
* @param mixed $referencedCols
*/
public function setReferencedCols($referencedCols)
{
$this->referencedCols = (array)$referencedCols;
}
/**
* @param string $onDelete
*/
public function setOnDelete($onDelete)
{
if (in_array($onDelete, self::ACTIONS_DELETE)) {
$this->onDelete = $onDelete;
} else {
trigger_error(sprintf(self::TEXT_ActionNotSupported, $onDelete));
}
}
/**
* @param string $onUpdate
*/
public function setOnUpdate($onUpdate)
{
if (in_array($onUpdate, self::ACTIONS_UPDATE)) {
$this->onUpdate = $onUpdate;
} else {
trigger_error(sprintf(self::TEXT_ActionNotSupported, $onUpdate));
}
}
/**
* @return array
*/
public function getKeys()
{
return $this->keys;
}
/**
* @return string
*/
public function getReferencedTable()
{
return $this->referencedTable;
}
/**
* @return array
*/
public function getReferencedCols()
{
return $this->referencedCols;
}
/**
* @return string
*/
public function getOnDelete()
{
return $this->onDelete;
}
/**
* @return string
*/
public function getOnUpdate()
{
return $this->onUpdate;
}
/**
* get a constraint name for this table.
* This can either be used to generate unique constraint names for foreign keys in parent tables
* or generate a "part" of a name. e.g. for db-Query all constraints of this table (ignore columns)
* by "LIKE" selecting "information_schema"
* -> To get a certain constraint or generate a unique constraint, ALL params are required!
* @return string
*/
public function getConstraintName()
{
$constraintName = 'fk_' . $this->table->name;
if (!empty($this->getKeys())) {
$constraintName .= '___' . implode('__', $this->getKeys());
if (!empty($this->getReferencedTable())) {
$constraintName .= '___' . $this->getReferencedTable();
if (!empty($this->getReferencedCols())) {
$constraintName .= '___' . implode('__', $this->getReferencedCols());
}
}
}
return $constraintName;
}
/**
* checks if constraint is valid
* -> all required members must be set!
* @return bool
*/
public function isValid()
{
$valid = false;
if (
!empty($this->getKeys()) &&
!empty($this->getReferencedTable()) &&
!empty($this->getReferencedCols())
) {
$valid = true;
}
return $valid;
}
}