Exemplo n.º 1
0
 /**
  * @throws WrongArgumentException
  * @return DBTable
  **/
 public function addColumn(DBColumn $column)
 {
     $name = $column->getName();
     Assert::isFalse(isset($this->columns[$name]), "column '{$name}' already exist");
     $this->order[] = $this->columns[$name] = $column;
     $column->setTable($this);
     return $this;
 }
Exemplo n.º 2
0
 public function preAutoincrement(DBColumn $column)
 {
     $column->setDefault(null);
     return null;
 }
Exemplo n.º 3
0
 public function getTableInfo($table)
 {
     static $types = ['tinyint' => DataType::SMALLINT, 'smallint' => DataType::SMALLINT, 'int' => DataType::INTEGER, 'mediumint' => DataType::INTEGER, 'bigint' => DataType::BIGINT, 'double' => DataType::DOUBLE, 'decimal' => DataType::NUMERIC, 'char' => DataType::CHAR, 'varchar' => DataType::VARCHAR, 'text' => DataType::TEXT, 'tinytext' => DataType::TEXT, 'mediumtext' => DataType::TEXT, 'date' => DataType::DATE, 'time' => DataType::TIME, 'timestamp' => DataType::TIMESTAMP, 'datetime' => DataType::TIMESTAMP, 'set' => null, 'enum' => null, 'year' => null];
     try {
         $result = $this->queryRaw('SHOW COLUMNS FROM ' . $table);
     } catch (BaseException $e) {
         throw new ObjectNotFoundException("unknown table '{$table}'");
     }
     $table = new DBTable($table);
     while ($row = mysqli_fetch_array($result)) {
         $name = strtolower($row['Field']);
         $matches = [];
         $info = ['type' => null, 'extra' => null];
         if (preg_match('~(\\w+)(\\((\\d+?)\\)){0,1}\\s*(\\w*)~', strtolower($row['Type']), $matches)) {
             $info['type'] = $matches[1];
             $info['size'] = $matches[3];
             $info['extra'] = $matches[4];
         }
         Assert::isTrue(array_key_exists($info['type'], $types), 'unknown type "' . $types[$info['type']] . '" found in column "' . $name . '"');
         if (empty($types[$info['type']])) {
             continue;
         }
         $column = DBColumn::create(DataType::create($types[$info['type']])->setUnsigned(strtolower($info['extra']) == 'unsigned')->setNull(strtolower($row['Null']) == 'yes'), $name)->setAutoincrement(strtolower($row['Extra']) == 'auto_increment')->setPrimaryKey(strtolower($row['Key']) == 'pri');
         if ($row['Default']) {
             $column->setDefault($row['Default']);
         }
         $table->addColumn($column);
     }
     return $table;
 }
Exemplo n.º 4
0
 private static function checkColumn(DBColumn $column)
 {
     Assert::isTrue($column->getTable() !== null && $column->getDefault() === null);
 }
Exemplo n.º 5
0
 private static function checkColumn(DBColumn $column)
 {
     $type = $column->getType();
     Assert::isTrue(($type->getId() == DataType::BIGINT || $type->getId() == DataType::INTEGER) && $column->isPrimaryKey());
 }