Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
table.php
1<?php
2namespace Bitrix\Perfmon\Sql;
3
5
6class Table extends BaseObject
7{
9 public $columns = null;
11 public $indexes = null;
13 public $constraints = null;
15 public $triggers = null;
16
20 public function __construct($name = '')
21 {
22 parent::__construct($name);
23 $this->columns = new Collection;
24 $this->indexes = new Collection;
25 $this->constraints = new Collection;
26 $this->triggers = new Collection;
27 }
28
39 public function createTrigger(Tokenizer $tokenizer)
40 {
41 $trigger = Trigger::create($tokenizer);
42 $trigger->setParent($this);
43 $this->triggers->add($trigger);
44 return $this;
45 }
46
58 public function createConstraint(Tokenizer $tokenizer, $constraintName = '')
59 {
60 $constraint = Constraint::create($tokenizer, $constraintName);
61 $constraint->setParent($this);
62 $this->constraints->add($constraint);
63 return $this;
64 }
65
79 public function createIndex(Tokenizer $tokenizer, $unique = false, $fulltext = false, $indexName = '')
80 {
81 $index = Index::create($tokenizer, $unique, $fulltext, $indexName);
82 $index->setParent($this);
83 $this->indexes->add($index);
84 return $this;
85 }
86
97 public function createColumn(Tokenizer $tokenizer)
98 {
99 $column = Column::create($tokenizer);
100 $column->setParent($this);
101 $this->columns->add($column);
102 return $this;
103 }
104
113 public function modifyColumn(Tokenizer $tokenizer)
114 {
115 $column = Column::create($tokenizer);
116 $column->setParent($this);
117 $columnIndex = $this->columns->searchIndex($column->name);
118 if ($columnIndex === null)
119 {
120 throw new NotSupportedException('Column ' . $this->name . '.' . $column->name . ' not found line:' . $tokenizer->getCurrentToken()->line);
121 }
122 $this->columns->set($columnIndex, $column);
123 return $this;
124 }
125
136 public static function create(Tokenizer $tokenizer)
137 {
138 $tokenizer->skipWhiteSpace();
139
140 if ($tokenizer->testUpperText('IF'))
141 {
142 $tokenizer->skipWhiteSpace();
143
144 if ($tokenizer->testUpperText('NOT'))
145 {
146 $tokenizer->skipWhiteSpace();
147 }
148
149 if ($tokenizer->testUpperText('EXISTS'))
150 {
151 $tokenizer->skipWhiteSpace();
152 }
153 }
154
155 $table = new Table($tokenizer->getCurrentToken()->text);
156
157 $tokenizer->nextToken();
158 $tokenizer->skipWhiteSpace();
159
160 if ($tokenizer->testText('('))
161 {
162 $tokenizer->skipWhiteSpace();
163
164 $token = $tokenizer->getCurrentToken();
165 $level = $token->level;
166 do
167 {
168 if (
169 $tokenizer->testUpperText('INDEX')
170 || $tokenizer->testUpperText('KEY')
171 )
172 {
173 $tokenizer->skipWhiteSpace();
174 $table->createIndex($tokenizer, false);
175 }
176 elseif ($tokenizer->testUpperText('UNIQUE'))
177 {
178 $tokenizer->skipWhiteSpace();
179 if ($tokenizer->testUpperText('KEY'))
180 {
181 $tokenizer->skipWhiteSpace();
182 }
183 elseif ($tokenizer->testUpperText('INDEX'))
184 {
185 $tokenizer->skipWhiteSpace();
186 }
187 $table->createIndex($tokenizer, true);
188 }
189 elseif ($tokenizer->testUpperText('FULLTEXT'))
190 {
191 $tokenizer->skipWhiteSpace();
192 if ($tokenizer->testUpperText('KEY'))
193 {
194 $tokenizer->skipWhiteSpace();
195 }
196 elseif ($tokenizer->testUpperText('INDEX'))
197 {
198 $tokenizer->skipWhiteSpace();
199 }
200 $table->createIndex($tokenizer, false, true);
201 }
202 elseif ($tokenizer->testUpperText('PRIMARY'))
203 {
204 $tokenizer->skipWhiteSpace();
205 if (!$tokenizer->testUpperText('KEY'))
206 {
207 throw new NotSupportedException("'KEY' expected. line:" . $tokenizer->getCurrentToken()->line);
208 }
209
210 $tokenizer->putBack(); //KEY
211 $tokenizer->putBack(); //WS
212 $tokenizer->putBack(); //PRIMARY
213 $table->createConstraint($tokenizer, false);
214 }
215 elseif ($tokenizer->testUpperText('CONSTRAINT'))
216 {
217 $tokenizer->skipWhiteSpace();
218 $constraintName = $tokenizer->getCurrentToken()->text;
219
220 $tokenizer->nextToken();
221 $tokenizer->skipWhiteSpace();
222
223 if ($tokenizer->testUpperText('PRIMARY') || $tokenizer->testUpperText('UNIQUE'))
224 {
225 $tokenizer->putBack();
226 $table->createConstraint($tokenizer, $constraintName);
227 }
228 elseif ($tokenizer->testUpperText('FOREIGN'))
229 {
230 $tokenizer->putBack();
231 $table->createConstraint($tokenizer, $constraintName);
232 }
233 else
234 {
235 throw new NotSupportedException("'PRIMARY KEY' expected. line:" . $tokenizer->getCurrentToken()->line);
236 }
237 }
238 elseif ($tokenizer->testUpperText(')'))
239 {
240 break;
241 }
242 else
243 {
244 $table->createColumn($tokenizer);
245 }
246
247 $tokenizer->skipWhiteSpace();
248
249 $token = $tokenizer->getCurrentToken();
250
251 if ($token->level == $level && $token->text === ',')
252 {
253 $token = $tokenizer->nextToken();
254 }
255 elseif ($token->level < $level && $token->text === ')')
256 {
257 $tokenizer->nextToken();
258 break;
259 }
260 else
261 {
262 throw new NotSupportedException("',' or ')' expected got (" . $token->text . '). line:' . $token->line);
263 }
264
265 $tokenizer->skipWhiteSpace();
266 }
267 while (!$tokenizer->endOfInput() && $token->level >= $level);
268
269 $suffix = '';
270 while (!$tokenizer->endOfInput())
271 {
272 $suffix .= $tokenizer->getCurrentToken()->text;
273 $tokenizer->nextToken();
274 }
275 if ($suffix)
276 {
277 $table->setBody($suffix);
278 }
279 }
280 else
281 {
282 throw new NotSupportedException("'(' expected. line:" . $tokenizer->getCurrentToken()->line);
283 }
284
285 return $table;
286 }
287
295 public function getCreateDdl($dbType = '')
296 {
297 $result = [];
298
299 $items = [];
301 foreach ($this->columns->getList() as $column)
302 {
303 $items[] = $column->name . ' ' . $column->body;
304 }
305
306 if ($dbType === 'MYSQL')
307 {
309 foreach ($this->indexes->getList() as $index)
310 {
311 $items[] = ($index->fulltext ? 'FULLTEXT ' : '') . ($index->unique ? 'UNIQUE ' : '') . 'KEY ' . $index->name . ' (' . $index->body . ')';
312 }
313 }
314
316 foreach ($this->constraints->getList() as $constraint)
317 {
318 if ($constraint->name === '')
319 {
320 $items[] = $constraint->body;
321 }
322 else
323 {
324 $items[] = 'CONSTRAINT ' . $constraint->name . ' ' . $constraint->body;
325 }
326 }
327
328 $result[] = 'CREATE TABLE ' . $this->name . "(\n\t" . implode(",\n\t", $items) . "\n)" . $this->body;
329
330 if ($dbType !== 'MYSQL')
331 {
332 foreach ($this->indexes->getList() as $index)
333 {
334 $result[] = $index->getCreateDdl($dbType);
335 }
336 }
337
338 return $result;
339 }
340
348 public function getDropDdl($dbType = '')
349 {
350 return 'DROP TABLE ' . $this->name;
351 }
352}
static create(Tokenizer $tokenizer)
Definition column.php:179
static create(Tokenizer $tokenizer, $constraintName='')
static create(Tokenizer $tokenizer, $unique=false, $fulltext=false, $indexName='')
Definition index.php:52
createTrigger(Tokenizer $tokenizer)
Definition table.php:39
createConstraint(Tokenizer $tokenizer, $constraintName='')
Definition table.php:58
static create(Tokenizer $tokenizer)
Definition table.php:136
__construct($name='')
Definition table.php:20
getDropDdl($dbType='')
Definition table.php:348
createIndex(Tokenizer $tokenizer, $unique=false, $fulltext=false, $indexName='')
Definition table.php:79
createColumn(Tokenizer $tokenizer)
Definition table.php:97
modifyColumn(Tokenizer $tokenizer)
Definition table.php:113
static create(Tokenizer $tokenizer)
Definition trigger.php:17