Source for file OracleDatabase.class.php

Documentation is available at OracleDatabase.class.php

  1. <?php
  2. /**
  3. * @package harmoni.dbc.oracle
  4. *
  5. * @copyright Copyright &copy; 2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  7. *
  8. * @version $Id: OracleDatabase.class.php,v 1.15 2007/09/05 21:39:00 adamfranco Exp $
  9. */
  10. require_once(HARMONI."DBHandler/Database.abstract.php");
  11. require_once(HARMONI."DBHandler/Oracle/OracleSelectQueryResult.class.php");
  12. require_once(HARMONI."DBHandler/Oracle/OracleInsertQueryResult.class.php");
  13. require_once(HARMONI."DBHandler/Oracle/OracleUpdateQueryResult.class.php");
  14. require_once(HARMONI."DBHandler/Oracle/OracleDeleteQueryResult.class.php");
  15. require_once(HARMONI."DBHandler/Oracle/Oracle_SQLGenerator.class.php");
  16.  
  17. /**
  18. * A OracleDatabase class provides the tools to connect, query, etc., a Oracle database.
  19. *
  20. * @package harmoni.dbc.oracle
  21. *
  22. * @copyright Copyright &copy; 2005, Middlebury College
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  24. *
  25. * @version $Id: OracleDatabase.class.php,v 1.15 2007/09/05 21:39:00 adamfranco Exp $
  26. */
  27. class OracleDatabase
  28. extends DatabaseAbstract
  29. {
  30.  
  31. /**
  32. * The hostname of the database, i.e. myserver.mydomain.edu.
  33. * The hostname of the database, i.e. myserver.mydomain.edu.
  34. * @var string $_dbHost The hostname of the database, i.e. myserver.mydomain.edu.
  35. * @access private
  36. */
  37. var $_dbHost;
  38. /**
  39. * The name of the default database to use.
  40. * The name of the default database to use.
  41. * @var string $_dbName The name of the default database to use.
  42. * @access private
  43. */
  44. var $_dbName;
  45. /**
  46. * The username with which to connect to the database.
  47. * The username with which to connect to the database.
  48. * @var string $_dbUser The username with which to connect to the database.
  49. * @access private
  50. */
  51. var $_dbUser;
  52. /**
  53. * The password for $_dbUser with which to connect to the database.
  54. * The password for $_dbUser with which to connect to the database.
  55. * @var string $_dbPass The password for $_dbUser with which to connect to the database.
  56. * @access private
  57. */
  58. var $_dbPass;
  59. /**
  60. * Stores the current connection's link identifier.
  61. * If a connection is open, this stores the connection's identifier. Otherwise,
  62. * it stores FALSE.
  63. * @var mixed $_linkId If a connection is open, this stores the connection's identifier. Otherwise,
  64. * it stores FALSE.
  65. * @access private
  66. */
  67. var $_linkId;
  68. /**
  69. * The total number of successful queries executed since the last call to connect().
  70. * The total number of failed queries executed since the last call to connect().
  71. * @var integer $_successfulQueries The total number of failed queries executed since the last call to connect().
  72. * @access private
  73. */
  74. var $_successfulQueries;
  75. /**
  76. * The total number of failed queries executed since the last call to connect().
  77. * The total number of failed queries executed since the last call to connect().
  78. * @var integer $_failedQueries The total number of failed queries executed since the last call to connect().
  79. * @access private
  80. */
  81. var $_failedQueries;
  82.  
  83. /**
  84. * Creates a new database connection.
  85. * @param string $dbName The TNS name of the database to use (found in tnsnames.ora file).
  86. * @param string $dbUser The username with which to connect to the database.
  87. * @param string $dbPass The password for $_dbUser with which to connect to the database.
  88. * @return integer $dbIndex The index of the new database
  89. * @access public
  90. */
  91. function OracleDatabase($dbName, $dbUser, $dbPass) {
  92. // ** parameter validation
  93. $stringRule = StringValidatorRule::getRule();
  94. //ArgumentValidator::validate($dbHost, $stringRule, true);
  95. ArgumentValidator::validate($dbName, $stringRule, true);
  96. ArgumentValidator::validate($dbUser, $stringRule, true);
  97. ArgumentValidator::validate($dbPass, $stringRule, true);
  98. // ** end of parameter validation
  99.  
  100. //$this->_dbHost = $dbHost;
  101. $this->_dbName = $dbName;
  102. $this->_dbUser = $dbUser;
  103. $this->_dbPass = $dbPass;
  104. $this->_linkId = false;
  105. $this->_successfulQueries = 0;
  106. $this->_failedQueries = 0;
  107. }
  108. /**
  109. * Returns a short string name for this database type. Example: 'MySQL'
  110. * @access public
  111. * @return string
  112. */
  113. function getStringName() {
  114. return "Oracle";
  115. }
  116.  
  117. /**
  118. * Returns a list of the tables that exist in the currently connected database.
  119. * @return array
  120. * @access public
  121. */
  122. function getTableList() {
  123. $query = new SelectQuery();
  124. $query->addTable("all_tables");
  125. $query->addColumn("table_name");
  126. $res =$this->query($query);
  127. $list = array();
  128. while($res->hasMoreRows()) {
  129. $list[] = $res->field(0);
  130. $res->advanceRow();
  131. }
  132. $res->free();
  133. return $list;
  134. }
  135.  
  136. /**
  137. * Connects to the database.
  138. * Connects to the database.
  139. * @access public
  140. * @return mixed The connection's link identifier, if successful; False, otherwise.
  141. */
  142. function connect() {
  143. // if connected, need to disconnect first
  144. if ($this->isConnected())
  145. return false;
  146. $linkId = ocilogon($this->_dbUser, $this->_dbPass, $this->_dbName);
  147. // see if successful
  148. if ($linkId) {
  149. // reset the query counters
  150. $this->_successfulQueries = 0;
  151. $this->_failedQueries = 0;
  152. $this->_linkId = $linkId;
  153. return $linkId;
  154. }
  155. else {
  156. throw new ConnectionDatabaseException($this->getConnectionErrorInfo()."Cannot connect to database.");
  157. $this->_linkId = false;
  158. return false;
  159. }
  160. }
  161.  
  162. /**
  163. * Makes a persistent database connection.
  164. * Makes a persistent database connection.
  165. * @access public
  166. * @return mixed The connection's link identifier, if successful; False, otherwise.
  167. */
  168. function pConnect() {
  169. // if connected, need to disconnect first
  170. if ($this->isConnected())
  171. return false;
  172.  
  173. $linkId = ociplogon($this->_dbUser, $this->_dbPass, $this->_dbName);
  174. // see if successful
  175. if ($linkId) {
  176. // reset the query counters
  177. $this->_successfulQueries = 0;
  178. $this->_failedQueries = 0;
  179. $this->_linkId = $linkId;
  180. return $linkId;
  181. }
  182. else {
  183. throw new ConnectionDatabaseException($this->getConnectionErrorInfo()."Cannot connect to database.");
  184. $this->_linkId = false;
  185. return false;
  186. }
  187.  
  188. }
  189.  
  190.  
  191. /**
  192. * Executes an SQL query.
  193. * Executes an SQL query. The method is passed a query object, which it
  194. * converts to a SQL query string using the appropriate SQLGenerator
  195. * object.
  196. * @access public
  197. * @param object Query $query A Query object from which the SQL query will be constructed.
  198. * @return mixed The appropriate QueryResult object. If the query failed, it would
  199. * return NULL.
  200. */
  201. function query(Query $query) {
  202. // do not attempt, to query, if not connected
  203. if (!$this->isConnected()) {
  204. throw new ConnectionDatabaseException("Attempted to query but there was no database connection.");
  205. return false;
  206. }
  207. // generate the SQL query string
  208. $queryString = Oracle_SQLGenerator::generateSQLQuery($query);
  209. // if query is an insert, do it in a transaction (cause you will need
  210. // to fetch the last inserted id)
  211. if ($query->getType() == INSERT && $query->_sequence)
  212. $this->_query("BEGIN");
  213. // attempt to run the query
  214. $resourceId = $this->_query($queryString);
  215.  
  216. // if query was unsuccessful, return a null QueryResult object
  217. if ($resourceId === false)
  218. return null;
  219.  
  220. // create the appropriate QueryResult object
  221. switch($query->getType()) {
  222. case INSERT : {
  223. // we need to fetch the last inserted id
  224. // this is only possible if the user has specified the sequence
  225. // object with the setAutoIncrementColumn() method.
  226. $lastId = null;
  227. if ($query->_sequence) {
  228. $lastIdQuery = "SELECT ".$query->_sequence.".CURRVAL";
  229. $lastIdResourceId = $this->_query($lastIdQuery);
  230. $this->_query("COMMIT");
  231. $arr = pg_fetch_row($lastIdResourceId, 0);
  232. $lastId = intval($arr[0]);
  233. }
  234. $result = new OracleInsertQueryResult($resourceId, $lastId);
  235. break;
  236. }
  237. case UPDATE :
  238. $result = new OracleUpdateQueryResult($resourceId);
  239. break;
  240. case DELETE :
  241. $result = new OracleDeleteQueryResult($resourceId);
  242. break;
  243. case SELECT :
  244. $result = new OracleSelectQueryResult($resourceId, $this->_linkId);
  245. break;
  246. case GENERIC :
  247. $result = new OracleGenericQueryResult($resourceId, $this->_linkId);
  248. break;
  249. default:
  250. throw new DatabaseException("Unsupported query type.");
  251. } // switch
  252. return $result;
  253. }
  254. /**
  255. * Answer the string SQL for the query
  256. *
  257. * @param object $query
  258. * @return string
  259. * @access public
  260. * @since 11/14/06
  261. */
  262. function generateSQL ($query) {
  263. return Oracle_SQLGenerator::generateSQLQuery($query);
  264. }
  265.  
  266. /**
  267. * Executes an SQL query.
  268. * Executes an SQL query.
  269. * @access private
  270. * @param mixed query Either a string (this would be the case, normally) or
  271. * an array of strings. Each string is corresponding to an SQL query.
  272. * @return mixed For a SELECT statement, a resource identifier, if
  273. * successful; For INSERT, DELETE, UPDATE statements, TRUE if successful;
  274. * for all: FALSE, if not successful. If <code>$query</code> had several
  275. * queries, this would be the result for the last one.
  276. */
  277. function _query($query) {
  278. // do not attempt to query, if not connected
  279. if (!$this->isConnected()) {
  280. throw new ConnectionDatabaseException("Attempted to query but there was no database connection.");
  281. return false;
  282. }
  283. if (is_array($query))
  284. $queries = $query;
  285. else if (is_string($query))
  286. $queries = array($query);
  287. $count = count($queries);
  288. // if more than one queries - do them in a transaction
  289. if ($count > 1)
  290. $this->_query("BEGIN");
  291. foreach ($queries as $q) {
  292. // attempt to execute the query
  293. $resourceId = ociparse($this->_linkId, $q);
  294. if ($resourceId === false) {
  295. $this->_failedQueries++;
  296. throw new QueryDatabaseException(pg_last_error($this->_linkId));
  297. }
  298. else {
  299. $this->_successfulQueries++;
  300. ociexecute($resourceId);
  301. }
  302. }
  303. if ($count > 1)
  304. $this->_query("COMMIT");
  305.  
  306. return $resourceId;
  307. }
  308.  
  309.  
  310.  
  311. /**
  312. * Disconnects from the database.
  313. * Disconnects from the database.
  314. * @access public
  315. * @return boolean True, if successful; False, otherwise.
  316. */
  317. function disconnect() {
  318. // do not disconnect, if not connected
  319. if (!$this->isConnected())
  320. return false;
  321. // attempt to disconnect
  322. $isSuccessful = ocilogoff($this->_linkId);
  323. if ($isSuccessful)
  324. $this->_linkId = false;
  325. return $isSuccessful;
  326. }
  327.  
  328.  
  329. /**
  330. * Indicates whether there is an open connection to the database.
  331. * Indicates whether there is an open connection to the database.
  332. * @access public
  333. * @return boolean True if there is an open connection to the database; False, otherwise.
  334. */
  335. function isConnected() {
  336. return ($this->_linkId !== false);
  337. }
  338.  
  339.  
  340.  
  341.  
  342. /**
  343. * Returns the total number of successful queries executed since the last call to connect().
  344. * Returns the total number of successful queries executed since the last call to connect().
  345. * @access public
  346. * @return integer The total number of successful queries executed since the last call to connect().
  347. ***/
  348. function getNumberSuccessfulQueries() {
  349. return $this->_successfulQueries;
  350. }
  351. /**
  352. * Returns the total number of failed queries executed since the last call to connect().
  353. * Returns the total number of failed queries executed since the last call to connect().
  354. * @access public
  355. * @return integer The total number of failed queries executed since the last call to connect().
  356. ***/
  357. function getNumberFailedQueries() {
  358. return $this->_failedQueries;
  359. }
  360.  
  361. /**
  362. * This method selects the default database to use in queries.
  363. * @access public
  364. * @param string database The name of the default database.
  365. * @return boolean True, if successful; False, otherwise.
  366. */
  367. function selectDatabase($database) {
  368. // ** parameter validation
  369. throw new ConnectionDatabaseException("Oracle database connections cannot change the database once connected!");
  370. return false;
  371. }
  372.  
  373. /**
  374. * Converts a DateAndTime object to a proper datetime/timestamp/time representation
  375. * for this Database.
  376. *
  377. * @access public
  378. * @param ref object dateAndTime The DateAndTime object to convert.
  379. * @return mixed A proper datetime/timestamp/time representation for this Database.
  380. */
  381. function toDBDate(DateAndTime $dateAndTime) {
  382. $dt =$dateAndTime->asDateAndTime();
  383. $string = sprintf("%s/%02d/%02d %02d:%02d:%02d", $dt->year(),
  384. $dt->month(), $dt->dayOfMonth(),
  385. $dt->hour24(), $dt->minute(),
  386. $dt->second());
  387. return "to_date('$string', 'yyyy/mm/dd hh24:mi:ss')";
  388. }
  389. /**
  390. * Converts a database datetime/timestamp/time value (that has been fetched
  391. * from the db) to a DateAndTime object.
  392. *
  393. * @access public
  394. * @param mixed A database datetime/timestamp/time value (that has been fetched
  395. * from the db).
  396. * @return ref object The DateAndTime object.
  397. */
  398. function fromDBDate($value) {
  399. /*
  400. * NOT SURE HOW TO DO THIS FOR ORACLE
  401. */
  402. $obj = DateAndTime::fromString($value);
  403. return $obj;
  404. }
  405. /**
  406. * Return TRUE if this database supports transactions.
  407. *
  408. * @return boolean
  409. * @access public
  410. * @since 3/9/05
  411. */
  412. function supportsTransactions () {
  413. return TRUE;
  414. }
  415. /**
  416. * Begin a transaction.
  417. *
  418. * @return void
  419. * @access public
  420. * @since 3/9/05
  421. */
  422. function beginTransaction () {
  423. $this->_query("BEGIN");
  424. }
  425. /**
  426. * Commit a transaction. This will roll-back changes if errors occured in the
  427. * transaction block.
  428. *
  429. * @return void
  430. * @access public
  431. * @since 3/9/05
  432. */
  433. function commitTransaction () {
  434. $this->_query("COMMIT");
  435. }
  436. /**
  437. * Roll-back a transaction manually instead of committing
  438. *
  439. * @return void
  440. * @access public
  441. * @since 3/9/05
  442. */
  443. function rollbackTransaction () {
  444. $this->_query("ROLLBACK");
  445. }
  446. }
  447.  
  448. ?>

Documentation generated on Wed, 19 Sep 2007 10:25:18 -0400 by phpDocumentor 1.3.0RC3