A Qcodo based CMS/ecommerce framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
7.8 KiB

12 years ago
  1. <?php
  2. require_once('QuasiMysql.class.php');
  3. /**
  4. * QuasiDBI - abstract database interface
  5. * This class provides a base class for various
  6. * server types (MySQL, PgSQL, SQLite) which
  7. * are implemented by inheritors (as of 2008-02-20 only
  8. * MySQL is supported.). It is a Singleton class to
  9. * ensure that we only instantiate one database
  10. * connection - this means that you may only access
  11. * it using QuasiDBI::getInstance(). which will
  12. * return an object of type QuasiDBI that is a child
  13. * corresponding to the configured db server type.
  14. *
  15. * This class is provided as a light weight alternative
  16. * to the QCodo DBI (and may one day replace it..) for
  17. * quick/utility queries - eg. it is used by the OsCommerce
  18. * import module.
  19. *
  20. * @author Erik Winn <ewinn@erikwinn.com>
  21. *
  22. * $Id: QuasiDBI.class.php 97 2008-08-29 21:36:11Z erikwinn $
  23. *@version 0.1
  24. *
  25. *@copyright (C) 2008 by Erik Winn
  26. *@license GPL v.2
  27. This program is free software; you can redistribute it and/or modify
  28. it under the terms of the GNU General Public License as published by
  29. the Free Software Foundation; either version 2 of the License, or
  30. (at your option) any later version.
  31. This program is distributed in the hope that it will be useful,
  32. but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. GNU General Public License for more details.
  35. You should have received a copy of the GNU General Public License
  36. along with this program; if not, write to the Free Software
  37. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
  38. *
  39. *@package Quasi
  40. * @subpackage Classes
  41. */
  42. abstract class QuasiDBI {
  43. const BADNESS = 'LOAD_FILE|OUTFILE|DUMPFILE|ESCAPED|TERMINATED|CASCADE|INFILE|X509|TRIGGER|REVOKE';
  44. protected $strDatabase;
  45. protected $strUsername;
  46. protected $strPassword;
  47. protected $strDatabaseServer;
  48. protected $strDatabaseType;
  49. protected static $objDatabaseHandle;
  50. protected static $blnIsConnected = false;
  51. protected $objResultSet;
  52. protected $strDbErrors;
  53. protected $strErrors;
  54. private static $_instance = null;
  55. protected function __construct($strDb=DB_DATABASE,
  56. $strUser=DB_SERVER_USERNAME,
  57. $strPass=DB_SERVER_PASSWORD,
  58. $strServer=DB_SERVER)
  59. {
  60. $this->strDatabase = $strDb;
  61. $this->strUsername = $strUser;
  62. $this->strPassword = $strPass;
  63. $this->strDatabaseServer = $strServer;
  64. if( !$this->connect())
  65. $strErrors .= "QuasiDBI::__construct Failed to connect to database!\n";
  66. }
  67. /** getInstance()
  68. * Returns an appropriate DBI object - you may provide the
  69. * parameter $type to obtain a driver for a different kind
  70. * of server - currently the default is MySQL
  71. *
  72. * Note: see class description for supported $type parameters ..
  73. *
  74. *@access public
  75. *@param string db server type
  76. *@return object QuasiDBI of appropriate type
  77. */
  78. public static function getInstance($type="MySQL")
  79. {
  80. if(!isset(self::$_instance))
  81. {
  82. switch($type)
  83. {
  84. case "MySQL":
  85. self::$_instance = new QuasiMysql();
  86. break;
  87. default:
  88. // for now do nothing - maybe set instance to null for unsupported types and die(errormsg) ..
  89. }
  90. }
  91. // ensure a current connection ..
  92. if(! self::$_instance->blnIsConnected)
  93. self::$_instance->connect();
  94. return self::$_instance;
  95. }
  96. public function isConnected(){ return $this->blnIsConnected;}
  97. // Accessors ..
  98. public function getServerType() {return $this->strDatabaseType;}
  99. public function setServerType($t) {$this->strDatabaseType = $t;}
  100. public function getServerName() {return $this->strDatabaseServer;}
  101. public function setServerName($n) {$this->strDatabaseType = $n;}
  102. public function getDbUser() {return $this->strUsername;}
  103. public function setDbUser($u) {$this->strUsername = $u;}
  104. public function setDbPassword($p) {$this->strPassword = $p;}
  105. public function getDbPassword() {return $this->strPassword;}
  106. public function getDbName() {return $this->strDatabase;}
  107. public function setDbName($n) {$this->strDatabase = $n;}
  108. public function getErrors() {return $this->strErrors . "\n" . $this->getDbError();}
  109. public function reconnect($strNewHost = DB_SERVER,
  110. $strNewDatabase = DB_DATABASE,
  111. $strNewUsername = DB_SERVER_USERNAME,
  112. $strNewPassword = DB_SERVER_PASSWORD)
  113. {
  114. $this->disconnect();
  115. $this->blnIsConnected = false;
  116. $this->setServerName($strNewHost);
  117. $this->setDbName($strNewDatabase);
  118. $this->setDbUser($strNewUsername);
  119. $this->setDbPassword($strNewPassword);
  120. if( ! $this->connect())
  121. return false;
  122. $this->blnIsConnected = true;
  123. return true;
  124. }
  125. // Utilities ..
  126. /** prepInput()
  127. * Returns string(s) suitable for database input with escaped
  128. * single quotes, slashes, etc.. Note: this is only accessable via
  129. * child classes - and used only if there is no valid objDatabaseHandle ..
  130. * which is unlikely and this may dissappear in future..
  131. *
  132. *@access protected
  133. *@param mixed mixInput string or array to process
  134. * @return string the prepared string
  135. */
  136. protected function prepInput($mixInput)
  137. {
  138. if(is_string($mixInput))
  139. {
  140. if( get_magic_quotes_gpc() )
  141. return $mixInput;
  142. return addslashes($mixInput);
  143. }
  144. elseif (is_array($mixInput))
  145. {
  146. foreach($mixInput as $key => &$value)
  147. $mixInput[$key] = $this->prepInput($value);
  148. return $mixInput;
  149. } else
  150. return $mixInput;
  151. }
  152. public function isBad($strQuery)
  153. {
  154. if(eregi(self::BADNESS, $strQuery))
  155. return true;
  156. return false;
  157. }
  158. /** fetchRow()
  159. * Returns the first row of results for a given query.
  160. *
  161. *@access public
  162. *@param string $strQuery query to process.
  163. * @return array results
  164. */
  165. public function fetchRow ($strQuery)
  166. {
  167. $resultset = $this->doQuery($strQuery, true);
  168. return $this->nextRow($resultset);
  169. }
  170. /** getResultSet()
  171. * Returns a new result set for a given query, or the current
  172. * resultset internal to this class if not called with a query
  173. * string.
  174. *
  175. * This is essentially equivalent to doQuery($string, true).
  176. *
  177. *@access public
  178. *@param string strQuery query to process.
  179. * @return mixed mysqli_result or false
  180. */
  181. public function getResultSet ($strQuery)
  182. {
  183. // Did we get a strQuery? If so, get new results and pass them on...
  184. if ($strQuery)
  185. return $this->doQuery($strQuery, true);
  186. }
  187. // Abstract public functions .. see child class for more complete documentation.
  188. // connect to server
  189. abstract public function connect();
  190. // close connection
  191. abstract public function disconnect();
  192. // return last insert id
  193. abstract public function getInsertId();
  194. //send a query, return resultset handle or false on failure.
  195. abstract public function doQuery($strQuery, $blnReturnResultSet=false, $blnUnbuffered=true);
  196. // fetch the next row as an associative array, optionally supply result set to use, return 0 after last row.
  197. abstract public function nextRow($objResultSet=null, $blnReturnAssocArray = true);
  198. // return a string containing the error from the last query.
  199. abstract public function getDbError();
  200. // perform a simple insert or update using an array of values with optional where clause
  201. abstract public function insertArray($strTable, $aryValues, $strAction = "INSERT", $strWhereClause = "");
  202. // return number of rows in last result set, passing the result set is optional,
  203. // but _must_ be used in the case that you are using doQuery($q,true)
  204. abstract public function getNumRows($objResultSet=NULL);
  205. abstract public function changeDatabase($strNewDatabase);
  206. }
  207. ?>