public function testParseStringTable() { $schema = '<database name="foo"><table name="bar"><column name="id" primaryKey="true" type="INTEGER" autoIncrement="true"/></table></database>'; $xtad = new XmlToAppData(); $appData = $xtad->parseString($schema); $database = $appData->getDatabase(); $table = $database->getTable('bar'); $expectedTable = <<<EOF <table name="bar" phpName="Bar" idMethod="native" skipSql="false" readOnly="false" reloadOnInsert="false" reloadOnUpdate="false" abstract="false"> <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/> </table> EOF; $this->assertEquals($expectedTable, $table->toString()); }
public function testDescColumn() { $schema = '<database name="reverse_bookstore"><table name="book"><column name="title" type="VARCHAR" size="255" description="Book Title with accent éài" /></table></database>'; $xtad = new XmlToAppData(); $appData = $xtad->parseString($schema); $database = $appData->getDatabase(); $table = $database->getTable('book'); $c1 = $table->getColumn('title'); $parser = new MysqlSchemaParser(Propel::getConnection('reverse-bookstore')); $parser->setGeneratorConfig(new QuickGeneratorConfig()); $database = new Database(); $database->setPlatform(new DefaultPlatform()); $parser->parse($database); $c2 = $database->getTable('book')->getColumn('title'); $this->assertEquals($c1->getDescription(), $c2->getDescription()); }
public function testDecimal() { $t1 = new Table('foo'); $schema = '<database name="reverse_bookstore"><table name="foo"><column name="longitude" type="DECIMAL" scale="7" size="10" /></table></database>'; $xtad = new XmlToAppData(); $appData = $xtad->parseString($schema); $database = $appData->getDatabase(); $table = $database->getTable('foo'); $c1 = $table->getColumn('longitude'); $parser = new MysqlSchemaParser(Propel::getConnection('reverse-bookstore')); $parser->setGeneratorConfig(new QuickGeneratorConfig()); $database = new Database(); $database->setPlatform(new MysqlPlatform()); $parser->parse($database); $table = $database->getTable('foo'); $c2 = $table->getColumn('longitude'); $this->assertEquals($c1->getSize(), $c2->getSize()); $this->assertEquals($c1->getScale(), $c2->getScale()); }
public function testGetModifyColumnDDLWithVarcharWithoutSizeAndPlatform() { $t1 = new Table('foo'); $c1 = new Column('bar'); $c1->setTable($t1); $c1->getDomain()->copy($this->getPlatform()->getDomainForType('VARCHAR')); $c1->getDomain()->replaceSize(null); $c1->getDomain()->replaceScale(null); $t1->addColumn($c1); $schema = <<<EOF <database name="test"> <table name="foo"> <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" /> <column name="bar"/> </table> </database> EOF; $xtad = new XmlToAppData(null); $appData = $xtad->parseString($schema); $db = $appData->getDatabase(); $table = $db->getTable('foo'); $c2 = $table->getColumn('bar'); $columnDiff = PropelColumnComparator::computeDiff($c1, $c2); $expected = false; $this->assertSame($expected, $columnDiff); }
public function testGetValidator() { $xmlToAppData = new XmlToAppData(new DefaultPlatform()); $schema = <<<EOF <database name="test"> <table name="table1"> <column name="id" primaryKey="true" /> <column name="title1" type="VARCHAR" /> <validator column="title1"> <rule name="minLength" value="4" message="Username must be at least 4 characters !" /> </validator> <column name="title2" type="VARCHAR" /> <validator column="title2"> <rule name="minLength" value="4" message="Username must be at least 4 characters !" /> <rule name="maxLength" value="10" message="Username must be at most 10 characters !" /> </validator> </table> </database> EOF; $appData = $xmlToAppData->parseString($schema); $table1 = $appData->getDatabase('test')->getTable('table1'); $this->assertNull($table1->getColumn('id')->getValidator()); $title1Column = $table1->getColumn('title1'); $title1Validator = $title1Column->getValidator(); $this->assertInstanceOf('Validator', $title1Validator); $this->assertEquals(1, count($title1Validator->getRules())); $title2Column = $table1->getColumn('title2'); $title2Validator = $title2Column->getValidator(); $this->assertInstanceOf('Validator', $title2Validator); $this->assertEquals(2, count($title2Validator->getRules())); }
public function testGetAddTableDDLEngine() { $schema = <<<EOF <database name="test"> <table name="foo"> <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" /> </table> </database> EOF; $platform = new MysqlPlatform(); $platform->setTableEngineKeyword('TYPE'); $platform->setDefaultTableEngine('MEMORY'); $xtad = new XmlToAppData($platform); $appData = $xtad->parseString($schema); $table = $appData->getDatabase()->getTable('foo'); $expected = "\nCREATE TABLE `foo`\n(\n `id` INTEGER NOT NULL AUTO_INCREMENT,\n PRIMARY KEY (`id`)\n) TYPE=MEMORY;\n"; $this->assertEquals($expected, $platform->getAddTableDDL($table)); }
public function getDatabase() { if (null === $this->database) { $xtad = new XmlToAppData($this->getPlatform()); $appData = $xtad->parseString($this->schema); $this->database = $appData->getDatabase(); // does final initialization } return $this->database; }
public function testGetColumnForParameter() { $xmlToAppData = new XmlToAppData(); $schema = <<<EOF <database name="test1"> <table name="table1"> <column name="id" type="INTEGER" primaryKey="true" /> <column name="title" type="VARCHAR" size="100" primaryString="true" /> <column name="created_on" type="TIMESTAMP" /> <column name="updated_on" type="TIMESTAMP" /> <behavior name="timestampable"> <parameter name="create_column" value="created_on" /> <parameter name="update_column" value="updated_on" /> </behavior> </table> </database> EOF; $appData = $xmlToAppData->parseString($schema); $table = $appData->getDatabase('test1')->getTable('table1'); $behavior = $table->getBehavior('timestampable'); $this->assertEquals($table->getColumn('created_on'), $behavior->getColumnForParameter('create_column'), 'getColumnForParameter() returns the configured column for behavior based on a parameter name'); }
public function getDatabase() { if (null === $this->database) { $xtad = new XmlToAppData($this->getPlatform()); $xtad->setGeneratorConfig($this->getConfig()); $this->database = $xtad->parseString($this->schema)->getDatabase(); } return $this->database; }
public function testIsCrossRefAttribute() { $xmlToAppData = new XmlToAppData(); $schema = <<<EOF <database name="iddb" defaultIdMethod="native"> <table name="table_native"> <column name="table_a_id" required="true" primaryKey="true" type="INTEGER" /> <column name="col_a" type="CHAR" size="5" /> </table> <table name="table_is_cross_ref_true" isCrossRef="true"> <column name="table_a_id" required="true" primaryKey="true" type="INTEGER" /> <column name="col_a" type="CHAR" size="5" /> </table> <table name="table_is_cross_ref_false" isCrossRef="false"> <column name="table_a_id" required="true" primaryKey="true" type="INTEGER" /> <column name="col_a" type="CHAR" size="5" /> </table> </database> EOF; $appData = $xmlToAppData->parseString($schema); $db = $appData->getDatabase("iddb"); $table1 = $db->getTable("table_native"); $this->assertFalse($table1->getIsCrossRef()); $table2 = $db->getTable("table_is_cross_ref_true"); $this->assertTrue($table2->getIsCrossRef()); $table3 = $db->getTable("table_is_cross_ref_false"); $this->assertFalse($table3->getIsCrossRef()); }
/** * Gets all matching XML schema files and loads them into data models for class. * @return void */ protected function loadDataModels() { $ads = array(); $totalNbTables = 0; $this->log('Loading XML schema files...'); // Get all matched files from schemaFilesets foreach ($this->schemaFilesets as $fs) { $ds = $fs->getDirectoryScanner($this->project); $srcDir = $fs->getDir($this->project); $dataModelFiles = $ds->getIncludedFiles(); sort($dataModelFiles); $defaultPlatform = $this->getGeneratorConfig()->getConfiguredPlatform(); // Make a transaction for each file foreach ($dataModelFiles as $dmFilename) { $this->log("Processing: " . $dmFilename, Project::MSG_VERBOSE); $xmlFile = new PhingFile($srcDir, $dmFilename); $dom = new DomDocument('1.0', 'UTF-8'); $dom->load($xmlFile->getAbsolutePath()); // modify schema to include any external schemas (and remove the external-schema nodes) $this->includeExternalSchemas($dom, $srcDir); // normalize (or transform) the XML document using XSLT if ($this->getGeneratorConfig()->getBuildProperty('schemaTransform') && $this->xslFile) { $this->log("Transforming " . $dmFilename . " using stylesheet " . $this->xslFile->getPath(), Project::MSG_VERBOSE); if (!class_exists('XSLTProcessor')) { $this->log("Could not perform XLST transformation. Make sure PHP has been compiled/configured to support XSLT.", Project::MSG_ERR); } else { // normalize the document using normalizer stylesheet $xslDom = new DomDocument('1.0', 'UTF-8'); $xslDom->load($this->xslFile->getAbsolutePath()); $xsl = new XsltProcessor(); $xsl->importStyleSheet($xslDom); $dom = $xsl->transformToDoc($dom); } } // validate the XML document using XSD schema if ($this->validate && $this->xsdFile) { $this->log(" Validating XML using schema " . $this->xsdFile->getPath(), Project::MSG_VERBOSE); if (!$dom->schemaValidate($this->xsdFile->getAbsolutePath())) { throw new EngineException("XML schema file (" . $xmlFile->getPath() . ") does not validate. See warnings above for reasons validation failed (make sure error_reporting is set to show E_WARNING if you don't see any).", $this->getLocation()); } } $xmlParser = new XmlToAppData($defaultPlatform, $this->getTargetPackage(), $this->dbEncoding); $xmlParser->setGeneratorConfig($this->getGeneratorConfig()); $ad = $xmlParser->parseString($dom->saveXML(), $xmlFile->getAbsolutePath()); $nbTables = $ad->getDatabase(null, false)->countTables(); $totalNbTables += $nbTables; $this->log(sprintf(' %d tables processed successfully', $nbTables), Project::MSG_VERBOSE); $ad->setName($dmFilename); $ads[] = $ad; } $this->log(sprintf('%d tables found in %d schema files.', $totalNbTables, count($dataModelFiles))); } if (empty($ads)) { throw new BuildException("No schema files were found (matching your schema fileset definition)."); } foreach ($ads as $ad) { // map schema filename with database name $this->dataModelDbMap[$ad->getName()] = $ad->getDatabase(null, false)->getName(); } if (count($ads) > 1 && $this->packageObjectModel) { $ad = $this->joinDataModels($ads); $this->dataModels = array($ad); } else { $this->dataModels = $ads; } foreach ($this->dataModels as &$ad) { $ad->doFinalInitialization(); } if ($this->validate) { foreach ($this->dataModels as $dataModel) { $validator = new PropelSchemaValidator($dataModel); if (!$validator->validate()) { throw new EngineException(sprintf("The database schema contains errors:\n - %s", join("\n - ", $validator->getErrors()))); } } } $this->dataModelsLoaded = true; }
protected function getModels($databaseManager, $verbose = false) { Phing::startup(); // required to locate behavior classes... $schemas = sfFinder::type('file')->name('*schema.xml')->follow_link()->in(sfConfig::get('sf_config_dir')); if (!$schemas) { throw new sfCommandException('You must create a schema.yml or schema.xml file.'); } $ads = array(); foreach ($schemas as $schema) { if ($verbose) { $this->logSection('schema', sprintf(' Parsing schema "%s"', $schema), null, 'COMMENT'); } $dom = new DomDocument('1.0', 'UTF-8'); $dom->load($schema); //$this->includeExternalSchemas($dom, sfConfig::get('sf_config_dir')); $xmlParser = new XmlToAppData(new DefaultPlatform(), ''); $generatorConfig = $this->getGeneratorConfig(); $generatorConfig->setBuildConnections($this->getConnections($databaseManager)); $xmlParser->setGeneratorConfig($generatorConfig); $ad = $xmlParser->parseString($dom->saveXML(), $schema); $ads[] = $ad; $nbTables = $ad->getDatabase(null, false)->countTables(); if ($verbose) { $this->logSection('schema', sprintf(' %d tables processed successfully', $nbTables), null, 'COMMENT'); } } if (count($ads) > 1) { $ad = array_shift($ads); $ad->joinAppDatas($ads); //$ad = $this->joinDataModels($ads); //$this->dataModels = array($ad); } else { $ad = $ads[0]; } $ad->doFinalInitialization(); return $ad; }
protected function getDatabaseFromSchema($schema) { $xtad = new XmlToAppData($this->getPlatform()); $appData = $xtad->parseString($schema); return $appData->getDatabase(); }
public function testPrefixDoesntAffectPhpName() { $xmlToAppData = new XmlToAppData(); $schema = <<<EOF <database name="test1" tablePrefix="pf_"> <table name="table1"> <column name="id" type="INTEGER" primaryKey="true" /> </table> </database> EOF; $appData = $xmlToAppData->parseString($schema); $table = $appData->getDatabase('test1')->getTable('pf_table1'); $this->assertEquals('Table1', $table->getPhpName()); }
public function testSetPackageOverridesNamespaceAutoPackage() { $schema = <<<EOF <database name="DB" namespace="NS1"> <table name="table" namespace="NS2" package="foo"> <column name="id" primaryKey="true" /> <column name="title1" type="VARCHAR" /> </table> </database> EOF; $config = new GeneratorConfig(); $config->setBuildProperties(array('propel.namespace.autoPackage' => 'true')); $xmlToAppData = new XmlToAppData(new DefaultPlatform()); $xmlToAppData->setGeneratorConfig($config); $table = $xmlToAppData->parseString($schema)->getDatabase('DB')->getTable('table'); $this->assertEquals('foo', $table->getPackage()); }