/** * @param Table $table */ protected function analyseColumns(Table $table) { $tableName = $table->getName(); // Analyse columns $columns = $this->driver->getColumns($tableName); foreach ($columns as $key => $col) { $column = new Column(); $column->setName($col['name']); $column->setNullable($col['nullable']); $column->setType(Helpers::columnType($col['nativetype'])); $column->setDefault($col['default']); $column->setOnUpdate(Strings::contains($col['vendor']['Extra'], 'on update')); // Analyse ENUM if ($col['nativetype'] === ColumnTypes::NATIVE_TYPE_ENUM) { $enum = Strings::matchAll($col['vendor']['Type'], ColumnTypes::NATIVE_REGEX_ENUM, PREG_PATTERN_ORDER); if ($enum) { $column->setEnum($enum[1]); $column->setType(ColumnTypes::TYPE_ENUM); $column->setSubType(Helpers::columnType($col['nativetype'])); } } $table->addColumn($column); } }
/** * @param Column $column * @return mixed */ protected function getRealDefault(Column $column) { switch ($column->getType()) { case ColumnTypes::TYPE_ENUM: return 'self::' . $column->getDefault(); default: return $column->getDefault(); } }
/** * @param Column $column */ public function addColumn(Column $column) { $column->attach($this); $this->columns[$column->getName()] = $column; }
/** * @param PhpNamespace $namespace * @param ClassType $class * @param Column $column * @return void */ public function doDecorate(Column $column, ClassType $class, PhpNamespace $namespace) { switch ($column->getType()) { // Map: DateTime case ColumnTypes::TYPE_DATETIME: $column->setType('DateTime'); if ($column->getDefault() !== NULL) { $column->setDefault('now'); } $namespace->addUse('Nette\\Utils\\DateTime'); break; // Map: Enum // Map: Enum case ColumnTypes::TYPE_ENUM: foreach ($column->getEnum() as $enum) { $name = Strings::upper($column->getName()) . '_' . $enum; $class->addConst($name, $enum); } if ($column->getDefault() !== NULL) { $column->setDefault(Strings::upper($column->getName()) . '_' . $column->getDefault()); } break; } }