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

Documentation generated by phpDocumentor 1.3.0RC3