[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * $Id: PropelDataDumpTask.php 352 2006-03-16 12:46:14Z gamr $ 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 'creole/Creole.php'; 24 25 /** 26 * Dumps the contenst of selected databases to XML data dump file. 27 * 28 * The generated XML files can have corresponding DTD files generated using the 29 * PropelDataDTDTask. The results of the data dump can be converted to SQL using 30 * the PropelDataSQLTask class. 31 * 32 * The database may be specified (via 'databaseName' attribute) if you only want to dump 33 * the contents of one database. Otherwise it is assumed that all databases described 34 * by datamodel schema file(s) will be dumped. 35 * 36 * @author Hans Lellelid <hans@xmpl.org> (Propel) 37 * @author Fedor Karpelevitch <fedor.karpelevitch@home.com> (Torque) 38 * @author Jason van Zyl <jvanzyl@zenplex.com> (Torque) 39 * @author Daniel Rall <dlr@finemaltcoding.com> (Torque) 40 * @version $Revision: 352 $ 41 * @package propel.phing 42 */ 43 class PropelDataDumpTask extends AbstractPropelDataModelTask { 44 45 /** 46 * Database name. 47 * The database name may be optionally specified in the XML if you only want 48 * to dump the contents of one database. 49 */ 50 private $databaseName; 51 52 /** 53 * Database URL used for Creole connection. 54 * This is a PEAR-compatible (loosely) DSN URL. 55 */ 56 private $databaseUrl; 57 58 /** 59 * Database driver used for Creole connection. 60 * This should normally be left blank so that default (Creole built-in) driver for database type is used. 61 */ 62 private $databaseDriver; 63 64 /** 65 * Database user used for Creole connection. 66 * @deprecated Put username in databaseUrl. 67 */ 68 private $databaseUser; 69 70 /** 71 * Database password used for Creole connection. 72 * @deprecated Put password in databaseUrl. 73 */ 74 private $databasePassword; 75 76 /** 77 * Properties file that maps a data XML file to a particular database. 78 * @var PhingFile 79 */ 80 private $datadbmap; 81 82 /** 83 * The database connection used to retrieve the data to dump. 84 * Needs to be public so that the TableInfo class can access it. 85 */ 86 public $conn; 87 88 /** 89 * The statement used to acquire the data to dump. 90 */ 91 private $stmt; 92 93 /** 94 * Set the file that maps between data XML files and databases. 95 * 96 * @param PhingFile $sqldbmap the db map 97 * @return void 98 */ 99 public function setDataDbMap(PhingFile $datadbmap) 100 { 101 $this->datadbmap = $datadbmap; 102 } 103 104 /** 105 * Get the file that maps between data XML files and databases. 106 * 107 * @return PhingFile $datadbmap. 108 */ 109 public function getDataDbMap() 110 { 111 return $this->datadbmap; 112 } 113 114 /** 115 * Get the database name to dump 116 * 117 * @return The DatabaseName value 118 */ 119 public function getDatabaseName() 120 { 121 return $this->databaseName; 122 } 123 124 /** 125 * Set the database name 126 * 127 * @param v The new DatabaseName value 128 */ 129 public function setDatabaseName($v) 130 { 131 $this->databaseName = $v; 132 } 133 134 /** 135 * Get the database url 136 * 137 * @return The DatabaseUrl value 138 */ 139 public function getDatabaseUrl() 140 { 141 return $this->databaseUrl; 142 } 143 144 /** 145 * Set the database url 146 * 147 * @param string $v The PEAR-compatible database DSN URL. 148 */ 149 public function setDatabaseUrl($v) 150 { 151 $this->databaseUrl = $v; 152 } 153 154 /** 155 * Get the database user 156 * 157 * @return string database user 158 * @deprecated 159 */ 160 public function getDatabaseUser() 161 { 162 return $this->databaseUser; 163 } 164 165 /** 166 * Set the database user 167 * 168 * @param string $v The new DatabaseUser value 169 * @deprecated Specify user in DSN URL. 170 */ 171 public function setDatabaseUser($v) 172 { 173 $this->databaseUser = $v; 174 } 175 176 /** 177 * Get the database password 178 * 179 * @return string database password 180 */ 181 public function getDatabasePassword() 182 { 183 return $this->databasePassword; 184 } 185 186 /** 187 * Set the database password 188 * 189 * @param string $v The new DatabasePassword value 190 * @deprecated Specify database password in DSN URL. 191 */ 192 public function setDatabasePassword($v) 193 { 194 $this->databasePassword = $v; 195 } 196 197 /** 198 * Get the database driver name 199 * 200 * @return string database driver name 201 */ 202 public function getDatabaseDriver() 203 { 204 return $this->databaseDriver; 205 } 206 207 /** 208 * Set the database driver name 209 * 210 * @param string $v The new DatabaseDriver value 211 */ 212 public function setDatabaseDriver($v) 213 { 214 $this->databaseDriver = $v; 215 } 216 217 /** 218 * Create the data XML -> database map. 219 * 220 * This is necessary because there is currently no other method of knowing which 221 * data XML files correspond to which database. This map allows us to convert multiple 222 * data XML files into SQL. 223 * 224 * @throws IOException - if unable to store properties 225 */ 226 private function createDataDbMap() 227 { 228 if ($this->getDataDbMap() === null) { 229 return; 230 } 231 232 // Produce the sql -> database map 233 $datadbmap = new Properties(); 234 235 // Check to see if the sqldbmap has already been created. 236 if ($this->getDataDbMap()->exists()) { 237 $datadbmap->load($this->getDataDbMap()); 238 } 239 240 foreach ($this->getDataModels() as $dataModel) { // there is really one 1 db per datamodel 241 foreach ($dataModel->getDatabases() as $database) { 242 243 // if database name is specified, then we only want to dump that one db. 244 if (empty($this->databaseName) || ($this->databaseName && $database->getName() == $this->databaseName)) { 245 $outFile = $this->getMappedFile($dataModel->getName()); 246 $datadbmap->setProperty($outFile->getName(), $database->getName()); 247 } 248 } 249 } 250 251 try { 252 $datadbmap->store($this->getDataDbMap(), "Data XML file -> Database map"); 253 } catch (IOException $e) { 254 throw new IOException("Unable to store properties: ". $e->getMessage()); 255 } 256 } 257 258 /** 259 * Iterates through each datamodel/database, dumps the contents of all tables and creates a DOM XML doc. 260 * 261 * @return void 262 * @throws BuildException 263 */ 264 public function main() 265 { 266 $this->validate(); 267 268 $buf = "Database settings:\n" 269 . " driver: " . ($this->databaseDriver ? $this->databaseDriver : "(default)" ). "\n" 270 . " URL: " . $this->databaseUrl . "\n" 271 . ($this->databaseUser ? " user: " . $this->databaseUser . "\n" : "") // deprecated 272 . ($this->databasePassword ? " password: " . $this->databasePassword . "\n" : ""); // deprecated 273 274 $this->log($buf, PROJECT_MSG_VERBOSE); 275 276 // 1) First create the Data XML -> database name map. 277 $this->createDataDbMap(); 278 279 // 2) Now go create the XML files from teh database(s) 280 foreach ($this->getDataModels() as $dataModel) { // there is really one 1 db per datamodel 281 foreach ($dataModel->getDatabases() as $database) { 282 283 // if database name is specified, then we only want to dump that one db. 284 if (empty($this->databaseName) || ($this->databaseName && $database->getName() == $this->databaseName)) { 285 286 $outFile = $this->getMappedFile($dataModel->getName()); 287 288 $this->log("Dumping data to XML for database: " . $database->getName()); 289 $this->log("Writing to XML file: " . $outFile->getName()); 290 291 try { 292 293 $url = str_replace("@DB@", $database->getName(), $this->databaseUrl); 294 295 $buf = "Database settings:\n" 296 . " driver: " . ($this->databaseDriver ? $this->databaseDriver : "(default)" ). "\n" 297 . " URL: " . $url . "\n" 298 . ($this->databaseUser ? " user: " . $this->databaseUser . "\n" : "") 299 . ($this->databasePassword ? " password: " . $this->databasePassword . "\n" : ""); 300 301 $this->log($buf, PROJECT_MSG_VERBOSE); 302 303 $dsn = Creole::parseDSN($url); 304 305 // deprecated, but here for BC 306 if ($this->databaseUser) { 307 $dsn['username'] = $this->databaseUser; 308 } 309 310 if ($this->databasePassword) { 311 $dsn['password'] = $this->databasePassword; 312 } 313 314 if ($this->databaseName) { 315 $dsn['database'] = $this->databaseName; 316 } 317 318 if ($this->databaseDriver) { 319 Creole::registerDriver($dsn['phptype'], $this->databaseDriver); 320 } 321 322 $this->conn = Creole::getConnection($dsn); 323 324 $doc = $this->createXMLDoc($database); 325 $doc->save($outFile->getAbsolutePath()); 326 327 } catch (SQLException $se) { 328 $this->log("SQLException while connecting to DB: ". $se->getMessage(), PROJECT_MSG_ERR); 329 throw new BuildException($se); 330 } 331 } // if databaseName && database->getName == databaseName 332 } // foreach database 333 } // foreach datamodel 334 } 335 336 /** 337 * Gets ResultSet of query to fetch all data from a table. 338 * @param string $tableName 339 * @return ResultSet 340 */ 341 private function getTableDataRS($tableName) { 342 // Set Statement object in associated PropelDataDump 343 // instance. 344 return $this->conn->createStatement()->executeQuery("SELECT * FROM " . $this->getPlatformForTargetDatabase()->quoteIdentifier ( $tableName ) ); 345 } 346 347 /** 348 * Creates a DOM document containing data for specified database. 349 * @param Database $database 350 * @return DOMDocument 351 */ 352 private function createXMLDoc(Database $database) { 353 354 $doc = new DOMDocument('1.0', 'utf-8'); 355 $doc->formatOutput = true; // pretty printing 356 $doc->appendChild($doc->createComment("Created by data/dump/Control.tpl template.")); 357 358 $dsNode = $doc->createElement("dataset"); 359 $dsNode->setAttribute("name", "all"); 360 $doc->appendChild($dsNode); 361 362 $this->log("Building DOM tree containing data from tables:"); 363 364 foreach ($database->getTables() as $tbl) { 365 $this->log("\t+ " . $tbl->getName()); 366 $rs = $this->getTableDataRS($tbl->getName()); 367 while ($rs->next()) { 368 $rowNode = $doc->createElement($tbl->getPhpName()); 369 foreach ($tbl->getColumns() as $col) { 370 $cval = $rs->get($col->getName()); 371 if ($cval !== null) { 372 $rowNode->setAttribute($col->getPhpName(), iconv($this->dbEncoding, 'utf-8', $cval)); 373 } 374 } 375 $dsNode->appendChild($rowNode); 376 unset($rowNode); 377 } 378 $rs->close(); 379 } 380 381 return $doc; 382 } 383 }
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 |