PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/gulliver/thirdparty/propel-generator/classes/propel/phing/PropelDataDumpTask.php

https://bitbucket.org/ferOnti/processmaker
PHP | 383 lines | 160 code | 56 blank | 167 comment | 18 complexity | 9cb2a998c0f5e03568a24d890b35f7c7 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: PropelDataDumpTask.php 536 2007-01-10 14:30:38Z heltem $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://propel.phpdb.org>.
  20. */
  21. include_once 'creole/Creole.php';
  22. /**
  23. * Dumps the contenst of selected databases to XML data dump file.
  24. *
  25. * The generated XML files can have corresponding DTD files generated using the
  26. * PropelDataDTDTask. The results of the data dump can be converted to SQL using
  27. * the PropelDataSQLTask class.
  28. *
  29. * The database may be specified (via 'databaseName' attribute) if you only want to dump
  30. * the contents of one database. Otherwise it is assumed that all databases described
  31. * by datamodel schema file(s) will be dumped.
  32. *
  33. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  34. * @author Fedor Karpelevitch <fedor.karpelevitch@home.com> (Torque)
  35. * @author Jason van Zyl <jvanzyl@zenplex.com> (Torque)
  36. * @author Daniel Rall <dlr@finemaltcoding.com> (Torque)
  37. * @version $Revision: 536 $
  38. * @package propel.phing
  39. */
  40. class PropelDataDumpTask extends AbstractPropelDataModelTask {
  41. /**
  42. * Database name.
  43. * The database name may be optionally specified in the XML if you only want
  44. * to dump the contents of one database.
  45. */
  46. private $databaseName;
  47. /**
  48. * Database URL used for Creole connection.
  49. * This is a PEAR-compatible (loosely) DSN URL.
  50. */
  51. private $databaseUrl;
  52. /**
  53. * Database driver used for Creole connection.
  54. * This should normally be left blank so that default (Creole built-in) driver for database type is used.
  55. */
  56. private $databaseDriver;
  57. /**
  58. * Database user used for Creole connection.
  59. * @deprecated Put username in databaseUrl.
  60. */
  61. private $databaseUser;
  62. /**
  63. * Database password used for Creole connection.
  64. * @deprecated Put password in databaseUrl.
  65. */
  66. private $databasePassword;
  67. /**
  68. * Properties file that maps a data XML file to a particular database.
  69. * @var PhingFile
  70. */
  71. private $datadbmap;
  72. /**
  73. * The database connection used to retrieve the data to dump.
  74. * Needs to be public so that the TableInfo class can access it.
  75. */
  76. public $conn;
  77. /**
  78. * The statement used to acquire the data to dump.
  79. */
  80. private $stmt;
  81. /**
  82. * Set the file that maps between data XML files and databases.
  83. *
  84. * @param PhingFile $sqldbmap the db map
  85. * @return void
  86. */
  87. public function setDataDbMap(PhingFile $datadbmap)
  88. {
  89. $this->datadbmap = $datadbmap;
  90. }
  91. /**
  92. * Get the file that maps between data XML files and databases.
  93. *
  94. * @return PhingFile $datadbmap.
  95. */
  96. public function getDataDbMap()
  97. {
  98. return $this->datadbmap;
  99. }
  100. /**
  101. * Get the database name to dump
  102. *
  103. * @return The DatabaseName value
  104. */
  105. public function getDatabaseName()
  106. {
  107. return $this->databaseName;
  108. }
  109. /**
  110. * Set the database name
  111. *
  112. * @param v The new DatabaseName value
  113. */
  114. public function setDatabaseName($v)
  115. {
  116. $this->databaseName = $v;
  117. }
  118. /**
  119. * Get the database url
  120. *
  121. * @return The DatabaseUrl value
  122. */
  123. public function getDatabaseUrl()
  124. {
  125. return $this->databaseUrl;
  126. }
  127. /**
  128. * Set the database url
  129. *
  130. * @param string $v The PEAR-compatible database DSN URL.
  131. */
  132. public function setDatabaseUrl($v)
  133. {
  134. $this->databaseUrl = $v;
  135. }
  136. /**
  137. * Get the database user
  138. *
  139. * @return string database user
  140. * @deprecated
  141. */
  142. public function getDatabaseUser()
  143. {
  144. return $this->databaseUser;
  145. }
  146. /**
  147. * Set the database user
  148. *
  149. * @param string $v The new DatabaseUser value
  150. * @deprecated Specify user in DSN URL.
  151. */
  152. public function setDatabaseUser($v)
  153. {
  154. $this->databaseUser = $v;
  155. }
  156. /**
  157. * Get the database password
  158. *
  159. * @return string database password
  160. */
  161. public function getDatabasePassword()
  162. {
  163. return $this->databasePassword;
  164. }
  165. /**
  166. * Set the database password
  167. *
  168. * @param string $v The new DatabasePassword value
  169. * @deprecated Specify database password in DSN URL.
  170. */
  171. public function setDatabasePassword($v)
  172. {
  173. $this->databasePassword = $v;
  174. }
  175. /**
  176. * Get the database driver name
  177. *
  178. * @return string database driver name
  179. */
  180. public function getDatabaseDriver()
  181. {
  182. return $this->databaseDriver;
  183. }
  184. /**
  185. * Set the database driver name
  186. *
  187. * @param string $v The new DatabaseDriver value
  188. */
  189. public function setDatabaseDriver($v)
  190. {
  191. $this->databaseDriver = $v;
  192. }
  193. /**
  194. * Create the data XML -> database map.
  195. *
  196. * This is necessary because there is currently no other method of knowing which
  197. * data XML files correspond to which database. This map allows us to convert multiple
  198. * data XML files into SQL.
  199. *
  200. * @throws IOException - if unable to store properties
  201. */
  202. private function createDataDbMap()
  203. {
  204. if ($this->getDataDbMap() === null) {
  205. return;
  206. }
  207. // Produce the sql -> database map
  208. $datadbmap = new Properties();
  209. // Check to see if the sqldbmap has already been created.
  210. if ($this->getDataDbMap()->exists()) {
  211. $datadbmap->load($this->getDataDbMap());
  212. }
  213. foreach ($this->getDataModels() as $dataModel) { // there is really one 1 db per datamodel
  214. foreach ($dataModel->getDatabases() as $database) {
  215. // if database name is specified, then we only want to dump that one db.
  216. if (empty($this->databaseName) || ($this->databaseName && $database->getName() == $this->databaseName)) {
  217. $outFile = $this->getMappedFile($dataModel->getName());
  218. $datadbmap->setProperty($outFile->getName(), $database->getName());
  219. }
  220. }
  221. }
  222. try {
  223. $datadbmap->store($this->getDataDbMap(), "Data XML file -> Database map");
  224. } catch (IOException $e) {
  225. throw new IOException("Unable to store properties: ". $e->getMessage());
  226. }
  227. }
  228. /**
  229. * Iterates through each datamodel/database, dumps the contents of all tables and creates a DOM XML doc.
  230. *
  231. * @return void
  232. * @throws BuildException
  233. */
  234. public function main()
  235. {
  236. $this->validate();
  237. $buf = "Database settings:\n"
  238. . " driver: " . ($this->databaseDriver ? $this->databaseDriver : "(default)" ). "\n"
  239. . " URL: " . $this->databaseUrl . "\n"
  240. . ($this->databaseUser ? " user: " . $this->databaseUser . "\n" : "") // deprecated
  241. . ($this->databasePassword ? " password: " . $this->databasePassword . "\n" : ""); // deprecated
  242. $this->log($buf, PROJECT_MSG_VERBOSE);
  243. // 1) First create the Data XML -> database name map.
  244. $this->createDataDbMap();
  245. // 2) Now go create the XML files from teh database(s)
  246. foreach ($this->getDataModels() as $dataModel) { // there is really one 1 db per datamodel
  247. foreach ($dataModel->getDatabases() as $database) {
  248. // if database name is specified, then we only want to dump that one db.
  249. if (empty($this->databaseName) || ($this->databaseName && $database->getName() == $this->databaseName)) {
  250. $outFile = $this->getMappedFile($dataModel->getName());
  251. $this->log("Dumping data to XML for database: " . $database->getName());
  252. $this->log("Writing to XML file: " . $outFile->getName());
  253. try {
  254. $url = str_replace("@DB@", $database->getName(), $this->databaseUrl);
  255. $buf = "Database settings:\n"
  256. . " driver: " . ($this->databaseDriver ? $this->databaseDriver : "(default)" ). "\n"
  257. . " URL: " . $url . "\n"
  258. . ($this->databaseUser ? " user: " . $this->databaseUser . "\n" : "")
  259. . ($this->databasePassword ? " password: " . $this->databasePassword . "\n" : "");
  260. $this->log($buf, PROJECT_MSG_VERBOSE);
  261. $dsn = Creole::parseDSN($url);
  262. // deprecated, but here for BC
  263. if ($this->databaseUser) {
  264. $dsn['username'] = $this->databaseUser;
  265. }
  266. if ($this->databasePassword) {
  267. $dsn['password'] = $this->databasePassword;
  268. }
  269. if ($this->databaseName) {
  270. $dsn['database'] = $this->databaseName;
  271. }
  272. if ($this->databaseDriver) {
  273. Creole::registerDriver($dsn['phptype'], $this->databaseDriver);
  274. }
  275. $this->conn = Creole::getConnection($dsn);
  276. $doc = $this->createXMLDoc($database);
  277. $doc->save($outFile->getAbsolutePath());
  278. } catch (SQLException $se) {
  279. $this->log("SQLException while connecting to DB: ". $se->getMessage(), PROJECT_MSG_ERR);
  280. throw new BuildException($se);
  281. }
  282. } // if databaseName && database->getName == databaseName
  283. } // foreach database
  284. } // foreach datamodel
  285. }
  286. /**
  287. * Gets ResultSet of query to fetch all data from a table.
  288. * @param string $tableName
  289. * @return ResultSet
  290. */
  291. private function getTableDataRS($tableName) {
  292. // Set Statement object in associated PropelDataDump
  293. // instance.
  294. return $this->conn->createStatement()->executeQuery("SELECT * FROM " . $this->getPlatformForTargetDatabase()->quoteIdentifier ( $tableName ) );
  295. }
  296. /**
  297. * Creates a DOM document containing data for specified database.
  298. * @param Database $database
  299. * @return DOMDocument
  300. */
  301. private function createXMLDoc(DatabasePropel $database) {
  302. $doc = new DOMDocument('1.0', 'utf-8');
  303. $doc->formatOutput = true; // pretty printing
  304. $doc->appendChild($doc->createComment("Created by data/dump/Control.tpl template."));
  305. $dsNode = $doc->createElement("dataset");
  306. $dsNode->setAttribute("name", "all");
  307. $doc->appendChild($dsNode);
  308. $this->log("Building DOM tree containing data from tables:");
  309. foreach ($database->getTables() as $tbl) {
  310. $this->log("\t+ " . $tbl->getName());
  311. $rs = $this->getTableDataRS($tbl->getName());
  312. while ($rs->next()) {
  313. $rowNode = $doc->createElement($tbl->getPhpName());
  314. foreach ($tbl->getColumns() as $col) {
  315. $cval = $rs->get($col->getName());
  316. if ($cval !== null) {
  317. $rowNode->setAttribute($col->getPhpName(), iconv($this->dbEncoding, 'utf-8', $cval));
  318. }
  319. }
  320. $dsNode->appendChild($rowNode);
  321. unset($rowNode);
  322. }
  323. $rs->close();
  324. }
  325. return $doc;
  326. }
  327. }