Пример #1
0
 public function createEnumColumn($defaultValues, $defaultValue)
 {
     $column = new Column();
     $column->setType(PropelTypes::ENUM);
     $column->setValueSet($defaultValues);
     $column->setDefaultValue($defaultValue);
     return $column;
 }
 public static function compareColumns(Column $fromColumn, Column $toColumn)
 {
     $changedProperties = [];
     // compare column types
     $fromDomain = $fromColumn->getDomain();
     $toDomain = $toColumn->getDomain();
     if ($fromDomain->getScale() !== $toDomain->getScale()) {
         $changedProperties['scale'] = [$fromDomain->getScale(), $toDomain->getScale()];
     }
     if ($fromDomain->getSize() !== $toDomain->getSize()) {
         $changedProperties['size'] = [$fromDomain->getSize(), $toDomain->getSize()];
     }
     if (strtoupper($fromDomain->getSqlType()) !== strtoupper($toDomain->getSqlType())) {
         if ($fromDomain->getOriginSqlType()) {
             if (strtoupper($fromDomain->getOriginSqlType()) !== strtoupper($toDomain->getSqlType())) {
                 if ($fromDomain->getType() !== $toDomain->getType()) {
                     $changedProperties['type'] = [$fromDomain->getType(), $toDomain->getType()];
                 }
                 $changedProperties['sqlType'] = [$fromDomain->getSqlType(), $toDomain->getSqlType()];
             }
         } else {
             $changedProperties['sqlType'] = [$fromDomain->getSqlType(), $toDomain->getSqlType()];
             if ($fromDomain->getType() !== $toDomain->getType()) {
                 $changedProperties['type'] = [$fromDomain->getType(), $toDomain->getType()];
             }
         }
     }
     if ($fromColumn->isNotNull() !== $toColumn->isNotNull()) {
         $changedProperties['notNull'] = [$fromColumn->isNotNull(), $toColumn->isNotNull()];
     }
     // compare column default value
     $fromDefaultValue = $fromColumn->getDefaultValue();
     $toDefaultValue = $toColumn->getDefaultValue();
     if ($fromDefaultValue && !$toDefaultValue) {
         $changedProperties['defaultValueType'] = [$fromDefaultValue->getType(), null];
         $changedProperties['defaultValueValue'] = [$fromDefaultValue->getValue(), null];
     } elseif (!$fromDefaultValue && $toDefaultValue) {
         $changedProperties['defaultValueType'] = [null, $toDefaultValue->getType()];
         $changedProperties['defaultValueValue'] = [null, $toDefaultValue->getValue()];
     } elseif ($fromDefaultValue && $toDefaultValue) {
         if (!$fromDefaultValue->equals($toDefaultValue)) {
             if ($fromDefaultValue->getType() !== $toDefaultValue->getType()) {
                 $changedProperties['defaultValueType'] = [$fromDefaultValue->getType(), $toDefaultValue->getType()];
             }
             if ($fromDefaultValue->getValue() !== $toDefaultValue->getValue()) {
                 $changedProperties['defaultValueValue'] = [$fromDefaultValue->getValue(), $toDefaultValue->getValue()];
             }
         }
     }
     if ($fromColumn->isAutoIncrement() !== $toColumn->isAutoIncrement()) {
         $changedProperties['autoIncrement'] = [$fromColumn->isAutoIncrement(), $toColumn->isAutoIncrement()];
     }
     return $changedProperties;
 }
Пример #3
0
    /**
     * Change default propel behaviour
     *
     * Adds setter method for boolean columns.
     *
     * @see \Propel\Generator\Builder\Om\ObjectBuilder::addColumnMutators()
     *
     * @param string &$script The script will be modified in this method.
     * @param \Propel\Generator\Model\Column $col The current column.
     *
     * @return void
     */
    protected function addBooleanMutator(&$script, Column $col)
    {
        $clo = $col->getLowercasedName();
        $this->addBooleanMutatorComment($script, $col);
        $this->addMutatorOpenOpen($script, $col);
        $this->addMutatorOpenBody($script, $col);
        $allowNullValues = $col->getAttribute('required', 'true') === 'true' ? 'false' : 'true';
        $script .= "\n        if (\$v !== null) {\n            if (is_string(\$v)) {\n                \$v = in_array(strtolower(\$v), array('false', 'off', '-', 'no', 'n', '0', '')) ? false : true;\n            } else {\n                \$v = (bool) \$v;\n            }\n        }\n\n        \$allowNullValues = {$allowNullValues};\n\n        if (\$v === null && !\$allowNullValues) {\n            return \$this;\n        }\n\n        if (\$this->{$clo} !== \$v) {\n            \$this->{$clo} = \$v;\n            \$this->modifiedColumns[" . $this->getColumnConstant($col) . '] = true;
        }
