Example #1
0
 /**
  * @inheritdoc
  */
 public function checkColumn(Column $column)
 {
     if ($column->flags & Column::TIMESTAMP) {
         if (!$column->getDefault()) {
             $column->setFlag(Column::NOT_NULL);
             $column->setDefault(new Raw('CURRENT_TIMESTAMP'));
         }
     }
 }
Example #2
0
 public function getTableDefinition($tableName)
 {
     $tableSymbol = new Symbol($tableName);
     $res = $this->database->query("DESC ?", $tableSymbol);
     $columns = new \stdClass();
     while ($row = $res->fetchRow()) {
         $type = $row['Type'];
         $field = $row['Field'];
         $phpType = $this->getTypeByString($type);
         if ('auto_increment' === $row['Extra']) {
             $phpType += Column::AUTO_ID;
         }
         $column = new Column($phpType);
         $columns->{$field} = $column;
         $column->schemaName = $field;
         $notNull = $row['Null'] === 'NO';
         if ($row['Default'] !== null || !$notNull) {
             $column->setDefault($row['Default']);
         }
         $column->setFlag(Column::NOT_NULL, $notNull);
     }
     $definition = new Table($columns, $this->database, $tableName);
     $res = $this->database->query("SHOW INDEX FROM ?", $tableSymbol);
     $indexes = array();
     $uniqueIndex = array();
     foreach ($res as $row) {
         $indexes[$row['Key_name']][$row['Seq_in_index']] = $columns->{$row['Column_name']};
         $uniqueIndex[$row['Key_name']] = !$row['Non_unique'];
     }
     foreach ($indexes as $indexName => $indexData) {
         ksort($indexData);
         $index = new Index(array_values($indexData));
         $index->setName($indexName);
         $index->setType($uniqueIndex[$indexName] ? Index::TYPE_UNIQUE : Index::TYPE_KEY);
         if ($indexName === self::_PRIMARY) {
             $definition->setPrimaryKey($index->columns);
         } else {
             $definition->addIndex($index);
         }
     }
     return $definition;
 }
Example #3
0
 private function parseColumn(Parser $parser)
 {
     $columnName = $parser->inner(null, array(' ', "\r", "\n", "\t"));
     $columnName = (string) $this->resolve(trim($columnName));
     $type = (string) $parser->inner(null, ' ');
     $unsigned = $parser->contain('UNSIGNED', true);
     $notNull = $parser->contain('NOT NULL', true);
     $autoId = $this->isAutoId($parser);
     $default = $parser->inner('DEFAULT ', null, false, true);
     if (!$default->isEmpty()) {
         $default = (string) $default;
         $defUpper = strtoupper($default);
         if ('NULL' === $defUpper) {
             $default = null;
         } elseif ('CURRENT_TIMESTAMP' === $defUpper) {
             $default = new Raw($defUpper);
         } elseif (strpos($default, self::BIND_PREFIX) !== false) {
             $default = $this->resolve($default);
         }
     } else {
         $default = false;
     }
     $flags = $this->getTypeByString($type);
     if ($notNull) {
         $flags += Column::NOT_NULL;
     }
     if ($autoId) {
         $flags += Column::AUTO_ID;
     }
     if ($unsigned) {
         $flags += Column::UNSIGNED;
     }
     $column = new Column($flags);
     if ($length = (string) $parser->setOffset(0)->inner('VARCHAR(', ')', false, true)) {
         $column->setStringLength($length, false);
     } elseif ($length = (string) $parser->setOffset(0)->inner('CHAR(', ')', false, true)) {
         $column->setStringLength($length, true);
     }
     $column->setDefault($default);
     $column->schemaName = $columnName;
     $this->columns->{$columnName} = $column;
 }
Example #4
0
 public function getColumns($tableName)
 {
     //echo PHP_EOL . 'table: ' . $tableName . PHP_EOL;
     $res = $this->database->select()->select('c.column_name, c.is_nullable, c.data_type, c.column_default, tc.constraint_type')->from('INFORMATION_SCHEMA.COLUMNS AS c')->leftJoin('INFORMATION_SCHEMA.constraint_column_usage AS ccu ON c.column_name = ccu.column_name AND c.table_name = ccu.table_name')->leftJoin('INFORMATION_SCHEMA.table_constraints AS tc ON ccu.constraint_name = tc.constraint_name')->where('c.table_name = ?', $tableName)->order('c.ordinal_position ASC')->query();
     $columns = new \stdClass();
     while ($r = $res->fetchRow()) {
         //print_r($r);
         $field = $r['column_name'];
         //echo 'field: ' . $field . PHP_EOL;
         $phpType = $this->getColumnFlagsByString($r['data_type']);
         $notNull = $r['is_nullable'] === 'NO';
         $column = new Column($phpType);
         $skipDefault = false;
         if ('nextval' === substr($r['column_default'], 0, 7)) {
             $column->setFlag(Column::AUTO_ID);
             $column->setFlag(Column::INTEGER);
             $skipDefault = true;
         }
         $column->setFlag(Column::NOT_NULL, $notNull);
         if (!$skipDefault) {
             $default = $r['column_default'];
             if ($default !== null || !$notNull) {
                 if (is_string($default)) {
                     if ("'" === $default[0]) {
                         $pos = strrpos($default, "'::");
                         if ($pos !== false) {
                             $default = substr($default, 1, $pos - 1);
                         }
                     } elseif ('NULL::' === substr($default, 0, 6)) {
                         $default = null;
                     }
                 }
                 $column->setDefault($default);
             }
         }
         $columns->{$field} = $column;
     }
     return $columns;
 }