/** * @param BaseObject $source Source object. * @param BaseObject $target Target object. * * @return void */ protected function handleChange(BaseObject $source, BaseObject $target) { if ($source instanceof Sequence || $source instanceof Procedure) { $this->conditions[""][] = $this->multiLinePhp("\t\t\$DB->Query(\"", $source->getDropDdl($this->dbType), "\", true);\n") . $this->multiLinePhp("\t\t\$DB->Query(\"", $target->getCreateDdl($this->dbType), "\", true);\n"); $dropStmt = $this->createStatement("\$DB->Query(\"", $source->getDropDdl($this->dbType), "\", true);"); $createStmt = $this->createStatement("\$DB->Query(\"", $target->getCreateDdl($this->dbType), "\", true);"); $stmt = new Php\Statement(); $stmt->merge($dropStmt); $stmt->merge($createStmt); $stmt->addCondition("\$updater->CanUpdateDatabase()"); $stmt->addCondition("\$DB->type == \"" . EscapePHPString($this->dbType) . "\""); $stmt->addCondition("\$updater->TableExists(\"" . EscapePHPString($this->tableCheck->getLowercasedName()) . "\")"); } elseif ($target instanceof Column) { $ddl = $source->getModifyDdl($target, $this->dbType); $predicate = "\$updater->TableExists(\"" . EscapePHPString($source->parent->name) . "\")"; $cond = "\t\tif ({$predicate})\n"; $predicate2 = "\$DB->Query(\"SELECT " . EscapePHPString($source->name) . " FROM " . EscapePHPString($source->parent->name) . " WHERE 1=0\", true)"; $this->conditions[$cond][] = "\t\t\tif ({$predicate2})\n" . "\t\t\t{\n" . $this->multiLinePhp("\t\t\t\t\$DB->Query(\"", $ddl, "\");\n") . "\t\t\t}\n"; $stmt = $this->createStatement("\$DB->Query(\"", $ddl, "\");"); $stmt->addCondition("\$updater->CanUpdateDatabase()"); $stmt->addCondition("\$DB->type == \"" . EscapePHPString($this->dbType) . "\""); $stmt->addCondition("\$updater->TableExists(\"" . EscapePHPString($source->parent->getLowercasedName()) . "\")"); $stmt->addCondition("\$DB->Query(\"SELECT " . EscapePHPString($source->name) . " FROM " . EscapePHPString($source->parent->getLowercasedName()) . " WHERE 1=0\", true)"); } elseif ($source instanceof Index) { $this->conditions["\t\tif (\$updater->TableExists(\"" . EscapePHPString($source->parent->name) . "\"))\n"][] = "\t\t\tif (\$DB->IndexExists(\"" . EscapePHPString($source->parent->name) . "\", array(" . $this->multiLinePhp("\"", $source->columns, "\", ") . ")))\n" . "\t\t\t{\n" . $this->multiLinePhp("\t\t\t\t\$DB->Query(\"", $source->getDropDdl($this->dbType), "\");\n") . $this->multiLinePhp("\t\t\t\t\$DB->Query(\"", $target->getCreateDdl($this->dbType), "\");\n") . "\t\t\t}\n"; $dropStmt = $this->createStatement("\$DB->Query(\"", $source->getDropDdl($this->dbType), "\", true);"); $createStmt = $this->createStatement("\$DB->Query(\"", $target->getCreateDdl($this->dbType), "\", true);"); $stmt = new Php\Statement(); $stmt->merge($dropStmt); $stmt->merge($createStmt); $stmt->addCondition("\$updater->CanUpdateDatabase()"); $stmt->addCondition("\$DB->type == \"" . EscapePHPString($this->dbType) . "\""); $stmt->addCondition("\$updater->TableExists(\"" . EscapePHPString($source->parent->getLowercasedName()) . "\")"); $stmt->addCondition("\$DB->IndexExists(\"" . EscapePHPString($source->parent->getLowercasedName()) . "\", array(" . $this->multiLinePhp("\"", $source->columns, "\", ") . "))"); $stmt->addCondition("!\$DB->IndexExists(\"" . EscapePHPString($target->parent->getLowercasedName()) . "\", array(" . $this->multiLinePhp("\"", $target->columns, "\", ") . "))"); } elseif ($source instanceof Trigger || $source instanceof Constraint) { $ddl = $source->getModifyDdl($target, $this->dbType); $predicate = "\$updater->TableExists(\"" . EscapePHPString($source->parent->name) . "\")"; $cond = "\t\tif ({$predicate})\n"; $this->conditions[$cond][] = $this->multiLinePhp("\t\t\t\$DB->Query(\"", $ddl, "\", true);\n"); $stmt = $this->createStatement("\$DB->Query(\"", $ddl, "\", true);"); $stmt->addCondition("\$updater->CanUpdateDatabase()"); $stmt->addCondition("\$DB->type == \"" . EscapePHPString($this->dbType) . "\""); $stmt->addCondition("\$updater->TableExists(\"" . EscapePHPString($source->parent->getLowercasedName()) . "\")"); } else { $this->conditions[""][] = "\t\t//change for " . get_class($source) . " not supported yet\n"; $stmt = $this->createStatement("", "//change for " . get_class($source) . " not supported yet", ""); } if ($stmt) { $this->statements[] = $stmt; } }
/** * @param Tokenizer $tokenizer Statement tokens. * * @return void * @throws NotSupportedException */ protected function executeCreateTable(Tokenizer $tokenizer) { $tokenizer->skipWhiteSpace(); $this->tables->add(Table::create($tokenizer)); }
/** * Creates table object from tokens. * <p> * Current position should point to the name of the sequence or 'if not exists' clause. * * @param Tokenizer $tokenizer Tokens collection. * * @return Table * @throws NotSupportedException */ public static function create(Tokenizer $tokenizer) { $tokenizer->skipWhiteSpace(); if ($tokenizer->testUpperText('IF')) { $tokenizer->skipWhiteSpace(); if ($tokenizer->testUpperText('NOT')) { $tokenizer->skipWhiteSpace(); } if ($tokenizer->testUpperText('EXISTS')) { $tokenizer->skipWhiteSpace(); } } $table = new Table($tokenizer->getCurrentToken()->text); $tokenizer->nextToken(); $tokenizer->skipWhiteSpace(); if ($tokenizer->testText('(')) { $tokenizer->skipWhiteSpace(); $token = $tokenizer->getCurrentToken(); $level = $token->level; do { if ($tokenizer->testUpperText('INDEX') || $tokenizer->testUpperText('KEY')) { $tokenizer->skipWhiteSpace(); $table->createIndex($tokenizer, false); } elseif ($tokenizer->testUpperText('UNIQUE')) { $tokenizer->skipWhiteSpace(); if ($tokenizer->testUpperText('KEY')) { $tokenizer->skipWhiteSpace(); } elseif ($tokenizer->testUpperText('INDEX')) { $tokenizer->skipWhiteSpace(); } $table->createIndex($tokenizer, true); } elseif ($tokenizer->testUpperText('PRIMARY')) { $tokenizer->skipWhiteSpace(); if (!$tokenizer->testUpperText('KEY')) { throw new NotSupportedException("'KEY' expected. line:" . $tokenizer->getCurrentToken()->line); } $tokenizer->skipWhiteSpace(); $table->createIndex($tokenizer, true); } elseif ($tokenizer->testUpperText('CONSTRAINT')) { $tokenizer->skipWhiteSpace(); $constraintName = $tokenizer->getCurrentToken()->text; $tokenizer->nextToken(); $tokenizer->skipWhiteSpace(); if ($tokenizer->testUpperText('PRIMARY') || $tokenizer->testUpperText('UNIQUE')) { $tokenizer->putBack(); $table->createConstraint($tokenizer, $constraintName); } elseif ($tokenizer->testUpperText('FOREIGN')) { $tokenizer->putBack(); $table->createConstraint($tokenizer, $constraintName); } else { throw new NotSupportedException("'PRIMARY KEY' expected. line:" . $tokenizer->getCurrentToken()->line); } } elseif ($tokenizer->testUpperText(')')) { break; } else { $table->createColumn($tokenizer); } $tokenizer->skipWhiteSpace(); $token = $tokenizer->getCurrentToken(); if ($token->level == $level && $token->text == ',') { $token = $tokenizer->nextToken(); } elseif ($token->level < $level && $token->text == ')') { $tokenizer->nextToken(); break; } else { throw new NotSupportedException("',' or ')' expected. line:" . $token->line); } $tokenizer->skipWhiteSpace(); } while (!$tokenizer->endOfInput() && $token->level >= $level); $suffix = ''; while (!$tokenizer->endOfInput()) { $suffix .= $tokenizer->getCurrentToken()->text; $tokenizer->nextToken(); } if ($suffix) { $table->setBody($suffix); } } else { throw new NotSupportedException("'(' expected. line:" . $tokenizer->getCurrentToken()->line); } return $table; }