|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mindplay\sql\model\schema; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\sql\model\Driver; |
|
6
|
|
|
use mindplay\sql\model\TypeProvider; |
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
use ReflectionMethod; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This is an abstract base-class for user-defined Table-types belonging to a Schema. |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Table |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Schema |
|
17
|
|
|
*/ |
|
18
|
|
|
private $schema; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Driver |
|
22
|
|
|
*/ |
|
23
|
|
|
private $driver; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var TypeProvider |
|
27
|
|
|
*/ |
|
28
|
|
|
private $types; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $name; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string|null |
|
37
|
|
|
*/ |
|
38
|
|
|
private $alias; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Schema $schema |
|
42
|
|
|
* @param Driver $driver |
|
43
|
|
|
* @param TypeProvider $types |
|
44
|
|
|
* @param string $name |
|
45
|
|
|
* @param string|null $alias |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function __construct(Schema $schema, Driver $driver, TypeProvider $types, $name, $alias) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->schema = $schema; |
|
50
|
1 |
|
$this->driver = $driver; |
|
51
|
1 |
|
$this->types = $types; |
|
52
|
1 |
|
$this->name = $name; |
|
53
|
1 |
|
$this->alias = $alias; |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return Schema owner Schema instance |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function getSchema() |
|
60
|
|
|
{ |
|
61
|
1 |
|
return $this->schema; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function getName() |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->name; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string|null |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function getAlias() |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->alias; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string table expression (e.g. "{table} AS {alias}" for use in the FROM clause of an SQL statement) |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getNode() |
|
84
|
|
|
{ |
|
85
|
1 |
|
$alias = $this->getAlias(); |
|
86
|
|
|
|
|
87
|
1 |
|
$quoted_table_name = $this->driver->quoteTableName($this->schema->getName(), $this->getName()); |
|
88
|
|
|
|
|
89
|
1 |
|
if ($alias) { |
|
|
|
|
|
|
90
|
1 |
|
return $quoted_table_name . ' AS ' . $this->driver->quoteName($alias); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
return $quoted_table_name; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string|null $prefix optional Column Alias prefix |
|
98
|
|
|
* |
|
99
|
|
|
* @return Column[] list of all available Columns |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function listColumns($prefix = null) |
|
102
|
|
|
{ |
|
103
|
|
|
// create a whitelist of parent types, excluding the Table class itself: |
|
104
|
|
|
|
|
105
|
1 |
|
$type = get_class($this); |
|
106
|
|
|
|
|
107
|
1 |
|
$whitelist = []; |
|
108
|
|
|
|
|
109
|
1 |
|
while ($type && $type !== self::class) { |
|
110
|
1 |
|
$whitelist[$type] = true; |
|
111
|
|
|
|
|
112
|
1 |
|
$type = get_parent_class($type); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// reflect all available public methods: |
|
116
|
|
|
|
|
117
|
1 |
|
$reflection = new ReflectionClass($this); |
|
118
|
|
|
|
|
119
|
1 |
|
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); |
|
120
|
|
|
|
|
121
|
1 |
|
$columns = []; |
|
122
|
|
|
|
|
123
|
1 |
|
foreach ($methods as $method) { |
|
124
|
1 |
|
if (isset($whitelist[$method->class]) && !$method->isStatic()) { |
|
125
|
1 |
|
$alias = $prefix |
|
126
|
1 |
|
? "{$prefix}_{$method->name}" |
|
127
|
1 |
|
: null; |
|
128
|
|
|
|
|
129
|
1 |
|
$columns[] = $method->invoke($this, $alias); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
return $columns; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $name |
|
138
|
|
|
* @param string $type Type class-name |
|
139
|
|
|
* @param string|null $alias |
|
140
|
|
|
* @param mixed $default |
|
141
|
|
|
* |
|
142
|
|
|
* @return Column |
|
143
|
|
|
*/ |
|
144
|
1 |
|
protected function requiredColumn($name, $type, $alias = null, $default = null) |
|
145
|
|
|
{ |
|
146
|
1 |
|
return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, true, $default, false); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $name |
|
151
|
|
|
* @param string $type Type class-name |
|
152
|
|
|
* @param string|null $alias |
|
153
|
|
|
* @param mixed $default |
|
154
|
|
|
* |
|
155
|
|
|
* @return Column |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function optionalColumn($name, $type, $alias = null, $default = null) |
|
158
|
|
|
{ |
|
159
|
|
|
return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, false, $default, false); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param string $name |
|
164
|
|
|
* @param string $type Type class-name |
|
165
|
|
|
* @param string|null $alias |
|
166
|
|
|
* |
|
167
|
|
|
* @return Column |
|
168
|
|
|
*/ |
|
169
|
1 |
|
protected function autoColumn($name, $type, $alias = null) |
|
170
|
|
|
{ |
|
171
|
1 |
|
return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, false, null, true); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @ignore |
|
176
|
|
|
* |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function __toString() |
|
180
|
|
|
{ |
|
181
|
1 |
|
return $this->alias |
|
182
|
1 |
|
? $this->driver->quoteName($this->alias) |
|
183
|
1 |
|
: $this->driver->quoteTableName($this->schema->getName(), $this->name); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @ignore |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $name |
|
190
|
|
|
* |
|
191
|
|
|
* @return Column |
|
192
|
|
|
*/ |
|
193
|
1 |
|
public function __get($name) |
|
194
|
|
|
{ |
|
195
|
|
|
// TODO caching |
|
196
|
|
|
|
|
197
|
1 |
|
return $this->$name($this->alias ? "{$this->alias}_{$name}" : null); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: