PageRenderTime 24ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/saf/lib/PEAR/Cache/DB.php

https://github.com/wokkie/sitellite
PHP | 425 lines | 192 code | 48 blank | 185 comment | 29 complexity | cea54dd703a0a57350fc112da69ac4be MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license, |
  8. // | that is bundled with this package in the file LICENSE, and is |
  9. // | available at through the world-wide-web at |
  10. // | http://www.php.net/license/2_02.txt. |
  11. // | If you did not receive a copy of the PHP license and are unable to |
  12. // | obtain it through the world-wide-web, please send a note to |
  13. // | license@php.net so we can mail you a copy immediately. |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: DB.php,v 1.1.1.1 2005/04/29 04:44:35 lux Exp $
  19. require_once 'Cache.php';
  20. require_once 'DB.php';
  21. /**
  22. * Cache_DB
  23. *
  24. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  25. * @module Cache_DB
  26. * @modulegroup Cache_DB
  27. * @package Cache
  28. * @version $Revision: 1.1.1.1 $
  29. * @access public
  30. */
  31. class Cache_DB extends Cache {
  32. /**
  33. * PEAR DB Object
  34. *
  35. * @var object
  36. */
  37. var $db;
  38. /**
  39. * Lifetime of a cached result set (in seconds)
  40. *
  41. * @var integer
  42. */
  43. var $expires = 3600;
  44. /**
  45. * PEAR DB DSN
  46. *
  47. * @var string
  48. */
  49. var $dsn = '';
  50. /**
  51. * PEAR DB Options
  52. *
  53. * @var mixed
  54. */
  55. var $options = false;
  56. /**
  57. * Fetchmode
  58. *
  59. * @var integer
  60. */
  61. var $fetchmode = DB_FETCHMODE_ASSOC;
  62. /**
  63. * Fetchmode Object Class
  64. *
  65. * @var string
  66. */
  67. var $fetchmode_object_class = 'DB_row';
  68. /**
  69. * Constructor
  70. *
  71. * @param string Name of container class
  72. * @param array Array with container class options
  73. * @param integer Lifetime of a cached result set (in seconds)
  74. */
  75. function Cache_DB($container = 'file',
  76. $container_options = array(
  77. 'cache_dir' => '.',
  78. 'filename_prefix' => 'query_'
  79. ),
  80. $expires = 3600) {
  81. $this->Cache($container, $container_options);
  82. $this->expires = $expires;
  83. }
  84. /**
  85. * PEAR-Deconstructor
  86. * Call deconstructor of parent,
  87. * close database connection if open
  88. */
  89. function _Cache_DB() {
  90. $this->_Cache();
  91. if (is_object($this->db)) {
  92. $this->db->disconnect();
  93. }
  94. }
  95. /**
  96. * Connect to a database.
  97. *
  98. * @param string PEAR DB DSN for the database connection
  99. * @param mixed options
  100. * @throws object DB_Error
  101. */
  102. function connect($dsn, $options = false) {
  103. if (!isset($this->db)) {
  104. $this->db = DB::connect($dsn, $options);
  105. if (DB::isError($this->db)) {
  106. return $this->db;
  107. }
  108. }
  109. }
  110. /**
  111. * Register a database connection for connect on demand.
  112. *
  113. * @param string PEAR DB DSN for the database connection
  114. * @param mixed options
  115. */
  116. function setConnection($dsn, $options = false) {
  117. $this->dsn = $dsn;
  118. $this->options = $options;
  119. }
  120. /**
  121. * Sets which fetch mode should be used by default on queries
  122. * on this connection.
  123. *
  124. * @param integer DB_FETCHMODE_ASSOC, DB_FETCHMODE_OBJECT or
  125. * DB_FETCHMODE_ORDERED
  126. *
  127. * @param string The class of the object to be returned by
  128. * the fetch methods when the DB_FETCHMODE_OBJECT
  129. * mode is selected.
  130. */
  131. function setFetchMode($fetchmode, $object_class = null) {
  132. switch ($fetchmode) {
  133. case DB_FETCHMODE_OBJECT: {
  134. if ($object_class) {
  135. $this->fetchmode_object_class = $object_class;
  136. }
  137. }
  138. case DB_FETCHMODE_ORDERED:
  139. case DB_FETCHMODE_ASSOC: {
  140. $this->fetchmode = $fetchmode;
  141. }
  142. break;
  143. }
  144. }
  145. /**
  146. * Perform an SQL query.
  147. *
  148. * @param string SQL Query String
  149. * @return object Cache_DB_Result
  150. * @throws object Cache_Error
  151. */
  152. function &query($query) {
  153. if (stristr($query, 'SELECT')) {
  154. $cache_id = md5($query);
  155. $result = $this->get($cache_id, 'db_cache');
  156. if ($result == NULL) {
  157. if (!isset($this->db)) {
  158. if (!empty($this->dsn)) {
  159. $this->connect($this->dsn, $this->options);
  160. } else {
  161. return new Cache_Error(
  162. 'No database connection. Either open a connection ' .
  163. 'using connect() or register a connection with ' .
  164. 'setConnection($dsn, $options)',
  165. __FILE__,
  166. __LINE__
  167. );
  168. }
  169. }
  170. $_result = $this->db->query($query);
  171. if (!DB::isError($_result)) {
  172. $rows = array();
  173. while ($row = $_result->fetchRow(DB_FETCHMODE_ASSOC)) {
  174. $rows[] = $row;
  175. }
  176. $result = new Cache_DB_Result(
  177. $rows,
  178. $this->fetchmode,
  179. $this->fetchmode_object_class
  180. );
  181. $this->save($cache_id, $result, $this->expires, 'db_cache');
  182. } else {
  183. return $_result;
  184. }
  185. }
  186. } else {
  187. if (!isset($this->db)) {
  188. if (!empty($this->dsn)) {
  189. $this->connect($this->dsn, $this->options);
  190. $result = $this->db->query($query);
  191. } else {
  192. return new Cache_Error(
  193. 'No database connection. Either open a connection ' .
  194. 'using connect() or register a connection with ' .
  195. 'setConnection($dsn, $options)',
  196. __FILE__,
  197. __LINE__
  198. );
  199. }
  200. }
  201. }
  202. return $result;
  203. }
  204. }
  205. /**
  206. * Cache_DB_Result
  207. *
  208. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  209. * @module Cache_DB
  210. * @modulegroup Cache_DB
  211. * @package Cache
  212. * @version $Revision: 1.1.1.1 $
  213. * @access public
  214. */
  215. class Cache_DB_Result {
  216. /**
  217. * Names of the result set's columns
  218. *
  219. * @var array
  220. */
  221. var $column_names = array();
  222. /**
  223. * Cursor
  224. *
  225. * @var integer
  226. */
  227. var $cursor = 0;
  228. /**
  229. * Fetchmode
  230. *
  231. * @var integer
  232. */
  233. var $fetchmode = DB_FETCHMODE_ASSOC;
  234. /**
  235. * Fetchmode Object Class
  236. *
  237. * @var string
  238. */
  239. var $fetchmode_object_class = 'DB_row';
  240. /**
  241. * Number of columns in the result set
  242. *
  243. * @var integer
  244. */
  245. var $num_columns = 0;
  246. /**
  247. * Number of rows in the result set
  248. *
  249. * @var integer
  250. */
  251. var $num_rows = 0;
  252. /**
  253. * Rows of the result set
  254. *
  255. * @var array
  256. */
  257. var $rows = array();
  258. /**
  259. * Constructor
  260. *
  261. * @param array rows
  262. * @param integer fetchmode
  263. * @param string fetchmode_object_class
  264. */
  265. function Cache_DB_Result(&$rows, $fetchmode, $fetchmode_object_class) {
  266. $this->rows = $rows;
  267. $this->fetchmode = $fetchmode;
  268. $this->fetchmode_object_class = $fetchmode_object_class;
  269. $this->column_names = array_keys($this->rows[0]);
  270. $this->cursor = 0;
  271. $this->num_columns = sizeof($this->column_names);
  272. $this->num_rows = sizeof($this->rows);
  273. }
  274. /**
  275. * Fetch and return a row of data.
  276. * @param integer format of fetched row
  277. * @param mixed row to fetch
  278. * @return mixed a row of data, NULL on no more rows
  279. * @throws object DB_Error
  280. */
  281. function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) {
  282. if ($fetchmode === DB_FETCHMODE_DEFAULT) {
  283. $fetchmode = $this->fetchmode;
  284. }
  285. if ($fetchmode === DB_FETCHMODE_OBJECT) {
  286. $fetchmode = DB_FETCHMODE_ASSOC;
  287. $return_object = true;
  288. }
  289. if ($rownum === null) {
  290. $this->cursor++;
  291. } else {
  292. $this->cursor = $rownum;
  293. }
  294. if ($rownum < sizeof($this->rows)) {
  295. $row = $this->rows[$this->cursor];
  296. } else {
  297. return false;
  298. }
  299. switch ($fetchmode) {
  300. case DB_FETCHMODE_ASSOC: {
  301. if (isset($return_object)) {
  302. $class = $this->fetchmode_object_class;
  303. $object =& new $class($row);
  304. return $object;
  305. } else {
  306. return $row;
  307. }
  308. }
  309. break;
  310. case DB_FETCHMODE_ORDERED: {
  311. $_row = array();
  312. foreach ($this->column_names as $column_name) {
  313. $_row[] = $row[$column_name];
  314. }
  315. return $_row;
  316. }
  317. break;
  318. default: {
  319. return false;
  320. }
  321. }
  322. }
  323. /**
  324. * Fetch a row of data into an existing variable.
  325. *
  326. * @param array reference to data containing the row
  327. * @param integer format of fetched row
  328. * @param integer row number to fetch
  329. * @return mixed DB_OK on success, NULL on no more rows
  330. */
  331. function fetchInto(&$row, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) {
  332. if ($row = $this->fetchRow($fetchmode, $rownum)) {
  333. return DB_OK;
  334. } else {
  335. return NULL;
  336. }
  337. }
  338. /**
  339. * Get the the number of columns in a result set.
  340. *
  341. * @return integer number of columns
  342. */
  343. function numCols() {
  344. return $this->num_columns;
  345. }
  346. /**
  347. * Get the number of rows in a result set.
  348. *
  349. * @return integer number of rows
  350. */
  351. function numRows() {
  352. return $this->num_rows;
  353. }
  354. /**
  355. * Frees the resources allocated for this result set.
  356. */
  357. function free() {
  358. $this->column_names = array();
  359. $this->rows = array();
  360. $this->num_columns = 0;
  361. $this->num_rows = 0;
  362. }
  363. /**
  364. * tableInfo() is not implemented in the PEAR Cache DB module.
  365. * @param mixed $mode
  366. * @throws object Cache_Error
  367. */
  368. function tableInfo($mode = null) {
  369. return new Cache_Error(
  370. 'tableInfo() is not implemented in the ' .
  371. 'PEAR Cache DB module.',
  372. __FILE__,
  373. __LINE__
  374. );
  375. }
  376. }
  377. ?>