コード例 #1
0
 /**
  * Defines a table in the connection with the given table definition. If the table does not
  * exist, it will be created. If the table structure is different then the definition, it
  * will be altered
  * @param Table $table table definition
  * @return null
  */
 public function defineTable(Table $table)
 {
     if ($this->tableExists($table->getName())) {
         $this->alterTable($table);
     } else {
         $this->createTable($table);
     }
 }
コード例 #2
0
 public function testGetDatabaseTable()
 {
     $field1 = new PropertyField('field1', 'type');
     $field2 = new PropertyField('field2', 'type');
     $field3 = new HasManyField('field3', 'model');
     $index = new Index('field2', array($field2));
     $modelTable = new ModelTable('table');
     $modelTable->addField($field1);
     $modelTable->addField($field2);
     $modelTable->addField($field3);
     $modelTable->addIndex($index);
     $pk = new PropertyField('id', 'pk');
     $pk->setIsAutonumbering(true);
     $pk->setIsPrimaryKey(true);
     $databaseTable = new Table('table');
     $databaseTable->addField($pk);
     $databaseTable->addField($field1);
     $databaseTable->addField($field2);
     $databaseTable->addIndex($index);
     $this->assertEquals($databaseTable, $modelTable->getDatabaseTable());
 }
コード例 #3
0
ファイル: TableTest.php プロジェクト: BGCX261/zibo-svn-to-git
 /**
  * @dataProvider providerHasIndexThrowsExceptionWhenInvalidValuePassed
  * @expectedException zibo\ZiboException
  */
 public function testHasIndexThrowsExceptionWhenInvalidValuePassed($value)
 {
     $table = new Table('table');
     $table->hasIndex($value);
 }
コード例 #4
0
 /**
  * Get the database table definition of this model
  * @return zibo\library\database\definition\Table
  */
 public function getDatabaseTable()
 {
     $table = new Table($this->name);
     foreach ($this->fields as $fieldName => $field) {
         if ($field->isLocalized() || $field instanceof HasManyField || $field instanceof HasOneField) {
             continue;
         }
         $table->addField($field);
         if ($field instanceof BelongsToField) {
             $name = $this->name . '_' . ucfirst($fieldName);
             if (strlen($name) > 64) {
                 $name = '_' . ucfirst($fieldName);
                 $name = substr($this->name, 0, 64 - strlen($name)) . $name;
             }
             $foreignKey = new ForeignKey($fieldName, $field->getRelationModelName(), self::PRIMARY_KEY, $name);
             $table->setForeignKey($foreignKey);
         }
     }
     foreach ($this->indexes as $index) {
         $fields = $index->getFields();
         foreach ($fields as $field) {
             if ($field->isLocalized()) {
                 continue 2;
             }
         }
         $table->addIndex($index);
     }
     return $table;
 }
コード例 #5
0
 /**
  * Defines the foreign keys for the provided table
  * @param Table $table table definition
  * @return null
  */
 public function defineForeignKeys(Table $table)
 {
     $tableName = $table->getName();
     $databaseTable = $this->getTable($tableName);
     $foreignKeys = $table->getForeignKeys();
     $foreignKeysToDrop = array();
     foreach ($foreignKeys as $fieldName => $foreignKey) {
         if (!$databaseTable->hasForeignKey($fieldName)) {
             continue;
         }
         $databaseForeignKey = $databaseTable->getForeignKey($fieldName);
         if ($foreignKey->equals($databaseForeignKey)) {
             unset($foreignKeys[$fieldName]);
         } else {
             $foreignKeysToDrop[] = $this->connection->quoteIdentifier($databaseForeignKey->getName());
         }
     }
     $tableName = $this->connection->quoteIdentifier($tableName);
     foreach ($foreignKeysToDrop as $foreignKey) {
         $this->connection->execute('ALTER TABLE ' . $tableName . ' DROP FOREIGN KEY ' . $foreignKey);
     }
     foreach ($foreignKeys as $foreignKey) {
         $name = $this->connection->quoteIdentifier($foreignKey->getName());
         $fieldName = $this->connection->quoteIdentifier($foreignKey->getFieldName());
         $referenceTableName = $this->connection->quoteIdentifier($foreignKey->getReferenceTableName());
         $referenceFieldName = $this->connection->quoteIdentifier($foreignKey->getReferenceFieldName());
         $this->connection->execute('ALTER TABLE ' . $tableName . ' ADD CONSTRAINT ' . $name . ' FOREIGN KEY (' . $fieldName . ') REFERENCES ' . $referenceTableName . ' (' . $referenceFieldName . ') ON DELETE SET NULL ON UPDATE NO ACTION');
     }
 }
コード例 #6
0
 /**
  * Creates a new table
  * @param zibo\library\database\definition\Table $table Table definition for the new table
  * @return null
  */
 protected function createTable(Table $table)
 {
     $tableName = $this->connection->quoteIdentifier($table->getName());
     $fields = $table->getFields();
     $primaryKeys = array();
     $indexes = array();
     $uniques = array();
     $sql = '';
     foreach ($fields as $field) {
         $fieldName = $this->connection->quoteIdentifier($field->getName());
         $sql .= $sql == '' ? '' : ', ';
         $sql .= $fieldName;
         $sql .= ' ' . $this->getFieldType($field);
         if ($field->isPrimaryKey()) {
             $primaryKeys[] = $fieldName;
         } else {
             $sql .= ' DEFAULT ' . $this->getDefaultValue($field);
             if ($field->isUnique()) {
                 $uniques[] = $fieldName;
             } elseif ($field->isIndexed()) {
                 $indexes[] = $field->getName();
             }
         }
     }
     if ($primaryKeys) {
         $sql .= ', PRIMARY KEY (' . implode(', ', $primaryKeys) . ')';
     }
     if ($uniques) {
         $sql .= ', UNIQUE (' . implode(', ', $uniques) . ')';
     }
     $sql = 'CREATE TABLE ' . $tableName . ' (' . $sql . ')';
     $this->connection->execute($sql);
     foreach ($indexes as $fieldName) {
         $this->addIndexFromFieldName($table->getName(), $fieldName);
     }
     $indexes = $table->getIndexes();
     foreach ($indexes as $index) {
         $this->addIndex($table->getName(), $index);
     }
 }