PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_virtuemart/classes/ps_database.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 448 lines | 264 code | 45 blank | 139 comment | 44 complexity | 5c0a2269786d063828da85a07b9eb79f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. *
  5. * @version $Id: ps_database.php 1397 2008-05-10 03:34:46Z gregdev $
  6. * @package VirtueMart
  7. * @subpackage classes
  8. * @copyright Copyright (C) 2004-2007 soeren - All rights reserved.
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  10. * VirtueMart is free software. This version may have been modified pursuant
  11. * to the GNU General Public License, and as distributed it includes or
  12. * is derivative of works licensed under the GNU General Public License or
  13. * other free or open source software licenses.
  14. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  15. *
  16. * http://virtuemart.net
  17. */
  18. /***********************************************************************
  19. Wrapper Class for the $database - Object
  20. ************************************************************************/
  21. class ps_DB {
  22. /** @var int Current row in query result set */
  23. var $row = 0;
  24. /** @var stdclass Current row record data */
  25. var $record = null;
  26. /** @var string Error Message */
  27. var $error = "";
  28. /** @var int Error Number */
  29. var $errno = "";
  30. /** @var string The current sql Query */
  31. var $_sql = "";
  32. /** @var boolean Flag to see if a query has been renewed between two query calls */
  33. var $_query_set= false;
  34. /** @var boolean true if next_record has already been called */
  35. var $called = false;
  36. /** @var database The core database object */
  37. var $_database = null;
  38. function ps_DB() {
  39. if( is_callable(array('jfactory', 'getdbo'))) {
  40. $this->_database =& jfactory::getDBO();
  41. } else {
  42. $this->_database =& $GLOBALS['database'];
  43. }
  44. }
  45. /**
  46. * Clone an object
  47. *
  48. * @param mixed $obj
  49. * @return mixed copy of $obj
  50. */
  51. function _clone( $obj ) {
  52. return $obj;
  53. }
  54. /**
  55. * Sets the SQL query string for later execution.
  56. *
  57. * This function replaces a string identifier <var>$prefix</var> with the
  58. * string held is the <var>_table_prefix</var> class variable.
  59. *
  60. * @param string The SQL query
  61. */
  62. function setQuery( $sql ) {
  63. $vm_prefix = "{vm}";
  64. $sql = trim( $sql );
  65. $this->_sql = trim(str_replace( $vm_prefix, VM_TABLEPREFIX, $sql ));
  66. $this->_database->setQuery( $this->_sql );
  67. $this->_query_set = true;
  68. if( defined('DEBUG') && DEBUG == '1' ) {
  69. // Register Double-run (multiple-run) queries
  70. if( !isset($GLOBALS['queries']))$GLOBALS['queries'] = array();
  71. if( !isset($GLOBALS['double_queries']))$GLOBALS['double_queries'] = array();
  72. if( !in_array($this->_database->_sql, $GLOBALS['queries'] ) ) {
  73. $GLOBALS['queries'][] = $this->_database->_sql;
  74. } else {
  75. $GLOBALS['double_queries'][] = $this->_database->_sql;
  76. }
  77. }
  78. }
  79. /**
  80. * Runs query and sets up the query id for the class.
  81. *
  82. * @param string The SQL query
  83. */
  84. function query( $q='' ) {
  85. global $mosConfig_dbprefix, $mosConfig_debug, $vmLogger;
  86. $prefix = "#__";
  87. $vm_prefix = "{vm}";
  88. if (empty($q) ) {
  89. if( empty($this->_sql)) {
  90. $vmLogger->debug( '"'.__CLASS__.'::'.__FUNCTION__.'" called without a pending query.');
  91. }
  92. elseif( !$this->_query_set ) {
  93. $vmLogger->debug( '"'.__CLASS__.'::'.__FUNCTION__.'": A query was run twice without having changed the SQL text.');
  94. }
  95. }
  96. else {
  97. $this->setQuery( $q );
  98. }
  99. $this->row = 0;
  100. $this->called = false;
  101. $this->record = null;
  102. $this->record = Array(0);
  103. if (strtoupper(substr( $this->_sql , 0, 6 )) == "SELECT"
  104. || strtoupper(substr( $this->_sql , 0, 4 ))=='SHOW'
  105. || strtoupper(substr( $this->_sql , 0, 7 ))=='EXPLAIN'
  106. || strtoupper(substr( $this->_sql , 0, 8 ))=='DESCRIBE'
  107. ) {
  108. $this->record = $this->_database->loadObjectList();
  109. if( $this->record === false ) {
  110. $result = false;
  111. }
  112. }
  113. else {
  114. $result = $this->_database->query();
  115. }
  116. $this->_query_set = false;
  117. if( isset( $result )) {
  118. return $result;
  119. }
  120. }
  121. /**
  122. * Returns the next row in the RecordSet for the last query run.
  123. *
  124. * @return boolean False if RecordSet is empty or the pointer is at the end.
  125. */
  126. function next_record() {
  127. global $vmLogger;
  128. if ( empty( $this->_sql ) ) {
  129. $vmLogger->debug( '"'.__CLASS__.'::'.__FUNCTION__.'()" called with no query pending.' );
  130. return false;
  131. }
  132. if ( $this->called ) {
  133. $this->row++;
  134. }
  135. else {
  136. $this->called = true;
  137. }
  138. if ($this->row < sizeof( $this->record ) ) {
  139. return true;
  140. }
  141. else {
  142. $this->row--;
  143. return false;
  144. }
  145. }
  146. function nextRow() {
  147. return isset( $this->record[$this->row + 1] ) ? $this->record[$this->row + 1] : false;
  148. }
  149. function previousRow() {
  150. return isset( $this->record[$this->row - 1] ) ? $this->record[$this->row - 1] : false;
  151. }
  152. /**
  153. * Returns the value of the given field name for the current
  154. * record in the RecordSet.
  155. * f == fetch
  156. * @param string The field name
  157. * @param boolean Strip slashes from the data?
  158. * @return string the value of the field $field_name in the recent row of the record set
  159. */
  160. function f($field_name, $stripslashes=true) {
  161. if (isset($this->record[$this->row]->$field_name)) {
  162. if($stripslashes) {
  163. return( stripslashes( $this->record[$this->row]->$field_name ) );
  164. }
  165. else {
  166. return( $this->record[$this->row]->$field_name );
  167. }
  168. }
  169. }
  170. /**
  171. * Returns the value of the field name from the $vars variable
  172. * if it is set, otherwise returns the value of the current
  173. * record in the RecordSet. Useful for handling forms that have
  174. * been submitted with errors. This way, fields retain the values
  175. * sent in the $vars variable (user input) instead of the database values.
  176. * sf == selective fetch
  177. * @param string The field name
  178. * @param boolean Strip slashes from the data?
  179. * @return string the value of the field $field_name in the recent row of the record set
  180. */
  181. function sf($field_name, $stripslashes=true) {
  182. global $vars, $default;
  183. if ((defined( '_VM_LOG_ERRORS' ) || isset($vars["error"])) && !empty($vars["$field_name"])) {
  184. if($stripslashes) {
  185. return stripslashes($vars[$field_name] );
  186. }
  187. else {
  188. return( $vars[$field_name] );
  189. }
  190. }
  191. elseif (isset($this->record[$this->row]->$field_name)) {
  192. if($stripslashes) {
  193. return stripslashes($this->record[$this->row]->$field_name );
  194. }
  195. else {
  196. return( $this->record[$this->row]->$field_name );
  197. }
  198. }
  199. elseif (isset($default[$field_name])) {
  200. if($stripslashes) {
  201. return stripslashes($default[$field_name]);
  202. }
  203. else {
  204. return( $default[$field_name] );
  205. }
  206. }
  207. }
  208. /**
  209. * Prints the value of the given field name for the current
  210. * record in the RecordSet.
  211. * p == print
  212. * @param string The field name
  213. * @param boolean Strip slashes from the data?
  214. */
  215. function p($field_name, $stripslashes=true) {
  216. echo $this->f( $field_name, $stripslashes );
  217. }
  218. /**
  219. * Prints the value of the field name from the $vars variable
  220. * if it is set, otherwise prints the value of the current
  221. * record in the RecordSet. Useful for handling forms that have
  222. * been submitted with errors. This way, fields retain the values
  223. * sent in the $vars variable (user input) instead of the database
  224. * values.
  225. * sp == selective print
  226. * @param string The field name
  227. * @param boolean Strip slashes from the data?
  228. */
  229. function sp($field_name, $stripslashes=true) {
  230. echo $this->sf( $field_name, $stripslashes);
  231. }
  232. /**
  233. * Returns the object of the current row in the rowset
  234. *
  235. * @return mixed
  236. */
  237. function get_row() {
  238. return $this->record[$this->row];
  239. }
  240. /**
  241. * Returns the number of rows in the RecordSet from a query.
  242. * @return int
  243. */
  244. function num_rows() {
  245. return sizeof( $this->record );
  246. }
  247. /**
  248. * Returns the ID of the last AUTO_INCREMENT INSERT.
  249. *
  250. * @return int
  251. */
  252. function last_insert_id() {
  253. return $this->_database->insertid();
  254. }
  255. /**
  256. * returns true when the actual row is the last record in the record set
  257. * otherwise returns false
  258. *
  259. * @return boolean
  260. */
  261. function is_last_record() {
  262. return ($this->row+1 >= $this->num_rows());
  263. }
  264. /**
  265. * Set the "next_record" pointer back to the first row.
  266. *
  267. */
  268. function reset() {
  269. $this->row = 0;
  270. $this->called = false;
  271. }
  272. /**
  273. * Returns the current row of the recordset
  274. * @since VirtueMart 1.1.0
  275. * @return stdClass Object
  276. */
  277. function getCurrentRow() {
  278. return $this->record[$this->row];
  279. }
  280. /**
  281. * Query Builder Functions
  282. * @author soeren
  283. * @since VirtueMart 1.1.0
  284. *
  285. * @param string $type Either INSERT or UPDATE
  286. * @param string $table Example: #__{vm}_user_info
  287. * @param array $values Array of the format array( FieldName => Value ), Example: array( 'user_info_id' => md5( $hash ) )
  288. * @param string $whereClause
  289. *
  290. */
  291. function buildQuery( $type='INSERT', $table, $values, $whereClause='', $doNotEnclose=array() ) {
  292. global $vmLogger;
  293. if( empty($table) || empty($values)) {
  294. return;
  295. }
  296. $table = trim( $table );
  297. $type = trim( $type );
  298. $type = strtoupper($type);
  299. switch( $type ) {
  300. case 'INSERT':
  301. case 'REPLACE':
  302. $q = "$type INTO `$table` (`";
  303. $q .= implode( "`,\n`", array_keys($values) );
  304. $q .= "`) VALUES (\n";
  305. $count = count( $values );
  306. $i = 1;
  307. foreach ( $values as $key => $value ) {
  308. if( in_array( $key, $doNotEnclose )) {
  309. // Important when using MySQL functions like "AES_ENCRYPT", "ENCODE", "REPLACE" or such
  310. $q .= $value;
  311. }
  312. else {
  313. $q .= '\'' . $this->getEscaped($value)."'\n";
  314. }
  315. if( $i++ < $count ) {
  316. $q.= ',';
  317. }
  318. }
  319. $q .= ')';
  320. break;
  321. case 'UPDATE':
  322. $q = "UPDATE `$table` SET ";
  323. $count = count( $values );
  324. $i = 1;
  325. foreach ( $values as $key => $value ) {
  326. if( in_array( $key, $doNotEnclose )) {
  327. // Important when using MySQL functions like "AES_ENCRYPT", "ENCODE", "REPLACE" or such
  328. $q .= "`$key` = ".$value;
  329. }
  330. else {
  331. //$q .= '\'' . $this->getEscaped($value)."'\n";
  332. $q .= "`$key` = '" . $this->getEscaped($value)."'";
  333. }
  334. if( $i++ < $count ) {
  335. $q.= ",\n";
  336. }
  337. }
  338. $q .= "\n$whereClause";
  339. break;
  340. default:
  341. $vmLogger->debug( 'Function '.__FUNCTION__.' can\'t build a query of the type "'.$type.'"' );
  342. return;
  343. }
  344. $this->setQuery( $q );
  345. }
  346. /**
  347. * @param array A list of valid (and safe!) table names
  348. * @return array An array of fields by table
  349. */
  350. function getTableFields( $tables ) {
  351. $result = array();
  352. foreach ($tables as $tblval) {
  353. $this->setQuery( 'SHOW FIELDS FROM ' . $tblval );
  354. $fields = $this->loadObjectList();
  355. foreach ($fields as $field) {
  356. $result[$tblval][$field->Field] = preg_replace("/[(0-9)]/",'', $field->Type );
  357. }
  358. }
  359. return $result;
  360. }
  361. ///////////////////////////////
  362. // Parental Database functions
  363. // We must overwrite them because
  364. // we still use a global database
  365. // object, not a ps_DB object
  366. ///////////////////////////////
  367. function loadResult() {
  368. return $this->_database->loadResult();
  369. }
  370. function loadResultArray($numinarray = 0) {
  371. return $this->_database->loadResultArray( $numinarray );
  372. }
  373. function loadAssocList( $key='' ) {
  374. return $this->_database->loadAssocList( $key );
  375. }
  376. function loadObject( &$object ) {
  377. return $this->_database->loadObject($object);
  378. }
  379. function loadObjectList( $key='' ) {
  380. return $this->_database->loadObjectList( $key );
  381. }
  382. function loadRow() {
  383. return $this->_database->loadRow();
  384. }
  385. function loadRowList( $key='' ) {
  386. return $this->_database->loadRowList($key);
  387. }
  388. function getErrorMsg() {
  389. return $this->_database->getErrorMsg();
  390. }
  391. function getErrorNum() {
  392. return $this->_database->getErrorNum();
  393. }
  394. function stderr() {
  395. return $this->_database->stderr();
  396. }
  397. function getEscaped( $text ) {
  398. return $this->_database->getEscaped( $text );
  399. }
  400. }
  401. ?>