This repository was archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractTableQuery.php
executable file
·202 lines (186 loc) · 5.46 KB
/
AbstractTableQuery.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Dbal\Sql\Component\Query\Table;
use Ulrack\Dbal\Common\QueryInterface;
use Ulrack\Dbal\Sql\Common\CascadeEnum;
use Ulrack\Dbal\Sql\Common\ColumnDefinitionInterface;
abstract class AbstractTableQuery implements QueryInterface
{
/**
* Contains the columns for the table query.
*
* @var array
*/
private $columns = ['add' => [], 'drop' => [], 'alter' => []];
/**
* Contains the keys for the table.
*
* @var array
*/
private $keys = ['add' => [], 'drop' => []];
/**
* Adds a primary key to the query.
*
* @param string ...$keys
*
* @return void
*/
public function addPrimaryKey(string ...$keys): void
{
$this->keys['add'][] = sprintf(
'CONSTRAINT PRIMARY KEY(`%s`)',
implode('`, `', $keys)
);
}
/**
* Drops the primary key from the table.
*
* @return void
*/
public function dropPrimaryKey(): void
{
$this->keys['drop'][] = 'DROP PRIMARY KEY';
}
/**
* Adds a foreign key to the table query.
*
* @param string $column
* @param string $table
* @param string $tableColumn
*
* @return void
*/
public function addForeignKey(
string $keyName,
string $column,
string $table,
string $tableColumn,
?CascadeEnum $onDelete,
?CascadeEnum $onUpdate
): void {
$this->keys['add'][] = sprintf(
'CONSTRAINT %s FOREIGN KEY(%s) REFERENCES %s(%s)',
$keyName,
$column,
$table,
$tableColumn
) . ($onDelete !== null ? ' ON DELETE ' . $onDelete : '') .
($onUpdate !== null ? ' ON UPDATE ' . $onUpdate : '');
}
/**
* Drops a foreign key.
*
* @param string $keyName
*
* @return void
*/
public function dropForeignKey(string $keyName): void
{
$this->keys['drop'][] = sprintf(
'DROP FOREIGN KEY %s',
$keyName
);
}
/**
* Returns the key query statements.
*
* @return array
*/
public function getKeys(): array
{
return $this->keys;
}
/**
* Adds an add column operation to the operation list for the query.
*
* @param ColumnDefinitionInterface $column
*
* @return void
*/
public function addColumn(
ColumnDefinitionInterface $column
): void {
$default = $column->getDefault();
$defaultDefinition = '';
if ($default !== null) {
$defaultDefinition = (
is_string($default) ? "'" . $default . "'" :
(is_bool($default) ? (int) $default : $default)
);
}
$typeOption = $column->getTypeOption();
$attribute = $column->getAttribute();
$comment = $column->getComment();
$this->columns['add'][] = sprintf(
'`%s` %s%s%s%s%s%s%s%s',
$column->getName(),
$column->getType(),
$typeOption === null ? '' : sprintf('(%s)', $typeOption),
$attribute === null ? '' : sprintf(' %s ', $attribute),
$column->isNullable() ? ' NULL' : ' NOT NULL',
$defaultDefinition === '' ? '' : ' DEFAULT ' . $defaultDefinition,
$column->isAutoIncrement() ? ' AUTO_INCREMENT' : '',
$column->isUnique() ? ' UNIQUE' : '',
empty($comment) ? '' : sprintf(" COMMENT '%s'", $comment)
);
}
/**
* Adds an alter column operation to the operation list for the query.
*
* @param ColumnDefinitionInterface $column
* @param string $newName
*
* @return void
*/
public function alterColumn(
ColumnDefinitionInterface $column,
string $newName = ''
): void {
$default = $column->getDefault();
$defaultDefinition = '';
if ($default !== null) {
$defaultDefinition = (
is_string($default) ? "'" . $default . "'" :
(is_bool($default) ? (int) $default : $default)
);
}
$typeOption = $column->getTypeOption();
$attribute = $column->getAttribute();
$comment = $column->getComment();
$this->columns['alter'][] = sprintf(
(empty($newName) ? 'MODIFY ' : 'ALTER ') . '`%s` %s%s%s%s%s%s%s%s',
$column->getName() . (empty($newName) ? '' : '` `' . $newName),
$column->getType(),
$typeOption === null ? '' : sprintf('(%s)', $typeOption),
$attribute === null ? '' : sprintf(' %s ', $attribute),
$column->isNullable() ? ' NULL' : ' NOT NULL',
$defaultDefinition === '' ? '' : ' DEFAULT ' . $defaultDefinition,
$column->isAutoIncrement() ? ' AUTO_INCREMENT' : '',
$column->isUnique() ? ' UNIQUE' : '',
empty($comment) ? '' : sprintf(" COMMENT '%s'", $comment)
);
}
/**
* Adds a drop column operation to the query.
*
* @param string $column
*
* @return void
*/
public function dropColumn(string $column): void
{
$this->columns['drop'][] = sprintf('DROP COLUMN `%s`', $column);
}
/**
* Retrieves a list of column operations.
*
* @return array
*/
public function getColumns(): array
{
return $this->columns;
}
}