Permalink
Browse files

Moved logic for the new build SQL task in a dedicated class.

  • Loading branch information...
1 parent 31c5462 commit 9d9b2469b617e35e12752d6c710b0a8501b20b65 @willdurand willdurand committed Sep 21, 2011
View
@@ -619,5 +619,21 @@
</propel-graphviz>
</target>
+ <taskdef
+ name="propel-build-sql"
+ classname="task.PropelSqlBuildTask" classpathRef="propelclasses" />
+
+ <target name="build-sql">
+ <propel-build-sql outputDirectory="${propel.sql.dir}"
+ validate="${propel.schema.validate}"
+ xsd="${propel.schema.xsd.file}"
+ xsl="${propel.schema.xsl.file}"
+ >
+ <schemafileset dir="${propel.schema.dir}"
+ includes="${propel.schema.sql.includes}"
+ excludes="${propel.schema.sql.excludes}"
+ />
+ </propel-build-sql>
+ </target>
-</project>
+</project>
View
@@ -234,5 +234,8 @@
<phing phingfile="build-propel.xml" target="graphviz"/>
</target>
+<target name="build-sql" depends="configure">
+ <phing phingfile="build-propel.xml" target="build-sql"/>
+</target>
</project>
@@ -145,42 +145,38 @@ public function main() {
$generatorConfig = $this->getGeneratorConfig();
- $databases = array();
foreach ($dataModels as $package => $dataModel) {
+
foreach ($dataModel->getDatabases() as $database) {
- if (!isset($databases[$database->getName()])) {
- $databases[$database->getName()] = $database;
+
+ $platform = $database->getPlatform();
+ if (!$this->packageObjectModel) {
+ $name = $dataModel->getName();
} else {
- $tables = $database->getTables();
- //merge tables from different schema.xml to the same database
- foreach ($tables as $table) {
- if (!$databases[$database->getName()]->hasTable($table->getName(), true)) {
- $databases[$database->getName()]->addTable($table);
- }
+ if (false !== strpos($package, '/')) {
+ $name = $database->getName() . '.' . $dataModel->getName();
+ } else {
+ $name = ($package ? $package . '.' : '') . '.schema.xml';
}
}
- }
- }
- foreach ($databases as $databaseName => $database) {
- $platform = $database->getPlatform();
- $name = $database->getName() . '.xml';
- $outFile = $this->getMappedFile($name);
- $absPath = $outFile->getAbsolutePath();
- if ($this->getGeneratorConfig()->getBuildProperty('disableIdentifierQuoting')) {
- $platform->setIdentifierQuoting(false);
- }
- $this->log('Using ' . get_class($platform), Project::MSG_VERBOSE);
- $ddl = $platform->getAddTablesDDL($database);
-
- if (file_exists($absPath) && $ddl == file_get_contents($absPath)) {
- $this->log('[Unchanged] ' . $outFile->getName());
- } else {
- $this->getWarnings($database, $platform);
- $this->log('Writing to SQL file: ' . $outFile->getPath());
- file_put_contents($absPath, $ddl, FILE_APPEND);
- }
- } //foreach database
+ $outFile = $this->getMappedFile($name);
+ $absPath = $outFile->getAbsolutePath();
+ if ($this->getGeneratorConfig()->getBuildProperty('disableIdentifierQuoting')) {
+ $platform->setIdentifierQuoting(false);
+ }
+ $this->log('Using ' . get_class($platform), Project::MSG_VERBOSE);
+ $ddl = $platform->getAddTablesDDL($database);
+ if (file_exists($absPath) && $ddl == file_get_contents($absPath)) {
+ $this->log('[Unchanged] ' . $outFile->getName());
+ } else {
+ $this->getWarnings($database, $platform);
+ $this->log('Writing to SQL file: ' . $outFile->getPath());
+ file_put_contents($absPath, $ddl);
+ }
+
+ } // foreach database
+ } //foreach datamodels
} // main()
@@ -0,0 +1,33 @@
+<?php
+
+/**
+ * This file is part of the Propel package.
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @license MIT License
+ */
+
+require_once 'util/PropelSqlManager.php';
+
+/**
+ * The new task for building SQL DDL based on the XML datamodel.
+ *
+ * @author William Durand <william.durand1@gmail.com>
+ * @package propel.generator.task
+ */
+class PropelSqlBuildTask extends AbstractPropelDataModelTask
+{
+ public function main()
+ {
+ $this->validate();
+ $this->packageObjectModel = true;
+
+ $manager = new PropelSqlManager();
+ $manager->setGeneratorConfig($this->getGeneratorConfig());
+ $manager->setDataModels($this->getDataModels());
+ $manager->setOutputDir($this->getOutputDirectory());
+
+ $manager->buildSql();
+ }
+}
@@ -0,0 +1,142 @@
+<?php
+
+/**
+ * This file is part of the Propel package.
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @license MIT License
+ */
+
+require_once dirname(__FILE__) . '/../config/GeneratorConfigInterface.php';
+
+/**
+ * Service class for managing SQL.
+ *
+ * @author William Durand <william.durand1@gmail.com>
+ * @version $Revision$
+ * @package propel.generator.util
+ */
+class PropelSqlManager
+{
+ /**
+ * @var GeneratorConfigInterface
+ */
+ protected $generatorConfig;
+ /**
+ * @var array
+ */
+ protected $dataModels;
+ /**
+ * @var array
+ */
+ protected $databases = null;
+ /**
+ * @var string
+ */
+ protected $outputDir;
+
+ /**
+ * @param GeneratorConfigInterface $generatorConfig
+ */
+ public function setGeneratorConfig(GeneratorConfigInterface $generatorConfig)
+ {
+ $this->generatorConfig = $generatorConfig;
+ }
+
+ /**
+ * @return GeneratorConfigInterface
+ */
+ public function getGeneratorConfig()
+ {
+ return $this->generatorConfig;
+ }
+
+ /**
+ * @param array $dataModels
+ */
+ public function setDataModels($dataModels)
+ {
+ $this->dataModels = $dataModels;
+ }
+
+ /**
+ * @return array
+ */
+ public function getDataModels()
+ {
+ return $this->dataModels;
+ }
+
+ /**
+ * @param string $outputDir
+ */
+ public function setOutputDir($outputDir)
+ {
+ $this->outputDir = $outputDir;
+ }
+
+ /**
+ * return string
+ */
+ public function getOutputDir()
+ {
+ return $this->outputDir;
+ }
+
+ /**
+ * @return array
+ */
+ public function getDatabases()
+ {
+ if (null === $this->databases) {
+ $databases = array();
+ foreach ($this->getDataModels() as $package => $dataModel) {
+ foreach ($dataModel->getDatabases() as $database) {
+ if (!isset($databases[$database->getName()])) {
+ $databases[$database->getName()] = $database;
+ } else {
+ $tables = $database->getTables();
+ // Merge tables from different schema.xml to the same database
+ foreach ($tables as $table) {
+ if (!$databases[$database->getName()]->hasTable($table->getName(), true)) {
+ $databases[$database->getName()]->addTable($table);
+ }
+ }
+ }
+ }
+ }
+ $this->databases = $databases;
+ }
+ return $this->databases;
+ }
+
+ /**
+ * Build SQL files.
+ */
+ public function buildSql()
+ {
+ $sqlDbMapContent = "# Sqlfile -> Database map\n";
+ foreach ($this->getDatabases() as $databaseName => $database) {
+ $platform = $database->getPlatform();
+ $filename = $database->getName() . '.sql';
+
+ if ($this->getGeneratorConfig()->getBuildProperty('disableIdentifierQuoting')) {
+ $platform->setIdentifierQuoting(false);
+ }
+
+ $ddl = $platform->getAddTablesDDL($database);
+
+ $file = $this->getOutputDir() . DIRECTORY_SEPARATOR . $filename;
+ if (file_exists($file) && $ddl == file_get_contents($file)) {
+ // Unchanged
+ } else {
+ file_put_contents($file, $ddl);
+ }
+
+ $sqlDbMapContent .= sprintf("%s=%s\n", $filename, $databaseName);
+ }
+
+ file_put_contents ($this->getOutputDir() . DIRECTORY_SEPARATOR . 'sqldb.map', $sqlDbMapContent);
+ }
+}

0 comments on commit 9d9b246

Please sign in to comment.