Beispiel #1
0
    public function testParseStringTable()
    {
        $xmlSchema = '<database name="foo"><table name="bar"><column name="id" primaryKey="true" type="INTEGER" autoIncrement="true"/></table></database>';
        $expectedSchema = <<<EOF
<?xml version="1.0" encoding="utf-8"?>
<app-data>
  <database name="foo" defaultIdMethod="native" defaultPhpNamingMethod="underscore">
    <table name="bar" idMethod="native" phpName="Bar">
      <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
    </table>
  </database>
</app-data>
EOF;
        $schema = $this->reader->parseString($xmlSchema);
        $this->assertEquals($expectedSchema, $schema->toString());
    }
Beispiel #2
0
    public function testParseStringTable()
    {
        $xmlSchema = '<database name="foo"><table name="bar"><column name="id" primaryKey="true" type="INTEGER" autoIncrement="true"/></table></database>';
        $schemaReader = new SchemaReader();
        $schema = $schemaReader->parseString($xmlSchema);
        $database = $schema->getDatabase();
        $table = $database->getTable('bar');
        $expectedTable = <<<EOF
<table name="bar" phpName="Bar" idMethod="native">
  <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
</table>
EOF;
        $this->assertEquals($expectedTable, $table->toString());
    }
Beispiel #3
0
    public function testGetColumnForParameter()
    {
        $schemaReader = new SchemaReader();
        $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 = $schemaReader->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');
    }
Beispiel #4
0
 public function getDatabase()
 {
     if (null === $this->database) {
         $xtad = new SchemaReader($this->getPlatform());
         $xtad->setGeneratorConfig($this->getConfig());
         $appData = $xtad->parseString($this->schema);
         $this->database = $appData->getDatabase();
         // does final initialization
     }
     return $this->database;
 }
 /**
  * Returns all matching XML schema files and loads them into data models for
  * class.
  */
 protected function loadDataModels()
 {
     $schemas = array();
     $totalNbTables = 0;
     $dataModelFiles = $this->getSchemas();
     $defaultPlatform = $this->getGeneratorConfig()->getConfiguredPlatform();
     // Make a transaction for each file
     foreach ($dataModelFiles as $schema) {
         $dmFilename = $schema->getPathName();
         $this->log('Processing: ' . $schema->getFileName());
         $dom = new \DOMDocument('1.0', 'UTF-8');
         $dom->load($dmFilename);
         $this->includeExternalSchemas($dom, $schema->getPath());
         // normalize (or transform) the XML document using XSLT
         if ($this->getGeneratorConfig()->get()['generator']['schema']['transform'] && $this->xsl) {
             $this->log('Transforming ' . $dmFilename . ' using stylesheet ' . $this->xsl->getPath());
             if (!class_exists('\\XSLTProcessor')) {
                 $this->log('Could not perform XLST transformation. Make sure PHP has been compiled/configured to support XSLT.');
             } else {
                 // normalize the document using normalizer stylesheet
                 $xslDom = new \DOMDocument('1.0', 'UTF-8');
                 $xslDom->load($this->xsl->getAbsolutePath());
                 $xsl = new \XsltProcessor();
                 $xsl->importStyleSheet($xslDom);
                 $dom = $xsl->transformToDoc($dom);
             }
         }
         // validate the XML document using XSD schema
         if ($this->validate && $this->xsd) {
             $this->log('  Validating XML using schema ' . $this->xsd->getPath());
             if (!$dom->schemaValidate($this->xsd->getAbsolutePath())) {
                 throw new EngineException(sprintf("XML schema file (%s) 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).", $dmFilename), $this->getLocation());
             }
         }
         $xmlParser = new SchemaReader($defaultPlatform, $this->dbEncoding);
         $xmlParser->setGeneratorConfig($this->getGeneratorConfig());
         $schema = $xmlParser->parseString($dom->saveXML(), $dmFilename);
         $nbTables = $schema->getDatabase(null, false)->countTables();
         $totalNbTables += $nbTables;
         $this->log(sprintf('  %d tables processed successfully', $nbTables));
         $schema->setName($dmFilename);
         $schemas[] = $schema;
     }
     $this->log(sprintf('%d tables found in %d schema files.', $totalNbTables, count($dataModelFiles)));
     if (empty($schemas)) {
         throw new BuildException('No schema files were found (matching your schema fileset definition).');
     }
     foreach ($schemas as $schema) {
         // map schema filename with database name
         $this->dataModelDbMap[$schema->getName()] = $schema->getDatabase(null, false)->getName();
     }
     if (count($schemas) > 1 && $this->getGeneratorConfig()->get()['generator']['packageObjectModel']) {
         $schema = $this->joinDataModels($schemas);
         $this->dataModels = array($schema);
     } else {
         $this->dataModels = $schemas;
     }
     foreach ($this->dataModels as &$schema) {
         $schema->doFinalInitialization();
     }
     $this->dataModelsLoaded = true;
 }
    public function testGetAddTableDDLEngine()
    {
        $schema = <<<EOF
<database name="test" identifierQuoting="true">
    <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 SchemaReader($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));
    }
Beispiel #7
0
 protected function getDatabaseFromSchema($schema)
 {
     $xtad = new SchemaReader($this->getPlatform());
     $appData = $xtad->parseString($schema);
     return $appData->getDatabase();
 }
    public function testGetModifyColumnDDLWithVarcharWithoutSizeAndPlatform()
    {
        $t1 = new Table('foo');
        $t1->setIdentifierQuoting(true);
        $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" identifierQuoting="true">
    <table name="foo">
        <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
        <column name="bar"/>
    </table>
</database>
EOF;
        $xtad = new SchemaReader(null);
        $appData = $xtad->parseString($schema);
        $db = $appData->getDatabase();
        $table = $db->getTable('foo');
        $c2 = $table->getColumn('bar');
        $columnDiff = ColumnComparator::computeDiff($c1, $c2);
        $expected = false;
        $this->assertSame($expected, $columnDiff);
    }