Exemple #1
0
 /**
  * Tests getting a field type.
  *
  * @depends testCreateTable
  * @covers empire\framework\db\DB::getType
  *
  * @param DB[] $dbs the database objects to work on
  */
 public function testGetType($dbs)
 {
     foreach ($dbs as $db) {
         /* @var $db DB */
         $this->assertEquals(self::$idField->getType(), $db->getType('test', 'id'));
         $this->assertEquals(self::$testField->getType(), $db->getType('test', 'test'));
     }
 }
 /**
  * Builds a column definition from a <code>Field</code> that can be used in CREATE or ALTER
  * statements.
  *
  * @param Field $field the <code>Field</code> object to be parsed
  * @return string a string that can be used in a statement
  */
 private function buildColumnDefinition(Field $field)
 {
     $type = $this->mapInternalType($field->getType()->getName());
     $length = $field->getType()->getLength();
     $res = $field->getName() . ' ' . $type;
     if ($field->getType()->getName() === 'BOOLEAN') {
         $length = '1';
     }
     if ($length !== null) {
         $res .= '(' . $length . ')';
     }
     if ($field->getNullAllowed()) {
         $res .= ' NULL';
     } else {
         $res .= ' NOT NULL';
     }
     $default = $field->getDefault();
     if ($default === null) {
         if ($field->getNullAllowed()) {
             $res .= ' DEFAULT NULL';
         }
     } else {
         if ($field->getType()->getName() === 'BOOLEAN') {
             $res .= ' DEFAULT ';
             if ($field->getDefault() === true) {
                 $res .= '1';
             } else {
                 $res .= '0';
             }
         } elseif (strlen($default) > 0) {
             $default = $this->pdoDriver->escapeString($default);
         }
         if (strlen($default) !== 0) {
             $res .= ' DEFAULT ' . $default;
         }
     }
     if ($field->getAutoIncrement()) {
         $res .= ' AUTO_INCREMENT';
     }
     $fieldComment = $field->getComment();
     if (strlen($fieldComment) > 0) {
         $res .= ' COMMENT ' . $this->pdoDriver->escapeString($fieldComment);
     }
     return $res;
 }