';
        $this->addMutatorClose($script, $col);
    }
 protected function addTranslatedColumnSetter(Column $column)
 {
     $visibility = $column->getTable()->isReadOnly() ? 'protected' : $column->getMutatorVisibility();
     $typeHint = '';
     $null = '';
     if ($column->getTypeHint()) {
         $typeHint = $column->getTypeHint();
         if ('array' !== $typeHint) {
             $typeHint = $this->declareClass($typeHint);
         }
         $typeHint .= ' ';
         if (!$column->isNotNull()) {
             $null = ' = null';
         }
     }
     $typeHint = "{$typeHint}\$v{$null}";
     $i18nTablePhpName = $this->builder->getClassNameFromBuilder($this->builder->getNewStubObjectBuilder($this->behavior->getI18nTable()));
     $tablePhpName = $this->builder->getObjectClassName();
     $objectBuilder = $this->builder->getNewObjectBuilder($this->behavior->getI18nTable());
     $comment = '';
     if ($this->isDateType($column->getType())) {
         $objectBuilder->addTemporalMutatorComment($comment, $column);
     } else {
         $objectBuilder->addMutatorComment($comment, $column);
     }
     $comment = preg_replace('/^\\t/m', '', $comment);
     $comment = str_replace('@return     $this|' . $i18nTablePhpName, '@return     $this|' . $tablePhpName, $comment);
     return $this->renderTemplate('objectTranslatedColumnSetter', ['comment' => $comment, 'column' => $column, 'visibility' => $visibility, 'typeHint' => $typeHint, 'columnPhpName' => $column->getPhpName(), 'localeColumnName' => $this->behavior->getLocaleColumn()->getPhpName()]);
 }
Пример #5
0
 public static function getDefaultValueStringProvider()
 {
     $col1 = new Column('Bar');
     $col1->setDomain(new Domain('VARCHAR'));
     $col1->setDefaultValue(new ColumnDefaultValue('abc', ColumnDefaultValue::TYPE_VALUE));
     $val1 = "'abc'";
     $col2 = new Column('Bar');
     $col2->setDomain(new Domain('INTEGER'));
     $col2->setDefaultValue(new ColumnDefaultValue(1234, ColumnDefaultValue::TYPE_VALUE));
     $val2 = "1234";
     $col3 = new Column('Bar');
     $col3->setDomain(new Domain('DATE'));
     $col3->setDefaultValue(new ColumnDefaultValue('0000-00-00', ColumnDefaultValue::TYPE_VALUE));
     $val3 = "NULL";
     return array(array($col1, $val1), array($col2, $val2), array($col3, $val3));
 }
 /**
  * Gets the refPhpName parameter from config array.
  *
  * @return string
  */
 protected function getRefPhpName()
 {
     $name = $this->getParameter('refPhpName');
     if ($name == null) {
         $name = Column::generatePhpName($this->getForeignTable());
     }
     return $name;
 }
Пример #7
0
 /**
  * Returns the string representation of the difference.
  *
  * @return string
  */
 public function __toString()
 {
     $ret = '';
     $ret .= sprintf("      %s:\n", $this->fromColumn->getFullyQualifiedName());
     $ret .= "        modifiedProperties:\n";
     foreach ($this->changedProperties as $key => $value) {
         $ret .= sprintf("          %s: %s\n", $key, json_encode($value));
     }
     return $ret;
 }
