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

/CC_Database.php

https://github.com/coverall/N2O
PHP | 1170 lines | 515 code | 252 blank | 403 comment | 54 complexity | bcbeaa8db5edc42444959cbd220f0e65 MD5 | raw file
  1. <?php
  2. // $Id: CC_Database.php,v 1.71 2008/01/09 00:03:30 patrick Exp $
  3. //=======================================================================
  4. // CLASS: CC_Database
  5. //=======================================================================
  6. /** This is N2O's database manager. It should not be constructed manually. It uses PEAR's DB library.
  7. *
  8. * @package CC_Managers
  9. * @access public
  10. * @author The Crew <n2o@coverallcrew.com>
  11. * @copyright Copyright &copy; 2003, Coverall Crew
  12. * @see http://pear.php.net/manual/en/package.database.php
  13. *
  14. */
  15. define('DB_MYSQL', 4200);
  16. define('DB_POSTGRES', 4201);
  17. require_once('DB.php');
  18. class CC_Database
  19. {
  20. /**
  21. * The password used to encode data in the database.
  22. *
  23. * @var string $_encodePassword
  24. * @access private
  25. */
  26. var $_encodePassword;
  27. /**
  28. * The type of database used in this application. Types are defined at the top of this file.
  29. *
  30. * @var int $_databaseType
  31. * @access private
  32. */
  33. var $_databaseType;
  34. /**
  35. * The PEAR DB object
  36. *
  37. * @var string $_db
  38. * @access private
  39. */
  40. var $_db; // PEAR DB object
  41. /**
  42. * The PEAR DB datasource connection string.
  43. *
  44. * @var string $_datasource
  45. * @access private
  46. */
  47. var $_datasource;
  48. //-------------------------------------------------------------------
  49. // CONSTRUCTOR: CC_Database
  50. //-------------------------------------------------------------------
  51. /**
  52. * The CC_Database constructor initializes the object with all the pertinent parameters needed for accessing the database.
  53. *
  54. * @access private
  55. * @param string $databaseHost The database hostname.
  56. * @param string $databaseName The name of the database.
  57. * @param string $databaseUsername The username used to access the database.
  58. * @param string $databasePassword The password used to access the database.
  59. * @param string $encodePassword The password used to encode data in the database.
  60. * @param string $databaseType The type of the database which defaults to DB_MYSQL.
  61. */
  62. function CC_Database($databaseHost, $databaseName, $databaseUsername, $databasePassword, $encodePassword, $databaseType = DB_MYSQL)
  63. {
  64. $this->setEncodePassword($encodePassword);
  65. $this->_databaseType = ($databaseType ? $databaseType : DB_MYSQL);
  66. switch ($this->_databaseType)
  67. {
  68. case DB_POSTGRES:
  69. $databaseTypeString = 'pgsql';
  70. break;
  71. case DB_MYSQL:
  72. default:
  73. $databaseTypeString = 'mysql';
  74. break;
  75. }
  76. $this->_datasource = strSlide13($databaseTypeString . '://' . $databaseUsername . ':' . $databasePassword . '@' . $databaseHost . '/' . $databaseName);
  77. }
  78. //-------------------------------------------------------------------
  79. // METHOD: openDatabase
  80. //-------------------------------------------------------------------
  81. /**
  82. * This method connects to and opens the database so that actions can be called on it.
  83. *
  84. * @access private
  85. */
  86. function openDatabase($query)
  87. {
  88. global $ccDatabaseAlertEmail;
  89. if (!$this->_db)
  90. {
  91. $this->_db = DB::connect(strSlide13($this->_datasource));
  92. }
  93. if (DB::isError($this->_db))
  94. {
  95. switch ($this->_databaseType)
  96. {
  97. case 4200:
  98. {
  99. $dbType = 'MySQL';
  100. }
  101. break;
  102. case 4201:
  103. {
  104. $dbType = 'Postgres';
  105. }
  106. break;
  107. default:
  108. {
  109. $dbType = 'Unknown';
  110. }
  111. }
  112. $errorMessage = $this->_db->getMessage();
  113. trigger_error($errorMessage, E_USER_WARNING);
  114. if (!isset($ccDatabaseAlertEmail))
  115. {
  116. $ccDatabaseAlertEmail = 'support@coverallcrew.com';
  117. }
  118. $port = '';
  119. if ($_SERVER['SERVER_PORT'] == 443)
  120. {
  121. $addressPrefix = 'https://';
  122. }
  123. else
  124. {
  125. $addressPrefix = 'http://';
  126. if ($_SERVER['SERVER_PORT'] != 80)
  127. {
  128. $port = ':' . $_SERVER['SERVER_PORT'];
  129. }
  130. }
  131. $address = $addressPrefix. $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
  132. $dsn = strSlide13($this->_datasource);
  133. $dsn = preg_replace('/([a-z]+):\/\/([a-z_]+):[^@]+@(.+)/', '\1://\2:xxxxxx@\3', $dsn);
  134. $stack = getStackTrace();
  135. $email = <<<EOF
  136. Error: $errorMessage
  137. Address: $address
  138. DSN: $dsn
  139. User IP: {$_SERVER['REMOTE_ADDR']}
  140. Query: $query
  141. Stack: $stack
  142. EOF;
  143. trigger_error('Could not connect to ' . preg_replace('/([a-z]+):\/\/([a-z_]+):[a-z0-9_]+@([a-z\.-]+)\/([a-z0-9]+)/', '\1://\2:xxxxxx@\3/\4', strSlide13($this->_datasource)), E_USER_WARNING);
  144. //dbAlertEmail is set in CC_Config
  145. cc_mail($ccDatabaseAlertEmail, (isset($ccDatabaseAlertPrefix) ? $ccDatabaseAlertPrefix . ' ' : '') . 'Database Error!', $email, array('From' => 'cc_database_bot@coverallcrew.com'));
  146. }
  147. return $this->_db;
  148. }
  149. //-------------------------------------------------------------------
  150. // METHOD: closeDatabase
  151. //-------------------------------------------------------------------
  152. /**
  153. * This method closes the database. Called after an openDatabase call.
  154. *
  155. * @access private
  156. * @return bool Whether or not the database was closed successfully.
  157. * @see openDatabase()
  158. */
  159. function closeDatabase()
  160. {
  161. $this->_db->disconnect();
  162. unset($this->_db);
  163. return true;
  164. }
  165. //-------------------------------------------------------------------
  166. // METHOD: isPostgres
  167. //-------------------------------------------------------------------
  168. /**
  169. * This method returns true if the current database is Postgres.
  170. *
  171. * @access public
  172. * @return boolean Is this a Postgres database?
  173. */
  174. function isPostgres()
  175. {
  176. return ($this->_databaseType == DB_POSTGRES);
  177. }
  178. //-------------------------------------------------------------------
  179. // METHOD: isMysql
  180. //-------------------------------------------------------------------
  181. /**
  182. * This method returns true if the current database is MySQL.
  183. *
  184. * @access public
  185. * @return boolean Is this a MySQL database?
  186. */
  187. function isMysql()
  188. {
  189. return ($this->_databaseType == DB_MYSQL);
  190. }
  191. //-------------------------------------------------------------------
  192. // METHOD: doInsert
  193. //-------------------------------------------------------------------
  194. /**
  195. * This method executes the given insert query on the database. The database is openeed and closed automatically.
  196. *
  197. * @access public
  198. * @param string $query The insert query to execute.
  199. * @return int The id of the newly inserted record.
  200. */
  201. function doInsert($query, $update = false, $function = __FUNCTION__)
  202. {
  203. global $application;
  204. if (PEAR::isError($this->openDatabase($query)))
  205. {
  206. return $this->_db;
  207. }
  208. else
  209. {
  210. $this->begin();
  211. $result = $this->_db->query($query);
  212. if (DB::isError($result))
  213. {
  214. trigger_error('CC_Database::' . $function . '() ' . $result->getMessage() . ' Query: ' . $query, E_USER_WARNING);
  215. $this->rollback();
  216. return $result;
  217. }
  218. if ($update)
  219. {
  220. $return = $this->_db->affectedRows();
  221. }
  222. else
  223. {
  224. // somehow grab the tablename from the query.
  225. $queryArray = preg_split('/ /', $query);
  226. $return = $this->getLastInsertId($queryArray[2]);
  227. }
  228. $this->commit();
  229. return $return;
  230. }
  231. }
  232. //-------------------------------------------------------------------
  233. // METHOD: doBatchInsert
  234. //-------------------------------------------------------------------
  235. /**
  236. * This method executes a batch of insert queries on the database. The queries will all be part of a transaction, and should any query fail, the entire transaction will be rolled back (providing the underlying DB supports it).
  237. *
  238. * @access public
  239. * @param array $queryArray Numeric array of queries.
  240. * @return array The ids of the newly inserted record.
  241. */
  242. function doBatchInsert($queryArray, $update = false, $function = __FUNCTION__)
  243. {
  244. global $application;
  245. $return = array();
  246. if (PEAR::isError($this->openDatabase($query)))
  247. {
  248. return $this->_db;
  249. }
  250. else
  251. {
  252. $this->begin();
  253. $size = sizeof($queryArray);
  254. for ($i = 0; $i < $size; $i++)
  255. {
  256. $result = $this->_db->query($queryArray[$i]);
  257. if (DB::isError($result))
  258. {
  259. trigger_error('CC_Database::' . $function . '() ' . $result->getMessage() . ' Query: ' . $queryArray[$i], E_USER_WARNING);
  260. $this->rollback();
  261. return $result;
  262. }
  263. else
  264. {
  265. if ($update)
  266. {
  267. $return[$i] = $this->_db->affectedRows();
  268. }
  269. else
  270. {
  271. // somehow grab the tablename from the query.
  272. $splitArray = preg_split('/ /', $queryArray[$i]);
  273. $return[$i] = $this->getLastInsertId($splitArray[2]);
  274. }
  275. }
  276. }
  277. $this->commit();
  278. return $return;
  279. }
  280. }
  281. //-------------------------------------------------------------------
  282. // METHOD: doBatchInsert
  283. //-------------------------------------------------------------------
  284. /**
  285. * This method executes a batch of update queries on the database. The queries will all be part of a transaction, and should any query fail, the entire transaction will be rolled back (providing the underlying DB supports it).
  286. *
  287. * @access public
  288. * @param array $queryArray Numeric array of queries.
  289. * @return array The number of updated rows per query.
  290. */
  291. function doBatchUpdate($queryArray)
  292. {
  293. return $this->doBatchInsert($queryArray, true, __FUNCTION__);
  294. }
  295. //-------------------------------------------------------------------
  296. // METHOD: getLastInsertId
  297. //-------------------------------------------------------------------
  298. /**
  299. * This method retrieves the id of the latest record inserted into the database.
  300. *
  301. * @access private
  302. * @param string $tableName The table to check.
  303. * @return int The id of the newly inserted record.
  304. */
  305. function getLastInsertId($tableName, $dbAlreadyOpen = false)
  306. {
  307. global $application;
  308. switch ($this->_databaseType)
  309. {
  310. case DB_POSTGRES:
  311. $lastInsertIdQuery = 'SELECT last_value FROM ' . strtolower($tableName) . '_id_seq';
  312. break;
  313. case DB_MYSQL:
  314. default:
  315. $lastInsertIdQuery = 'SELECT LAST_INSERT_ID()';
  316. break;
  317. }
  318. $result = $this->_db->getOne($lastInsertIdQuery);
  319. if (DB::isError($result))
  320. {
  321. trigger_error('CC_Database::getLastInsertId() ' . $result->getMessage() . ' Query: ' . $lastInsertIdQuery, E_USER_WARNING);
  322. }
  323. return $result;
  324. }
  325. //-------------------------------------------------------------------
  326. // METHOD: doOrderedInsert
  327. //-------------------------------------------------------------------
  328. /**
  329. * This method executes the given insert query on the database. Records are updated as necessary to maintain the records' order.
  330. *
  331. * @access public
  332. * @param string $query The insert query to execute.
  333. * @param string $tableName The name of the table we are inserting into.
  334. * @param int $sortId The position where the record should be inserted (which is also the value of the SORT_ID column).
  335. * @return int The id of the newly inserted record.
  336. */
  337. function doOrderedInsert($query, $tableName, $sortId)
  338. {
  339. global $application;
  340. if (PEAR::isError($this->openDatabase($query)))
  341. {
  342. return $this->_db;
  343. }
  344. else
  345. {
  346. $recordId = $this->doInsert($query);
  347. if (DB::isError($recordId))
  348. {
  349. trigger_error('CC_Database::doOrderedInsert() ' . $recordId->getMessage() . ' Query: ' . $query, E_USER_ERROR);
  350. }
  351. if ($sortId != 0)
  352. {
  353. $currentSortId = $sortId;
  354. while (true)
  355. {
  356. $query = 'update ' . $tableName . ' set SORT_ID="' . ($currentSortId + 1) . '" where SORT_ID="' . $currentSortId . '"';
  357. $result = $this->doUpdate($query);
  358. if (DB::isError($result))
  359. {
  360. break;
  361. }
  362. $currentSortId++;
  363. }
  364. }
  365. else //add at the end of the sort list
  366. {
  367. // get the number of records
  368. $numRecords = $this->doCount($tableName);
  369. $query = 'update ' . $tableName . ' set SORT_ID="' . $numRecords . '" where SORT_ID="0"';
  370. trigger_error('ordered update query is ' . $query, E_USER_WARNING);
  371. $this->doUpdate($query);
  372. }
  373. return $recordId;
  374. }
  375. }
  376. //-------------------------------------------------------------------
  377. // METHOD: doQuery
  378. //-------------------------------------------------------------------
  379. /**
  380. * This method executes the given query on the database.
  381. *
  382. * @access public
  383. * @param string $query The query to execute.
  384. * @return bool Whether or not the update was successful.
  385. */
  386. function doQuery($query)
  387. {
  388. return $this->doUpdate($query);
  389. }
  390. //-------------------------------------------------------------------
  391. // METHOD: doUpdate
  392. //-------------------------------------------------------------------
  393. /**
  394. * This method executes the given update query on the database.
  395. *
  396. * @access public
  397. * @param string $query The update query to execute.
  398. * @return integer The number of affected rows.
  399. */
  400. function doUpdate($query)
  401. {
  402. return $this->doInsert($query, true, __FUNCTION__);
  403. }
  404. //-------------------------------------------------------------------
  405. // METHOD: doDelete
  406. //-------------------------------------------------------------------
  407. /**
  408. * This method executes the given delete query on the database.
  409. *
  410. * @access public
  411. * @param string $query The delete query to execute.
  412. */
  413. function doDelete($query)
  414. {
  415. global $application;
  416. if (PEAR::isError($this->openDatabase($query)))
  417. {
  418. return $this->_db;
  419. }
  420. else
  421. {
  422. $result = $this->_db->query($query);
  423. if (DB::isError($result))
  424. {
  425. trigger_error('CC_Database::doDelete() ' . $result->getMessage() . ' Query: ' . $query, E_USER_WARNING);
  426. }
  427. return $result;
  428. }
  429. }
  430. //-------------------------------------------------------------------
  431. // METHOD: doOrderedDelete
  432. //-------------------------------------------------------------------
  433. /**
  434. * This method executes the given insert query on the database. Records are updated as necessary to maintain the records' order.
  435. *
  436. * @access public
  437. * @param string $tableName The name of the table we are inserting into.
  438. * @param int $recordId The id of the record to delete.
  439. * @param int $sortId The position of the record to delete (which is also the value of its SORT_ID column).
  440. */
  441. function doOrderedDelete($tableName, $recordId, $sortId)
  442. {
  443. global $application;
  444. $query = 'delete from ' . $tableName . " where ID='" . $recordId . "'";
  445. if (PEAR::isError($this->openDatabase($query)))
  446. {
  447. return $this->_db;
  448. }
  449. else
  450. {
  451. $result = $this->_db->query($query);
  452. if (DB::isError($result))
  453. {
  454. trigger_error('CC_Database::doOrderedDelete() ' . $result->getMessage() . ' Query: ' . $query, E_USER_WARNING);
  455. return $result;
  456. }
  457. else
  458. {
  459. $currentSortId = $sortId;
  460. do
  461. {
  462. $updateQuery = 'update ' . $tableName . ' set SORT_ID="' . $currentSortId . '" where SORT_ID="' . (++$currentSortId) . '"';
  463. $resultUpdate = $this->_db->query($updateQuery);
  464. if (DB::isError($resultUpdate))
  465. {
  466. trigger_error($resultUpdate->getMessage() . ' Query: ' . $updateQuery, E_USER_WARNING);
  467. }
  468. }
  469. while ($this->_db->affectedRows() > 0);
  470. }
  471. }
  472. }
  473. //-------------------------------------------------------------------
  474. // METHOD: doCount
  475. //-------------------------------------------------------------------
  476. /**
  477. * This method counts records in a given table with a given where clause.
  478. *
  479. * @access public
  480. * @param string $tableName The name of the table from which we are counting records.
  481. * @param string $whereClause The where clause to filter records. The whereClause is blank by default so all records in the table are counted if no whereClause is present.
  482. * @return int The number of records that match the where clause.
  483. */
  484. function doCount($tableName, $whereClause = '')
  485. {
  486. $countQuery = 'select count(*) from ' . $tableName;
  487. if ($whereClause != '')
  488. {
  489. $countQuery .= ' where ' . $whereClause;
  490. }
  491. $countResult = $this->doSelect($countQuery);
  492. $countRow = cc_fetch_row($countResult);
  493. return $countRow[0];
  494. }
  495. //-------------------------------------------------------------------
  496. // METHOD: doSelect
  497. //-------------------------------------------------------------------
  498. /**
  499. * This method executes the given select query on the database.
  500. *
  501. * @access public
  502. * @param string $query The select query to execute.
  503. * @return resource The result returned from the query. Use cc_fetch_row to cycle through the results.
  504. */
  505. function doSelect($query)
  506. {
  507. $start = time();
  508. if (PEAR::isError($this->openDatabase($query)))
  509. {
  510. return $this->_db;
  511. }
  512. else
  513. {
  514. $result = $this->_db->query($query);
  515. if (DB::isError($result))
  516. {
  517. trigger_error('CC_Database::doSelect() ' . $result->getMessage() . ' Query: ' . $query, E_USER_WARNING);
  518. }
  519. return $result;
  520. }
  521. }
  522. //-------------------------------------------------------------------
  523. // METHOD: doGetOne
  524. //-------------------------------------------------------------------
  525. /**
  526. * This method returns the first column of the first row in the given result set.
  527. *
  528. * @access public
  529. * @param string $query The select statement query
  530. * @return string The first column of the first row.
  531. * @see doSelect()
  532. */
  533. function doGetOne($query)
  534. {
  535. if (PEAR::isError($this->openDatabase($query)))
  536. {
  537. return $this->_db;
  538. }
  539. else
  540. {
  541. $start = time();
  542. $result = $this->_db->getOne($query);
  543. if (DB::isError($result))
  544. {
  545. trigger_error('CC_Database::doGetOne() ' . $result->getMessage() . ' Query: ' . $query, E_USER_WARNING);
  546. }
  547. return $result;
  548. }
  549. }
  550. //-------------------------------------------------------------------
  551. // METHOD: doGetRow
  552. //-------------------------------------------------------------------
  553. /**
  554. * This method returns the first row in the given result set.
  555. *
  556. * @access public
  557. * @param string $query The select statement query
  558. * @return string The first column of the first row.
  559. * @see doSelect()
  560. */
  561. function doGetRow($query)
  562. {
  563. if (PEAR::isError($this->openDatabase($query)))
  564. {
  565. return $this->_db;
  566. }
  567. else
  568. {
  569. $start = time();
  570. $this->_db->setFetchMode(DB_FETCHMODE_ASSOC);
  571. $result = $this->_db->getRow($query);
  572. $this->_db->setFetchMode(DB_FETCHMODE_ORDERED);
  573. if (DB::isError($result))
  574. {
  575. trigger_error('CC_Database::doGetRow() ' . $result->getMessage() . ' Query: ' . $query, E_USER_WARNING);
  576. }
  577. return $result;
  578. }
  579. }
  580. //-------------------------------------------------------------------
  581. // METHOD: doPreparedSelect
  582. //-------------------------------------------------------------------
  583. /**
  584. * A prepared query is an easy way to insert the data of an array into a query. For example, you could have a query, "insert into USERS (FIRSTNAME, LASTNAME, EMAIL) values (?, ?, ?)". By passing in this query, and an array containing the first name, last name, and email, the PEAR DB classes will parse the query, and replace each question mark with the quoted and escaped value of the array. This is useful because you don't have to manually construct and concatenate your query.
  585. *
  586. * @access public
  587. * @see http://pear.php.net/manual/en/package.database.db.intro-execute.php
  588. * @param string $query The select query to prepare.
  589. * @param string $data A numeric array of your data to go into the query.
  590. * @param bool $transaction When set to true, an error will cause the database to rollback (if supported).
  591. * @return resource The result returned from the query. Use cc_fetch_row to cycle through the results.
  592. *
  593. */
  594. function doPreparedSelect($query, $data, $transaction = false)
  595. {
  596. if (PEAR::isError($this->openDatabase($query)))
  597. {
  598. return $this->_db;
  599. }
  600. else
  601. {
  602. if ($transaction)
  603. {
  604. $this->begin();
  605. }
  606. $statement = $this->_db->prepare($query);
  607. $result = $this->_db->execute($statement, $data);
  608. if (DB::isError($result))
  609. {
  610. if ($transaction)
  611. {
  612. $this->rollback();
  613. }
  614. trigger_error('CC_Database::doPreparedSelect() ' . $result->getMessage() . ' Query: ' . $query, E_USER_WARNING);
  615. }
  616. else
  617. {
  618. if (is_a($result, 'DB_common'))
  619. {
  620. $result->freePrepared($statement);
  621. }
  622. }
  623. return $result;
  624. }
  625. }
  626. //-------------------------------------------------------------------
  627. // METHOD: begin
  628. //-------------------------------------------------------------------
  629. /**
  630. * This method starts a database transaction. A transaction is a series of database queries. Should an error happen five queries into the transaction, you can "roll back" each of the previous queries, and restore the database to what it used to be. Should everything complete successfully, you need to "commit" your changes.
  631. *
  632. * @see rollback(),commit()
  633. * @access public
  634. *
  635. */
  636. function begin()
  637. {
  638. $this->_db->autoCommit(false);
  639. }
  640. //-------------------------------------------------------------------
  641. // METHOD: rollback
  642. //-------------------------------------------------------------------
  643. /**
  644. * This method rolls back the last action(s) in the current transactions. This method will only work if you have started a transaction using begin().
  645. *
  646. * @see begin(), commit()
  647. * @access public
  648. * @return bool Whether the rollback was successful.
  649. *
  650. */
  651. function rollback()
  652. {
  653. $result = $this->_db->rollback();
  654. $this->_db->autoCommit(true);
  655. if (DB::isError($result))
  656. {
  657. trigger_error('CC_Database::rollback() ' . $result->getMessage() . ' Query: ' . $query, E_USER_ERROR);
  658. }
  659. return true;
  660. }
  661. //-------------------------------------------------------------------
  662. // METHOD: commit
  663. //-------------------------------------------------------------------
  664. /**
  665. * This method commits the last database actions in the current transaction. This method only works if you have started a transaction using begin().
  666. *
  667. * @see begin(), rollback()
  668. * @access public
  669. * @return bool Whether the commit was successful.
  670. *
  671. */
  672. function commit()
  673. {
  674. $result = $this->_db->commit();
  675. $this->_db->autoCommit(true);
  676. if (DB::isError($result))
  677. {
  678. trigger_error('CC_Database::commit() ' . $result->getMessage(), E_USER_ERROR);
  679. }
  680. return true;
  681. }
  682. //-------------------------------------------------------------------
  683. // METHOD: getEncodePassword
  684. //-------------------------------------------------------------------
  685. /**
  686. * This method gets the password used to encode data in the database.
  687. *
  688. * @access public
  689. * @return string The database encode password.
  690. */
  691. function getEncodePassword()
  692. {
  693. return strSlide13($this->_encodePassword);
  694. }
  695. //-------------------------------------------------------------------
  696. // METHOD: setEncodePassword
  697. //-------------------------------------------------------------------
  698. /**
  699. * This method sets the password used to encode data in the database.
  700. *
  701. * @access public
  702. * @param string The database encode password.
  703. */
  704. function setEncodePassword($encodePassword)
  705. {
  706. $this->_encodePassword = strSlide13($encodePassword);
  707. }
  708. //-------------------------------------------------------------------
  709. // METHOD: getPassword
  710. //-------------------------------------------------------------------
  711. /**
  712. * This method gets the password used to connect to the database.
  713. *
  714. * @access public
  715. * @return string The database password.
  716. */
  717. function getPassword()
  718. {
  719. return strSlide13($this->_password);
  720. }
  721. //-------------------------------------------------------------------
  722. // METHOD: setPassword
  723. //-------------------------------------------------------------------
  724. /**
  725. * This method sets the password used to connect to the database.
  726. *
  727. * @access public
  728. * @param string The database password.
  729. */
  730. function setPassword($password)
  731. {
  732. $this->_password = strSlide13($password);
  733. }
  734. //-------------------------------------------------------------------
  735. // METHOD: cc_show_tables
  736. //-------------------------------------------------------------------
  737. /**
  738. * This method returns an array of all the tables in the database.
  739. *
  740. * @access public
  741. * @return array An array of all the database's table names.
  742. */
  743. function cc_show_tables()
  744. {
  745. switch ($this->_databaseType)
  746. {
  747. case DB_POSTGRES:
  748. $query = "select tablename from pg_tables where schemaname='public'";
  749. break;
  750. case DB_MYSQL:
  751. default:
  752. $query = "show tables";
  753. break;
  754. }
  755. $result = $this->doSelect($query);
  756. $returnArray = array();
  757. while($row = cc_fetch_row($result))
  758. {
  759. $returnArray[] = $row[0];
  760. }
  761. return $returnArray;
  762. }
  763. //-------------------------------------------------------------------
  764. // METHOD: cc_get_fields
  765. //-------------------------------------------------------------------
  766. /**
  767. * This method returns an array of all the fields in a given table.
  768. *
  769. * @access public
  770. * @param string $table The name of the table to search.
  771. * @return array An array of all the given table's field names.
  772. */
  773. function cc_get_fields($table)
  774. {
  775. switch ($this->_databaseType)
  776. {
  777. case DB_POSTGRES:
  778. $query = 'SELECT a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod) as type, a.attnotnull, a.atthasdef, adef.adsrc FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_attrdef adef ON a.attrelid=adef.adrelid AND a.attnum=adef.adnum where a.attrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname=\'' . strtolower($table) . '\') AND a.attnum > 0 AND NOT a.attisdropped';
  779. break;
  780. case DB_MYSQL:
  781. default:
  782. $query = 'desc ' . $table;
  783. break;
  784. }
  785. $result = $this->doSelect($query);
  786. $returnArray = array();
  787. if (PEAR::isError($result))
  788. {
  789. trigger_error('Could not query ' . $table . ' (' . $result->getMessage() . ')', E_USER_ERROR);
  790. }
  791. else
  792. {
  793. while($row = cc_fetch_row($result))
  794. {
  795. $returnArray[] = $row[0];
  796. }
  797. }
  798. return $returnArray;
  799. }
  800. //-------------------------------------------------------------------
  801. // METHOD: __sleep
  802. //-------------------------------------------------------------------
  803. function __sleep()
  804. {
  805. if ($this->_db && !PEAR::isError($this->_db))
  806. {
  807. $this->closeDatabase();
  808. }
  809. return array_keys(get_object_vars($this));
  810. }
  811. }
  812. //-------------------------------------------------------------------
  813. // METHOD: cc_fetch_row
  814. //-------------------------------------------------------------------
  815. /**
  816. * This method returns a row in the given result set as a zero-indexed array. This is equivalent to mysql_fetch_row() or pgsql_fetch_row().
  817. *
  818. * @access public
  819. * @param resource $result A result set returned by doSelect.
  820. * @param int $fetchMode
  821. * @return array A zero-indexed array of all the data in the row.
  822. * @see doSelect()
  823. */
  824. function cc_fetch_row(&$result, $fetchMode = DB_FETCHMODE_ORDERED)
  825. {
  826. $row = $result->fetchRow($fetchMode);
  827. if (DB::isError($row))
  828. {
  829. return false;
  830. }
  831. else
  832. {
  833. return $row;
  834. }
  835. }
  836. //-------------------------------------------------------------------
  837. // METHOD: cc_fetch_array
  838. //-------------------------------------------------------------------
  839. /**
  840. * This method returns a row in the given result set as a zero-indexed and associative array. This is equivalent to mysql_fetch_array() or pgsql_fetch_array().
  841. *
  842. * @access public
  843. * @param resource $result A result set returned by doSelect.
  844. * @return array An associative array of all the data in the row.
  845. * @see doSelect()
  846. *
  847. */
  848. function cc_fetch_array(&$result)
  849. {
  850. return cc_fetch_row($result, DB_FETCHMODE_ASSOC);
  851. }
  852. //-------------------------------------------------------------------
  853. // METHOD: cc_fetch_assoc
  854. //-------------------------------------------------------------------
  855. /**
  856. * This method returns a row in the given result set as an associative array. This is equivalent to mysql_fetch_assoc() or pgsql_fetch_assoc().
  857. *
  858. * @access public
  859. * @param resource $result A result set returned by doSelect.
  860. * @return array An associative array of all the data in the row.
  861. * @see doSelect()
  862. *
  863. */
  864. function cc_fetch_assoc(&$result)
  865. {
  866. return cc_fetch_row($result, DB_FETCHMODE_ASSOC);
  867. }
  868. //-------------------------------------------------------------------
  869. // METHOD: cc_num_cols
  870. //-------------------------------------------------------------------
  871. /**
  872. * This method returns the number of columns returned in a result set.
  873. *
  874. * @access public
  875. * @param resource $result A result set returned by doSelect.
  876. * @return int The number of columns in the given result set.
  877. * @see doSelect()
  878. *
  879. */
  880. function cc_num_cols(&$result)
  881. {
  882. return $result->numCols();
  883. }
  884. //-------------------------------------------------------------------
  885. // METHOD: cc_num_rows
  886. //-------------------------------------------------------------------
  887. /**
  888. * This method returns the number of rows returned in a result set.
  889. *
  890. * @access public
  891. * @param resource $result A result set returned by doSelect.
  892. * @return int The number of rows in the given result set.
  893. * @see doSelect()
  894. *
  895. */
  896. function cc_num_rows(&$result)
  897. {
  898. return $result->numRows();
  899. }
  900. //-------------------------------------------------------------------
  901. // METHOD: cc_field_name
  902. //-------------------------------------------------------------------
  903. /**
  904. * This method mimics the mysql_field_name method. It gets the name of the specified field in a result.
  905. *
  906. * @access public
  907. * @param resource $result A result set returned by doSelect.
  908. * @param int $colNumber The index of the column for which we want the name.
  909. * @return string The column name at the given index in the given result set.
  910. * @see doSelect()
  911. * @todo Comment this properly. Mike?
  912. */
  913. function cc_field_name(&$result, $colNumber)
  914. {
  915. $fields = $result->tableInfo();
  916. return $fields[$colNumber][name];
  917. }
  918. ?>