PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/classes/class.dbhandler.php

https://github.com/Sunlightshadow/wowarmory
PHP | 266 lines | 182 code | 18 blank | 66 comment | 27 complexity | 56e775a251444188ceb314bd56ca0e6a MD5 | raw file
  1. <?php
  2. /**
  3. * @package World of Warcraft Armory
  4. * @version Release Candidate 1
  5. * @revision 365
  6. * @copyright (c) 2009-2010 Shadez
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. **/
  23. if(!defined('__ARMORY__')) {
  24. die('Direct access to this file not allowed!');
  25. }
  26. /** Database Query Types **/
  27. define('SINGLE_CELL', 0x01);
  28. define('SINGLE_ROW', 0x02);
  29. define('MULTIPLY_ROW', 0x03);
  30. define('SQL_QUERY', 0x04);
  31. define('OBJECT_QUERY', 0x05);
  32. define('SQL_RAW_QUERY', 0x06);
  33. Class ArmoryDatabaseHandler {
  34. private $dbLink = false;
  35. private $connectionLink = false;
  36. private $databaseInfo = array();
  37. /** Queries counter **/
  38. private $queryCount = 0;
  39. private $queryTimeGeneration = 0;
  40. private $logHandler;
  41. private $armory_prefix = null;
  42. /**
  43. * Connect to DB
  44. * @category Armory Database Handler
  45. * @access public
  46. * @param string $host
  47. * @param string $user
  48. * @param string $password
  49. * @param string $dbName
  50. * @param string $charset = false
  51. * @param string $logHandler = null
  52. * @param string $prefix = 'armory'
  53. * @return bool
  54. **/
  55. public function ArmoryDatabaseHandler($host, $user, $password, $dbName, $charset = false, $logHandler = null, $prefix = null) {
  56. $this->connectionLink = @mysql_connect($host, $user, $password, true);
  57. $this->dbLink = @mysql_select_db($dbName, $this->connectionLink);
  58. if($charset === false) {
  59. $this->query("SET NAMES UTF8");
  60. }
  61. else {
  62. $this->query("SET NAMES %s", $charset);
  63. }
  64. $this->logHandler = $logHandler;
  65. $this->databaseInfo = array(
  66. 'host' => $host,
  67. 'user' => $user,
  68. 'password' => $password,
  69. 'name' => $dbName,
  70. 'charset' => ($charset === false) ? 'UTF8' : $charset,
  71. );
  72. $this->armory_prefix = $prefix;
  73. return true;
  74. }
  75. /**
  76. * Returns current database info
  77. * @category Armory Database Handler
  78. * @access public
  79. * @param string $info
  80. * @return mixed
  81. **/
  82. public function GetDatabaseInfo($info) {
  83. return (isset($this->databaseInfo[$info])) ? $this->databaseInfo[$info] : false;
  84. }
  85. /**
  86. * Tests conection link
  87. * @category Armory Database Handler
  88. * @access public
  89. * @return bool
  90. **/
  91. public function TestLink() {
  92. if($this->connectionLink == true) {
  93. return true;
  94. }
  95. return false;
  96. }
  97. /**
  98. * Execute SQL query
  99. * @category Armory Database Handler
  100. * @access public
  101. * @param string $safe_sql
  102. * @param int $queryType
  103. * @return mixed
  104. **/
  105. private function _query($safe_sql, $queryType) {
  106. // Execute query and calculate execution time
  107. $make_array = array();
  108. $query_start = microtime(true);
  109. $this->queryCount++;
  110. $performed_query = @mysql_query($safe_sql, $this->connectionLink);
  111. if($performed_query === false) {
  112. if($this->logHandler != null && is_object($this->logHandler)) {
  113. $this->logHandler->writeLog('%s : unable to execute SQL query (%s). MySQL error: %s', __METHOD__, $safe_sql, mysql_error() ? mysql_error() : 'none');
  114. }
  115. return false;
  116. }
  117. $result = false;
  118. switch($queryType) {
  119. case SINGLE_CELL:
  120. $result = @mysql_result($performed_query, 0);
  121. break;
  122. case SINGLE_ROW:
  123. $result = @mysql_fetch_array($performed_query);
  124. if(is_array($result)) {
  125. foreach($result as $rKey => $rValue) {
  126. if(is_string($rKey)) {
  127. $make_array[$rKey] = $rValue;
  128. }
  129. }
  130. $result = $make_array;
  131. }
  132. break;
  133. case MULTIPLY_ROW:
  134. $result = array();
  135. while($_result = @mysql_fetch_array($performed_query)) {
  136. if(is_array($_result)) {
  137. foreach($_result as $rKey => $rValue) {
  138. if(is_string($rKey)) {
  139. $make_array[$rKey] = $rValue;
  140. }
  141. }
  142. $result[] = $make_array;
  143. }
  144. else {
  145. $result[] = $_result;
  146. }
  147. }
  148. break;
  149. case OBJECT_QUERY:
  150. $result = array();
  151. while($_result = @mysql_fetch_object($performed_query)) {
  152. $result[] = $_result;
  153. }
  154. break;
  155. case SQL_QUERY:
  156. $result = true;
  157. break;
  158. default:
  159. $result = false;
  160. break;
  161. }
  162. $query_end = microtime(true);
  163. $queryTime = round($query_end - $query_start, 4);
  164. $this->queryCount++;
  165. $this->queryTimeGeneration += $queryTime;
  166. return $result;
  167. }
  168. private function _prepareQuery($funcArgs, $numArgs, $query_type) {
  169. // funcArgs[0] - SQL query text (with placeholders)
  170. if($query_type != SQL_RAW_QUERY) {
  171. for($i = 1; $i < $numArgs; $i++) {
  172. if(is_string($funcArgs[$i])) {
  173. $funcArgs[$i] = addslashes($funcArgs[$i]);
  174. }
  175. if(is_array($funcArgs[$i])) {
  176. $funcArgs[$i] = $this->ConvertArray($funcArgs[$i]);
  177. }
  178. }
  179. }
  180. $safe_sql = call_user_func_array('sprintf', $funcArgs);
  181. if(preg_match('/ARMORYDBPREFIX/', $safe_sql)) {
  182. if($this->armory_prefix == null) {
  183. $this->logHandler->writeError('%s : fatal error: armory database prefix is not defined, unable to execute SQL query (%s)!', __METHOD__, $safe_sql);
  184. return false;
  185. }
  186. $safe_sql = str_replace('ARMORYDBPREFIX', $this->armory_prefix, $safe_sql);
  187. }
  188. return $this->_query($safe_sql, $query_type);
  189. }
  190. public function selectCell($query) {
  191. $funcArgs = func_get_args();
  192. $numArgs = func_num_args();
  193. return $this->_prepareQuery($funcArgs, $numArgs, SINGLE_CELL);
  194. }
  195. public function selectRow($query) {
  196. $funcArgs = func_get_args();
  197. $numArgs = func_num_args();
  198. return $this->_prepareQuery($funcArgs, $numArgs, SINGLE_ROW);
  199. }
  200. public function select($query) {
  201. $funcArgs = func_get_args();
  202. $numArgs = func_num_args();
  203. return $this->_prepareQuery($funcArgs, $numArgs, MULTIPLY_ROW);
  204. }
  205. public function query($query) {
  206. $funcArgs = func_get_args();
  207. $numArgs = func_num_args();
  208. return $this->_prepareQuery($funcArgs, $numArgs, SQL_QUERY);
  209. }
  210. public function RawQuery($query) {
  211. $funcArgs = func_get_args();
  212. $numArgs = func_num_args();
  213. return $this->_prepareQuery($funcArgs, $numArgs, SQL_RAW_QUERY);
  214. }
  215. public function selectObject($query) {
  216. $funcArgs = func_get_args();
  217. $numArgs = func_num_args();
  218. return $this->_prepareQuery($funcArgs, $numArgs, OBJECT_QUERY);
  219. }
  220. /**
  221. * Converts array values to string format (for IN(%s) cases)
  222. * @category Armory Database Handler
  223. * @access public
  224. * @param array $source
  225. * @return string
  226. **/
  227. private function ConvertArray($source) {
  228. if(!is_array($source)) {
  229. $this->logHandler->writeError('%s : source must have array type!', __METHOD__);
  230. return null;
  231. }
  232. $returnString = null;
  233. $count = count($source);
  234. for($i = 0; $i < $count; $i++) {
  235. if(!isset($source[$i])) {
  236. continue;
  237. }
  238. if($i) {
  239. $returnString .= ", '" . addslashes($source[$i]) . "'";
  240. }
  241. else {
  242. $returnString .="'" . addslashes($source[$i]) . "'";
  243. }
  244. }
  245. return $returnString;
  246. }
  247. }
  248. ?>