66 public function generate($sourceSql, $targetSql)
77 $sourceTables = $source->tables->getList();
80 $this->tableCheck = array_shift($sourceTables);
84 $targetTables = $target->tables->getList();
87 $this->tableCheck = array_shift($targetTables);
91 $this->tableCheck =
null;
95 if (!$this->tableCheck)
100 return $this->
handle($diff);
116 foreach ($diff as $pair)
118 if (!isset($pair[0]))
122 elseif (!isset($pair[1]))
132 foreach ($this->columns as
$columns)
134 $ddl =
''; $predicate2 = [];
137 $predicate2[] = $column[0];
140 $ddl .=
', ' . preg_replace(
'/^ALTER TABLE [^ ]+ /',
'', $column[1]);
150 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
151 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
152 $stmt->addCondition(
$columns[0][3]);
153 $stmt->addCondition(
$columns[0][0]);
155 $this->statements[] = $stmt;
159 $codeTree = new \Bitrix\Perfmon\Php\CodeTree($updaterSteps);
160 $result = $codeTree->getCode(0);
179 $stmt->dependOn = $this->tableCheck->getLowercasedName();
180 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
181 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
182 $stmt->addCondition(
'$updater->TableExists("' . EscapePHPString($this->tableCheck->getLowercasedName()) .
'")');
184 elseif ($object instanceof
Table)
187 $predicate =
'!$updater->TableExists("' . EscapePHPString($object->name) .
'")';
188 $cond =
"\tif (${predicate})\n";
192 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
193 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
196 $stmt->addCondition(
'$updater->TableExists("' . EscapePHPString($this->tableCheck->getLowercasedName()) .
'")');
198 $stmt->addCondition(
'!$updater->TableExists("' . EscapePHPString($object->
getLowercasedName()) .
'")');
200 elseif ($object instanceof
Column)
203 $predicate =
'$updater->TableExists("' . EscapePHPString($object->parent->
getLowercasedName()) .
'")';
204 $cond =
"\t\tif (${predicate})\n";
205 $predicate2 =
'!$DB->Query("SELECT ' . EscapePHPString($object->name) .
' FROM ' . EscapePHPString($object->parent->
getLowercasedName()) .
' WHERE 1=0", true)';
207 $this->columns[$cond][] = [$predicate2, $ddl, $object->parent->
getLowercasedName(), $predicate, $predicate2];
209 elseif ($object instanceof
Index)
212 $predicate =
'$updater->TableExists("' . EscapePHPString($object->parent->
getLowercasedName()) .
'")';
217 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
218 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
219 $stmt->addCondition($predicate);
220 $stmt->addCondition($predicate2);
225 $predicate =
'$updater->TableExists("' . EscapePHPString($object->parent->
getLowercasedName()) .
'")';
229 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
230 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
231 $stmt->addCondition($predicate);
235 $stmt = $this->
createStatement(
'',
'//create for ' . get_class($object) .
' not supported yet',
'');
240 $this->statements[] = $stmt;
258 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
259 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
260 $stmt->addCondition(
'$updater->TableExists("' . EscapePHPString($this->tableCheck->getLowercasedName()) .
'")');
262 elseif ($object instanceof
Table)
265 $predicate =
'$updater->TableExists("' . EscapePHPString($object->
getLowercasedName()) .
'")';
268 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
269 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
270 $stmt->addCondition($predicate);
272 elseif ($object instanceof
Column)
275 $predicate =
'$updater->TableExists("' . EscapePHPString($object->parent->name) .
'")';
276 $cond =
"\t\tif (${predicate})\n";
277 $predicate2 =
'$DB->Query("SELECT ' . EscapePHPString($object->name) .
' FROM ' . EscapePHPString($object->parent->
getLowercasedName()) .
' WHERE 1=0", true)';
279 $this->columns[$cond][] = [$predicate2, $ddl, $object->parent->
getLowercasedName(), $predicate, $predicate2];
281 elseif ($object instanceof
Index)
284 $predicate =
'$updater->TableExists("' . EscapePHPString($object->parent->
getLowercasedName()) .
'")';
288 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
289 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
290 $stmt->addCondition($predicate);
291 $stmt->addCondition($predicate2);
296 $predicate =
'$updater->TableExists("' . EscapePHPString($object->parent->
getLowercasedName()) .
'")';
299 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
300 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
301 $stmt->addCondition($predicate);
305 $stmt = $this->
createStatement(
'',
'//drop for ' . get_class($object) .
' not supported yet',
'');
310 $this->statements[] = $stmt;
329 $stmt->dependOn = $this->tableCheck->getLowercasedName();
330 $stmt->
merge($dropStmt);
331 $stmt->merge($createStmt);
332 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
333 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
334 $stmt->addCondition(
'$updater->TableExists("' . EscapePHPString($this->tableCheck->getLowercasedName()) .
'")');
336 elseif ($target instanceof
Column)
339 $predicate =
'$updater->TableExists("' . EscapePHPString($source->parent->
getLowercasedName()) .
'")';
340 $cond =
"\t\tif (${predicate})\n";
341 $predicate2 =
'$DB->Query("SELECT ' . EscapePHPString($source->name) .
' FROM ' . EscapePHPString($source->parent->
getLowercasedName()) .
' WHERE 1=0", true)';
343 $this->columns[$cond][] = [$predicate2, $ddl, $source->parent->
getLowercasedName(), $predicate, $predicate2];
345 elseif ($source instanceof
Index)
347 $predicate =
'$updater->TableExists("' . EscapePHPString($source->parent->
getLowercasedName()) .
'")';
354 $stmt->merge($dropStmt);
355 $stmt->merge($createStmt);
356 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
357 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
358 $stmt->addCondition($predicate);
359 $stmt->addCondition($predicate2);
360 $stmt->addCondition(
'!$DB->IndexExists("' . EscapePHPString($target->parent->
getUnquotedName()) .
'", array(' . $this->multiLinePhp(
'"', $source->
getUnquotedName($source->columns),
'", ') .
'), true)');
365 $predicate =
'$updater->TableExists("' . EscapePHPString($source->parent->
getLowercasedName()) .
'")';
369 $stmt->addCondition(
'$updater->CanUpdateDatabase()');
370 $stmt->addCondition(
'$DB->type == "' . EscapePHPString($this->dbType) .
'"');
371 $stmt->addCondition($predicate);
375 $stmt = $this->
createStatement(
'',
'//change for ' . get_class($source) .
' not supported yet',
'');
380 $this->statements[] = $stmt;