Пример #8
0
 /**
  * Returns the subclasses that can be created from this table.
  *
  * @return array
  */
 public function getChildrenNames()
 {
     if (null === $this->inheritanceColumn || !$this->inheritanceColumn->isEnumeratedClasses()) {
         return null;
     }
     $names = [];
     foreach ($this->inheritanceColumn->getChildren() as $child) {
         $names[] = get_class($child);
     }
     return $names;
 }
 protected function addColumn(Table $table, $name)
 {
     if (!$table->hasColumn($name)) {
         $column = new Column($name);
         // don't know how to define unsigned :(
         $domain = new Domain('TINYINT', 'tinyint(3) unsigned');
         $column->setDomain($domain);
         $table->addColumn($column);
         $column_idx_name = $name . '_idx';
         if (!$table->hasIndex($column_idx_name)) {
             $column_idx = new Index($column_idx_name);
             $column_idx->addColumn(['name' => $column->getName()]);
             $table->addIndex($column_idx);
         }
     }
 }
Пример #10
0
 /**
  * Get the PHP snippet for binding a value to a column.
  * Warning: duplicates logic from OracleAdapter::bindValue().
  * Any code modification here must be ported there.
  */
 public function getColumnBindingPHP(Column $column, $identifier, $columnValueAccessor, $tab = "            ")
 {
     if ($column->getPDOType() == PropelTypes::CLOB_EMU) {
         return sprintf("%s\$stmt->bindParam(%s, %s, %s, strlen(%s));\n", $tab, $identifier, $columnValueAccessor, PropelTypes::getPdoTypeString($column->getType()), $columnValueAccessor);
     }
     return parent::getColumnBindingPHP($column, $identifier, $columnValueAccessor, $tab);
 }
 public function testCompareSeveralColumnDifferences()
 {
     $t1 = new Table();
     $c1 = new Column('col1');
     $c1->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
     $c1->getDomain()->replaceSize(255);
     $c1->setNotNull(false);
     $t1->addColumn($c1);
     $c2 = new Column('col2');
     $c2->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
     $c2->setNotNull(true);
     $t1->addColumn($c2);
     $c3 = new Column('col3');
     $c3->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
     $c3->getDomain()->replaceSize(255);
     $t1->addColumn($c3);
     $t2 = new Table();
     $c4 = new Column('col1');
     $c4->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
     $c4->getDomain()->replaceScale(2);
     $c4->getDomain()->replaceSize(3);
     $c4->setNotNull(true);
     $c4->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
     $t2->addColumn($c4);
     $c5 = new Column('col22');
     $c5->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
     $c5->setNotNull(true);
     $t2->addColumn($c5);
     $c6 = new Column('col4');
     $c6->getDomain()->copy($this->platform->getDomainForType('LONGVARCHAR'));
     $c6->getDomain()->setDefaultValue(new ColumnDefaultValue('123', ColumnDefaultValue::TYPE_VALUE));
     $t2->addColumn($c6);
     // col1 was modified, col2 was renamed, col3 was removed, col4 was added
     $tc = new PropelTableComparator();
     $tc->setFromTable($t1);
     $tc->setToTable($t2);
     $nbDiffs = $tc->compareColumns();
     $tableDiff = $tc->getTableDiff();
     $this->assertEquals(4, $nbDiffs);
     $this->assertEquals(array(array($c2, $c5)), $tableDiff->getRenamedColumns());
     $this->assertEquals(array('col4' => $c6), $tableDiff->getAddedColumns());
     $this->assertEquals(array('col3' => $c3), $tableDiff->getRemovedColumns());
     $columnDiff = PropelColumnComparator::computeDiff($c1, $c4);
     $this->assertEquals(array('col1' => $columnDiff), $tableDiff->getModifiedColumns());
 }
