Beispiel #1
0
 public function main()
 {
     $this->validate();
     if (!$this->mapperElement) {
         throw new BuildException("You must use a <mapper/> element to describe how names should be transformed.");
     }
     if ($this->packageObjectModel) {
         $dataModels = $this->packageDataModels();
     } else {
         $dataModels = $this->getDataModels();
     }
     // 1) first create a map of filenames to databases; this is used by other tasks like
     // the SQLExec task.
     $this->createSqlDbMap();
     // 2) Now actually create the DDL based on the datamodel(s) from XML schema file.
     $targetDatabase = $this->getTargetDatabase();
     DataModelBuilder::setBuildProperties($this->getPropelProperties());
     $builderClazz = DataModelBuilder::getBuilderClass('ddl');
     foreach ($dataModels as $package => $dataModel) {
         foreach ($dataModel->getDatabases() as $database) {
             // file we are going to create
             if (!$this->packageObjectModel) {
                 $name = $dataModel->getName();
             } else {
                 $name = ($package ? $package . '.' : '') . 'schema.xml';
             }
             $outFile = $this->getMappedFile($name);
             $this->log("Writing to SQL file: " . $outFile->getPath());
             // First add any "header" SQL
             $ddl = call_user_func(array($builderClazz, 'getDatabaseStartDDL'));
             foreach ($database->getTables() as $table) {
                 if (!$table->isSkipSql()) {
                     $builder = DataModelBuilder::builderFactory($table, 'ddl');
                     $this->log("\t+ " . $table->getName() . " [builder: " . get_class($builder) . "]");
                     $ddl .= $builder->build();
                     foreach ($builder->getWarnings() as $warning) {
                         $this->log($warning, PROJECT_MSG_WARN);
                     }
                 } else {
                     $this->log("\t + (skipping) " . $table->getName());
                 }
             }
             // foreach database->getTables()
             // Finally check to see if there is any "footer" SQL
             $ddl .= call_user_func(array($builderClazz, 'getDatabaseEndDDL'));
             // Now we're done.  Write the file!
             file_put_contents($outFile->getAbsolutePath(), $ddl);
         }
         // foreach database
     }
     //foreach datamodels
 }
Beispiel #2
0
 /**
  * Main method builds all the targets for a typical propel project.
  */
 public function main()
 {
     // check to make sure task received all correct params
     $this->validate();
     $basepath = $this->getOutputDirectory();
     // Get new Capsule context
     $generator = $this->createContext();
     $generator->put("basepath", $basepath);
     // make available to other templates
     $targetPlatform = $this->getTargetPlatform();
     // convenience for embedding in strings below
     // we need some values that were loaded into the template context
     $basePrefix = $generator->get('basePrefix');
     $project = $generator->get('project');
     DataModelBuilder::setBuildProperties($this->getPropelProperties());
     foreach ($this->getDataModels() as $dataModel) {
         $this->log("Processing Datamodel : " . $dataModel->getName());
         foreach ($dataModel->getDatabases() as $database) {
             $this->log("  - processing database : " . $database->getName());
             $generator->put("platform", $database->getPlatform());
             foreach ($database->getTables() as $table) {
                 if (!$table->isForReferenceOnly()) {
                     $this->log("\t+ " . $table->getName());
                     // -----------------------------------------------------------------------------------------
                     // Create Peer, Object, and MapBuilder classes
                     // -----------------------------------------------------------------------------------------
                     // these files are always created / overwrite any existing files
                     foreach (array('peer', 'object', 'mapbuilder') as $target) {
                         $builder = DataModelBuilder::builderFactory($table, $target);
                         $this->build($builder);
                     }
                     // -----------------------------------------------------------------------------------------
                     // Create [empty] stub Peer and Object classes if they don't exist
                     // -----------------------------------------------------------------------------------------
                     // these classes are only generated if they don't already exist
                     foreach (array('peerstub', 'objectstub') as $target) {
                         $builder = DataModelBuilder::builderFactory($table, $target);
                         $this->build($builder, $overwrite = false);
                     }
                     // -----------------------------------------------------------------------------------------
                     // Create [empty] stub child Object classes if they don't exist
                     // -----------------------------------------------------------------------------------------
                     // If table has enumerated children (uses inheritance) then create the empty child stub classes if they don't already exist.
                     if ($table->getChildrenColumn()) {
                         $col = $table->getChildrenColumn();
                         if ($col->isEnumeratedClasses()) {
                             foreach ($col->getChildren() as $child) {
                                 $builder = DataModelBuilder::builderFactory($table, 'objectmultiextend');
                                 $builder->setChild($child);
                                 $this->build($builder, $overwrite = false);
                             }
                             // foreach
                         }
                         // if col->is enumerated
                     }
                     // if tbl->getChildrenCol
                     // -----------------------------------------------------------------------------------------
                     // Create [empty] Interface if it doesn't exist
                     // -----------------------------------------------------------------------------------------
                     // Create [empty] interface if it does not already exist
                     if ($table->getInterface()) {
                         $builder = DataModelBuilder::builderFactory($table, 'interface');
                         $this->build($builder, $overwrite = false);
                     }
                     // -----------------------------------------------------------------------------------------
                     // Create tree Node classes
                     // -----------------------------------------------------------------------------------------
                     if ($table->isTree()) {
                         foreach (array('nodepeer', 'node') as $target) {
                             $builder = DataModelBuilder::builderFactory($table, $target);
                             $this->build($builder);
                         }
                         foreach (array('nodepeerstub', 'nodestub') as $target) {
                             $builder = DataModelBuilder::builderFactory($table, $target);
                             $this->build($builder, $overwrite = false);
                         }
                     }
                     // if Table->isTree()
                 }
                 // if !$table->isForReferenceOnly()
             }
             // foreach table
         }
         // foreach database
     }
     // foreach dataModel
 }
