[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/propel-generator/classes/propel/phing/ -> PropelSQLTask.php (source)

   1  <?php
   2  
   3  /*
   4   *  $Id: PropelSQLTask.php 180 2005-08-30 09:04:00Z david $
   5   *
   6   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   7   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   8   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   9   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  10   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  12   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  13   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  14   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17   *
  18   * This software consists of voluntary contributions made by many individuals
  19   * and is licensed under the LGPL. For more information please see
  20   * <http://propel.phpdb.org>.
  21   */
  22  
  23  include_once 'propel/engine/database/model/AppData.php';
  24  
  25  /**
  26   * The task for building SQL DDL based on the XML datamodel.
  27   * 
  28   * This class uses the new DDLBuilder classes instead of the Capsule PHP templates.
  29   * 
  30   * @author Hans Lellelid <hans@xmpl.org>
  31   * @package propel.phing
  32   */
  33  class PropelSQLTask extends AbstractPropelDataModelTask {
  34  
  35      /**
  36       * The properties file that maps an SQL file to a particular database.
  37       * @var PhingFile
  38       */
  39      private $sqldbmap;
  40  
  41      /**
  42       * Name of the database.
  43       */
  44      private $database;
  45  
  46      /**
  47       * Set the sqldbmap.
  48       * @param PhingFile $sqldbmap The db map.
  49       */
  50      public function setSqlDbMap(PhingFile $sqldbmap)
  51      {
  52          $this->sqldbmap = $sqldbmap;
  53      }
  54  
  55      /**
  56       * Get the sqldbmap.
  57       * @return PhingFile $sqldbmap.
  58       */
  59      public function getSqlDbMap()
  60      {
  61          return $this->sqldbmap;
  62      }
  63  
  64      /**
  65       * Set the database name.
  66       * @param string $database
  67       */
  68      public function setDatabase($database)
  69      {
  70          $this->database = $database;
  71      }
  72  
  73      /**
  74       * Get the database name.
  75       * @return string
  76       */
  77      public function getDatabase()
  78      {
  79          return $this->database;
  80      }
  81  
  82      /**
  83       * Create the sql -> database map.
  84       *
  85       * @throws IOException - if unable to store properties
  86       */
  87      private function createSqlDbMap()
  88      {
  89          if ($this->getSqlDbMap() === null) {
  90              return;
  91          }
  92  
  93          // Produce the sql -> database map
  94          $sqldbmap = new Properties();
  95  
  96          // Check to see if the sqldbmap has already been created.
  97          if ($this->getSqlDbMap()->exists()) {
  98              $sqldbmap->load($this->getSqlDbMap());
  99          }
 100  
 101          if ($this->packageObjectModel) {
 102              // in this case we'll get the sql file name from the package attribute
 103              $dataModels = $this->packageDataModels();
 104              foreach ($dataModels as $package => $dataModel) {
 105                  foreach ($dataModel->getDatabases() as $database) {
 106                      $name = ($package ? $package . '.' : '') . 'schema.xml';
 107                      $sqlFile = $this->getMappedFile($name);
 108                      $sqldbmap->setProperty($sqlFile->getName(), $database->getName());
 109                  }
 110              }
 111          } else {
 112              // the traditional way is to map the schema.xml filenames
 113              $dmMap = $this->getDataModelDbMap();
 114              foreach(array_keys($dmMap) as $dataModelName) {
 115                  $sqlFile = $this->getMappedFile($dataModelName);
 116                  if ($this->getDatabase() === null) {
 117                      $databaseName = $dmMap[$dataModelName];
 118                  } else {
 119                      $databaseName = $this->getDatabase();
 120                  }
 121                  $sqldbmap->setProperty($sqlFile->getName(), $databaseName);
 122              }
 123          }
 124  
 125          try {
 126              $sqldbmap->store($this->getSqlDbMap(), "Sqlfile -> Database map");
 127          } catch (IOException $e) {
 128              throw new IOException("Unable to store properties: ". $e->getMessage());
 129          }
 130      }
 131  
 132      public function main() {
 133  
 134          $this->validate();
 135  
 136          if(!$this->mapperElement) {
 137              throw new BuildException("You must use a <mapper/> element to describe how names should be transformed.");
 138          }
 139  
 140          if ($this->packageObjectModel) {
 141              $dataModels = $this->packageDataModels();
 142          } else {
 143              $dataModels = $this->getDataModels();
 144          }
 145  
 146          // 1) first create a map of filenames to databases; this is used by other tasks like
 147          // the SQLExec task.
 148          $this->createSqlDbMap();
 149  
 150          // 2) Now actually create the DDL based on the datamodel(s) from XML schema file.
 151          $targetDatabase = $this->getTargetDatabase();
 152  
 153          DataModelBuilder::setBuildProperties($this->getPropelProperties());
 154          $builderClazz = DataModelBuilder::getBuilderClass('ddl');
 155          
 156          foreach ($dataModels as $package => $dataModel) {
 157  
 158              foreach ($dataModel->getDatabases() as $database) {
 159  
 160                  // file we are going to create
 161                  if (!$this->packageObjectModel) {
 162                      $name = $dataModel->getName();
 163                  } else {
 164                      $name = ($package ? $package . '.' : '') . 'schema.xml';
 165                  }
 166  
 167                  $outFile = $this->getMappedFile($name);
 168  
 169                  $this->log("Writing to SQL file: " . $outFile->getPath());
 170  
 171                  // First add any "header" SQL
 172                  $ddl = call_user_func(array($builderClazz, 'getDatabaseStartDDL'));
 173                      
 174                  foreach($database->getTables() as $table) {
 175                  
 176                      if (!$table->isSkipSql()) {
 177                          $builder = DataModelBuilder::builderFactory($table, 'ddl');
 178                          $this->log("\t+ " . $table->getName() . " [builder: " . get_class($builder) . "]");
 179                          $ddl .= $builder->build();
 180                          foreach($builder->getWarnings() as $warning) {
 181                              $this->log($warning, PROJECT_MSG_WARN);
 182                          }
 183                      } else {
 184                          $this->log("\t + (skipping) " . $table->getName());
 185                      }
 186                      
 187                  } // foreach database->getTables()
 188  
 189                  // Finally check to see if there is any "footer" SQL
 190                  $ddl .= call_user_func(array($builderClazz, 'getDatabaseEndDDL'));
 191                  
 192                  
 193                  // Now we're done.  Write the file!
 194                  file_put_contents($outFile->getAbsolutePath(), $ddl);
 195                  
 196              } // foreach database
 197          } //foreach datamodels
 198  
 199      } // main()
 200  
 201      /**
 202       * Packages the datamodels to one datamodel per package
 203       *
 204       * This applies only when the the packageObjectModel option is set. We need to
 205       * re-package the datamodels to allow the database package attribute to control
 206       * which tables go into which SQL file.
 207       *
 208       * @return array The packaged datamodels
 209       */
 210      protected function packageDataModels() {
 211  
 212          static $packagedDataModels;
 213  
 214          if (is_null($packagedDataModels)) {
 215  
 216              $dataModels = $this->getDataModels();
 217              $dataModel = array_shift($dataModels);
 218              $packagedDataModels = array();
 219              
 220              $platform = $this->getPlatformForTargetDatabase();
 221              
 222              foreach ($dataModel->getDatabases() as $db) {
 223                  foreach ($db->getTables() as $table) {
 224                      $package = $table->getPackage();
 225                      if (!isset($packagedDataModels[$package])) {
 226                          $dbClone = $this->cloneDatabase($db);
 227                          $dbClone->setPackage($package);
 228                          $ad = new AppData($platform);
 229                          $ad->setName($dataModel->getName());
 230                          $ad->addDatabase($dbClone);
 231                          $packagedDataModels[$package] = $ad;
 232                      }
 233                      $packagedDataModels[$package]->getDatabase($db->getName())->addTable($table);
 234                  }
 235              }
 236          }
 237  
 238          return $packagedDataModels;
 239      }
 240  
 241  	protected function cloneDatabase($db) {
 242  
 243          $attributes = array (
 244              'name' => $db->getName(),
 245              'baseClass' => $db->getBaseClass(),
 246              'basePeer' => $db->getBasePeer(),
 247              //'defaultPhpType' => $db->getDefaultPhpType(),
 248              'defaultIdMethod' => $db->getDefaultIdMethod(),
 249              'defaultPhpNamingMethod' => $db->getDefaultPhpNamingMethod(),
 250              'defaultTranslateMethod' => $db->getDefaultTranslateMethod(),
 251              //'heavyIndexing' => $db->getHeavyIndexing(),
 252          );
 253  
 254          $clone = new Database();
 255          $clone->loadFromXML($attributes);
 256          return $clone;
 257      }
 258  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7