Пример #12
0
 /**
  * Adds the singular filterByCol method for an Array column.
  * @param string &$script The script will be modified in this method.
  * @param Column $col
  */
 protected function addFilterByArrayCol(&$script, Column $col)
 {
     $colPhpName = $col->getPhpName();
     $singularPhpName = $col->getPhpSingularName();
     $colName = $col->getName();
     $variableName = $col->getCamelCaseName();
     $qualifiedName = $this->getColumnConstant($col);
     $script .= "\n    /**\n     * Filter the query on the {$colName} column\n     * @param     mixed \${$variableName} The value to use as filter\n     * @param     string \$comparison Operator to use for the column comparison, defaults to Criteria::CONTAINS_ALL\n     *\n     * @return \$this|" . $this->getQueryClassName() . " The current query, for fluid interface\n     */\n    public function filterBy{$singularPhpName}(\${$variableName} = null, \$comparison = null)\n    {\n        if (null === \$comparison || \$comparison == Criteria::CONTAINS_ALL) {\n            if (is_scalar(\${$variableName})) {\n                \${$variableName} = '%| ' . \${$variableName} . ' |%';\n                \$comparison = Criteria::LIKE;\n            }\n        } elseif (\$comparison == Criteria::CONTAINS_NONE) {\n            \${$variableName} = '%| ' . \${$variableName} . ' |%';\n            \$comparison = Criteria::NOT_LIKE;\n            \$key = \$this->getAliasedColName({$qualifiedName});\n            if (\$this->containsKey(\$key)) {\n                \$this->addAnd(\$key, \${$variableName}, \$comparison);\n            } else {\n                \$this->addAnd(\$key, \${$variableName}, \$comparison);\n            }\n            \$this->addOr(\$key, null, Criteria::ISNULL);\n\n            return \$this;\n        }\n\n        return \$this->addUsingAlias({$qualifiedName}, \${$variableName}, \$comparison);\n    }\n";
 }
 public function getColumnDDL(Column $col)
 {
     if ($col->isAutoIncrement()) {
         $col->setType('INTEGER');
         $col->setDomainForType('INTEGER');
     }
     if ($col->getDefaultValue() && $col->getDefaultValue()->isExpression() && 'CURRENT_TIMESTAMP' === $col->getDefaultValue()->getValue()) {
         //sqlite use CURRENT_TIMESTAMP different than mysql/pgsql etc
         //we set it to the more common behavior
         $col->setDefaultValue(new ColumnDefaultValue("(datetime(CURRENT_TIMESTAMP, 'localtime'))", ColumnDefaultValue::TYPE_EXPR));
     }
     return parent::getColumnDDL($col);
 }
Пример #14
0
 /**
  * Adds Columns to the specified table.
  *
  * @param Table $table The Table model class to add columns to.
  */
 protected function addColumns(Table $table)
 {
     $tableName = $table->getName();
     //        var_dump("PRAGMA table_info('$tableName') //");
     $stmt = $this->dbh->query("PRAGMA table_info('{$tableName}')");
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         $name = $row['name'];
         $fulltype = $row['type'];
         $size = null;
         $scale = null;
         if (preg_match('/^([^\\(]+)\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)$/', $fulltype, $matches)) {
             $type = $matches[1];
             $size = $matches[2];
             $scale = $matches[3];
         } elseif (preg_match('/^([^\\(]+)\\(\\s*(\\d+)\\s*\\)$/', $fulltype, $matches)) {
             $type = $matches[1];
             $size = $matches[2];
         } else {
             $type = $fulltype;
         }
         $notNull = $row['notnull'];
         $default = $row['dflt_value'];
         $propelType = $this->getMappedPropelType(strtolower($type));
         if (!$propelType) {
             $propelType = Column::DEFAULT_TYPE;
             $this->warn('Column [' . $table->getName() . '.' . $name . '] has a column type (' . $type . ') that Propel does not support.');
         }
         $column = new Column($name);
         $column->setTable($table);
         $column->setDomainForType($propelType);
         // We may want to provide an option to include this:
         // $column->getDomain()->replaceSqlType($type);
         $column->getDomain()->replaceSize($size);
         $column->getDomain()->replaceScale($scale);
         if (null !== $default) {
             if ("'" !== substr($default, 0, 1) && strpos($default, '(')) {
                 $defaultType = ColumnDefaultValue::TYPE_EXPR;
                 if ('datetime(CURRENT_TIMESTAMP, \'localtime\')' === $default) {
                     $default = 'CURRENT_TIMESTAMP';
                 }
             } else {
                 $defaultType = ColumnDefaultValue::TYPE_VALUE;
                 $default = str_replace("'", '', $default);
             }
             $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, $defaultType));
         }
         $column->setNotNull($notNull);
         if (0 < $row['pk'] + 0) {
             $column->setPrimaryKey(true);
         }
         if ($column->isPrimaryKey()) {
             // check if autoIncrement
             $autoIncrementStmt = $this->dbh->prepare('
             SELECT tbl_name
             FROM sqlite_master
             WHERE
               tbl_name = ?
             AND
               sql LIKE "%AUTOINCREMENT%"
             ');
             $autoIncrementStmt->execute([$table->getName()]);
             $autoincrementRow = $autoIncrementStmt->fetch(\PDO::FETCH_ASSOC);
             if ($autoincrementRow && $autoincrementRow['tbl_name'] == $table->getName()) {
                 $column->setAutoIncrement(true);
             }
         }
         $table->addColumn($column);
     }
 }
