[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * $Id: PropelOldSQLTask.php 497 2006-01-22 13:07:47Z fabien $ 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 * An extended Capsule task used for generating SQL source from an XML schema describing a database structure. 27 * 28 * This is deprecated; the new PropelSQLTask should be used instead. 29 * 30 * @author Hans Lellelid <hans@xmpl.org> (Propel) 31 * @author Jason van Zyl <jvanzyl@periapt.com> (Torque) 32 * @author John McNally <jmcnally@collab.net> (Torque) 33 * @version $Revision: 277 $ 34 * @package propel.phing 35 * @deprecated 36 */ 37 class PropelOldSQLTask extends AbstractPropelDataModelTask { 38 39 /** 40 * The properties file that maps an SQL file to a particular database. 41 * @var PhingFile 42 */ 43 private $sqldbmap; 44 45 /** 46 * Name of the database. 47 */ 48 private $database; 49 50 /** 51 * Set the sqldbmap. 52 * @param PhingFile $sqldbmap The db map. 53 */ 54 public function setSqlDbMap(PhingFile $sqldbmap) 55 { 56 $this->sqldbmap = $sqldbmap; 57 } 58 59 /** 60 * Get the sqldbmap. 61 * @return PhingFile $sqldbmap. 62 */ 63 public function getSqlDbMap() 64 { 65 return $this->sqldbmap; 66 } 67 68 /** 69 * Set the database name. 70 * @param string $database 71 */ 72 public function setDatabase($database) 73 { 74 $this->database = $database; 75 } 76 77 /** 78 * Get the database name. 79 * @return string 80 */ 81 public function getDatabase() 82 { 83 return $this->database; 84 } 85 86 /** 87 * Create the sql -> database map. 88 * 89 * @throws IOException - if unable to store properties 90 */ 91 private function createSqlDbMap() 92 { 93 if ($this->getSqlDbMap() === null) { 94 return; 95 } 96 97 // Produce the sql -> database map 98 $sqldbmap = new Properties(); 99 100 // Check to see if the sqldbmap has already been created. 101 if ($this->getSqlDbMap()->exists()) { 102 $sqldbmap->load($this->getSqlDbMap()); 103 } 104 105 if ($this->packageObjectModel) { 106 // in this case we'll get the sql file name from the package attribute 107 $dataModels = $this->packageDataModels(); 108 foreach ($dataModels as $package => $dataModel) { 109 foreach ($dataModel->getDatabases() as $database) { 110 $name = ($package ? $package . '.' : '') . 'schema.xml'; 111 $sqlFile = $this->getMappedFile($name); 112 $sqldbmap->setProperty($sqlFile->getName(), $database->getName()); 113 } 114 } 115 } else { 116 // the traditional way is to map the schema.xml filenames 117 $dmMap = $this->getDataModelDbMap(); 118 foreach(array_keys($dmMap) as $dataModelName) { 119 $sqlFile = $this->getMappedFile($dataModelName); 120 if ($this->getDatabase() === null) { 121 $databaseName = $dmMap[$dataModelName]; 122 } else { 123 $databaseName = $this->getDatabase(); 124 } 125 $sqldbmap->setProperty($sqlFile->getName(), $databaseName); 126 } 127 } 128 129 try { 130 $sqldbmap->store($this->getSqlDbMap(), "Sqlfile -> Database map"); 131 } catch (IOException $e) { 132 throw new IOException("Unable to store properties: ". $e->getMessage()); 133 } 134 } 135 136 public function main() { 137 138 $this->validate(); 139 140 if(!$this->mapperElement) { 141 throw new BuildException("You must use a <mapper/> element to describe how names should be transformed."); 142 } 143 144 if ($this->packageObjectModel) { 145 $dataModels = $this->packageDataModels(); 146 } else { 147 $dataModels = $this->getDataModels(); 148 } 149 150 // 1) first create a map of filenames to databases; this is used by other tasks like 151 // the SQLExec task. 152 $this->createSqlDbMap(); 153 154 // 2) Now actually create the DDL based on the datamodel(s) from XML schema file. 155 $targetDatabase = $this->getTargetDatabase(); 156 157 $basepath = "sql/base/$targetDatabase"; 158 159 $generator = $this->createContext(); 160 $generator->put("basepath", $basepath); // make available to sub-templates 161 162 163 $fname = "sql/base/$targetDatabase/table.tpl" ; 164 // $generator->put("fname", $fname); // make available to sub-templates 165 166 $fnamekeys= "sql/base/$targetDatabase/tablefk.tpl"; 167 //$generator->put("fnamekeys", $fnamekeys); // make available to sub-templates 168 169 $ddlStartFile = new PhingFile($this->getTemplatePath(), "sql/base/$targetDatabase/database-start.tpl"); 170 $ddlEndFile = new PhingFile($this->getTemplatePath(), "sql/base/$targetDatabase/database-end.tpl"); 171 172 foreach ($dataModels as $package => $dataModel) { 173 174 foreach ($dataModel->getDatabases() as $database) { 175 176 // file we are going to create 177 if (!$this->packageObjectModel) { 178 $name = $dataModel->getName(); 179 } else { 180 $name = ($package ? $package . '.' : '') . 'schema.xml'; 181 } 182 $outFile = $this->getMappedFile($name); 183 184 $generator->put("database", $database); // make available to sub-templates 185 $generator->put("platform", $database->getPlatform()); 186 187 $this->log("Generating SQL tables for database: " . $database->getName()); 188 189 $this->log("Writing to SQL file: " . $outFile->getPath()); 190 191 192 // this variable helps us overwrite the first time we write to file 193 // and then append thereafter 194 $append=false; 195 196 // First check to see if there is a "header" SQL file 197 if ($ddlStartFile->exists()) { 198 $generator->parse($ddlStartFile->getAbsolutePath(), $outFile->getAbsolutePath(), false); 199 $append = true; 200 } 201 202 foreach($database->getTables() as $tbl) { 203 if (!$tbl->isSkipSql()) { 204 $this->log("\t + " . $tbl->getName()); 205 $generator->put("table", $tbl); 206 $generator->parse($fname, $outFile->getAbsolutePath(), $append); 207 if ($append === false) $append = true; 208 } else { 209 $this->log("\t + (skipping) " . $tbl->getName()); 210 } 211 } // foreach database->getTables() 212 213 foreach ($database->getTables() as $tbl) { 214 if (!$tbl->isSkipSql()) { 215 $generator->put("tablefk", $tbl); 216 $generator->parse($fnamekeys, $outFile->getAbsolutePath(), true); // always append 217 } 218 } 219 220 // Finally check to see if there is a "footer" SQL file 221 if ($ddlEndFile->exists()) { 222 $generator->parse($ddlEndFile->getAbsolutePath(), $outFile->getAbsolutePath(), true); 223 } 224 225 } // foreach database 226 } //foreach datamodels 227 228 } // main() 229 230 /** 231 * Packages the datamodels to one datamodel per package 232 * 233 * This applies only when the the packageObjectModel option is set. We need to 234 * re-package the datamodels to allow the database package attribute to control 235 * which tables go into which SQL file. 236 * 237 * @return array The packaged datamodels 238 */ 239 protected function packageDataModels() { 240 241 static $packagedDataModels; 242 243 if (is_null($packagedDataModels)) { 244 245 $dataModels = $this->getDataModels(); 246 $dataModel = array_shift($dataModels); 247 $packagedDataModels = array(); 248 249 $platform = $this->getPlatformForTargetDatabase(); 250 251 foreach ($dataModel->getDatabases() as $db) { 252 foreach ($db->getTables() as $table) { 253 $package = $table->getPackage(); 254 if (!isset($packagedDataModels[$package])) { 255 $dbClone = $this->cloneDatabase($db); 256 $dbClone->setPackage($package); 257 $ad = new AppData($platform); 258 $ad->setName($dataModel->getName()); 259 $ad->addDatabase($dbClone); 260 $packagedDataModels[$package] = $ad; 261 } 262 $packagedDataModels[$package]->getDatabase($db->getName())->addTable($table); 263 } 264 } 265 } 266 267 return $packagedDataModels; 268 } 269 270 protected function cloneDatabase($db) { 271 272 $attributes = array ( 273 'name' => $db->getName(), 274 'baseClass' => $db->getBaseClass(), 275 'basePeer' => $db->getBasePeer(), 276 //'defaultPhpType' => $db->getDefaultPhpType(), 277 'defaultIdMethod' => $db->getDefaultIdMethod(), 278 'defaultPhpNamingMethod' => $db->getDefaultPhpNamingMethod(), 279 'defaultTranslateMethod' => $db->getDefaultTranslateMethod(), 280 //'heavyIndexing' => $db->getHeavyIndexing(), 281 ); 282 283 $clone = new Database(); 284 $clone->loadFromXML($attributes); 285 return $clone; 286 } 287 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |