Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
constraint.php
1<?php
2namespace Bitrix\Perfmon\Sql;
3
4class Constraint extends BaseObject
5{
6 public $columns = [];
7
18 public static function create(Tokenizer $tokenizer, $constraintName = '')
19 {
20 if ($constraintName === false)
21 {
22 $constraintName = '';
23 }
24 elseif (!$constraintName)
25 {
26 $constraintName = $tokenizer->getCurrentToken()->text;
27 $tokenizer->nextToken();
28 $tokenizer->skipWhiteSpace();
29 }
30
31 $constraint = new self($constraintName);
32
33 $token = $tokenizer->getCurrentToken();
34 $level = $token->level;
35 $column = '';
36 $constraintDefinition = '';
37 do
38 {
39 if ($token->text === '(' && $token->level == $level)
40 {
41 $column = '';
42 }
43 elseif (
44 $token->text === ','
45 || ($token->text === ')' && $token->level == $level)
46 )
47 {
48 $column = trim($column);
49 if ($column)
50 {
51 $constraint->columns[] = trim($column);
52 $column = '';
53 }
54 }
55 else
56 {
57 $column .= $token->text;
58 }
59
60 if ($token->level == $level && $token->text === ',')
61 {
62 break;
63 }
64 if ($token->level < $level && $token->text === ')')
65 {
66 break;
67 }
68
69 $constraintDefinition .= $token->text;
70
71 $token = $tokenizer->nextToken();
72 }
73 while (!$tokenizer->endOfInput());
74
75 $constraint->setBody($constraintDefinition);
76
77 return $constraint;
78 }
79
87 public function getCreateDdl($dbType = '')
88 {
89 switch ($dbType)
90 {
91 case 'MYSQL':
92 case 'MSSQL':
93 case 'ORACLE':
94 return 'ALTER TABLE ' . $this->parent->name . ' ADD CONSTRAINT ' . $this->name . ' ' . $this->body;
95 default:
96 return '// ' . get_class($this) . ':getCreateDdl for database type [' . $dbType . '] not implemented';
97 }
98 }
99
107 public function getDropDdl($dbType = '')
108 {
109 switch ($dbType)
110 {
111 case 'MSSQL':
112 case 'ORACLE':
113 return 'ALTER TABLE ' . $this->parent->name . ' DROP CONSTRAINT ' . $this->name;
114 case 'MYSQL':
115 default:
116 return '// ' . get_class($this) . ':getDropDdl for database type [' . $dbType . '] not implemented';
117 }
118 }
119
128 public function getModifyDdl(BaseObject $target, $dbType = '')
129 {
130 return [
131 $this->getDropDdl($dbType),
132 $target->getCreateDdl($dbType),
133 ];
134 }
135}
getModifyDdl(BaseObject $target, $dbType='')
static create(Tokenizer $tokenizer, $constraintName='')