Пример #15
0
 /**
  * Adds Columns to the specified table.
  *
  * @param Table $table The Table model class to add columns to.
  */
 protected function addColumns(Table $table)
 {
     $stmt = $this->dbh->query("PRAGMA table_info('" . $table->getName() . "')");
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         $name = $row['name'];
         $fulltype = $row['type'];
         $size = null;
         $precision = null;
         $scale = null;
         if (preg_match('/^([^\\(]+)\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)$/', $fulltype, $matches)) {
             $type = $matches[1];
             $precision = $matches[2];
             $scale = $matches[3];
             // aka precision
         } elseif (preg_match('/^([^\\(]+)\\(\\s*(\\d+)\\s*\\)$/', $fulltype, $matches)) {
             $type = $matches[1];
             $size = $matches[2];
         } else {
             $type = $fulltype;
         }
         // If column is primary key and of type INTEGER, it is auto increment
         // See: http://sqlite.org/faq.html#q1
         $autoincrement = 1 == $row['pk'] && 'integer' === strtolower($type);
         $notNull = $row['notnull'];
         $default = $row['dflt_value'];
         $propelType = $this->getMappedPropelType(strtolower($type));
         if (!$propelType) {
             $propelType = Column::DEFAULT_TYPE;
             $this->warn('Column [' . $table->getName() . '.' . $name . '] has a column type (' . $type . ') that Propel does not support.');
         }
         $column = new Column($name);
         $column->setTable($table);
         $column->setDomainForType($propelType);
         // We may want to provide an option to include this:
         // $column->getDomain()->replaceSqlType($type);
         $column->getDomain()->replaceSize($size);
         $column->getDomain()->replaceScale($scale);
         if (null !== $default) {
             $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, ColumnDefaultValue::TYPE_VALUE));
         }
         $column->setAutoIncrement($autoincrement);
         $column->setNotNull($notNull);
         if (1 == $row['pk'] || 'integer' === strtolower($type)) {
             $column->setPrimaryKey(true);
         }
         $table->addColumn($column);
     }
 }
Пример #16
0
 protected function getDateTimeClass(Column $column)
 {
     if (PropelTypes::isPhpObjectType($column->getPhpType())) {
         return $column->getPhpType();
     }
     $dateTimeClass = $this->getBuildProperty('generator.dateTime.dateTimeClass');
     if (!$dateTimeClass) {
         $dateTimeClass = '\\DateTime';
     }
     return $dateTimeClass;
 }
Пример #17
0
 /**
  * Get the subclasses that can be created from this table.
  * @return    array string[] Class names
  */
 public function getChildrenNames()
 {
     if ($this->inheritanceColumn === null || !$this->inheritanceColumn->isEnumeratedClasses()) {
         return null;
     }
     $children = $this->inheritanceColumn->getChildren();
     $names = array();
     for ($i = 0, $size = count($children); $i < $size; $i++) {
         $names[] = get_class($children[$i]);
     }
     return $names;
 }
