/[cvs]/nfo/php/libs/org.netfrag.glib/DataSource/Generic.php
ViewVC logotype

Contents of /nfo/php/libs/org.netfrag.glib/DataSource/Generic.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Mon Mar 10 23:25:03 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.3: +24 -21 lines
+ fixed metadata for phpDocumentor

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed