/** * Using the db connection will build the * type composite ready to convert to xml * * @param Doctrine\DBAL\Connection * @param Faker\Components\Engine\XML\Builder\NodeBuilder $builder */ public function analyse(Connection $db, NodeBuilder $builder) { $sm = $db->getSchemaManager(); # add schema element $builder->addSchema($db->getDatabase(), array()); # add writer for the platform $builder->addWriter($db->getDatabasePlatform()->getName(), 'sql'); # iterate over the table $tables = $sm->listTables(); foreach ($tables as $table) { $builder->addTable($table->getName(), array('generate' => 0)); foreach ($table->getColumns() as $column) { $builder->addColumn($column->getName(), array('type' => $column->getType()->getName()))->addType('alphanumeric', array())->setTypeOption('format', 'ccccc')->end()->end(); } $builder->end(); } $builder->end(); return $builder->build(); }
/** * The element handler functions for the XML parser * * @link http://www.php.net/manual/en/function.xml-set-element-handler.php */ protected function xmlStartTag($parser, $name, $attribs) { # convert attributes to phpType foreach ($attribs as &$value) { $value = self::phpize($value); } switch ($name) { case 'writer': if (isset($attribs['format']) === false) { throw new EngineException('Writter Tag Missing Format'); } if (isset($attribs['platform']) === false) { throw new EngineException('Writer Tag Missing Platform'); } $platform = $attribs['platform']; $format = $attribs['format']; unset($attribs['format']); unset($attribs['platform']); $this->builder->addWriter($platform, $format, $attribs); break; case 'schema': if (isset($attribs['name']) === false) { throw new EngineException('Schema Tag Missing Name'); } $this->builder->addSchema($attribs['name'], $attribs); break; case 'table': if (isset($attribs['name']) === false) { throw new EngineException('Table Tag Missing Name'); } $this->builder->addTable($attribs['name'], $attribs); break; case 'column': if (isset($attribs['name']) === false) { throw new EngineException('Column Tag Missing Name'); } $this->builder->addColumn($attribs['name'], $attribs); break; case 'datatype': if (isset($attribs['name']) === false) { throw new EngineException('Datatype Tag Missing Name'); } $this->builder->addType($attribs['name'], $attribs); break; case 'option': if (isset($attribs['name']) === false) { throw new EngineException('Have Type Option Tag Missing Name Attribute'); } if (isset($attribs['value']) === false) { throw new EngineException('Have Type Option Tag Missing Value Attribute'); } $this->builder->setTypeOption($attribs['name'], $attribs['value']); break; case 'foreign-key': if (isset($attribs['name']) === false) { throw new EngineException('Foreign-key must have a name unique name try foreignTable.foriegnColumn'); } $this->builder->addForeignKey($attribs['name'], $attribs); break; case 'alternate': case 'pick': case 'random': case 'when': case 'swap': $this->builder->addSelector($name, $attribs); break; default: throw new EngineException(sprintf('Tag name %s unknown', $name)); } }