1 |
<?php |
2 |
/** |
3 |
* This file contains the GenericDataSource child class |
4 |
* |
5 |
* @author Andreas Motl <andreas.motl@ilo.de> |
6 |
* @package org.netfrag.glib |
7 |
* @name DataSource::Generic |
8 |
* |
9 |
*/ |
10 |
|
11 |
/** |
12 |
* <b>Cvs-Log:</b> |
13 |
* |
14 |
* <pre> |
15 |
* $Id: Generic.php,v 1.17 2003/04/09 02:07:33 joko Exp $ |
16 |
* |
17 |
* $Log: Generic.php,v $ |
18 |
* Revision 1.17 2003/04/09 02:07:33 joko |
19 |
* CHANGE: renamed key 'classname' through 'nodename' |
20 |
* |
21 |
* Revision 1.16 2003/04/08 17:55:20 joko |
22 |
* CHANGE: renamed property 'datasource' to 'transport' |
23 |
* NEW: new case 'method' in 'function datasource_handler_buildoptions' |
24 |
* minor fixes: updated logging/debugging messages |
25 |
* |
26 |
* Revision 1.15 2003/04/06 04:46:33 joko |
27 |
* renamed linking function |
28 |
* mozilla fixes |
29 |
* |
30 |
* Revision 1.14 2003/04/04 21:19:21 joko |
31 |
* modified exception handling (enhanced, purged redundant code) |
32 |
* |
33 |
* Revision 1.13 2003/04/04 02:37:14 jonen |
34 |
* _query[action] == write |
35 |
* |
36 |
* Revision 1.12 2003/03/29 08:00:48 joko |
37 |
* modified ErrorBoxing |
38 |
* |
39 |
* Revision 1.11 2003/03/28 06:45:26 joko |
40 |
* VERBOSE mode |
41 |
* |
42 |
* Revision 1.10 2003/03/28 03:01:02 joko |
43 |
* more fancy debugging-output |
44 |
* |
45 |
* Revision 1.9 2003/03/27 16:24:26 jonen |
46 |
* + mugled namespace |
47 |
* + added enhanced 'queryData' |
48 |
* |
49 |
* Revision 1.8 2003/03/20 07:22:14 jonen |
50 |
* + modified case 'object' to 'objects' |
51 |
* (cause its loads all *objects* of a given classname) |
52 |
* |
53 |
* Revision 1.7 2003/03/11 01:43:00 joko |
54 |
* + fixed metadata for phpDocumentor |
55 |
* |
56 |
* Revision 1.6 2003/03/11 01:22:25 joko |
57 |
* + fixed metadata for phpDocumentor |
58 |
* |
59 |
* Revision 1.5 2003/03/11 00:12:49 joko |
60 |
* + fixed metadata for phpDocumentor |
61 |
* |
62 |
* Revision 1.4 2003/03/10 23:25:03 joko |
63 |
* + fixed metadata for phpDocumentor |
64 |
* |
65 |
* Revision 1.3 2003/03/09 15:50:36 joko |
66 |
* + additional metadata for Autodia |
67 |
* |
68 |
* Revision 1.2 2003/03/05 17:28:43 joko |
69 |
* updated docu (phpDocumentor testing....) |
70 |
* |
71 |
* Revision 1.1 2003/03/05 12:04:00 joko |
72 |
* + initial commit, from GenericDataSource |
73 |
* |
74 |
* Revision 1.3 2003/03/03 21:24:18 joko |
75 |
* now based on DesignPattern::RemoteProxy |
76 |
* |
77 |
* Revision 1.2 2003/03/02 01:05:13 joko |
78 |
* - purged old code |
79 |
* |
80 |
* Revision 1.1 2003/03/01 21:47:15 joko |
81 |
* renamed from AbstractDataSource.inc |
82 |
* |
83 |
* Revision 1.1 2003/03/01 20:17:15 joko |
84 |
* renamed from GenericDataSource.inc |
85 |
* replaced '_proxy' through '_handler' to better associate the ability to act as a generic dispatcher handler |
86 |
* |
87 |
* Revision 1.3 2003/03/01 15:36:11 joko |
88 |
* + debugging |
89 |
* + renamed some methods regarding new proposal (build_handler_options, fetch_result, etc.) |
90 |
* |
91 |
* Revision 1.2 2003/03/01 04:50:27 joko |
92 |
* encapsulated all output into tracing mode |
93 |
* disabled tracing |
94 |
* |
95 |
* Revision 1.1 2003/03/01 03:10:40 joko |
96 |
* + initial commit |
97 |
* </pre> |
98 |
* |
99 |
*/ |
100 |
|
101 |
/** |
102 |
* This requires the MemoryDataSource base class |
103 |
* |
104 |
*/ |
105 |
|
106 |
// V1: compile-time-include, stable since years ;-) |
107 |
//include_once($phphtmllib."/widgets/data_source/MemoryDataSource.inc"); |
108 |
|
109 |
// V2: runtime-load proposal, more flexible |
110 |
// loadModule('MemoryDataSource', 'inc'); // doesn't work (would do if basepath of module is in libpath) |
111 |
// loadModule($phphtmllib . '::widgets::data_source::MemoryDataSource', 'inc'); // works (by accident) |
112 |
// loadModule($phphtmllib . "/widgets/data_source/MemoryDataSource", "inc"); // works (should be stable) |
113 |
// loadModule($phphtmllib . "/widgets/data_source/MemoryDataSource.inc", null); // works (should be stable) |
114 |
|
115 |
|
116 |
|
117 |
/** |
118 |
* Have to have PEAR and DB included |
119 |
* the pear dir must be in the |
120 |
* include path. |
121 |
*/ |
122 |
// require_once("PEAR.php"); // FIXME: what about PEAR::XML::RPC? |
123 |
// require_once("DB.php"); |
124 |
|
125 |
|
126 |
loadModule('DesignPattern::Proxy'); |
127 |
loadModule('DesignPattern::TransparentProxy'); |
128 |
loadModule('DesignPattern::RemoteProxy'); |
129 |
|
130 |
//class GenericDataSource extends MemoryDataSource { |
131 |
//class GenericDataSource extends DesignPattern_Proxy { |
132 |
//class GenericDataSource extends DesignPattern_RemoteProxy { |
133 |
//class GenericDataSource extends DesignPattern_TransparentProxy { |
134 |
|
135 |
//class GenericDataSource extends MemoryDataSource { |
136 |
//loadModule("DataSource::Proxy::Memory"); |
137 |
//class GenericDataSource extends DataSource_Proxy_Memory { |
138 |
//class DataSource_GenericDataSource extends DataSource_Proxy_Memory { |
139 |
//class DataSource_GenericDataSource extends DesignPattern_Proxy { |
140 |
//class DataSource_GenericDataSource extends DataSource_Proxy_Memory { |
141 |
|
142 |
// now independent!!! new class-inheritance-tree possible now! |
143 |
// solved by having an Adapter *and* a Proxy here! |
144 |
//class GenericDataSource { |
145 |
|
146 |
loadModule('DesignPattern::AdapterProxy'); |
147 |
|
148 |
|
149 |
|
150 |
/** |
151 |
* This GenericDataSource child class is *completely* independent |
152 |
* of a specific storage implementation and intended to be a wrapper |
153 |
* class on the way from phpHtmlLib to Xyz. <br> |
154 |
* ("Yyz" gets represented by Data::Storage by now.....) |
155 |
* |
156 |
* GenericDataSource interacts with an intermediate "proxy" object |
157 |
* (e.g. Data::Driver::Proxy) when doing queries. <br> |
158 |
* <pre> |
159 |
* Don't mix this up with a persistent database handle which gets |
160 |
* reused each and every time for different queries. |
161 |
* Here *is* a new instance of Data::Driver::Proxy for *each* query. |
162 |
* </pre> |
163 |
* |
164 |
* But the point is: Caching! The actual *data* isn't read redundant! |
165 |
* |
166 |
* <pre> |
167 |
* --- snipped into here from above --- |
168 |
* that may use arbitrary code modules as database handlers. |
169 |
* It's aims are to get together: |
170 |
* o phpHtmlLibs "source-handlers" |
171 |
* o PEAR's database handlers |
172 |
* o custom ones (e.g. Data::Driver::Proxy object, which talks to a remote rpc server) |
173 |
* --- snipped into here from above --- |
174 |
* </pre> |
175 |
* |
176 |
* <p> |
177 |
* !!!!!! refactor this to Data::Driver::Proxy !!!!!! <----------------- |
178 |
* |
179 |
* Data::Driver::Proxy instantiates "handlers" below itself |
180 |
* which act as encapsulated workers to actually do the stuff to do. |
181 |
* It can cache data for "disconnected mode" using the |
182 |
* php PEAR DB abstraction objects. |
183 |
* One worker already implemented is Data::Driver::RPC::Remote, which |
184 |
* talks to a RPC::XML server (todo: talk SOAP!) using PEAR::XML::RPC. |
185 |
* |
186 |
* <pre> |
187 |
* --- refactored here, but: redundant somehow --- |
188 |
* Data::Driver::Proxy uses a PEAR XML::RPC object to actually |
189 |
* talk HTTP and serialize data chunks to and from XML, |
190 |
* but adds some caching to this "process of fetching data from a remote side". |
191 |
* It "proxies" arbitrary data chunks a) inside a native php4 session, |
192 |
* b) by talking to a rdbms (single proxy-table) or c) [TODO] using PEAR::Cache. |
193 |
* --- refactored here, but: redundant somehow --- |
194 |
* </pre> |
195 |
* |
196 |
* !!!!!! refactor this to Data::Driver::Proxy !!!!!! <----------------- |
197 |
* </p> |
198 |
* |
199 |
* |
200 |
* <p> |
201 |
* <b>How to use?</b> |
202 |
* |
203 |
* Pass an array holding "locator metadata" to the constructor. |
204 |
* GenericDataSource takes care of the rest. |
205 |
* |
206 |
* <pre> |
207 |
* Pass an array to the constructor: (e.g.) |
208 |
* |
209 |
* 1. doing rpc-calls.... |
210 |
* <code> |
211 |
* $locator = array( |
212 |
* type => 'rpc', |
213 |
* metadata => array( Host => 'localhost', Port => '8765' ), |
214 |
* ); |
215 |
* $source = ne w GenericDataSource($locator); |
216 |
* $this->set_data_source( &$source ); |
217 |
* </code> |
218 |
* |
219 |
* 2. [proposal] common datahandles.... |
220 |
* <code> |
221 |
* $locator = array( |
222 |
* type => 'mysql', |
223 |
* dsn => 'known dsn markup', |
224 |
* ); |
225 |
* $source = ne w GenericDataSource($locator); |
226 |
* $this->set_data_source( &$source ); |
227 |
* </code> |
228 |
* </pre> |
229 |
* </p> |
230 |
* |
231 |
* |
232 |
* @link http://www.netfrag.org/~joko/ |
233 |
* @author Andreas Motl <andreas.motl@ilo.de> |
234 |
* |
235 |
* @link http://www.netfrag.org/~jonen/ |
236 |
* @author Sebastian Utz <seut@tunemedia.de> |
237 |
* |
238 |
* @copyright (c) 2003 - All Rights reserved. |
239 |
* |
240 |
* @link http://www.gnu.org/licenses/lgpl.txt |
241 |
* @license GNU LGPL (GNU Lesser General Public License) |
242 |
* |
243 |
* |
244 |
* @package org.netfrag.glib |
245 |
* @subpackage DataSource |
246 |
* @name DataSource::Generic |
247 |
* |
248 |
* @todo this: |
249 |
* o mungle this to be able to be wrapped around phpHtmlLib's own storage-handles |
250 |
* o implement another Data::Driver::Proxy container |
251 |
* |
252 |
* <pre> |
253 |
* !!!!!!!! THIS IS THE PROBLEM !!!!!!!! |
254 |
* !!!!!!!! here is it where we have to break inheritance again !!!!!!!! |
255 |
* |
256 |
* THE CONFLICT: Beeing in phpHtmlLib *and* DesignPattern::TransparentProxy |
257 |
* inheritance trees at the same time, which is *not* possible at |
258 |
* declare-time. We *do* need some runtime-infrastructure to solve this! |
259 |
* |
260 |
* TODO: move build- and check-locator stuff from ObjectList to this place!!! |
261 |
* |
262 |
* ABOUT: |
263 |
* 1. otherwhere: WebApp - scope: |
264 |
* x handles page vs. block vs. widget; dispatches MVC-View |
265 |
* 2. here: DataSource - scope: |
266 |
* x handles bridge to frameworks (e.g. phpHtmlLib) vs. actual data driver libs (PEAR, etc.)) |
267 |
* o clean implementation using a DesignPattern::AdapterProxy |
268 |
* </pre> |
269 |
* |
270 |
* |
271 |
*/ |
272 |
class DataSource_Generic extends DesignPattern_AdapterProxy { |
273 |
|
274 |
// !!!!!!!! here is it where we have to break inheritance again !!!!!!!! |
275 |
// !!!!!!!! THIS IS THE PROBLEM !!!!!!!! |
276 |
|
277 |
|
278 |
|
279 |
/** |
280 |
* This var holds the locator metadata hash |
281 |
* that is used to feed metadata to a per-query-instance |
282 |
* of an Adapter object. |
283 |
* |
284 |
*/ |
285 |
//var $_locator = NULL; |
286 |
|
287 |
/** |
288 |
* This var holds the locator metadata hash |
289 |
* which is built from some predefined rules |
290 |
* using metadata from $_options and some |
291 |
* other presets. |
292 |
* |
293 |
* See '_buildLocator' which acts as a dispatcher |
294 |
* depending on $_options[transport]. |
295 |
* (main dispatching level) |
296 |
* |
297 |
* The structure of a full blown locator looks like this: |
298 |
* |
299 |
* $locator = array( |
300 |
* type => '<your type specifying the datasource-type>', |
301 |
* metadata => array( |
302 |
* ... your arbitrary deep metadata structure ... |
303 |
* ), |
304 |
* [dsn => '<your dsn markup>'], |
305 |
* ); |
306 |
* |
307 |
* Example 1 - data is inside a rdbms, using a dsn to connect to it: |
308 |
* $locator = array( |
309 |
* type => 'sql', |
310 |
* dsn => 'mysql://username:password@localhost/database', |
311 |
* ); |
312 |
* |
313 |
* Example 2 - data is inside an odbms, reachable by doing remote procedure calls (rpc): |
314 |
* $locator = array( |
315 |
* type => 'rpc', |
316 |
* metadata => array( |
317 |
* package => 'Data::Driver::Proxy', |
318 |
* Host => 'localhost', |
319 |
* Port => '8765', |
320 |
* ) |
321 |
* ); |
322 |
* |
323 |
*/ |
324 |
var $_locator = NULL; |
325 |
|
326 |
|
327 |
/** |
328 |
* This var holds the module information required to |
329 |
* create instances of a Proxy and an Adapter. |
330 |
* Information might get extracted from a |
331 |
* DataSource::Locator instance. |
332 |
* |
333 |
* --- A skeleton: |
334 |
* |
335 |
* $this->_modules = array( |
336 |
* _proxy => array( _id => '', _module => '', _options = array(), _instance => &$obj), |
337 |
* _adapter => array( _id => '', _module => '', _options = array(), _instance => &$obj), |
338 |
* ); |
339 |
* |
340 |
* |
341 |
* --- An example: |
342 |
* |
343 |
* $this->_modules = array( |
344 |
* _proxy => array( _id => 'rpc', _module => 'DesignPattern::RemoteProxy', _instance => &$obj), |
345 |
* _adapter => array( _id => 'phpHtmlLib', _module => 'DataSource::Adapter::phpHtmlLib::DataListSource', _instance => &$obj), |
346 |
* ); |
347 |
* |
348 |
* |
349 |
*/ |
350 |
//var $_modules = NULL; |
351 |
|
352 |
|
353 |
/** |
354 |
* This var holds the query hash |
355 |
* that is used to feed as query to a per-query-instance |
356 |
* of a Data::Driver::Proxy object. |
357 |
* |
358 |
*/ |
359 |
var $_query = NULL; |
360 |
|
361 |
|
362 |
/** |
363 |
* this holds the query result from the |
364 |
* Data::Driver::Proxy->queryXyz() call |
365 |
* |
366 |
*/ |
367 |
var $_result = NULL; |
368 |
|
369 |
|
370 |
|
371 |
/** |
372 |
* The constructor is used to pass in the |
373 |
* locator metadata hash. |
374 |
* |
375 |
* @param LocatorMetadataHash array - layout: array( type => '', metadata => '', dsn => '' ) |
376 |
* @param Query array - layout: array( type => '', metadata => '', dsn => '' ) |
377 |
*/ |
378 |
|
379 |
|
380 |
function DataSource_Generic( &$locator, $query ) { |
381 |
|
382 |
// copy parameter from query to locator |
383 |
//$this->_locator->merge_to($this->_locator, array( datasource_type => $query[transport] ), '_'); |
384 |
//$this->_locator->_datasource_type = $query[transport]; |
385 |
//$locator[_datasource_type] = $query[transport]; |
386 |
|
387 |
/** |
388 |
* <!-- Autodia --> |
389 |
* can do: (this is metadata supplied for Autodia, don't delete!) |
390 |
* $this->_locator = new DataSource_Locator() |
391 |
* |
392 |
*/ |
393 |
|
394 |
// build master locator |
395 |
$this->_locator = php::mkComponent('DataSource::Locator', $locator, array( datasource_type => $query[transport] ) ); |
396 |
//exit; |
397 |
//$this->_locator = php::mkComponent('DataSource::Locator'); |
398 |
|
399 |
$this->_locator->check(); |
400 |
|
401 |
// trace |
402 |
//print "locator-obj: " . Dumper($this->_locator); |
403 |
//print "locator-data: " . Dumper($locator_data); |
404 |
//exit; |
405 |
|
406 |
|
407 |
|
408 |
/* |
409 |
$this->_modules = array( |
410 |
_proxy => array( _id => $locator->_datasource_type ), |
411 |
_adapter => array( _id => $locator->_adapter_type ), |
412 |
); |
413 |
*/ |
414 |
|
415 |
//$this->set_locator( $locator ); |
416 |
$this->set_query( $query ); |
417 |
|
418 |
|
419 |
|
420 |
/* |
421 |
// V1: |
422 |
// pre-flight: establish and check locator metadata |
423 |
$this->_buildLocator(); |
424 |
$this->_checkLocator(); |
425 |
*/ |
426 |
|
427 |
|
428 |
|
429 |
// pre-flight: check locator metadata |
430 |
//$locator->build(); |
431 |
if (!$this->_locator->check()) { |
432 |
user_error("DataSource::Generic: locator-check failed." . "<br/>" . "Locator was: " . Dumper($this->_locator) ); |
433 |
//exit; |
434 |
return; |
435 |
} |
436 |
|
437 |
//exit; |
438 |
|
439 |
|
440 |
/** |
441 |
* <!-- Autodia --> |
442 |
* can do: (this is metadata supplied for Autodia, don't delete!) |
443 |
* $proxy = new DesignPattern_RemoteProxy() |
444 |
* |
445 |
*/ |
446 |
|
447 |
|
448 |
// --- Proxy selector/dispatcher ... |
449 |
|
450 |
switch ($this->_locator->_datasource_type) { |
451 |
case 'rpc': |
452 |
//$locator_data = $this->_locator->get_metadata(); |
453 |
//$this->_modules[_proxy][_options] = array( locator => $this->_locator, query => $query ); |
454 |
//$this->_modules[_proxy][_module] = 'DataSource::Proxy::XMLRPC'; |
455 |
|
456 |
//$this->_modules[_proxy][_module] = 'DesignPattern::RemoteProxy'; |
457 |
//$this->_modules[_proxy][_options] = $locator_data; |
458 |
|
459 |
//$this->set_component_name('DesignPattern::RemoteProxy'); |
460 |
//$this->set_component_options($locator_data); |
461 |
|
462 |
$this->set_component_name('DesignPattern::RemoteProxy'); |
463 |
|
464 |
break; |
465 |
default: |
466 |
user_error("Site::GenericPage: no Proxy could be selected!"); |
467 |
break; |
468 |
} |
469 |
|
470 |
|
471 |
|
472 |
/* |
473 |
// V1: |
474 |
// make up dummy args by now |
475 |
$args = array( |
476 |
adapter => 'phpHtmlLib', |
477 |
); |
478 |
*/ |
479 |
|
480 |
// V2: now propagated from caller! |
481 |
|
482 |
//print Dumper(array( locator => $locator, query => $query )); |
483 |
//exit; |
484 |
|
485 |
//$this->create_adapter($this->module, $this->function, $this->arguments); |
486 |
//$this->set_handler( $this->get_adapter() ); |
487 |
|
488 |
|
489 |
// --- Adapter selector/dispatcher ... |
490 |
// ... resolves this._modules.adapter._id to this._modules.adapter._module |
491 |
|
492 |
switch ($locator->_adapter_type) { |
493 |
case 'phpHtmlLib': |
494 |
//$adapter_arguments = $args[title]; |
495 |
|
496 |
$this->set_adapter_module('DataSource::Adapter::phpHtmlLib::DataSource'); |
497 |
|
498 |
// in order to let the Adapter communicate with the Proxy, |
499 |
// instantiate a wrapper method in a third namespace via |
500 |
// Exporter::export_symbol(from, to) |
501 |
$this->set_adapter_options($this); |
502 |
|
503 |
|
504 |
break; |
505 |
default: |
506 |
user_error("DataSource::Generic: no Adapter could be selected!"); |
507 |
break; |
508 |
} |
509 |
|
510 |
|
511 |
|
512 |
// --- module instantiation - Proxy and Adapter |
513 |
|
514 |
//user_error("handle proxy here!"); |
515 |
//$this->create_proxy(); |
516 |
|
517 |
//print Dumper($this); |
518 |
|
519 |
//print "this: " . Dumper($this); |
520 |
|
521 |
// V1: |
522 |
/* |
523 |
$proxy = php::mkComponent($this->_modules[_proxy][_module], $this->_modules[_proxy][_options]); |
524 |
print "proxy: " . Dumper($proxy); |
525 |
//exit; |
526 |
*/ |
527 |
|
528 |
// V2: |
529 |
/* |
530 |
$resultHandle = mkObject('DesignPattern::RemoteProxy', $cache_key, array( key => 1, command => $command, query => $query, remote => 1, rpcinfo => $rpcinfo, cache => array( db => 0, session => 1 ) ) ); |
531 |
$result = $resultHandle->getAttributes(); |
532 |
return $result; |
533 |
*/ |
534 |
|
535 |
// V3: |
536 |
// $this->set_handler( $proxy ); |
537 |
|
538 |
// V4: use _component_name & _component_options (proposal from GenericPage) |
539 |
// $this->_component_name = ... |
540 |
// $this->create_handler(); |
541 |
|
542 |
|
543 |
/** |
544 |
* <!-- Autodia --> |
545 |
* can do: (this is metadata supplied for Autodia, don't delete!) |
546 |
* $adapter = new DataSource_Adapter_phpHtmlLib_DataListSource() |
547 |
* |
548 |
*/ |
549 |
|
550 |
// V1: |
551 |
//$this->create_adapter($adapter_module, $this->function, $this->arguments); |
552 |
|
553 |
// V2: FIXME - better use V1? |
554 |
$adapter = php::mkComponent($this->get_adapter_module(), $this->get_adapter_options()); |
555 |
$this->set_adapter($adapter); |
556 |
|
557 |
|
558 |
|
559 |
|
560 |
} |
561 |
|
562 |
function make_adapter_transparent() { |
563 |
|
564 |
// ... known from Site::Adapter::php::Class: |
565 |
|
566 |
// At this place the Adapter becomes a Proxy. |
567 |
// This functionality currently correlates to the |
568 |
// name of our ancestor, DesignPattern::AdapterProxy. |
569 |
// FIXME: move this code to its namespace!!! |
570 |
// FIXME: rename to set_proxy... |
571 |
|
572 |
// The reason this has to occour here is required |
573 |
// by the TransparentProxy to actually make the |
574 |
// _handler (_proxy) transparent later.... |
575 |
//$this->set_proxy( $this->get_adapter() ); |
576 |
} |
577 |
|
578 |
/** |
579 |
* Set the metadata information |
580 |
* (a DataSource::Locator) we will use |
581 |
* to build encapsulated instances |
582 |
* of both a Proxy and an Adapter. |
583 |
* |
584 |
* @param LocatorMetadataHash array - |
585 |
* |
586 |
*/ |
587 |
function set_metadata( &$locator ) { |
588 |
$this->_locator = &$locator; |
589 |
} |
590 |
|
591 |
function &get_metadata() { |
592 |
return $this->_locator; |
593 |
} |
594 |
|
595 |
|
596 |
/** |
597 |
* Set the query metadata hash we will feed *completely* |
598 |
* to an encapsulated Proxy instance. |
599 |
* |
600 |
* @param QueryDeclaration array - |
601 |
* |
602 |
*/ |
603 |
function set_query( $query ) { |
604 |
$this->_query = $query; |
605 |
} |
606 |
|
607 |
/** |
608 |
* Issue remote/proxy call |
609 |
* Stolen from Application_AbstractBackend::_remote_method (RefactoringProposal?) |
610 |
* Tweaked a bit: proxy package now taken from $this->_handler_name |
611 |
* |
612 |
* Create a new instance of a proxy and store it for later reuse. |
613 |
* Read the locator metadata hash and create |
614 |
* the proxy handler instance using passed-in name. |
615 |
* |
616 |
* @param string - $proxy_name (namespaced classname - perl syntax - e.g.: Data::Driver::Proxy) |
617 |
* |
618 |
*/ |
619 |
function datasource_handler_call() { |
620 |
|
621 |
|
622 |
// 1. read args and build cache key |
623 |
|
624 |
$arg_list = func_get_args(); |
625 |
$command = array_shift($arg_list); |
626 |
$cache_key = join("-", array(session_id(), $command, join('_', $arg_list) )); |
627 |
//print "cache_key: $cache_key<br>"; |
628 |
|
629 |
// FIXME: what if arg_list still contains more elements after doing this? |
630 |
$query = array_shift($arg_list); |
631 |
|
632 |
// 2. prepare proxy-options (read from locator) |
633 |
|
634 |
//print "this: " . Dumper($this); |
635 |
//exit; |
636 |
|
637 |
// read from locator metadata |
638 |
//$proxy_name = $this->_locator[metadata][package]; |
639 |
|
640 |
// V1: |
641 |
//$rpcinfo = array( Host => $this->_locator[metadata][Host], Port => $this->_locator[metadata][Port] ); |
642 |
|
643 |
// V2: |
644 |
$rpcinfo = $this->_locator->get_metadata(); |
645 |
|
646 |
// FIXME! implement this into Data::Driver::RPC::Remote! |
647 |
//$rpcinfo[to_latin] = 1; |
648 |
|
649 |
// FIXME! patch query |
650 |
if (sizeof($query) == 1) { |
651 |
$query = $query[0]; |
652 |
} |
653 |
|
654 |
//print "query: " . Dumper($query); |
655 |
|
656 |
// !!! use DesignPattern::Proxy here !!! |
657 |
// $proxy = new DesignPattern_Proxy($proxy_name, ...) |
658 |
// or: |
659 |
// $proxy = mkObject('DesignPattern::Proxy'); |
660 |
// $this->set_handler( $proxy ); |
661 |
// or: |
662 |
// $proxy = mkObject('DesignPattern::Proxy'); |
663 |
// $proxy-> |
664 |
// $this->set_handler( $proxy ); |
665 |
|
666 |
|
667 |
|
668 |
//$this->set_component_name( $proxy_name ); |
669 |
$this->set_component_options( $cache_key, array( key => 1, command => $command, query => $query, remote => 1, rpcinfo => $rpcinfo, cache => array( db => 0, session => 1 ) ) ); |
670 |
//print Dumper($this); |
671 |
//exit; |
672 |
|
673 |
$this->create_proxy(); |
674 |
|
675 |
//print Dumper($this); |
676 |
//exit; |
677 |
|
678 |
/* |
679 |
// -------------------- clone this & modify ---------- |
680 |
$proxy = mkObject($proxy_name, $cache_key, array( key => 1, command => $command, query => $query, remote => 1, rpcinfo => $rpcinfo, cache => array( db => 0, session => 1 ) ) ); |
681 |
$this->set_handler( $proxy ); |
682 |
//$result = $resultHandle->getAttributes(); |
683 |
//return $result; |
684 |
//$this->_result = $resultHandle->getAttributes(); |
685 |
//$this->_result = $this->_handler->getAttributes(); |
686 |
// -------------------- clone this & modify ---------- |
687 |
*/ |
688 |
|
689 |
} |
690 |
|
691 |
|
692 |
function datasource_handler_buildoptions() { |
693 |
|
694 |
//print Dumper($this->_query); |
695 |
//exit; |
696 |
|
697 |
// make up a command from metadata |
698 |
// FIXME: abstract this some more (e.g. via a CommandMapper|Registry) |
699 |
switch ($this->_query[metatype]) { |
700 |
case 'data': |
701 |
$command = 'queryData'; |
702 |
|
703 |
//$command = 'getObjects'; // FIXME!!! |
704 |
//$this->_locator->set_option('metadata.command', $command); |
705 |
/* |
706 |
$args = array(); |
707 |
switch ($this->_query[vartype]) { |
708 |
case 'objects': |
709 |
if (!$this->_query[classname]) { |
710 |
$msg = "_query[vartype] == 'objects' requires _query[classname]"; |
711 |
user_error("DataSource::Generic: query_data() - failed: " . $msg); |
712 |
} |
713 |
array_push($args, $this->_query[classname]); |
714 |
break; |
715 |
} |
716 |
*/ |
717 |
$query_args = array(); |
718 |
switch ($this->_query[abstract_type]) { |
719 |
case 'list': |
720 |
if (!$this->_query[nodename]) { |
721 |
$msg = "_query[vartype] == 'objects' requires _query[nodename]"; |
722 |
user_error("DataSource::Generic: query_data() - failed: " . $msg); |
723 |
} |
724 |
//array_push($query_args, $this->_query[nodename]); |
725 |
$query_args[nodename] = $this->_query[nodename]; |
726 |
break; |
727 |
case 'item': |
728 |
if (!$this->_query[nodename]) { |
729 |
$msg = "_query[vartype] == 'objects' requires _query[nodename]"; |
730 |
user_error("DataSource::Generic: query_data() - failed: " . $msg); |
731 |
} |
732 |
$query_args[guid] = $this->_query[ident]; |
733 |
$query_args[nodename] = $this->_query[nodename]; |
734 |
break; |
735 |
} |
736 |
|
737 |
// dispatch action |
738 |
if ($this->_query[action] == 'write') { |
739 |
$query_args[action] = $this->_query[action]; |
740 |
$query_args[data] = $this->_query[data]; |
741 |
} elseif ($this->_query[action] == 'delete') { |
742 |
$query_args[action] = $this->_query[action]; |
743 |
} |
744 |
$args = array( |
745 |
'data_type' => $this->_query[abstract_type], |
746 |
'query_args' => $query_args |
747 |
); |
748 |
break; |
749 |
|
750 |
// querySchema |
751 |
case 'schema': |
752 |
//print "Testing schema:" . "<br>"; |
753 |
$command = 'querySchema'; |
754 |
$args = array( $this->_query[filter] ); |
755 |
break; |
756 |
|
757 |
// remoteMethod |
758 |
case 'method': |
759 |
$command = $this->_query[method]; |
760 |
$args = $this->_query[args]; |
761 |
break; |
762 |
|
763 |
} |
764 |
|
765 |
|
766 |
// FIXME: bad behaviour? |
767 |
if (!is_array($args)) { $args = array( $args ); } |
768 |
|
769 |
/* |
770 |
$this->_query[rpc_command] = $command; |
771 |
$this->_query[rpc_args] = $command; |
772 |
*/ |
773 |
|
774 |
// methods!!! |
775 |
$this->_locator->_call[_method] = $command; |
776 |
$this->_locator->_call[_arguments] = $args; |
777 |
|
778 |
//$adapter = $this->get_adapter(); |
779 |
|
780 |
// trace |
781 |
/* |
782 |
print Dumper(null, '<b>= = = = = = = = = = = = = = = = = = = = = =</b>'); |
783 |
print Dumper('datasource_handler_buildoptions', array( |
784 |
"_locator" => $this->_locator, |
785 |
"_query" => $this->_query, |
786 |
"command" => '<b>' . $command . "</b>", |
787 |
"args" => $args |
788 |
)); |
789 |
*/ |
790 |
|
791 |
/* |
792 |
$this->_handler_options = array( |
793 |
method => $command, |
794 |
args => $args, |
795 |
); |
796 |
*/ |
797 |
|
798 |
} |
799 |
|
800 |
|
801 |
|
802 |
function &fetch_result() { |
803 |
|
804 |
$this->datasource_handler_buildoptions(); |
805 |
|
806 |
$call = $this->_locator->get_call(); |
807 |
//print Dumper($call); |
808 |
|
809 |
// pre-flight checks |
810 |
if (!$call[method]) { |
811 |
$msg = "Remote method is empty, please pass in proper metadata or check configuration."; |
812 |
user_error("DataSource::Generic: query_data() - failed: " . $msg); |
813 |
return; |
814 |
} |
815 |
|
816 |
//print "fetch_result: this->_handler=" . Dumper($this->_handler); |
817 |
|
818 |
// do remote call here and get result |
819 |
// FIXME: handle synchronous/asynchronous mode here!!! |
820 |
$this->datasource_handler_call($call[method], $call[args]); |
821 |
|
822 |
|
823 |
// TODO: ... = $this->poll_handler_result and $this->get_handler_result |
824 |
$proxy = $this->get_proxy(); |
825 |
$this->_result = $proxy->getResult(); |
826 |
//print "result: " . Dumper($this->_result); exit; |
827 |
|
828 |
$this->_result_count = sizeof($this->_result); |
829 |
|
830 |
// trace |
831 |
//if (constants::get('VERBOSE') && $this->_debug[notice]) { |
832 |
if (constants::get('VERBOSE') or constants::get('ERRORS_ONLY')) { |
833 |
//print "_result = " . Dumper($this->_result); |
834 |
//print "<div><b><font color=\"darkgreen\">Debug:</font></b> DataSource::Generic->_result_count = <b>" . $this->_result_count . "</b></div>"; |
835 |
$this->draw_status_box(); |
836 |
} |
837 |
|
838 |
return $this->_result; |
839 |
|
840 |
} |
841 |
|
842 |
function draw_status_box() { |
843 |
|
844 |
static $boxcount; |
845 |
|
846 |
$boxcount++; |
847 |
|
848 |
$box = container(); |
849 |
|
850 |
// box style |
851 |
$style = container( |
852 |
html_style("text/css", '.boxlabel_darkgreen { color: darkgreen; font-weight:bold; }'), |
853 |
html_style("text/css", '.box_dsg { background: #20ab39; color: white; border: 2px black groove; width:640px; padding:10px; margin:40px; }') |
854 |
); |
855 |
$box->add( $style ); |
856 |
|
857 |
// box content |
858 |
$statusbox = html_div('box_dsg'); |
859 |
$statusbox->add( html_b("DataSource::Generic"), html_br() ); |
860 |
$locatorbox = html_div('box_dsg'); |
861 |
$locatorbox->set_id("locatorbox_$boxcount"); |
862 |
$locatorbox->add( Dumper($this->_locator) ); |
863 |
|
864 |
// FIXME: ie/mozilla? |
865 |
//$locatorbox->set_style('display:none;'); |
866 |
//$locatorbox->set_style('visibility:hidden;'); |
867 |
// already duplicate inside Tracer!!! |
868 |
$locatorbox->set_style('visibility:hidden; position:absolute; z-index:1; margin-top:30px; padding:5px;'); |
869 |
|
870 |
$statusbox->add( html_span('boxlabel_darkgreen', "Locator:"), link::js_function( 'toggleVisibility', array("locatorbox_$boxcount"), '[show]'), $locatorbox, html_br() ); |
871 |
$call = $this->_locator->get_call(); |
872 |
$statusbox->add( html_span('boxlabel_darkgreen', "Method:"), $call[method], html_br() ); |
873 |
if (sizeof($call[args])) { |
874 |
$statusbox->add( html_span('boxlabel_darkgreen', "Arguments:"), Dumper($call[args]), html_br() ); |
875 |
} |
876 |
$statusbox->add( html_span('boxlabel_darkgreen', "Count:"), $this->get_result_count(), html_br() ); |
877 |
$box->add( $statusbox ); |
878 |
|
879 |
//print $box->render(); |
880 |
trace( $box ); |
881 |
|
882 |
} |
883 |
|
884 |
|
885 |
function &query_data() { |
886 |
//print "query!<br/>"; |
887 |
return $this->fetch_result(); |
888 |
//$this->handle_result(); |
889 |
} |
890 |
|
891 |
function query_schema() { |
892 |
user_error("FIXME: query_schema"); |
893 |
// $this->datasource_handler_call( ... ); |
894 |
} |
895 |
|
896 |
function &get_result() { |
897 |
return $this->_result; |
898 |
} |
899 |
|
900 |
function get_result_count() { |
901 |
return $this->_result_count; |
902 |
} |
903 |
|
904 |
} |
905 |
|
906 |
?> |