PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/cod_daemon/lib/classes/DatabaseSQLite.php

https://code.google.com/p/php-blackops-rcon/
PHP | 243 lines | 97 code | 29 blank | 117 comment | 7 complexity | 8c0e06fa69f2d268b537fa7d819f41c8 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 DatabaseSQLite
  28. {
  29. /**
  30. * Last instance
  31. *
  32. * @var Database
  33. */
  34. protected static $instance = NULL;
  35. /**
  36. * SQLite3
  37. *
  38. * @var SQLite3
  39. */
  40. protected $link = NULL;
  41. /**
  42. * Constructor
  43. *
  44. * @param array $config
  45. * @throws Exception
  46. */
  47. public function __construct(array $config = array())
  48. {
  49. // Empty config
  50. if (empty($config))
  51. {
  52. throw new Exception('Invalid database configuration. Please check your config/core/database.yml file.');
  53. }
  54. // Define as const
  55. if(!defined('PREFIX')) define('PREFIX', '');
  56. // Connect
  57. $this->link = new SQLite3(ROOT_PATH.'data/'.$config['filename']);
  58. // Success?
  59. if(!$this->link)
  60. {
  61. throw new Exception('Cannot connect to database');
  62. }
  63. }
  64. /**
  65. * Close connection
  66. */
  67. public function close()
  68. {
  69. $this->link->close();
  70. $this->link = NULL;
  71. self::$instance = NULL;
  72. }
  73. /**
  74. * Execute query and return affected rows
  75. *
  76. * @param string $statement
  77. * @param array $params
  78. * @return int
  79. */
  80. public function exec($statement, array $params = array())
  81. {
  82. // Prepare
  83. $statement = $this->prepareStatement($statement, $params);
  84. // Query
  85. $statement = $this->query($statement);
  86. // Return
  87. return $this->link->changes();
  88. }
  89. /**
  90. * Get last insert ID
  91. *
  92. * @return int
  93. */
  94. public function getLastInsertID()
  95. {
  96. return $this->link->lastInsertRowID();
  97. }
  98. /**
  99. * Get all possible rows
  100. *
  101. * @param string $statement
  102. * @param array $params
  103. * @param string $sortByField
  104. * @param array
  105. */
  106. public function getAll($statement, array $params = array(), $sortByField = NULL)
  107. {
  108. // Prepare
  109. $statement = $this->prepareStatement($statement, $params);
  110. // Query
  111. $statement = $this->query($statement);
  112. // Rows
  113. $rows = array();
  114. // Fetch all
  115. while($row = $statement->fetchArray(SQLITE3_ASSOC))
  116. {
  117. // Sort by field?
  118. if($sortByField AND isset($row[$sortByField]))
  119. {
  120. $rows[$row[$sortByField]] = $row;
  121. }
  122. else
  123. {
  124. $rows[] = $row;
  125. }
  126. }
  127. // Return
  128. return $rows;
  129. }
  130. /**
  131. * Get a single row
  132. *
  133. * @param string $statement
  134. * @param array $params
  135. * @param bool|array
  136. */
  137. public function getSingle($statement, array $params = array())
  138. {
  139. // Prepare
  140. $statement = $this->prepareStatement($statement, $params);
  141. // Query
  142. $statement = $this->query($statement);
  143. // Return
  144. return $statement->fetchArray(SQLITE3_ASSOC);
  145. }
  146. /**
  147. * Get/create db instance
  148. *
  149. * @param array $config
  150. * @return Database
  151. */
  152. public static function instance(array $config = array())
  153. {
  154. // Instance
  155. if (self::$instance === NULL)
  156. {
  157. self::$instance = new Database($config);
  158. }
  159. // Return
  160. return self::$instance;
  161. }
  162. /**
  163. * Prepare statement
  164. *
  165. * @param string $statement
  166. * @param array $params
  167. * @return string
  168. */
  169. protected function prepareStatement($statement, array $params = array())
  170. {
  171. // Prefix
  172. $statement = str_replace('[prefix]', '', $statement);
  173. // Parameters
  174. foreach($params as $k => $v)
  175. {
  176. // Prepare value
  177. if($v === NULL)
  178. {
  179. $v = 'NULL';
  180. }
  181. elseif(is_string($v))
  182. {
  183. $v = "'".SQLite3::escapeString($v)."'";
  184. }
  185. elseif(is_bool($v))
  186. {
  187. $v = $v ? 1 : 0;
  188. }
  189. // Replace
  190. $statement = str_replace($k, $v, $statement);
  191. }
  192. // Return
  193. return $statement;
  194. }
  195. /**
  196. * Execute query
  197. *
  198. * @param string $statement
  199. * @throws Exception
  200. * @return mixed
  201. */
  202. public function query($statement)
  203. {
  204. // Send query
  205. $statement = $this->link->query($statement);
  206. // Error?
  207. if (!$statement)
  208. {
  209. throw new Exception('SQLite error: ['.$this->link->lastErrorMsg().']');
  210. }
  211. // Return result
  212. return $statement;
  213. }
  214. }