Пример #18
0
    public function testGetModifyColumnDDLWithVarcharWithoutSizeAndPlatform()
    {
        $t1 = new Table('foo');
        $c1 = new Column('bar');
        $c1->setTable($t1);
        $c1->getDomain()->copy($this->getPlatform()->getDomainForType('VARCHAR'));
        $c1->getDomain()->replaceSize(null);
        $c1->getDomain()->replaceScale(null);
        $t1->addColumn($c1);
        $schema = <<<EOF
<database name="test">
    <table name="foo">
        <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
        <column name="bar"/>
    </table>
</database>
EOF;
        $xtad = new XmlToAppData(null);
        $appData = $xtad->parseString($schema);
        $db = $appData->getDatabase();
        $table = $db->getTable('foo');
        $c2 = $table->getColumn('bar');
        $columnDiff = ColumnComparator::computeDiff($c1, $c2);
        $expected = false;
        $this->assertSame($expected, $columnDiff);
    }
 /**
  * Get the PHP snippet for binding a value to a column.
  * Warning: duplicates logic from AdapterInterface::bindValue().
  * Any code modification here must be ported there.
  */
 public function getColumnBindingPHP(Column $column, $identifier, $columnValueAccessor, $tab = "            ")
 {
     $script = '';
     if ($column->isTemporalType()) {
         $columnValueAccessor = $columnValueAccessor . " ? " . $columnValueAccessor . "->format(\"" . $this->getTimeStampFormatter() . "\") : null";
     } elseif ($column->isLobType()) {
         // we always need to make sure that the stream is rewound, otherwise nothing will
         // get written to database.
         $script .= "\nif (is_resource({$columnValueAccessor})) {\n    rewind({$columnValueAccessor});\n}";
     }
     $script .= sprintf("\n\$stmt->bindValue(%s, %s, %s);", $identifier, $columnValueAccessor, PropelTypes::getPdoTypeString($column->getType()));
     return preg_replace('/^(.+)/m', $tab . '$1', $script);
 }
Пример #20
0
 public function testGetPrimaryKeyDDLCompositeKey()
 {
     $table = new Table('foo');
     $column1 = new Column('bar1');
     $column1->setPrimaryKey(true);
     $table->addColumn($column1);
     $column2 = new Column('bar2');
     $column2->setPrimaryKey(true);
     $table->addColumn($column2);
     $expected = 'PRIMARY KEY ([bar1],[bar2])';
     $this->assertEquals($expected, $this->getPlatform()->getPrimaryKeyDDL($table));
 }
 public function testCompareSeveralRenamedSameColumns()
 {
     $t1 = new Table();
     $c1 = new Column('col1');
     $c1->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
     $c1->getDomain()->replaceSize(255);
     $t1->addColumn($c1);
     $c2 = new Column('col2');
     $c2->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
     $c2->getDomain()->replaceSize(255);
     $t1->addColumn($c2);
     $c3 = new Column('col3');
     $c3->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
     $c3->getDomain()->replaceSize(255);
     $t1->addColumn($c3);
     $t2 = new Table();
     $c4 = new Column('col4');
     $c4->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
     $c4->getDomain()->replaceSize(255);
     $t2->addColumn($c4);
     $c5 = new Column('col5');
     $c5->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
     $c5->getDomain()->replaceSize(255);
     $t2->addColumn($c5);
     $c6 = new Column('col3');
     $c6->getDomain()->copy($this->platform->getDomainForType('VARCHAR'));
     $c6->getDomain()->replaceSize(255);
     $t2->addColumn($c6);
     // col1 and col2 were renamed
     $tc = new TableComparator();
     $tc->setFromTable($t1);
     $tc->setToTable($t2);
     $nbDiffs = $tc->compareColumns();
     $tableDiff = $tc->getTableDiff();
     $this->assertEquals(2, $nbDiffs);
     $this->assertEquals([[$c1, $c4], [$c2, $c5]], $tableDiff->getRenamedColumns());
     $this->assertEquals([], $tableDiff->getAddedColumns());
     $this->assertEquals([], $tableDiff->getRemovedColumns());
     $this->assertEquals([], $tableDiff->getModifiedColumns());
 }
 public function providerForTestGetModifyColumnRemoveDefaultValueDDL()
 {
     $t1 = new Table('test');
     $c1 = new Column();
     $c1->setName('test');
     $c1->getDomain()->setType('INTEGER');
     $c1->setDefaultValue(0);
     $t1->addColumn($c1);
     $t2 = new Table('test');
     $c2 = new Column();
     $c2->setName('test');
     $c2->getDomain()->setType('INTEGER');
     $t2->addColumn($c2);
     return array(array(PropelColumnComparator::computeDiff($c1, $c2)));
 }
