[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * $Id: PropelDataSQLTask.php 265 2005-11-07 21:05:18Z hans $ 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 include_once 'propel/engine/database/model/Database.php'; 25 include_once 'propel/engine/database/transform/XmlToAppData.php'; 26 include_once 'propel/engine/database/transform/XmlToData.php'; 27 28 /** 29 * Task that transforms XML datadump files into files containing SQL INSERT statements. 30 * 31 * @author Hans Lellelid <hans@xmpl.org> (Propel) 32 * @author Jason van Zyl <jvanzyl@periapt.com> (Torque) 33 * @author John McNally <jmcnally@collab.net> (Torque) 34 * @author Fedor Karpelevitch <fedor.karpelevitch@home.com> (Torque) 35 * @version $Revision: 265 $ 36 * @package propel.phing 37 */ 38 class PropelDataSQLTask extends AbstractPropelDataModelTask { 39 40 /** 41 * Properties file that maps an SQL file to a particular database. 42 * @var PhingFile 43 */ 44 private $sqldbmap; 45 46 /** 47 * Properties file that maps a data XML file to a particular database. 48 * @var PhingFile 49 */ 50 private $datadbmap; 51 52 /** 53 * The base directory in which to find data XML files. 54 * @var PhingFile 55 */ 56 private $srcDir; 57 58 /** 59 * Set the file that maps between SQL files and databases. 60 * 61 * @param PhingFile $sqldbmap the sql -> db map. 62 * @return void 63 */ 64 public function setSqlDbMap(PhingFile $sqldbmap) 65 { 66 $this->sqldbmap = $sqldbmap; 67 } 68 69 /** 70 * Get the file that maps between SQL files and databases. 71 * 72 * @return PhingFile sqldbmap. 73 */ 74 public function getSqlDbMap() 75 { 76 return $this->sqldbmap; 77 } 78 79 /** 80 * Set the file that maps between data XML files and databases. 81 * 82 * @param PhingFile $sqldbmap the db map 83 * @return void 84 */ 85 public function setDataDbMap(PhingFile $datadbmap) 86 { 87 $this->datadbmap = $datadbmap; 88 } 89 90 /** 91 * Get the file that maps between data XML files and databases. 92 * 93 * @return PhingFile $datadbmap. 94 */ 95 public function getDataDbMap() 96 { 97 return $this->datadbmap; 98 } 99 100 /** 101 * Set the src directory for the data xml files listed in the datadbmap file. 102 * @param PhingFile $srcDir data xml source directory 103 */ 104 public function setSrcDir(PhingFile $srcDir) 105 { 106 $this->srcDir = $srcDir; 107 } 108 109 /** 110 * Get the src directory for the data xml files listed in the datadbmap file. 111 * 112 * @return PhingFile data xml source directory 113 */ 114 public function getSrcDir() 115 { 116 return $this->srcDir; 117 } 118 119 /** 120 * Search through all data models looking for matching database. 121 * @return Database or NULL if none found. 122 */ 123 private function getDatabase($name) 124 { 125 foreach($this->getDataModels() as $dm) { 126 foreach($dm->getDatabases() as $db) { 127 if ($db->getName() == $name) { 128 return $db; 129 } 130 } 131 } 132 } 133 134 /** 135 * Main method parses the XML files and creates SQL files. 136 * 137 * @return void 138 * @throws Exception If there is an error parsing the data xml. 139 */ 140 public function main() 141 { 142 $this->validate(); 143 144 $targetDatabase = $this->getTargetDatabase(); 145 146 $platform = $this->getPlatformForTargetDatabase(); 147 148 // Load the Data XML -> DB Name properties 149 $map = new Properties(); 150 try { 151 $map->load($this->getDataDbMap()); 152 } catch (IOException $ioe) { 153 throw new BuildException("Cannot open and process the datadbmap!", $ioe); 154 } 155 156 DataModelBuilder::setBuildProperties($this->getPropelProperties()); 157 158 // Parse each file in teh data -> db map 159 160 foreach($map->keys() as $dataXMLFilename) { 161 162 $dataXMLFile = new PhingFile($this->srcDir, $dataXMLFilename); 163 164 // if file exists then proceed 165 if ($dataXMLFile->exists()) { 166 167 $dbname = $map->get($dataXMLFilename); 168 169 $db = $this->getDatabase($dbname); 170 171 if (!$db) { 172 throw new BuildException("Cannot find instantiated Database for name '$dbname' from datadbmap file."); 173 } 174 175 $db->setPlatform($platform); 176 177 $outFile = $this->getMappedFile($dataXMLFilename); 178 179 $this->log("Creating SQL from XML data dump file: " . $dataXMLFile->getAbsolutePath()); 180 181 try { 182 $dataXmlParser = new XmlToData($db, $this->dbEncoding); 183 $data = $dataXmlParser->parseFile($dataXMLFile->getAbsolutePath()); 184 } catch (Exception $e) { 185 throw new Exception("Exception parsing data XML: " . $e->getMessage()); 186 } 187 188 $fp = fopen($outFile->getAbsolutePath(), 'w'); 189 190 $currTable = null; 191 foreach ($data as $dataRow) { 192 if ($currTable !== $dataRow->getTable()) { 193 $currTable = $dataRow->getTable(); 194 $builder = DataModelBuilder::builderFactory($currTable, 'datasql'); 195 } 196 $sql = $builder->buildRowSql($dataRow); 197 fwrite($fp, $sql); 198 } 199 200 fclose($fp); 201 202 // Place the generated SQL file(s) 203 $p = new Properties(); 204 if ($this->getSqlDbMap()->exists()) { 205 $p->load($this->getSqlDbMap()); 206 } 207 208 $p->setProperty($outFile->getName(), $db->getName()); 209 $p->store($this->getSqlDbMap(), "Sqlfile -> Database map"); 210 211 } else { 212 $this->log("File '" . $dataXMLFile->getAbsolutePath() 213 . "' in datadbmap does not exist, so skipping it.", PROJECT_MSG_WARN); 214 } 215 216 } // foreach data xml file 217 218 } // main() 219 }
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 |