Example #1
0
 /**
  * Adds a unique constraint to the table to enforce uniqueness of the slug_column
  *
  * @param Table $table
  */
 protected function addUniqueConstraint(Table $table)
 {
     $unique = new Unique($this->getColumnForParameter('slug_column'));
     $unique->setName($table->getCommonName() . '_slug');
     $unique->addColumn($table->getColumn($this->getParameter('slug_column')));
     if ($this->getParameter('scope_column')) {
         $unique->addColumn($table->getColumn($this->getParameter('scope_column')));
     }
     $table->addUnique($unique);
 }
Example #2
0
 public function endElement($parser, $name)
 {
     if ('index' === $name) {
         $this->currTable->addIndex($this->currIndex);
     } else {
         if ('unique' === $name) {
             $this->currTable->addUnique($this->currUnique);
         }
     }
     if (self::DEBUG) {
         print 'endElement(' . $name . ") called\n";
     }
     $this->popCurrentSchemaTag();
 }
 /**
  * Unfortunately, SQLite does not support composite pks where one is AUTOINCREMENT,
  * so we have to flag both as NOT NULL and create a UNIQUE constraint.
  *
  * @param Table $table
  */
 public function normalizeTable(Table $table)
 {
     if (count($pks = $table->getPrimaryKey()) > 1 && $table->hasAutoIncrementPrimaryKey()) {
         foreach ($pks as $pk) {
             //no pk can be NULL, as usual
             $pk->setNotNull(true);
             //in SQLite the column with the AUTOINCREMENT MUST be a primary key, too.
             if (!$pk->isAutoIncrement()) {
                 //for all other sub keys we remove it, since we create a UNIQUE constraint over all primary keys.
                 $pk->setPrimaryKey(false);
             }
         }
         //search if there is already a UNIQUE constraint over the primary keys
         $pkUniqueExist = false;
         foreach ($table->getUnices() as $unique) {
             $allPk = false;
             foreach ($unique->getColumns() as $columnName) {
                 $allPk &= $table->getColumn($columnName)->isPrimaryKey();
             }
             if ($allPk) {
                 //there's already a unique constraint with the composite pk
                 $pkUniqueExist = true;
                 break;
             }
         }
         //there is none, let's create it
         if (!$pkUniqueExist) {
             $unique = new Unique();
             foreach ($pks as $pk) {
                 $unique->addColumn($pk);
             }
             $table->addUnique($unique);
         }
     }
     parent::normalizeTable($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 testAddArrayUnique()
 {
     $table = new Table();
     $table->addUnique(array('name' => 'author_unq'));
     $this->assertCount(1, $table->getUnices());
 }
Example #6
0
$sluggableBehavior->setName('sluggable');
/* Tables */
$table1 = new Table('blog_post');
$table1->setDescription('The list of posts');
$table1->setNamespace('Blog');
$table1->setPackage('Acme.Blog');
$table1->addColumns([$column11, $column12, $column13, $column14, $column15, $column16, $column17]);
$table1->addForeignKeys([$fkAuthorPost, $fkCategoryPost]);
$table1->addBehavior($timestampableBehavior);
$table1->addBehavior($sluggableBehavior);
$table2 = new Table('blog_author');
$table2->setDescription('The list of authors');
$table2->setNamespace('Blog');
$table2->setPackage('Acme.Blog');
$table2->addColumns([$column21, $column22, $column23]);
$table2->addUnique($authorUsernameUnique);
$table3 = new Table('blog_category');
$table3->setDescription('The list of categories');
$table3->setNamespace('Blog');
$table3->setPackage('Acme.Blog');
$table3->addColumns([$column31, $column32]);
$table4 = new Table('blog_tag');
$table4->setDescription('The list of tags');
$table4->setNamespace('Blog');
$table4->setPackage('Acme.Blog');
$table4->addColumns([$column41, $column42]);
$table5 = new Table('blog_post_tag');
$table5->setNamespace('Blog');
$table5->setPackage('Acme.Blog');
$table5->setCrossRef();
$table5->addColumns([$column51, $column52]);
 public function providerForTestGetUniqueDDL()
 {
     $table = new Table('foo');
     $table->setIdentifierQuoting(true);
     $column1 = new Column('bar1');
     $column1->getDomain()->copy(new Domain('FOOTYPE'));
     $table->addColumn($column1);
     $column2 = new Column('bar2');
     $column2->getDomain()->copy(new Domain('BARTYPE'));
     $table->addColumn($column2);
     $index = new Unique('babar');
     $index->addColumn($column1);
     $index->addColumn($column2);
     $table->addUnique($index);
     return array(array($index));
 }
 /**
  * Load indexes for this table
  */
 protected function addIndexes(Table $table, $oid)
 {
     $stmt = $this->dbh->prepare("SELECT\n            DISTINCT ON(cls.relname)\n            cls.relname as idxname,\n            indkey,\n            indisunique\n            FROM pg_index idx\n            JOIN pg_class cls ON cls.oid=indexrelid\n            WHERE indrelid = ? AND NOT indisprimary\n            ORDER BY cls.relname");
     $stmt->bindValue(1, $oid);
     $stmt->execute();
     $stmt2 = $this->dbh->prepare("SELECT a.attname\n            FROM pg_catalog.pg_class c JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid\n            WHERE c.oid = ? AND a.attnum = ? AND NOT a.attisdropped\n            ORDER BY a.attnum");
     $indexes = array();
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         $name = $row['idxname'];
         $unique = in_array($row['indisunique'], ['t', true, 1, '1']) ? true : false;
         if (!isset($indexes[$name])) {
             if ($unique) {
                 $indexes[$name] = new Unique($name);
             } else {
                 $indexes[$name] = new Index($name);
             }
         }
         $arrColumns = explode(' ', $row['indkey']);
         foreach ($arrColumns as $intColNum) {
             $stmt2->bindValue(1, $oid);
             $stmt2->bindValue(2, $intColNum);
             $stmt2->execute();
             $row2 = $stmt2->fetch(\PDO::FETCH_ASSOC);
             $indexes[$name]->addColumn($table->getColumn($row2['attname']));
         }
     }
     foreach ($indexes as $index) {
         if ($index instanceof Unique) {
             $table->addUnique($index);
         } else {
             $table->addIndex($index);
         }
     }
 }
 /**
  * Load indexes for this table
  */
 protected function addIndexes(Table $table)
 {
     $stmt = $this->dbh->query(sprintf('SHOW INDEX FROM %s', $this->getPlatform()->quoteIdentifier($table->getName())));
     // Loop through the returned results, grouping the same key_name together
     // adding each column for that key.
     /** @var $indexes Index[] */
     $indexes = array();
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         $colName = $row['Column_name'];
         $colSize = $row['Sub_part'];
         $name = $row['Key_name'];
         if ('PRIMARY' === $name) {
             continue;
         }
         if (!isset($indexes[$name])) {
             $isUnique = 0 == $row['Non_unique'];
             if ($isUnique) {
                 $indexes[$name] = new Unique($name);
             } else {
                 $indexes[$name] = new Index($name);
             }
             if ($this->addVendorInfo) {
                 $vi = $this->getNewVendorInfoObject($row);
                 $indexes[$name]->addVendorInfo($vi);
             }
         }
         $indexes[$name]->addColumn(['name' => $colName, 'size' => $colSize]);
     }
     foreach ($indexes as $index) {
         if ($index instanceof Unique) {
             $table->addUnique($index);
         } else {
             $table->addIndex($index);
         }
     }
 }