Пример #23
0
 public function getColumnDDL(Column $col)
 {
     $domain = $col->getDomain();
     $ddl = array($this->quoteIdentifier($col->getName()));
     $sqlType = $domain->getSqlType();
     $table = $col->getTable();
     if ($col->isAutoIncrement() && $table && $table->getIdMethodParameters() == null) {
         $sqlType = $col->getType() === PropelTypes::BIGINT ? 'bigserial' : 'serial';
     }
     if ($this->hasSize($sqlType) && $col->isDefaultSqlType($this)) {
         $ddl[] = $sqlType . $domain->printSize();
     } else {
         $ddl[] = $sqlType;
     }
     if ($default = $this->getColumnDefaultValueDDL($col)) {
         $ddl[] = $default;
     }
     if ($notNull = $this->getNullString($col->isNotNull())) {
         $ddl[] = $notNull;
     }
     if ($autoIncrement = $col->getAutoIncrementString()) {
         $ddl[] = $autoIncrement;
     }
     return implode(' ', $ddl);
 }
Пример #24
0
 public function testGetIndexDDLFulltext()
 {
     $table = new Table('foo');
     $table->setIdentifierQuoting(true);
     $column1 = new Column('bar1');
     $column1->getDomain()->copy($this->getPlatform()->getDomainForType('LONGVARCHAR'));
     $table->addColumn($column1);
     $index = new Index('bar_index');
     $index->addColumn($column1);
     $vendor = new VendorInfo('mysql');
     $vendor->setParameter('Index_type', 'FULLTEXT');
     $index->addVendorInfo($vendor);
     $table->addIndex($index);
     $expected = 'FULLTEXT INDEX `bar_index` (`bar1`)';
     $this->assertEquals($expected, $this->getPlatform()->getIndexDDL($index));
 }
Пример #25
0
 /**
  * Get the column constant name (e.g. TableMapName::COLUMN_NAME).
  *
  * @param Column $col       The column we need a name for.
  * @param string $classname The TableMap classname to use.
  *
  * @return string If $classname is provided, then will return $classname::COLUMN_NAME; if not, then the TableMapName is looked up for current table to yield $currTableTableMap::COLUMN_NAME.
  */
 public function getColumnConstant($col, $classname = null)
 {
     if (null === $col) {
         throw new InvalidArgumentException('No columns were specified.');
     }
     if (null === $classname) {
         return $this->getBuildProperty('classPrefix') . $col->getConstantName();
     }
     // was it overridden in schema.xml ?
     if ($col->getTableMapName()) {
         $const = strtoupper($col->getTableMapName());
     } else {
         $const = strtoupper($col->getName());
     }
     return $classname . '::' . $const;
 }
Пример #26
0
 /**
  * Add an added Pk column
  *
  * @param string $columnName
  * @param Column $addedPkColumn
  */
 public function addAddedPkColumn($columnName, Column $addedPkColumn)
 {
     if (!$addedPkColumn->isPrimaryKey()) {
         throw new DiffException(sprintf('Column %s is not a valid primary key column.', $columnName));
     }
     $this->addedPkColumns[$columnName] = $addedPkColumn;
 }
 /**
  * Builds the DDL SQL to remove a column
  *
  * @return string
  */
 public function getRemoveColumnDDL(Column $column)
 {
     $pattern = "\nALTER TABLE %s DROP %s;\n";
     return sprintf($pattern, $this->quoteIdentifier($column->getTable()->getName()), $this->quoteIdentifier($column->getName()));
 }
