Source for file database-defs.php

Documentation is available at database-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: database-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for managing DATABASES */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package database */
  27. include_once("timer-defs.php");
  28.  
  29. /** Connect persistent to DB */
  30. ("PERSISTENT", true);
  31.  
  32. /** Connect non-persistent to DB */
  33. ("NOT_PERSISTENT", false);
  34.  
  35. /** Default datasource for queries @see add_database() */
  36. ("DEFAULT_DATASOURCE", true);
  37.  
  38. // ----------------------------------------------------------------------
  39. /**
  40. * Datasources
  41. * A datasources class is just a bunch of databases. If you want
  42. * to access a database, register it in here first, then you
  43. * can select it to perform queries on later.
  44. * @package database
  45. */
  46. class datasources {
  47. /** An array of database objects. All databases we can use as datasources */
  48.  
  49. var $database;
  50. /** Default database name */
  51.  
  52. var $db_name_default = "";
  53. /** Name of currently selected database */
  54.  
  55. var $db_name_selected = "";
  56. // ....................................................................
  57. /** Constructor */
  58.  
  59. function datasources() { }
  60. // ....................................................................
  61. /**
  62. * Constructor
  63. * Add a new base to our list of datasources. The dbtype and the name
  64. * are the only mandatory parameters.
  65. * @param string $dbtype The type of database eg: 'postgres', 'mssql' etc.
  66. * @param string $name The name of the database
  67. * @param string $user Name of a user who can access the database
  68. * @param string $passwd The password the user can access the database with
  69. * @param string $host The hostname of the machine running the database (TCP/IP)
  70. * @param integer $port The port number of the database server
  71. * @param boolean $default True if the database is the default database
  72. */
  73. function add_database($dbtype, $name, $user="", $passwd="", $host="", $port=0, $default=false) {
  74. switch ($dbtype) {
  75. case "postgres":
  76. include_once("db-postgres.php");
  77. $this->database[$name] = new db_postgres($name, $user, $passwd, $host, $port);
  78. break;
  79. case "odbc":
  80. include_once("db-odbc.php");
  81. $this->database[$name] = new db_odbc($name, $user, $passwd, $host, $port);
  82. break;
  83. case "mssql_server":
  84. include_once("db-mssql-server.php");
  85. $this->database[$name] = new db_mssql_server($name, $user, $passwd, $host, $port);
  86. break;
  87. case "mysql":
  88. include_once("db-mysql.php");
  89. $this->database[$name] = new db_mysql($name, $user, $passwd, $host, $port);
  90. break;
  91. case "oracle":
  92. include_once("db-oracle.php");
  93. $this->database[$name] = new db_oracle($name, $user, $passwd, $host, $port);
  94. break;
  95. }
  96. // Make sure the default database is selected..
  97. if ($default) {
  98. // Select the default DB. This tries to
  99. // connect to it..
  100. $this->set_default($name);
  101. $this->select($name);
  102.  
  103. // It is a fatal application error if the default
  104. // database cannot be connected..
  105. if (!$this->connected($name)) {
  106. $errmsg = "APPFATAL: " . APP_NAME . ": Default database not connected. Exit stage left.";
  107. error_log($errmsg, 0);
  108. die($errmsg);
  109. }
  110. }
  111. return $this;
  112. } // add_database
  113. // ....................................................................
  114. /**
  115. * This will connect it if it isn't already connected. Calling this
  116. * with no database name will select the default one. Returns the
  117. * database unique identifier, or false if none was selected.
  118. * @param string $db_name The name of the database to select
  119. * @return resource The database resource ID
  120. */
  121. function select($db_name="") {
  122. global $RESPONSE;
  123. global $HTTP_HOST;
  124. $dbid = false;
  125. if ($db_name == "") {
  126. $db_name = $this->db_name_default;
  127. }
  128. if (isset($this->database[$db_name])) {
  129. $db = $this->database[$db_name];
  130. $this->db_name_selected = $db_name;
  131. if (!$db->connected) {
  132. // Check if we should connect persistently..
  133. if (isset($RESPONSE) && $RESPONSE->InPersistentHostsList($HTTP_HOST)) {
  134. $connmode = PERSISTENT;
  135. }
  136. else {
  137. $connmode = NOT_PERSISTENT;
  138. }
  139. $db->connect($connmode);
  140. $this->database[$db_name] = $db;
  141. }
  142. if ($db->connected) {
  143. $dbid = $db->dbid;
  144. }
  145. }
  146. return $dbid;
  147. } // select
  148. // ....................................................................
  149. /**
  150. * Internal function to set the name of the default database.
  151. * The database must exist as a defined database already.
  152. * @param string $db_name The name of the database
  153. */
  154. function set_default($db_name) {
  155. if (isset($this->database[$db_name])) {
  156. $this->db_name_default = $db_name;
  157. return $this;
  158. }
  159. } // set_default
  160. // ....................................................................
  161. /**
  162. * Returns the database resource ID of the given database name.
  163. * If dbname is not given, returns ID of currently selected DB.
  164. * @param string $db_name The name of the database
  165. * @return resource Database resource ID
  166. */
  167. function dbid($db_name="") {
  168. $res = false;
  169. if ($db_name == "") {
  170. $db_name = $this->db_name_selected;
  171. }
  172. if (isset($this->database[$db_name])) {
  173. $res = $this->database[$db_name]->dbid;
  174. }
  175. return $res;
  176. } // dbid
  177. // ....................................................................
  178. /**
  179. * Returns the database type of the given database name.
  180. * If dbname is not given, returns type of DB currently selected.
  181. * @param string $db_name The name of the database
  182. * @return string Database type string
  183. */
  184. // Returns the database type of the selected database.
  185. function dbtype($db_name="") {
  186. $res = false;
  187. if ($db_name == "") {
  188. $db_name = $this->db_name_selected;
  189. }
  190. if (isset($this->database[$db_name])) {
  191. $res = $this->database[$db_name]->type;
  192. }
  193. return $res;
  194. } // dbtype
  195. // ....................................................................
  196. /**
  197. * Returns connected status of named database, or the currently
  198. * selected one if no name given.
  199. * @param string $db_name The name of the database
  200. * @return boolean Database connection status true or false
  201. */
  202. function connected($db_name="") {
  203. $res = false;
  204. if ($db_name == "") {
  205. $db_name = $this->db_name_selected;
  206. }
  207. if (isset($this->database[$db_name])) {
  208. $res = $this->database[$db_name]->connected;
  209. }
  210. return $res;
  211. } // connected
  212. // ....................................................................
  213. /**
  214. * Connects to the database which has been selected in the mode
  215. * specified, or non-peristent otherwise.
  216. * @param boolean $persistent Whether to connect persistently or not
  217. * @return boolean Whether database connection was successful
  218. */
  219. function connect($persistent=NOT_PERSISTENT) {
  220. $connected = false;
  221. if (isset($this->database[$this->db_name_selected])) {
  222. $this->database[$this->db_name_selected]->connect($persistent);
  223. if ($this->database[$this->db_name_selected]->connected) {
  224. $connected = true;
  225. }
  226. else {
  227. $errmsg = "Failed to connect to database '" . $this->name . "' ";
  228. $errmsg .= "type='" . $this->type . "' ";
  229. $errmsg .= "host='" . $this->host . "' ";
  230. $errmsg .= "port='" . $this->port . "' ";
  231. $errmsg .= "user='" . $this->user . "' ";
  232. $errmsg .= "passwd=" . ($this->passwd != "") ? "xxxx" : "(none) ";
  233. if ($persistent) $errmsg .= "persistent=yes";
  234. else $errmsg .= "persistent=no";
  235. error_log("CONNFAIL: $errmsg");
  236. }
  237. }
  238. return $connected;
  239. } // connect
  240. // ....................................................................
  241. /**
  242. * Disconnect the currently selected database.
  243. */
  244. function disconnect() {
  245. if (isset($this->database[$this->db_name_selected])) {
  246. $this->database[$this->db_name_selected]->disconnect();
  247. }
  248. } // disconnect
  249. // ....................................................................
  250. /**
  251. * Execute a query on the connected database.
  252. * @param string $sql The SQL query to execute on the database
  253. * @return resource A database query resource ID, or false if query failed
  254. */
  255. function query($sql) {
  256. $rid = false;
  257. if (isset($this->database[$this->db_name_selected])) {
  258. $rid = $this->database[$this->db_name_selected]->query($sql);
  259. }
  260. return $rid;
  261. } // query
  262. // ....................................................................
  263. /**
  264. * Returns SQL statement most recently executed on the current DB.
  265. * NB: the format and/or content of this SQL may differ from the SQL
  266. * originally submitted, due to database-dependent transformations,
  267. * hence the usefulness of this method.
  268. * @return string The SQL statement last executed on current database.
  269. */
  270. function get_last_sql() {
  271. $sql = "";
  272. if (isset($this->database[$this->db_name_selected])) {
  273. $sql = $this->database[$this->db_name_selected]->last_sql;
  274. }
  275. return $sql;
  276. } // get_last_sql
  277. // ....................................................................
  278. /**
  279. * Return the number of rows returned by a SELECT query.
  280. * @param resource $rid The resource ID for the executed query
  281. * @return integer The number of rows returned by the query
  282. */
  283. function numrows($rid) {
  284. $rows = 0;
  285. if (isset($this->database[$this->db_name_selected])) {
  286. $db = $this->database[$this->db_name_selected];
  287. $rows = ($rid !== false) ? $db->numrows($rid) : 0;
  288. }
  289. return $rows;
  290. } // numrows
  291. // ....................................................................
  292. /**
  293. * Return the number of rows affected by a query.
  294. * @param resource $rid The resource ID for the executed query
  295. * @return integer The number of rows affected by the query
  296. */
  297. function affectedrows($rid) {
  298. $rows = 0;
  299. if (isset($this->database[$this->db_name_selected])) {
  300. $db = $this->database[$this->db_name_selected];
  301. $rows = ($rid !== false) ? $db->affectedrows($rid) : 0;
  302. }
  303. return $rows;
  304. } // affectedrows
  305. // ....................................................................
  306. /**
  307. * Free the result of a query
  308. * @param resource $rid The query resource ID
  309. */
  310. function freeresult($rid) {
  311. if (isset($this->database[$this->db_name_selected]) && $rid !== false) {
  312. $this->database[$this->db_name_selected]->freeresult($rid);
  313. }
  314. } // freeresult
  315. // ....................................................................
  316. /**
  317. * Return the last error message.
  318. * @return string The last error message which was generated
  319. */
  320. function errormessage() {
  321. $errmsg = "";
  322. if (isset($this->database[$this->db_name_selected])) {
  323. $errmsg = $this->database[$this->db_name_selected]->errormessage();
  324. }
  325. return $errmsg;
  326. } // errormessage
  327. // ....................................................................
  328. /**
  329. * Return the specified row, as a standard (enumerated) array of
  330. * field values.
  331. * @param resource $rid The resource ID for the executed query
  332. * @param integer $rowno Row number (zero-based) of row to return
  333. * @return array Enumerated array of field values
  334. */
  335. function fetch_row($rid, $rowno) {
  336. $rows = false;
  337. if (isset($this->database[$this->db_name_selected]) && $rid !== false) {
  338. $rows = $this->database[$this->db_name_selected]->fetch_row($rid, $rowno);
  339. }
  340. return $rows;
  341. } // fetch_row
  342. // ....................................................................
  343. /**
  344. * Return the specified row, as an associative array of fields
  345. * in a fieldname => value format.
  346. * @param resource $rid The resource ID for the executed query
  347. * @param integer $rowno Row number (zero-based) of row to return
  348. * @return array Associative array of field values
  349. */
  350. function fetch_array($rid, $rowno) {
  351. $arr = false;
  352. if (isset($this->database[$this->db_name_selected]) && $rid !== false) {
  353. $arr = $this->database[$this->db_name_selected]->fetch_array($rid, $rowno);
  354. }
  355. return $arr;
  356. } // fetch_array
  357. // ....................................................................
  358. /**
  359. * Start a database transaction
  360. * @return boolean Flag indicating successful start of transaction
  361. */
  362. function begin_transaction() {
  363. $res = false;
  364. if (isset($this->database[$this->db_name_selected])) {
  365. $res = $this->database[$this->db_name_selected]->begin_transaction();
  366. }
  367. return $res;
  368. } // begin_transaction
  369. // ....................................................................
  370. /**
  371. * Commit open database transaction
  372. * @return boolean Flag indicating successful commit of transaction
  373. */
  374. function commit() {
  375. $res = false;
  376. if (isset($this->database[$this->db_name_selected])) {
  377. $res = $this->database[$this->db_name_selected]->commit();
  378. }
  379. return $res;
  380. } // commit
  381. // ....................................................................
  382. /**
  383. * Roll back the current database transaction. All queries executed
  384. * as part of the open transaction will be rolled back.
  385. * @return boolean Flag indicating successful rollback of transaction
  386. */
  387. function rollback() {
  388. $res = false;
  389. if (isset($this->database[$this->db_name_selected])) {
  390. $res = $this->database[$this->db_name_selected]->rollback();
  391. }
  392. return $res;
  393. } // rollback
  394. // ....................................................................
  395. /**
  396. * Return a Php boolean from a database field value. The database field
  397. * is expected to be a container of some form of logical value. Here
  398. * is where we convert it according to the current database.
  399. * @param mixed $dbvalue The value from the database field to convert
  400. * @return boolean The boolean value derived from the field value
  401. */
  402. function bool_from_db_value($dbvalue) {
  403. $res = false;
  404. if (isset($this->database[$this->db_name_selected])) {
  405. $res = $this->database[$this->db_name_selected]->bool_from_db_value($dbvalue);
  406. }
  407. return $res;
  408. } // bool_from_db_value
  409. // ....................................................................
  410. /**
  411. * Return a suitable database field value to contain the value for
  412. * the given boolean.
  413. * @param boolean $boolvalue The boolean value to convert
  414. * @return mixed The value suitable for the database field
  415. */
  416. function db_value_from_bool($boolvalue) {
  417. $res = false;
  418. if (isset($this->database[$this->db_name_selected])) {
  419. $res = $this->database[$this->db_name_selected]->db_value_from_bool($boolvalue);
  420. }
  421. return $res;
  422. } // db_value_from_bool
  423. // ....................................................................
  424. /**
  425. * Return the current sequence value, given a sequence name, the table
  426. * and the field it applies to.
  427. * @param string $sequencename The name of the sequence to use
  428. * @param string $table The name of the table the sequence is for
  429. * @param string $column The name of the table column the sequence is for
  430. * @return integer The current sequence value
  431. */
  432. function current_sequencevalue($sequencename, $table, $column) {
  433. $res = 0;
  434. if (isset($this->database[$this->db_name_selected])) {
  435. $res = $this->database[$this->db_name_selected]->current_sequencevalue($sequencename, $table, $column);
  436. }
  437. return $res;
  438. } // current_sequencevalue
  439. // ....................................................................
  440. /**
  441. * Return the next sequence value, given a sequence name, the table
  442. * and the field it applies to.
  443. * @param string $sequencename The name of the sequence to use
  444. * @param string $table The name of the table the sequence is for
  445. * @param string $column The name of the table column the sequence is for
  446. * @return integer The next sequence value
  447. */
  448. function next_sequencevalue($sequencename, $table, $column) {
  449. $res = 0;
  450. if (isset($this->database[$this->db_name_selected])) {
  451. $res = $this->database[$this->db_name_selected]->next_sequencevalue($sequencename, $table, $column);
  452. }
  453. return $res;
  454. } // next_sequencevalue
  455. // ....................................................................
  456. /**
  457. * Set the sequence value, given a sequence name, the table
  458. * and the field it applies to.
  459. * @param integer $newval The sequence value to set
  460. * @param string $sequencename The name of the sequence to use
  461. * @param string $table The name of the table the sequence is for
  462. * @param string $column The name of the table column the sequence is for
  463. * @return boolean Whether the assignment succeeded or not
  464. */
  465. function set_sequencevalue($newval, $sequencename, $table, $column) {
  466. $res = false;
  467. if (isset($this->database[$this->db_name_selected])) {
  468. $res = $this->database[$this->db_name_selected]->set_sequencevalue($newval, $sequencename, $table, $column);
  469. }
  470. return $res;
  471. } // set_sequencevalue
  472. // ....................................................................
  473. /**
  474. * Set the database date style. This affect the format that dates will
  475. * be displayed in, and the format they are submitted in.
  476. * @param string $datestyle The date style code to set
  477. * @return boolean Whether the setting succeeded or not
  478. */
  479. function set_datestyle($datestyle) {
  480. $res = false;
  481. if (isset($this->database[$this->db_name_selected])) {
  482. $res = $this->database[$this->db_name_selected]->set_datestyle($datestyle);
  483. }
  484. return $res;
  485. } // set_datestyle
  486. // ....................................................................
  487. /**
  488. * Set the database character encoding. This affects the encoding of
  489. * characters in the database.
  490. * @param string $encoding The character encoding to set
  491. * @return boolean Whether the setting succeeded or not
  492. */
  493. function set_char_encoding($encoding) {
  494. $res = false;
  495. if (isset($this->database[$this->db_name_selected])) {
  496. $res = $this->database[$this->db_name_selected]->set_char_encoding($encoding);
  497. }
  498. return $res;
  499. } // set_char_encoding
  500. // ....................................................................
  501. /**
  502. * General-purpose lock method. We pass the elements of the lock, which
  503. * is the list of tables to lock, and the lock-mode. The latter mode is
  504. * database-specific, and therefore flexible.
  505. * @param string $tablelist List of tables to lock, comma-delimited
  506. * @param string $mode Databes-specific locking-mode or type
  507. * @return boolean Whether the setting succeeded or not
  508. */
  509. function lock($tablelist, $mode) {
  510. $res = false;
  511. if (isset($this->database[$this->db_name_selected])) {
  512. $res = $this->database[$this->db_name_selected]->lock($tablelist, $mode);
  513. }
  514. return $res;
  515. } // lock
  516. // ....................................................................
  517. /**
  518. * Given an Axyl SQL query object, build the SQL string from it
  519. * in suitable format for the currently connected database server.
  520. * @param object $sqlquery An Axyl query object
  521. * @return string The SQL string built from the query object
  522. */
  523. function SQL($sqlquery) {
  524. $res = false;
  525. if (isset($this->database[$this->db_name_selected])) {
  526. $res = $this->database[$this->db_name_selected]->SQL($sqlquery);
  527. }
  528. return $res;
  529. } // SQL
  530.  
  531.  
  532.  
  533. } // datasources class
  534. // ----------------------------------------------------------------------
  535.  
  536. /**
  537. * Define a database. This is a parent class to all of the supported
  538. * database flavours. It holds the main data describing a database
  539. * and it's connection. The actual functionality to connect to a
  540. * physical database and access its data is defined in the child
  541. * classes of this one. For example, see file 'db-postgres.php'.
  542. *
  543. * Normal users of the system should not have to deal with this
  544. * class directly.
  545. *
  546. * The datasources class is a container for multiple 'databases' or
  547. * instances of this class.
  548. * @package database
  549. */
  550. class database {
  551. // Public
  552. /** Type of database eg: "postgres", "mssql_server".. */
  553.  
  554. var $type = "";
  555. /** Name of this database */
  556.  
  557. var $name = "";
  558. /** Host server of this database */
  559.  
  560. var $host = "";
  561. /** Port to access it via TCP */
  562.  
  563. var $port = 0;
  564. /** Default user to connect as */
  565.  
  566. var $user = "";
  567. /** Default password to connect as */
  568.  
  569. var $passwd = "";
  570. /** Flag true if database was connected ok */
  571.  
  572. var $connected = false;
  573.  
  574. // Private
  575. /** True if we want a persistent connection
  576. @access private */
  577. var $persistent = false;
  578. /** Unique identifier for database access
  579. @access private */
  580. var $dbid = false;
  581. /** The SQL statement last executed on this database. This
  582. value is set in the underlying DB module, query() method.
  583. @access private */
  584. var $executable_sql = "";
  585. /** The result ID last returned by a query on this DB. Also
  586. set in the underlying DB module query()
  587. @access private */
  588. var $rid;
  589. /** Microtimer for query execute timing
  590. @access private */
  591. var $timer;
  592. // ....................................................................
  593. /**
  594. * Constructor
  595. * @param string $name The database name
  596. * @param string $user The username of user who can access the database
  597. * @param string $passwd The user password which can access the database
  598. * @param string $host The hostname of the machine running the database
  599. * @param integer $port The port number of the database server
  600. */
  601. function database($name="", $user="", $passwd="", $host="", $port=0) {
  602. $this->name = $name;
  603.  
  604. // If host and port ar not specified, then
  605. // we assume the database is local..
  606. $this->host = $host;
  607. $this->port = $port;
  608.  
  609. // These can be used as defaults..
  610. $this->user = $user;
  611. $this->passwd = $passwd;
  612.  
  613. // Timer..
  614. $this->timer = new microtimer();
  615. } // database
  616.  
  617. // ....................................................................
  618. /** This method must be defined in the child class.
  619. * @abstract
  620. */
  621. function connect($persistent) {
  622. return false;
  623. }
  624. // ....................................................................
  625. /** This method must be defined in the child class.
  626. * @abstract
  627. */
  628. function disconnect() {
  629. }
  630. // ....................................................................
  631. /** This method must be defined in the child class.
  632. * @abstract
  633. */
  634. function query($sql) {
  635. return false;
  636. }
  637. // ....................................................................
  638. /**
  639. * This method is usually called after the underlying DB module query()
  640. * method has executed the query. It examines the returned code and
  641. * if debugging is enabled it reports the query & stats accordingly.
  642. * Usually an internally executed method only.
  643. * @access private
  644. */
  645. function query_report() {
  646. global $SQL_EXEC_THRESHOLD, $RESPONSE;
  647. // Now examine the result..
  648. if ($this->rid != false) {
  649. if (debugging()) {
  650. $errstr = "QOK: $this->executable_sql";
  651. $errstr .= " (Time: " . $this->timer->formatted_millisecs() . "mS)";
  652. debugbr($errstr, DBG_SQL);
  653. }
  654. // Log excessive query execution times to syslog..
  655. if (isset($SQL_EXEC_THRESHOLD) && $SQL_EXEC_THRESHOLD > 0) {
  656. if ($this->timer->millisecs() > $SQL_EXEC_THRESHOLD) {
  657. $errstr = APP_NAME . ": " . $timer->formatted_millisecs() . "mS ";
  658. $errstr .= "Exceeds Threshold ($SQL_EXEC_THRESHOLD): $executed_sql";
  659. error_log($errstr);
  660. }
  661. }
  662. }
  663. else {
  664. // Log the failed query..
  665. $errstr = "QFAIL: " . APP_NAME . ": $this->executable_sql";
  666. error_log($errstr, 0);
  667. $db_err = $RESPONSE->datasource->errormessage();
  668. if ($db_err) $errstr .= " DBSERVER: $db_err";
  669. $this->last_errormsg = $errstr;
  670. if (debugging()) {
  671. debugbr($errstr, DBG_SQL);
  672. }
  673. // Set failed status for any open transaction..
  674. if ($global_tran->open) {
  675. $global_tran->failed = true;
  676. $global_tran->failed_msg = $errstr;
  677. }
  678. }
  679. } // query_report
  680. // ....................................................................
  681. /** This method must be defined in the child class.
  682. * @abstract
  683. */
  684. function numrows($rid) {
  685. return 0;
  686. }
  687. // ....................................................................
  688. /** This method must be defined in the child class.
  689. * @abstract
  690. */
  691. function affectedrows($rid) {
  692. return 0;
  693. }
  694. // ....................................................................
  695. /** This method must be defined in the child class.
  696. * @abstract
  697. */
  698. function freeresult($rid) {
  699. }
  700. // ....................................................................
  701. /** This method must be defined in the child class.
  702. * @abstract
  703. */
  704. function errormessage($rid) {
  705. return "";
  706. }
  707. // ....................................................................
  708. /** This method must be defined in the child class.
  709. * @abstract
  710. */
  711. function fetch_row($rid, $rowno) {
  712. return false;
  713. }
  714. // ....................................................................
  715. /** This method must be defined in the child class.
  716. * @abstract
  717. */
  718. function fetch_array($rid, $rowno) {
  719. return false;
  720. }
  721. // ....................................................................
  722. /**
  723. * Start a new database transaction.
  724. * @return boolean Whether transaction was started or not
  725. */
  726. function begin_transaction() {
  727. return $this->query("BEGIN");
  728. }
  729. // ....................................................................
  730. /**
  731. * Commit the currently open database transaction.
  732. * @return boolean Whether the commit succeeded or not
  733. */
  734. function commit() {
  735. return $this->query("COMMIT");
  736. }
  737. // ....................................................................
  738. /**
  739. * Rollback the currently open database transaction.
  740. * @return boolean Whether the rollback succeeded or not
  741. */
  742. function rollback() {
  743. return $this->query("ROLLBACK");
  744. }
  745. // ....................................................................
  746. /**
  747. * Return a Php boolean from a database field value. The database field
  748. * is expected to be a container of some form of logical value. Here
  749. * is where we convert it according to the current database.
  750. * @param mixed $dbvalue The value from the database field to convert
  751. * @return boolean The boolean value derived from the field value
  752. */
  753. function bool_from_db_value($dbvalue) {
  754. return ($dbvalue == 1);
  755. }
  756. // ....................................................................
  757. /**
  758. * Return a suitable database field value to contain the value for
  759. * the given boolean.
  760. * @param boolean $boolvalue The boolean value to convert
  761. * @return mixed The value suitable for the database field
  762. */
  763. function db_value_from_bool($boolvalue) {
  764. return $boolvalue ? 1 : 0;
  765. }
  766. // ....................................................................
  767. /**
  768. * Return the current sequence value, given a sequence name, the table
  769. * and the field it applies to.
  770. * @param string $sequencename The name of the sequence to use
  771. * @param string $table The name of the table the sequence is for
  772. * @param string $column The name of the table column the sequence is for
  773. * @return integer The current sequence value
  774. */
  775. function current_sequencevalue($sequencename, $table, $column) {
  776. $seq = 0;
  777. $rid = $this->query("SELECT MAX($column) FROM $table" );
  778. if ($rid !== false) {
  779. $row = $this->fetch_row($rid, 0);
  780. $seq = $row[0];
  781. }
  782. return $seq;
  783. }
  784. // ....................................................................
  785. /**
  786. * Return the next sequence value, given a sequence name, the table
  787. * and the field it applies to.
  788. * @param string $sequencename The name of the sequence to use
  789. * @param string $table The name of the table the sequence is for
  790. * @param string $column The name of the table column the sequence is for
  791. * @return integer The next sequence value
  792. */
  793. function next_sequencevalue($sequencename, $table, $column) {
  794. return (1 + $this->current_sequencevalue($sequencename, $table, $column));
  795. }
  796. // ....................................................................
  797. /** This method must be defined in the child class.
  798. * @abstract
  799. */
  800. function set_sequencevalue($newval, $sequencename, $table, $column) {
  801. return true;
  802. }
  803. // ....................................................................
  804. /** This method must be defined in the child class.
  805. * @abstract
  806. */
  807. function set_datestyle($datestyle) {
  808. return true;
  809. }
  810. // ....................................................................
  811. /** This method must be defined in the child class.
  812. * @abstract
  813. */
  814. function set_char_encoding($encoding) {
  815. return true;
  816. }
  817. // ....................................................................
  818. /** This method must be defined in the child class.
  819. * @abstract
  820. */
  821. function lock($tablelist, $mode) {
  822. return true;
  823. }
  824. // ....................................................................
  825. /**
  826. * Given an Axyl SQL query object, build the SQL string from it
  827. * in suitable format for the currently connected database server.
  828. * @param pointer $sqlquery Pointer to an Axyl query object
  829. * @return string The SQL string built from the query object
  830. */
  831. function SQL($sqlquery) {
  832. $sql = "";
  833. switch (strtoupper($sqlquery->type)) {
  834. case "SELECT":
  835. $sql .= "SELECT ";
  836. if ($sqlquery->fields->total == 0) $sql .= "*";
  837. else $sql .= $sqlquery->fields->listed();
  838. $sql .= " FROM ";
  839. $sql .= $sqlquery->tables->listed();
  840. if ($sqlquery->where->total > 0) {
  841. $sql .= " WHERE ";
  842. $sql .= $sqlquery->where->listed(" ");
  843. }
  844. if ($sqlquery->groupby->total > 0) {
  845. $sql .= " GROUP BY ";
  846. $sql .= $sqlquery->groupby->listed();
  847. }
  848. if ($sqlquery->orderby->total > 0) {
  849. $sql .= " ORDER BY ";
  850. $sql .= $sqlquery->orderby->listed();
  851. }
  852. break;
  853.  
  854. case "INSERT":
  855. $sql .= "INSERT INTO ";
  856. $sql .= $sqlquery->tables->listed();
  857. if ($sqlquery->fields->total > 0) {
  858. $sql .= " (" . $sqlquery->fields->listed() . ")";
  859. }
  860. $sql .= " VALUES ";
  861. $sql .= "(" . $sqlquery->fields->values() . ")";
  862. break;
  863.  
  864. case "DELETE":
  865. $sql .= "DELETE FROM ";
  866. $sql .= $sqlquery->tables->listed();
  867. if ($sqlquery->where->total > 0) {
  868. $sql .= " WHERE ";
  869. $sql .= $sqlquery->where->listed(" ");
  870. }
  871. break;
  872.  
  873. case "UPDATE":
  874. $sql .= "UPDATE ";
  875. $sql .= $sqlquery->tables->listed();
  876. $sql .= " SET ";
  877. $sql .= $sqlquery->fields->equated();
  878. if ($sqlquery->where->total > 0) {
  879. $sql .= " WHERE ";
  880. $sql .= $sqlquery->where->listed(" ");
  881. }
  882. break;
  883. }
  884. // Render any NULL values..
  885. $sql = str_replace("'".NULLVALUE."'", "NULL", $sql);
  886.  
  887. // Return SQL we have built..
  888. return $sql;
  889. }
  890. // ....................................................................
  891. /**
  892. * Make conversions of boolean syntax found in the SQL string and
  893. * return the 'standardised' SQL. This assumes that Axyl SQL will
  894. * be written in the form 'WHERE foo=TRUE'.
  895. * @param string $sql SQL string to make conversions in
  896. * @return string The converted SQL string
  897. */
  898. function convert_boolean_syntax($sql) {
  899. $fixsql = $sql;
  900. // Quick check is more efficient then regexes..
  901. if (stristr($sql, "TRUE") || stristr($sql, "FALSE")) {
  902. $fixsql = preg_replace("/( WHERE.*?[\S]+=)TRUE/ie", "'\\1'.'1'", $sql);
  903. $fixsql = preg_replace("/( WHERE.*?[\S]+=)FALSE/ie", "'\\1'.'0'", $fixsql);
  904. }
  905. return $fixsql;
  906. }
  907. } // database class
  908. // ----------------------------------------------------------------------
  909.  
  910. ?>

Documentation generated by phpDocumentor 1.3.0RC3