| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* This file contains the class XML_Query2XML_Driver_Caching.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category XML
|
|
|
8 |
* @package XML_Query2XML
|
|
|
9 |
* @author Lukas Feiler <lukas.feiler@lukasfeiler.com>
|
|
|
10 |
* @copyright 2006 Lukas Feiler
|
|
|
11 |
* @license http://www.gnu.org/copyleft/lesser.html LGPL Version 2.1
|
|
|
12 |
* @version CVS: $Id: Caching.php 276633 2009-03-01 10:43:08Z lukasfeiler $
|
|
|
13 |
* @link http://pear.php.net/package/XML_Query2XML
|
|
|
14 |
* @access private
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* XML_Query2XML_Driver_Caching extends XML_Query2XML_Driver.
|
|
|
19 |
*/
|
|
|
20 |
require_once 'XML/Query2XML.php';
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Caching driver.
|
|
|
24 |
*
|
|
|
25 |
* usage:
|
|
|
26 |
* <code>
|
|
|
27 |
* $driver = new XML_Query2XML_Driver_Caching(
|
|
|
28 |
* XML_Query2XML_Driver::factory(...)
|
|
|
29 |
* );
|
|
|
30 |
* </code>
|
|
|
31 |
*
|
|
|
32 |
* @category XML
|
|
|
33 |
* @package XML_Query2XML
|
|
|
34 |
* @author Lukas Feiler <lukas.feiler@lukasfeiler.com>
|
|
|
35 |
* @copyright 2008 Lukas Feiler
|
|
|
36 |
* @license http://www.gnu.org/copyleft/lesser.html LGPL Version 2.1
|
|
|
37 |
* @version Release: 1.7.2
|
|
|
38 |
* @link http://pear.php.net/package/XML_Query2XML
|
|
|
39 |
* @access private
|
|
|
40 |
* @since Release 1.7.1RC1
|
|
|
41 |
*/
|
|
|
42 |
class XML_Query2XML_Driver_Caching extends XML_Query2XML_Driver
|
|
|
43 |
{
|
|
|
44 |
/**
|
|
|
45 |
* The record cache.
|
|
|
46 |
*
|
|
|
47 |
* @var array An associative array.
|
|
|
48 |
*/
|
|
|
49 |
private $_recordCache = array();
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* The driver who's results to be cached.
|
|
|
53 |
*
|
|
|
54 |
* @var XML_Query2XML_Driver
|
|
|
55 |
*/
|
|
|
56 |
private $_driver = null;
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* An associative array of query strings returned by
|
|
|
60 |
* $this->_driver->preprocessQuery(). The $configPath
|
|
|
61 |
* will be used as the key.
|
|
|
62 |
*
|
|
|
63 |
* @var array An assocative array.
|
|
|
64 |
*/
|
|
|
65 |
private $_queryStrings = array();
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Constructor function.
|
|
|
69 |
*
|
|
|
70 |
* @param XML_Query2XML_Driver $driver The driver this driver
|
|
|
71 |
* will wrap around.
|
|
|
72 |
*/
|
|
|
73 |
public function __construct(XML_Query2XML_Driver $driver)
|
|
|
74 |
{
|
|
|
75 |
$this->_driver = $driver;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Pre-processes a query specification and returns a string representation
|
|
|
80 |
* of the query.
|
|
|
81 |
*
|
|
|
82 |
* @param mixed &$query A string or an array containing the element 'query'.
|
|
|
83 |
* @param string $configPath The config path; used for exception messages.
|
|
|
84 |
*
|
|
|
85 |
* @return string The query statement as a string.
|
|
|
86 |
*/
|
|
|
87 |
public function preprocessQuery(&$query, $configPath)
|
|
|
88 |
{
|
|
|
89 |
return $this->_queryStrings[$configPath] =
|
|
|
90 |
$this->_driver->preprocessQuery($query, $configPath)
|
|
|
91 |
. ' (USING CACHING)';
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Fetch all records from the result set.
|
|
|
96 |
*
|
|
|
97 |
* @param mixed $sql The SQL query.
|
|
|
98 |
* @param string $configPath The config path.
|
|
|
99 |
*
|
|
|
100 |
* @return array An array of records.
|
|
|
101 |
* @see XML_Query2XML_Driver::getAllRecords()
|
|
|
102 |
*/
|
|
|
103 |
public function getAllRecords($sql, $configPath)
|
|
|
104 |
{
|
|
|
105 |
$queryString = $this->_queryStrings[$configPath];
|
|
|
106 |
if (is_array($sql) && isset($sql['data']) && is_array($sql['data'])) {
|
|
|
107 |
$queryString .= '; DATA:' . implode(',', $sql['data']);
|
|
|
108 |
}
|
|
|
109 |
if (!isset($this->_recordCache[$queryString])) {
|
|
|
110 |
$this->_recordCache[$queryString] =&
|
|
|
111 |
$this->_driver->getAllRecords($sql, $configPath);
|
|
|
112 |
}
|
|
|
113 |
return $this->_recordCache[$queryString];
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
?>
|