Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
index.php
1<?php
2namespace Bitrix\Perfmon\Sql;
3
5
6class Index extends BaseObject
7{
8 public $unique = false;
9 public $fulltext = false;
10 public $using = '';
11 public $columns = [];
12
18 public function __construct($name = '', $unique, $fulltext=false)
19 {
20 parent::__construct($name);
21 $this->unique = (bool)$unique;
22 $this->fulltext = (bool)$fulltext;
23 }
24
32 public function addColumn($name)
33 {
34 $this->columns[] = trim($name);
35 $this->setBody(implode(', ', $this->columns));
36 return $this;
37 }
38
52 public static function create(Tokenizer $tokenizer, $unique = false, $fulltext = false, $indexName = '')
53 {
54 if (!$indexName)
55 {
56 if ($tokenizer->getCurrentToken()->text !== '(')
57 {
58 $indexName = $tokenizer->getCurrentToken()->text;
59 $tokenizer->nextToken();
60 $tokenizer->skipWhiteSpace();
61 }
62 }
63
64 if ($tokenizer->testUpperText('ON'))
65 {
66 $tokenizer->skipWhiteSpace();
67 $tokenizer->nextToken();
68 $tokenizer->skipWhiteSpace();
69 }
70
71 if ($tokenizer->testUpperText('USING'))
72 {
73 $tokenizer->skipWhiteSpace();
74 $indexType = $tokenizer->getCurrentToken()->text;
75 if (strtoupper($indexType) !== 'GIN')
76 {
77 throw new NotSupportedException("'GIN' expected. line:" . $tokenizer->getCurrentToken()->line);
78 }
79 $fulltext = true;
80 $tokenizer->nextToken();
81 $tokenizer->skipWhiteSpace();
82 }
83
84 $index = new self($indexName, $unique, $fulltext);
85
86 if ($tokenizer->testText('('))
87 {
88 $tokenizer->skipWhiteSpace();
89 $token = $tokenizer->getCurrentToken();
90 $level = $token->level;
91 $column = '';
92 do
93 {
94 if ($token->text === ',')
95 {
96 $index->addColumn($column);
97 $column = '';
98 }
99 else
100 {
101 $column .= $token->text;
102 }
103 $token = $tokenizer->nextToken();
104 }
105 while (!$tokenizer->endOfInput() && $token->level >= $level);
106
107 if ($column)
108 {
109 $index->addColumn($column);
110 }
111
112 if (!$tokenizer->testText(')'))
113 {
114 throw new NotSupportedException("')' expected. line:" . $tokenizer->getCurrentToken()->line);
115 }
116
117 //USING BTREE
118 $tokenizer->skipWhiteSpace();
119 if ($tokenizer->testText('USING'))
120 {
121 $tokenizer->skipWhiteSpace();
122 $token = $tokenizer->nextToken();
123 $index->using = $token->text;
124 }
125 }
126 else
127 {
128 throw new NotSupportedException("'(' expected. line:" . $tokenizer->getCurrentToken()->line);
129 }
130
131 return $index;
132 }
133
144 public static function searchTableName(Tokenizer $tokenizer)
145 {
146 $lineToken = $tokenizer->getCurrentToken();
147 while (!$tokenizer->endOfInput())
148 {
149 if ($tokenizer->getCurrentToken()->upper === 'ON')
150 {
151 $tokenizer->nextToken();
152 $tokenizer->skipWhiteSpace();
153 return;
154 }
155 $tokenizer->nextToken();
156 }
157 throw new NotSupportedException('Index: table name not found. line: ' . $lineToken->line);
158 }
159
167 public function getCreateDdl($dbType = '')
168 {
169 switch ($dbType)
170 {
171 case 'MYSQL':
172 return 'CREATE ' . ($this->fulltext ? 'FULLTEXT ' : '') . ($this->unique ? 'UNIQUE ' : '') . 'INDEX ' . $this->name . ' ON ' . $this->parent->name . '(' . $this->body . ')';
173 case 'PGSQL':
174 return 'CREATE ' . ($this->unique ? 'UNIQUE ' : '') . 'INDEX ' . $this->name . ' ON ' . $this->parent->name . ($this->fulltext ? ' USING GIN (' . $this->body . ')' : '(' . $this->body . ')');
175 default:
176 return '// ' . get_class($this) . ':getDropDdl for database type [' . $dbType . '] not implemented';
177 }
178 }
179
187 public function getDropDdl($dbType = '')
188 {
189 switch ($dbType)
190 {
191 case 'MYSQL':
192 case 'MSSQL':
193 return 'DROP INDEX ' . $this->name . ' ON ' . $this->parent->name;
194 case 'ORACLE':
195 case 'PGSQL':
196 return 'DROP INDEX ' . $this->name;
197 default:
198 return '// ' . get_class($this) . ':getDropDdl for database type [' . $dbType . '] not implemented';
199 }
200 }
201
210 public function getModifyDdl(BaseObject $target, $dbType = '')
211 {
212 return [
213 $this->getDropDdl($dbType),
214 $target->getCreateDdl($dbType),
215 ];
216 }
217}
getCreateDdl($dbType='')
Definition index.php:167
static create(Tokenizer $tokenizer, $unique=false, $fulltext=false, $indexName='')
Definition index.php:52
static searchTableName(Tokenizer $tokenizer)
Definition index.php:144
getModifyDdl(BaseObject $target, $dbType='')
Definition index.php:210
__construct($name='', $unique, $fulltext=false)
Definition index.php:18
getDropDdl($dbType='')
Definition index.php:187