PageRenderTime 53ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/cod_daemon/lib/classes/Database.php

https://code.google.com/p/php-blackops-rcon/
PHP | 261 lines | 103 code | 33 blank | 125 comment | 8 complexity | 6a5c814b90ee45df0bf7c9ee01627e18 MD5 | raw file
  1. <?php defined('ROOT_PATH') or die('No direct script access.');
  2. /**
  3. * Database system
  4. *
  5. * Copyright (c) 2011, EpicLegion
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  17. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  18. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  20. * POSSIBILITY OF SUCH DAMAGE.
  21. *
  22. * @author EpicLegion
  23. * @package cod_daemon
  24. * @subpackage core
  25. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  26. */
  27. class Database
  28. {
  29. /**
  30. * Last instance
  31. *
  32. * @var Database
  33. */
  34. protected static $instance = NULL;
  35. /**
  36. * MySQL link
  37. *
  38. * @var resource
  39. */
  40. protected $link = NULL;
  41. /**
  42. * Database prefix
  43. *
  44. * @var string
  45. */
  46. protected $prefix = '';
  47. /**
  48. * Constructor
  49. *
  50. * @param array $config
  51. * @throws Exception
  52. */
  53. public function __construct(array $config = array())
  54. {
  55. // Empty config
  56. if (empty($config))
  57. {
  58. throw new Exception('Invalid database configuration. Please check your config/core/database.yml file.');
  59. }
  60. // Set prefix
  61. $this->prefix = $config['prefix'];
  62. // Define as const
  63. if(!defined('PREFIX')) define('PREFIX', $config['prefix']);
  64. // Connect
  65. $this->link = @mysql_connect($config['hostname'], $config['username'], $config['password'], TRUE);
  66. // Success?
  67. if(!$this->link)
  68. {
  69. throw new Exception('Cannot connect to database ['.mysql_errno().']: '.mysql_error());
  70. }
  71. // Select database
  72. if(!@mysql_select_db($config['database'], $this->link))
  73. {
  74. throw new Exception('Cannot select database ['.mysql_errno().']: '.mysql_error());
  75. }
  76. // UTF8
  77. $this->exec("SET NAMES 'utf8'");
  78. }
  79. /**
  80. * Close connection
  81. */
  82. public function close()
  83. {
  84. mysql_close($this->link);
  85. self::$instance = NULL;
  86. }
  87. /**
  88. * Execute query and return affected rows
  89. *
  90. * @param string $statement
  91. * @param array $params
  92. * @return int
  93. */
  94. public function exec($statement, array $params = array())
  95. {
  96. // Prepare
  97. $statement = $this->prepareStatement($statement, $params);
  98. // Query
  99. $statement = $this->query($statement);
  100. // Return
  101. return mysql_affected_rows($this->link);
  102. }
  103. /**
  104. * Get last insert ID
  105. *
  106. * @return int
  107. */
  108. public function getLastInsertID()
  109. {
  110. return mysql_insert_id($this->link);
  111. }
  112. /**
  113. * Get all possible rows
  114. *
  115. * @param string $statement
  116. * @param array $params
  117. * @param string $sortByField
  118. * @param array
  119. */
  120. public function getAll($statement, array $params = array(), $sortByField = NULL)
  121. {
  122. // Prepare
  123. $statement = $this->prepareStatement($statement, $params);
  124. // Query
  125. $statement = $this->query($statement);
  126. // Rows
  127. $rows = array();
  128. // Fetch all
  129. while($row = mysql_fetch_assoc($statement))
  130. {
  131. // Sort by field?
  132. if($sortByField AND isset($row[$sortByField]))
  133. {
  134. $rows[$row[$sortByField]] = $row;
  135. }
  136. else
  137. {
  138. $rows[] = $row;
  139. }
  140. }
  141. // Return
  142. return $rows;
  143. }
  144. /**
  145. * Get a single row
  146. *
  147. * @param string $statement
  148. * @param array $params
  149. * @param bool|array
  150. */
  151. public function getSingle($statement, array $params = array())
  152. {
  153. // Prepare
  154. $statement = $this->prepareStatement($statement, $params);
  155. // Query
  156. $statement = $this->query($statement);
  157. // Return
  158. return mysql_fetch_assoc($statement);
  159. }
  160. /**
  161. * Get/create db instance
  162. *
  163. * @param array $config
  164. * @return Database
  165. */
  166. public static function instance(array $config = array())
  167. {
  168. // Instance
  169. if (self::$instance === NULL)
  170. {
  171. self::$instance = new Database($config);
  172. }
  173. // Return
  174. return self::$instance;
  175. }
  176. /**
  177. * Prepare statement
  178. *
  179. * @param string $statement
  180. * @param array $params
  181. * @return string
  182. */
  183. protected function prepareStatement($statement, array $params = array())
  184. {
  185. // Prefix
  186. $statement = str_replace('[prefix]', $this->prefix, $statement);
  187. // Parameters
  188. foreach($params as $k => $v)
  189. {
  190. // Prepare value
  191. if($v === NULL)
  192. {
  193. $v = 'NULL';
  194. }
  195. elseif(is_string($v))
  196. {
  197. $v = "'".mysql_real_escape_string($v, $this->link)."'";
  198. }
  199. elseif(is_bool($v))
  200. {
  201. $v = $v ? 1 : 0;
  202. }
  203. // Replace
  204. $statement = str_replace($k, $v, $statement);
  205. }
  206. // Return
  207. return $statement;
  208. }
  209. /**
  210. * Execute query
  211. *
  212. * @param string $statement
  213. * @throws Exception
  214. * @return mixed
  215. */
  216. public function query($statement)
  217. {
  218. // Send query
  219. $statement = @mysql_query($statement, $this->link);
  220. // Error?
  221. if(!$statement)
  222. {
  223. throw new Exception('MySQL query error ['.mysql_errno($this->link).']: '.mysql_error($this->link));
  224. }
  225. // Return result
  226. return $statement;
  227. }
  228. }