Example #1
0
 /**
  * Run the generator.
  * Process the given options and generate a PersistentObject definition from it.
  * 
  * @return void
  */
 public function run()
 {
     try {
         $this->input->process();
     } catch (ezcConsoleException $e) {
         $this->raiseError("Error while processing your options: {$e->getMessage()}", true);
     }
     if ($this->input->getOption('h')->value === true) {
         $this->output->outputText($this->input->getHelpText(ezcPersistentObjectSchemaGenerator::PROGRAM_DESCRIPTION, 80, true), "help");
         exit(0);
     }
     $defDir = $this->input->argumentDefinition["def dir"]->value;
     $classDir = $this->input->argumentDefinition["class dir"]->value;
     $schema = null;
     try {
         $readerClass = ezcDbSchemaHandlerManager::getReaderByFormat($this->input->getOption("format")->value);
         $reader = new $readerClass();
         switch (true) {
             case $reader instanceof ezcDbSchemaDbReader:
                 $db = ezcDbFactory::create($this->input->getOption("source")->value);
                 $schema = ezcDbSchema::createFromDb($db);
                 break;
             case $reader instanceof ezcDbSchemaFileReader:
                 $schema = ezcDbSchema::createFromFile($this->input->getOption("format")->value, $this->input->getOption("source")->value);
                 break;
             default:
                 $this->raiseError("Reader class not supported: '{$readerClass}'.");
                 break;
         }
     } catch (Exception $e) {
         $this->raiseError("Error reading schema: {$e->getMessage()}");
     }
     try {
         $writer = new ezcDbSchemaPersistentWriter($this->input->getOption("overwrite")->value, $this->input->getOption("prefix")->value);
         $writer->saveToFile($defDir, $schema);
         if ($classDir !== null) {
             $writer = new ezcDbSchemaPersistentClassWriter($this->input->getOption("overwrite")->value, $this->input->getOption("prefix")->value);
             $writer->saveToFile($classDir, $schema);
         }
     } catch (ezcBaseException $e) {
         $this->raiseError("Error writing schema: {$e->getMessage()}");
     }
     $this->output->outputLine("PersistentObject definition successfully written to {$defDir}.", 'info');
     if ($classDir !== null) {
         $this->output->outputLine("Class files successfully written to {$classDir}.", 'info');
     }
 }
 public function testWrongDiffWriterClass2()
 {
     try {
         ezcDbSchemaHandlerManager::addDiffWriter('dummy', 'stdClass');
         self::fail("Expected exception not thrown");
     } catch (ezcDbSchemaInvalidDiffWriterClassException $e) {
         $this->assertEquals("Class 'stdClass' does not exist, or does not implement the 'ezcDbSchemaDiffWriter' interface.", $e->getMessage());
     }
 }
Example #3
0
 /**
  * Returns the $db specific SQL queries that would create the tables
  * defined in the schema.
  *
  * The database type can be given as both a database handler (instanceof
  * ezcDbHandler) or the name of the database as string as retrieved through
  * calling getName() on the database handler object.
  *
  * @see ezcDbHandler::getName()
  *
  * @throws ezcDbSchemaInvalidWriterClassException if the handler associated
  *         with the $format is not a database schema writer.
  *
  * @param string|ezcDbHandler $db
  * @return array(string)
  */
 public function convertToDDL($db)
 {
     self::initOptions();
     if ($db instanceof ezcDbHandler) {
         $db = $db->getName();
     }
     $className = ezcDbSchemaHandlerManager::getDiffWriterByFormat($db);
     $writer = new $className();
     self::checkSchemaWriter($writer, self::DATABASE);
     return $writer->convertToDDL($this);
 }
Example #4
0
<?php

/**
 * @package JetFuelCore
 */
require_once 'core/common.php';
$output = new ezcConsoleOutput();
$output->formats->info->color = 'blue';
// create a database schema from a database connection:
$db = ezcDbFactory::create(DB_DSN);
$dbSchema = ezcDbSchema::createFromDb($db);
// save a database schema to an XML file:
$dbSchema->writeToFile('xml', 'saved-schema.xml');
$messages = ezcDbSchemaValidator::validate($dbSchema);
foreach ($messages as $message) {
    $output->outputLine($message, 'info');
}
$readerClass = ezcDbSchemaHandlerManager::getReaderByFormat('xml');
$reader = new $readerClass();
$schema = ezcDbSchema::createFromFile('xml', 'saved-schema.xml');
$writer = new ezcDbSchemaPersistentWriter(true, null);
$writer->saveToFile('app/model/definitions', $schema);
$output->outputLine("Class files successfully written to app/model/definitions.", 'info');
Example #5
0
 /**
  * Returns the schema reader instance for the given schema path and format. 
  * 
  * @return object The schema reader.
  */
 protected function getSchema()
 {
     $schema = null;
     $readerClass = ezcDbSchemaHandlerManager::getReaderByFormat($this->schemaFormat);
     $reader = new $readerClass();
     switch (true) {
         case $reader instanceof ezcDbSchemaDbReader:
             $db = ezcDbFactory::create($this->pathSchema);
             $schema = ezcDbSchema::createFromDb($db);
             break;
         case $reader instanceof ezcDbSchemaFileReader:
             $schema = ezcDbSchema::createFromFile($this->schemaFormat, $this->pathSchema);
             break;
         default:
             $this->raiseError("Reader class not supported: '{$readerClass}'.");
             break;
     }
     return $schema;
 }
Example #6
0
 /**
  * Returns an {@link ezcDbSchema} created from $source which is of $format.
  * 
  * @param string $format 
  * @param string $source 
  * @return ezcDbSchema
  */
 private function getSchema($format, $source)
 {
     $readerClass = ezcDbSchemaHandlerManager::getReaderByFormat($format);
     $reader = new $readerClass();
     $schema = null;
     switch (true) {
         case $reader instanceof ezcDbSchemaDbReader:
             $db = ezcDbFactory::create($source);
             $schema = ezcDbSchema::createFromDb($db);
             break;
         case $reader instanceof ezcDbSchemaFileReader:
         default:
             $schema = ezcDbSchema::createFromFile($format, $source);
             break;
     }
     return $schema;
 }