Beispiel #3
0
 /**
  * Convenience method to return a NEW Object class builder instance.
  * This is used very frequently from the peer and object builders to get
  * an object builder for a RELATED table.
  * @param Table $table
  * @return ObjectBuilder
  */
 public static function getNewObjectBuilder(Table $table)
 {
     return DataModelBuilder::builderFactory($table, 'object');
 }
Beispiel #4
0
 /**
  * Main method parses the XML files and creates SQL files.
  *
  * @return void
  * @throws Exception If there is an error parsing the data xml.
  */
 public function main()
 {
     $this->validate();
     $targetDatabase = $this->getTargetDatabase();
     $platform = $this->getPlatformForTargetDatabase();
     // Load the Data XML -> DB Name properties
     $map = new Properties();
     try {
         $map->load($this->getDataDbMap());
     } catch (IOException $ioe) {
         throw new BuildException("Cannot open and process the datadbmap!", $ioe);
     }
     DataModelBuilder::setBuildProperties($this->getPropelProperties());
     // Parse each file in teh data -> db map
     foreach ($map->keys() as $dataXMLFilename) {
         $dataXMLFile = new PhingFile($this->srcDir, $dataXMLFilename);
         // if file exists then proceed
         if ($dataXMLFile->exists()) {
             $dbname = $map->get($dataXMLFilename);
             $db = $this->getDatabase($dbname);
             if (!$db) {
                 throw new BuildException("Cannot find instantiated Database for name '{$dbname}' from datadbmap file.");
             }
             $db->setPlatform($platform);
             $outFile = $this->getMappedFile($dataXMLFilename);
             $this->log("Creating SQL from XML data dump file: " . $dataXMLFile->getAbsolutePath());
             try {
                 $dataXmlParser = new XmlToData($db, $this->dbEncoding);
                 $data = $dataXmlParser->parseFile($dataXMLFile->getAbsolutePath());
             } catch (Exception $e) {
                 throw new Exception("Exception parsing data XML: " . $e->getMessage());
             }
             $fp = fopen($outFile->getAbsolutePath(), 'w');
             $currTable = null;
             foreach ($data as $dataRow) {
                 if ($currTable !== $dataRow->getTable()) {
                     $currTable = $dataRow->getTable();
                     $builder = DataModelBuilder::builderFactory($currTable, 'datasql');
                 }
                 $sql = $builder->buildRowSql($dataRow);
                 fwrite($fp, $sql);
             }
             fclose($fp);
             // Place the generated SQL file(s)
             $p = new Properties();
             if ($this->getSqlDbMap()->exists()) {
                 $p->load($this->getSqlDbMap());
             }
             $p->setProperty($outFile->getName(), $db->getName());
             $p->store($this->getSqlDbMap(), "Sqlfile -> Database map");
         } else {
             $this->log("File '" . $dataXMLFile->getAbsolutePath() . "' in datadbmap does not exist, so skipping it.", PROJECT_MSG_WARN);
         }
     }
     // foreach data xml file
 }