public function testJoinXmlSchemaWithMultipleDatabaseSchema()
    {
        $expectedSchema = <<<EOF
<?xml version="1.0" encoding="utf-8"?>
<app-data>
  <database name="default" defaultIdMethod="native" schema="foo" defaultPhpNamingMethod="underscore">
    <table name="table1" idMethod="native" phpName="Table1">
      <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
    </table>
    <table name="table2" schema="bar" idMethod="native" phpName="Table2">
      <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
      <column name="table1_id" phpName="Table1Id" type="INTEGER"/>
      <foreign-key foreignTable="table1" foreignSchema="foo" name="table2_fk_6e7121">
        <reference local="table1_id" foreign="id"/>
      </foreign-key>
    </table>
  </database>
</app-data>
EOF;
        $fooReader = new SchemaReader(new PgsqlPlatform());
        $barReader = new SchemaReader(new PgsqlPlatform());
        $fooSchema = $fooReader->parseFile($this->getSchemaFile('fooSchema.xml'));
        $barSchema = $barReader->parseFile($this->getSchemaFile('barSchema.xml'));
        $fooSchema->joinSchemas([$barSchema]);
        $this->assertEquals($expectedSchema, $fooSchema->toString());
    }
 public function setUp()
 {
     // run only once to save execution time
     if (null == self::$database) {
         $schemaReader = new SchemaReader(new DefaultPlatform());
         $appData = $schemaReader->parseFile(realpath(__DIR__ . '/../../../../../Fixtures/bookstore/schema.xml'));
         self::$database = $appData->getDatabase("bookstore");
     }
 }
Example #3
0
    public function testParseFileExternalSchema()
    {
        $schema = $this->reader->parseFile($this->getSchemaFile('outerSchema.xml'));
        $expectedSchema = <<<EOF
<?xml version="1.0" encoding="utf-8"?>
<app-data>
  <database name="foo" defaultIdMethod="native" defaultPhpNamingMethod="underscore">
    <table name="bar1" idMethod="native" phpName="Bar1">
      <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
    </table>
    <table name="bar2" idMethod="native" phpName="Bar2" skipSql="true" forReferenceOnly="true">
      <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
    </table>
  </database>
</app-data>
EOF;
        $this->assertEquals($expectedSchema, $schema->toString());
    }
Example #4
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');
    }
Example #5
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));
    }
Example #8
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);
    }
Example #10
0
    public function testParseFileExternalSchema()
    {
        $path = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'outerSchema.xml');
        $schemaReader = new SchemaReader();
        $schema = $schemaReader->parseFile($path);
        $expectedSchema = <<<EOF
<app-data>
<database name="foo" defaultIdMethod="native" defaultPhpNamingMethod="underscore">
  <table name="bar1" phpName="Bar1" idMethod="native">
    <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
  </table>
  <table name="bar2" phpName="Bar2" idMethod="native" forReferenceOnly="true">
    <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
  </table>
</database>
</app-data>
EOF;
        $this->assertEquals($expectedSchema, $schema->toString());
    }