Пример #28
0
 /**
  * Adds Columns to the specified table.
  *
  * @param Table $table The Table model class to add columns to.
  */
 protected function addColumns(Table $table)
 {
     $stmt = $this->dbh->query("SELECT COLUMN_NAME, DATA_TYPE, NULLABLE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, DATA_DEFAULT FROM USER_TAB_COLS WHERE TABLE_NAME = '" . $table->getName() . "'");
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         if (false !== strpos($row['COLUMN_NAME'], '$')) {
             // this is an Oracle internal column - prune
             continue;
         }
         $size = $row['DATA_PRECISION'] ? $row['DATA_PRECISION'] : $row['DATA_LENGTH'];
         $scale = $row['DATA_SCALE'];
         $default = $row['DATA_DEFAULT'];
         $type = $row['DATA_TYPE'];
         $isNullable = 'Y' === $row['NULLABLE'];
         if ($type === 'NUMBER' && $row['DATA_SCALE'] > 0) {
             $type = 'DECIMAL';
         }
         if ($type === 'NUMBER' && $size > 9) {
             $type = 'BIGINT';
         }
         if ($type === 'FLOAT' && $row['DATA_PRECISION'] == 126) {
             $type = 'DOUBLE';
         }
         if (false !== strpos($type, 'TIMESTAMP(')) {
             $type = substr($type, 0, strpos($type, '('));
             $default = '0000-00-00 00:00:00';
             $size = null;
             $scale = null;
         }
         if ('DATE' === $type) {
             $default = '0000-00-00';
             $size = null;
             $scale = null;
         }
         $propelType = $this->getMappedPropelType($type);
         if (!$propelType) {
             $propelType = Column::DEFAULT_TYPE;
             $this->warn('Column [' . $table->getName() . '.' . $row['COLUMN_NAME'] . '] has a column type (' . $row['DATA_TYPE'] . ') that Propel does not support.');
         }
         $column = new Column($row['COLUMN_NAME']);
         $column->setPhpName();
         // Prevent problems with strange col names
         $column->setTable($table);
         $column->setDomainForType($propelType);
         $column->getDomain()->setOriginSqlType($type);
         $column->getDomain()->replaceSize($size);
         $column->getDomain()->replaceScale($scale);
         if ($default !== null) {
             $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, ColumnDefaultValue::TYPE_VALUE));
         }
         $column->setAutoIncrement(false);
         // This flag sets in self::parse()
         $column->setNotNull(!$isNullable);
         $table->addColumn($column);
     }
 }
 public function providerForTestGetModifyColumnRemoveDefaultValueDDL()
 {
     $t1 = new Table('test');
     $t1->setIdentifierQuoting(true);
     $c1 = new Column();
     $c1->setName('test');
     $c1->getDomain()->setType('INTEGER');
     $c1->setDefaultValue(0);
     $t1->addColumn($c1);
     $t2 = new Table('test');
     $t2->setIdentifierQuoting(true);
     $c2 = new Column();
     $c2->setName('test');
     $c2->getDomain()->setType('INTEGER');
     $t2->addColumn($c2);
     return [[ColumnComparator::computeDiff($c1, $c2)]];
 }
 /**
  * Adds Columns to the specified table.
  *
  * @param Table $table The Table model class to add columns to.
  */
 protected function addColumns(Table $table)
 {
     $dataFetcher = $this->dbh->query("sp_columns '" . $table->getName() . "'");
     foreach ($dataFetcher as $row) {
         $name = $this->cleanDelimitedIdentifiers($row['COLUMN_NAME']);
         $type = $row['TYPE_NAME'];
         $size = $row['LENGTH'];
         $isNullable = $row['NULLABLE'];
         $default = $row['COLUMN_DEF'];
         $scale = $row['SCALE'];
         $autoincrement = false;
         if (strtolower($type) == 'int identity') {
             $autoincrement = true;
         }
         $propelType = $this->getMappedPropelType($type);
         if (!$propelType) {
             $propelType = Column::DEFAULT_TYPE;
             $this->warn(sprintf('Column [%s.%s] has a column type (%s) that Propel does not support.', $table->getName(), $name, $type));
         }
         $column = new Column($name);
         $column->setTable($table);
         $column->setDomainForType($propelType);
         $column->getDomain()->setOriginSqlType($type);
         // We may want to provide an option to include this:
         // $column->getDomain()->replaceSqlType($type);
         $column->getDomain()->replaceSize($size);
         $column->getDomain()->replaceScale($scale);
         if ($default !== null) {
             $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, ColumnDefaultValue::TYPE_VALUE));
         }
         $column->setAutoIncrement($autoincrement);
         $column->setNotNull(!$isNullable);
         $table->addColumn($column);
     }
 }