Example #1
0
 /**
  * Returns the SQL for the primary key of a Table object.
  *
  * @return string
  */
 public function getPrimaryKeyDDL(Table $table)
 {
     if ($table->hasPrimaryKey() && 1 < count($table->getPrimaryKey())) {
         if ($table->hasAutoIncrementPrimaryKey()) {
             return 'UNIQUE (' . $this->getColumnListDDL($table->getPrimaryKey()) . ')';
         }
         return 'PRIMARY KEY (' . $this->getColumnListDDL($table->getPrimaryKey()) . ')';
     }
 }
 /**
  * Returns whether or not this foreign key is also the primary key of
  * the local table.
  *
  * @return boolean True if all local columns are at the same time a primary key
  */
 public function isLocalPrimaryKey()
 {
     $localPKCols = [];
     foreach ($this->parentTable->getPrimaryKey() as $lPKCol) {
         $localPKCols[] = $lPKCol->getName();
     }
     return count($localPKCols) === count($this->localColumns) && !array_diff($localPKCols, $this->localColumns);
 }
Example #3
0
 public function getAddTableDDL(Table $table)
 {
     $tableDescription = $table->hasDescription() ? $this->getCommentLineDDL($table->getDescription()) : '';
     $lines = array();
     foreach ($table->getColumns() as $column) {
         $lines[] = $this->getColumnDDL($column);
     }
     if ($table->hasPrimaryKey() && count($table->getPrimaryKey()) > 1) {
         $lines[] = $this->getPrimaryKeyDDL($table);
     }
     foreach ($table->getUnices() as $unique) {
         $lines[] = $this->getUniqueDDL($unique);
     }
     $sep = ",\n    ";
     $pattern = "\n%sCREATE TABLE %s\n(\n    %s\n);\n";
     return sprintf($pattern, $tableDescription, $this->quoteIdentifier($table->getName()), implode($sep, $lines));
 }
 protected function addUpdateLoadedNodes(&$script)
 {
     $queryClassName = $this->queryClassName;
     $objectClassName = $this->objectClassName;
     $tableMapClassName = $this->tableMapClassName;
     $script .= "\n/**\n * Reload all already loaded nodes to sync them with updated db\n *\n * @param      {$objectClassName} \$prune        Object to prune from the update\n * @param      ConnectionInterface \$con        Connection to use.\n */\nstatic public function updateLoadedNodes(\$prune = null, ConnectionInterface \$con = null)\n{\n    if (Propel::isInstancePoolingEnabled()) {\n        \$keys = array();\n        /** @var \$obj {$objectClassName} */\n        foreach ({$tableMapClassName}::\$instances as \$obj) {\n            if (!\$prune || !\$prune->equals(\$obj)) {\n                \$keys[] = \$obj->getPrimaryKey();\n            }\n        }\n\n        if (!empty(\$keys)) {\n            // We don't need to alter the object instance pool; we're just modifying these ones\n            // already in the pool.\n            \$criteria = new Criteria({$tableMapClassName}::DATABASE_NAME);";
     if (1 === count($this->table->getPrimaryKey())) {
         $pkey = $this->table->getPrimaryKey();
         $col = array_shift($pkey);
         $script .= "\n            \$criteria->add(" . $this->builder->getColumnConstant($col) . ", \$keys, Criteria::IN);";
     } else {
         $fields = array();
         foreach ($this->table->getPrimaryKey() as $k => $col) {
             $fields[] = $this->builder->getColumnConstant($col);
         }
         $script .= "\n\n            // Loop on each instances in pool\n            foreach (\$keys as \$values) {\n              // Create initial Criterion\n                \$cton = \$criteria->getNewCriterion(" . $fields[0] . ", \$values[0]);";
         unset($fields[0]);
         foreach ($fields as $k => $col) {
             $script .= "\n\n                // Create next criterion\n                \$nextcton = \$criteria->getNewCriterion(" . $col . ", \$values[{$k}]);\n                // And merge it with the first\n                \$cton->addAnd(\$nextcton);";
         }
         $script .= "\n\n                // Add final Criterion to Criteria\n                \$criteria->addOr(\$cton);\n            }";
     }
     $script .= "\n            \$dataFetcher = {$queryClassName}::create(null, \$criteria)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find(\$con);\n            while (\$row = \$dataFetcher->fetch()) {\n                \$key = {$tableMapClassName}::getPrimaryKeyHashFromRow(\$row, 0);\n                /** @var \$object {$objectClassName} */\n                if (null !== (\$object = {$tableMapClassName}::getInstanceFromPool(\$key))) {";
     $n = 0;
     foreach ($this->table->getColumns() as $col) {
         if ($col->isLazyLoad()) {
             continue;
         }
         if ($col->getPhpName() == $this->getColumnPhpName('left_column')) {
             $script .= "\n                    \$object->setLeftValue(\$row[{$n}]);";
         } elseif ($col->getPhpName() == $this->getColumnPhpName('right_column')) {
             $script .= "\n                    \$object->setRightValue(\$row[{$n}]);";
         } elseif ($this->getParameter('use_scope') == 'true' && $col->getPhpName() == $this->getColumnPhpName('scope_column')) {
             $script .= "\n                    \$object->setScopeValue(\$row[{$n}]);";
         } elseif ($col->getPhpName() == $this->getColumnPhpName('level_column')) {
             $script .= "\n                    \$object->setLevel(\$row[{$n}]);\n                    \$object->clearNestedSetChildren();";
         }
         $n++;
     }
     $script .= "\n                }\n            }\n            \$dataFetcher->close();\n        }\n    }\n}\n";
 }
 /**
  * Returns the SQL for the primary key of a Table object
  *
  * @param Table $table
  *
  * @return string
  */
 public function getPrimaryKeyDDL(Table $table)
 {
     if ($table->hasPrimaryKey()) {
         $keys = $table->getPrimaryKey();
         //MySQL throws an 'Incorrect table definition; there can be only one auto column and it must be defined as a key'
         //if the primary key consists of multiple columns and if the first is not the autoIncrement one. So
         //this push the autoIncrement column to the first position if its not already.
         $autoIncrement = $table->getAutoIncrementPrimaryKey();
         if ($autoIncrement && $keys[0] != $autoIncrement) {
             $idx = array_search($autoIncrement, $keys);
             if ($idx !== false) {
                 unset($keys[$idx]);
                 array_unshift($keys, $autoIncrement);
             }
         }
         return 'PRIMARY KEY (' . $this->getColumnListDDL($keys) . ')';
     }
 }
