1 |
<?php |
2 |
/** |
3 |
* This file contains the GenericDataSource child class |
4 |
* that may use arbitrary code modules as database handlers. |
5 |
* It's aims are to get together: |
6 |
* o phpHtmlLibs "source-handlers" |
7 |
* o PEAR's database handlers |
8 |
* o custom ones (e.g. Data::Driver::Proxy object, which talks to a remote rpc server) |
9 |
* |
10 |
* @author Andreas Motl <andreas.motl@ilo.de> |
11 |
* @package phpHtmlLib |
12 |
* @module GenericDataSource |
13 |
* |
14 |
*/ |
15 |
|
16 |
/** |
17 |
* $Id: GenericDataSource.inc,v 1.2 2003/03/02 01:05:13 joko Exp $ |
18 |
* |
19 |
* $Log: GenericDataSource.inc,v $ |
20 |
* Revision 1.2 2003/03/02 01:05:13 joko |
21 |
* - purged old code |
22 |
* |
23 |
* Revision 1.1 2003/03/01 21:47:15 joko |
24 |
* renamed from AbstractDataSource.inc |
25 |
* |
26 |
* Revision 1.1 2003/03/01 20:17:15 joko |
27 |
* renamed from GenericDataSource.inc |
28 |
* replaced '_proxy' through '_handler' to better associate the ability to act as a generic dispatcher handler |
29 |
* |
30 |
* Revision 1.3 2003/03/01 15:36:11 joko |
31 |
* + debugging |
32 |
* + renamed some methods regarding new proposal (build_handler_options, fetch_result, etc.) |
33 |
* |
34 |
* Revision 1.2 2003/03/01 04:50:27 joko |
35 |
* encapsulated all output into tracing mode |
36 |
* disabled tracing |
37 |
* |
38 |
* Revision 1.1 2003/03/01 03:10:40 joko |
39 |
* + initial commit |
40 |
* |
41 |
* |
42 |
*/ |
43 |
|
44 |
/** |
45 |
* This requires the MemoryDataSource base class |
46 |
* |
47 |
*/ |
48 |
// V1: compile-time-include, stable since years ;-) |
49 |
include_once($phphtmllib."/widgets/data_source/MemoryDataSource.inc"); |
50 |
// V2: runtime-load proposal, more flexible |
51 |
// loadModule('MemoryDataSource', 'inc'); // doesn't work (would do if basepath of module is in libpath) |
52 |
// loadModule($phphtmllib . '::widgets::data_source::MemoryDataSource', 'inc'); // works (by accident) |
53 |
// loadModule($phphtmllib . "/widgets/data_source/MemoryDataSource", "inc"); // works (should be stable) |
54 |
// loadModule($phphtmllib . "/widgets/data_source/MemoryDataSource.inc", null); // works (should be stable) |
55 |
|
56 |
|
57 |
|
58 |
/** |
59 |
* Have to have PEAR and DB included |
60 |
* the pear dir must be in the |
61 |
* include path. |
62 |
*/ |
63 |
// require_once("PEAR.php"); // FIXME: what about PEAR::XML::RPC? |
64 |
// require_once("DB.php"); |
65 |
|
66 |
/** |
67 |
* This GenericDataSource child class is *completely* independent |
68 |
* of a specific storage implementation and intended to be a wrapper |
69 |
* class on the way from phpHtmlLib to Xyz. |
70 |
* ("Yyz" gets represented by Data::Storage by now.....) |
71 |
* |
72 |
* GenericDataSource interacts with an intermediate "proxy" object |
73 |
* (e.g. Data::Driver::Proxy) when doing queries. |
74 |
* Don't mix this up with a persistent database handle which gets |
75 |
* reused each and every time for different queries. |
76 |
* Here *is* a new instance of Data::Driver::Proxy for *each* query. |
77 |
* |
78 |
* But the point is: Caching! The actual *data* isn't read redundant! |
79 |
* |
80 |
* |
81 |
* !!!!!! refactor this to Data::Driver::Proxy !!!!!! <----------------- |
82 |
* |
83 |
* Data::Driver::Proxy instantiates "handlers" below itself |
84 |
* which act as encapsulated workers to actually do the stuff to do. |
85 |
* It can cache data for "disconnected mode" using the |
86 |
* php PEAR DB abstraction objects. |
87 |
* One worker already implemented is Data::Driver::RPC::Remote, which |
88 |
* talks to a RPC::XML server (todo: talk SOAP!) using PEAR::XML::RPC. |
89 |
* |
90 |
* --- refactored here, but: redundant somehow --- |
91 |
* Data::Driver::Proxy uses a PEAR XML::RPC object to actually |
92 |
* talk HTTP and serialize data chunks to and from XML, |
93 |
* but adds some caching to this "process of fetching data from a remote side". |
94 |
* It "proxies" arbitrary data chunks a) inside a native php4 session, |
95 |
* b) by talking to a rdbms (single proxy-table) or c) [TODO] using PEAR::Cache. |
96 |
* --- refactored here, but: redundant somehow --- |
97 |
* |
98 |
* !!!!!! refactor this to Data::Driver::Proxy !!!!!! <----------------- |
99 |
* |
100 |
* |
101 |
* How to use? |
102 |
* |
103 |
* Pass an array holding "locator metadata" to the constructor. |
104 |
* GenericDataSource takes care of the rest. |
105 |
* |
106 |
* Pass an array to the constructor: (e.g.) |
107 |
* |
108 |
* 1. doing rpc-calls.... |
109 |
* $locator = array( |
110 |
* type => 'rpc', |
111 |
* metadata => array( Host => 'localhost', Port => '8765' ), |
112 |
* ); |
113 |
* $source = new GenericDataSource($locator); |
114 |
* $this->set_data_source( &$source ); |
115 |
* |
116 |
* 2. [proposal] common datahandles.... |
117 |
* $locator = array( |
118 |
* type => 'mysql', |
119 |
* dsn => 'known dsn markup', |
120 |
* ); |
121 |
* $source = new GenericDataSource($locator); |
122 |
* $this->set_data_source( &$source ); |
123 |
* |
124 |
* @author Andreas Motl <andreas.motl@ilo.de> |
125 |
* @author Sebastian Utz <seut@tunemedia.de> |
126 |
* @copyright (c) 2003 - All Rights reserved. |
127 |
* @license GNU LGPL (GNU Lesser General Public License) |
128 |
* |
129 |
* @author-url http://www.netfrag.org/~joko/ |
130 |
* @author-url http://www.netfrag.org/~jonen/ |
131 |
* @license-url http://www.gnu.org/licenses/lgpl.txt |
132 |
* |
133 |
* @package phpHtmlLib |
134 |
* @module GenericDataSource |
135 |
* |
136 |
*/ |
137 |
|
138 |
/** |
139 |
* Todo: |
140 |
* |
141 |
* o mungle this to be able to be wrapped around phpHtmlLib's own storage-handles |
142 |
* o implement another Data::Driver::Proxy container |
143 |
* |
144 |
*/ |
145 |
|
146 |
|
147 |
//loadModule('DesignPattern::Proxy'); |
148 |
loadModule('DesignPattern::RemoteProxy'); |
149 |
|
150 |
//class GenericDataSource extends MemoryDataSource { |
151 |
//class GenericDataSource extends DesignPattern_Proxy { |
152 |
class GenericDataSource extends DesignPattern_RemoteProxy { |
153 |
|
154 |
/** |
155 |
* This var holds the locator metadata hash |
156 |
* that is used to feed metadata to a per-query-instance |
157 |
* of a Data::Driver::Proxy object. |
158 |
* |
159 |
*/ |
160 |
var $_locator = NULL; |
161 |
|
162 |
|
163 |
/** |
164 |
* This var holds the query hash |
165 |
* that is used to feed as query to a per-query-instance |
166 |
* of a Data::Driver::Proxy object. |
167 |
* |
168 |
*/ |
169 |
var $_query = NULL; |
170 |
|
171 |
|
172 |
/** |
173 |
* this holds the query result from the |
174 |
* Data::Driver::Proxy->queryXyz() call |
175 |
* |
176 |
*/ |
177 |
var $_result = NULL; |
178 |
|
179 |
|
180 |
|
181 |
/** |
182 |
* The constructor is used to pass in the |
183 |
* locator metadata hash. |
184 |
* |
185 |
* @param LocatorMetadataHash array - layout: array( type => '', metadata => '', dsn => '' ) |
186 |
* @param Query array - layout: array( type => '', metadata => '', dsn => '' ) |
187 |
*/ |
188 |
function GenericDataSource( &$locator, $query ) { |
189 |
|
190 |
$this->set_locator( $locator ); |
191 |
$this->set_query( $query ); |
192 |
} |
193 |
|
194 |
/** |
195 |
* Set the locator metadata hash we will feed *partly* |
196 |
* to an encapsulated Data::Driver::Proxy instance. |
197 |
* |
198 |
* @param LocatorMetadataHash array - |
199 |
* |
200 |
*/ |
201 |
function set_locator( &$locator ) { |
202 |
$this->_locator = &$locator; |
203 |
} |
204 |
|
205 |
/** |
206 |
* Set the query metadata hash we will feed *completely* |
207 |
* to an encapsulated Data::Driver::Proxy instance. |
208 |
* |
209 |
* @param QueryDeclaration array - |
210 |
* |
211 |
*/ |
212 |
function set_query( $query ) { |
213 |
$this->_query = $query; |
214 |
} |
215 |
|
216 |
/** |
217 |
* Issue remote/proxy call |
218 |
* Stolen from Application_AbstractBackend::_remote_method (RefactoringProposal?) |
219 |
* Tweaked a bit: proxy package now taken from $this->_handler_name |
220 |
* |
221 |
* Create a new instance of a proxy and store it for later reuse. |
222 |
* Read the locator metadata hash and create |
223 |
* the proxy handler instance using passed-in name. |
224 |
* |
225 |
* @param string - $proxy_name (namespaced classname - perl syntax - e.g.: Data::Driver::Proxy) |
226 |
* |
227 |
*/ |
228 |
function call_handler() { |
229 |
|
230 |
|
231 |
// 1. read args |
232 |
|
233 |
$arg_list = func_get_args(); |
234 |
$command = array_shift($arg_list); |
235 |
$cache_key = join("-", array(session_id(), $command, join('_', $arg_list) )); |
236 |
//print "cache_key: $cache_key<br>"; |
237 |
|
238 |
// FIXME: what if arg_list still contains more elements after doing this? |
239 |
$query = array_shift($arg_list); |
240 |
|
241 |
|
242 |
// 2. prepare proxy-options |
243 |
|
244 |
// read from locator metadata |
245 |
$proxy_name = $this->_locator[metadata][package]; |
246 |
$rpcinfo = array( Host => $this->_locator[metadata][Host], Port => $this->_locator[metadata][Port] ); |
247 |
|
248 |
// FIXME! implement this into Data::Driver::RPC::Remote! |
249 |
//$rpcinfo[to_latin] = 1; |
250 |
|
251 |
// FIXME! patch query |
252 |
if (sizeof($query) == 1) { |
253 |
$query = $query[0]; |
254 |
} |
255 |
|
256 |
// !!! use DesignPattern::Proxy here !!! |
257 |
// $proxy = new DesignPattern_Proxy($proxy_name, ...) |
258 |
// or: |
259 |
// $proxy = mkObject('DesignPattern::Proxy'); |
260 |
// $this->set_handler( $proxy ); |
261 |
// or: |
262 |
// $proxy = mkObject('DesignPattern::Proxy'); |
263 |
// $proxy-> |
264 |
// $this->set_handler( $proxy ); |
265 |
|
266 |
$this->set_component_name( $proxy_name ); |
267 |
$this->set_component_options( $cache_key, array( key => 1, command => $command, query => $query, remote => 1, rpcinfo => $rpcinfo, cache => array( db => 0, session => 1 ) ) ); |
268 |
|
269 |
$this->create_handler(); |
270 |
|
271 |
/* |
272 |
// -------------------- clone this & modify ---------- |
273 |
$proxy = mkObject($proxy_name, $cache_key, array( key => 1, command => $command, query => $query, remote => 1, rpcinfo => $rpcinfo, cache => array( db => 0, session => 1 ) ) ); |
274 |
$this->set_handler( $proxy ); |
275 |
//$result = $resultHandle->getAttributes(); |
276 |
//return $result; |
277 |
//$this->_result = $resultHandle->getAttributes(); |
278 |
//$this->_result = $this->_handler->getAttributes(); |
279 |
// -------------------- clone this & modify ---------- |
280 |
*/ |
281 |
|
282 |
} |
283 |
|
284 |
|
285 |
function build_handler_options() { |
286 |
|
287 |
//print Dumper($this->_query); |
288 |
//exit; |
289 |
|
290 |
// make up a command from metadata |
291 |
// FIXME: abstract this some more (e.g. via a CommandMapper|Registry) |
292 |
switch ($this->_query[metatype]) { |
293 |
case 'data': |
294 |
//$command = 'queryData'; |
295 |
$command = 'getObjects'; // FIXME!!! |
296 |
$args = array(); |
297 |
switch ($this->_query[vartype]) { |
298 |
case 'object': |
299 |
if (!$this->_query[classname]) { |
300 |
$msg = "_query[vartype] == 'object' requires _query[classname]"; |
301 |
user_error("GenericDataSource::do_query() - failed: " . $msg); |
302 |
} |
303 |
array_push($args, $this->_query[classname]); |
304 |
break; |
305 |
} |
306 |
break; |
307 |
case 'schema': |
308 |
$command = 'querySchema'; |
309 |
break; |
310 |
} |
311 |
|
312 |
// trace |
313 |
$this->debug(null, '<b>= = = = = = = = = = = = = = = = = = = = = =</b>'); |
314 |
$this->trace('build_handler_options', array( |
315 |
"_locator" => $this->_locator, |
316 |
"_query" => $this->_query, |
317 |
"command" => '<b>' . $command . "</b>", |
318 |
"args" => $args |
319 |
)); |
320 |
|
321 |
$this->_handler_options = array( |
322 |
method => $command, |
323 |
args => $args, |
324 |
); |
325 |
|
326 |
} |
327 |
|
328 |
function fetch_result() { |
329 |
|
330 |
$this->build_handler_options(); |
331 |
$method = $this->_handler_options[method]; |
332 |
$args = $this->_handler_options[args]; |
333 |
|
334 |
// pre-flight checks |
335 |
if (!$method) { |
336 |
$msg = "Remote command could not be resolved, please pass in or check configuration."; |
337 |
user_error("GenericDataSource::do_query() - failed: " . $msg); |
338 |
return; |
339 |
} |
340 |
|
341 |
// do remote call here and get result |
342 |
// FIXME: handle synchronous/asynchronous mode here!!! |
343 |
$this->call_handler($method, $args); |
344 |
// TODO: ... = $this->poll_handler_result and $this->get_handler_result |
345 |
$this->_result = $this->_handler->getAttributes(); |
346 |
|
347 |
// trace |
348 |
if ($this->_debug[notice]) { |
349 |
//print "_result = " . Dumper($this->_result); |
350 |
print "_result - count = " . sizeof($this->_result) . "<br/>"; |
351 |
} |
352 |
|
353 |
} |
354 |
|
355 |
|
356 |
function do_query() { |
357 |
//print "query!<br/>"; |
358 |
$this->fetch_result(); |
359 |
$this->handle_result(); |
360 |
} |
361 |
|
362 |
function do_query_schema() { |
363 |
user_error("FIXME: do_query_schema"); |
364 |
// $this->call_handler( ... ); |
365 |
} |
366 |
|
367 |
function get_header() { |
368 |
$this->fetch_result(); |
369 |
$this->read_labels_from_result(); |
370 |
return $this->get_labels(); |
371 |
} |
372 |
|
373 |
|
374 |
|
375 |
|
376 |
} |
377 |
|
378 |
?> |