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