-
Notifications
You must be signed in to change notification settings - Fork 6
/
Table.php
175 lines (146 loc) · 4.81 KB
/
Table.php
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
<?php
namespace mindplay\sql\model\schema;
use mindplay\sql\model\Driver;
use mindplay\sql\model\TypeProvider;
use ReflectionClass;
use ReflectionMethod;
/**
* This is an abstract base-class for user-defined Table-types belonging to a Schema.
*/
abstract class Table
{
private Schema $schema;
private Driver $driver;
private TypeProvider $types;
private string $name;
private string|null $alias;
public function __construct(Schema $schema, Driver $driver, TypeProvider $types, string $name, string|null $alias)
{
$this->schema = $schema;
$this->driver = $driver;
$this->types = $types;
$this->name = $name;
$this->alias = $alias;
}
/**
* @return Schema owner Schema instance
*/
public function getSchema(): Schema
{
return $this->schema;
}
public function getName(): string
{
return $this->name;
}
public function getAlias(): string|null
{
return $this->alias;
}
/**
* @return string table expression (e.g. "{table} AS {alias}" for use in the FROM clause of an SQL statement)
*/
public function getNode(): string
{
$alias = $this->getAlias();
$quoted_table_name = $this->driver->quoteTableName($this->schema->getName(), $this->getName());
if ($alias) {
return $quoted_table_name . ' AS ' . $this->driver->quoteName($alias);
}
return $quoted_table_name;
}
/**
* @param string|null $prefix optional Column Alias prefix
*
* @return Column[] list of all available Columns
*/
public function listColumns(string|null $prefix = null): array
{
// create a whitelist of parent types, excluding the Table class itself:
$type = get_class($this);
$whitelist = [];
while ($type && $type !== self::class) {
$whitelist[$type] = true;
$type = get_parent_class($type);
}
// reflect all available public methods:
$reflection = new ReflectionClass($this);
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
$columns = [];
foreach ($methods as $method) {
if (isset($whitelist[$method->class]) && !$method->isStatic()) {
$alias = $prefix
? "{$prefix}_{$method->name}"
: null;
$columns[] = $method->invoke($this, $alias);
}
}
return $columns;
}
/**
* Creates a required Column.
*
* A value *must* be specified when building an `INSERT` query - if you don't specify a value
* for this Column, the INSERT query-builder will throw an exception.
*/
protected function requiredColumn(string $name, string $type, string|null $alias = null): Column
{
return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, true, null, false);
}
/**
* Creates an optional Column.
*
* A value is optional (and may have a `$default`) when building an `INSERT` query - if you don't
* specify a value for this Column, the INSERT query-builder will automatically assign the `$default`.
*
* @param $name Column name
* @param $type Type class-name
* @param $alias Optional alias
* @param $default Optional default PHP value (Type-conversion will be applied.)
*
* @return Column
*/
protected function optionalColumn(string $name, string $type, string|null $alias = null, mixed $default = null): Column
{
return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, false, $default, false);
}
/**
* Creates an auto-defined Column.
*
* A value should *not* by specified when building an `INSERT` query.
*
* Use this for Columns that the database itself will populate, e.g. auto-incrementing keys or
* columns that are otherwise initialized by the database itself.
*
* @param $name Column name
* @param $type Type class-name
* @param $alias Optional alias
*/
protected function autoColumn(string $name, string $type, string|null $alias = null): Column
{
return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, false, null, true);
}
/**
* @ignore
*
* @return string
*/
public function __toString(): string
{
return $this->alias
? $this->driver->quoteName($this->alias)
: $this->driver->quoteTableName($this->schema->getName(), $this->name);
}
/**
* @ignore
*
* @param string $name
*
* @return Column
*/
public function __get($name)
{
// TODO caching
return $this->$name($this->alias ? "{$this->alias}_{$name}" : null);
}
}