Example #6
0
 public function getAddPrimaryKeyDDL(Table $table)
 {
     if (is_array($table->getPrimaryKey()) && count($table->getPrimaryKey())) {
         return parent::getAddPrimaryKeyDDL($table);
     }
 }
 /**
  * Load indexes for this table
  */
 protected function addIndexes(Table $table)
 {
     $stmt = $this->dbh->query('PRAGMA index_list("' . $table->getName() . '")');
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         $name = $row['name'];
         $internalName = $name;
         if (0 === strpos($name, 'sqlite_autoindex')) {
             $internalName = '';
         }
         $index = $row['unique'] ? new Unique($internalName) : new Index($internalName);
         $stmt2 = $this->dbh->query("PRAGMA index_info('" . $name . "')");
         while ($row2 = $stmt2->fetch(\PDO::FETCH_ASSOC)) {
             $colname = $row2['name'];
             $index->addColumn($table->getColumn($colname));
         }
         if (1 === count($table->getPrimaryKey()) && 1 === count($index->getColumns())) {
             // exclude the primary unique index, since it's autogenerated by sqlite
             if ($table->getPrimaryKey()[0]->getName() === $index->getColumns()[0]) {
                 continue;
             }
         }
         if ($index instanceof Unique) {
             $table->addUnique($index);
         } else {
             $table->addIndex($index);
         }
     }
 }
 public function testGetAutoIncrementPrimaryKey()
 {
     $column1 = $this->getColumnMock('id', array('primary' => true, 'auto_increment' => true));
     $column2 = $this->getColumnMock('title');
     $column3 = $this->getColumnMock('isbn');
     $table = new Table();
     $table->setIdMethod('native');
     $table->addColumn($column1);
     $table->addColumn($column2);
     $table->addColumn($column3);
     $this->assertCount(1, $table->getPrimaryKey());
     $this->assertTrue($table->hasPrimaryKey());
     $this->assertTrue($table->hasAutoIncrementPrimaryKey());
     $this->assertSame($column1, $table->getAutoIncrementPrimaryKey());
 }
 /**
  * Returns the SQL for the primary key of a Table object.
  *
  * @return string
  */
 public function getPrimaryKeyDDL(Table $table)
 {
     if ($table->hasPrimaryKey()) {
         return 'PRIMARY KEY (' . $this->getColumnListDDL($table->getPrimaryKey()) . ')';
     }
 }