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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by joko, Wed Mar 5 12:04:00 2003 UTC revision 1.13 by jonen, Fri Apr 4 02:37:14 2003 UTC
# Line 1  Line 1 
1  <?php  <?php
2  /**  /**
3   * This file contains the GenericDataSource child class   * This file contains the GenericDataSource child class
  * that may use arbitrary code modules as database handlers.  
  * It's aims are to get together:  
  *   o phpHtmlLibs "source-handlers"  
  *   o PEAR's database handlers  
  *   o custom ones (e.g. Data::Driver::Proxy object, which talks to a remote rpc server)  
4   *   *
5   * @author Andreas Motl <andreas.motl@ilo.de>   * @author Andreas Motl <andreas.motl@ilo.de>
6   * @package phpHtmlLib   * @package org.netfrag.glib
7   * @module GenericDataSource   * @name DataSource::Generic
8   *   *
9   */   */
10    
11  /**  /**
12     * <b>Cvs-Log:</b>
13     *
14     * <pre>
15   * $Id$   * $Id$
16   *   *
17   * $Log$   * $Log$
18     * Revision 1.13  2003/04/04 02:37:14  jonen
19     * _query[action] == write
20     *
21     * Revision 1.12  2003/03/29 08:00:48  joko
22     * modified ErrorBoxing
23     *
24     * Revision 1.11  2003/03/28 06:45:26  joko
25     * VERBOSE mode
26     *
27     * Revision 1.10  2003/03/28 03:01:02  joko
28     * more fancy debugging-output
29     *
30     * Revision 1.9  2003/03/27 16:24:26  jonen
31     * + mugled namespace
32     * + added enhanced 'queryData'
33     *
34     * Revision 1.8  2003/03/20 07:22:14  jonen
35     * + modified case 'object' to 'objects'
36     *   (cause its loads all *objects* of a given classname)
37     *
38     * Revision 1.7  2003/03/11 01:43:00  joko
39     * + fixed metadata for phpDocumentor
40     *
41     * Revision 1.6  2003/03/11 01:22:25  joko
42     * + fixed metadata for phpDocumentor
43     *
44     * Revision 1.5  2003/03/11 00:12:49  joko
45     * + fixed metadata for phpDocumentor
46     *
47     * Revision 1.4  2003/03/10 23:25:03  joko
48     * + fixed metadata for phpDocumentor
49     *
50     * Revision 1.3  2003/03/09 15:50:36  joko
51     * + additional metadata for Autodia
52     *
53     * Revision 1.2  2003/03/05 17:28:43  joko
54     * updated docu (phpDocumentor testing....)
55     *
56   * Revision 1.1  2003/03/05 12:04:00  joko   * Revision 1.1  2003/03/05 12:04:00  joko
57   * + initial commit, from GenericDataSource   * + initial commit, from GenericDataSource
58   *   *
# Line 43  Line 79 
79   *   *
80   * Revision 1.1  2003/03/01 03:10:40  joko   * Revision 1.1  2003/03/01 03:10:40  joko
81   * + initial commit   * + initial commit
82   *   * </pre>
83   *   *
84   */   */
85    
# Line 71  Line 107 
107    // require_once("PEAR.php");   // FIXME: what about PEAR::XML::RPC?    // require_once("PEAR.php");   // FIXME: what about PEAR::XML::RPC?
108    // require_once("DB.php");    // require_once("DB.php");
109    
110    
111    loadModule('DesignPattern::Proxy');
112    loadModule('DesignPattern::TransparentProxy');
113    loadModule('DesignPattern::RemoteProxy');
114    
115    //class GenericDataSource extends MemoryDataSource {
116    //class GenericDataSource extends DesignPattern_Proxy {
117    //class GenericDataSource extends DesignPattern_RemoteProxy {
118    //class GenericDataSource extends DesignPattern_TransparentProxy {
119    
120    //class GenericDataSource extends MemoryDataSource {
121    //loadModule("DataSource::Proxy::Memory");
122    //class GenericDataSource extends DataSource_Proxy_Memory {
123    //class DataSource_GenericDataSource extends DataSource_Proxy_Memory {
124    //class DataSource_GenericDataSource extends DesignPattern_Proxy {
125    //class DataSource_GenericDataSource extends DataSource_Proxy_Memory {
126    
127    // now independent!!! new class-inheritance-tree possible now!
128    // solved by having an Adapter *and* a Proxy here!
129    //class GenericDataSource {
130    
131    loadModule('DesignPattern::AdapterProxy');
132    
133    
134    
135  /**  /**
136   * This GenericDataSource child class is *completely* independent   * This GenericDataSource child class is *completely* independent
137   * of a specific storage implementation and intended to be a wrapper   * of a specific storage implementation and intended to be a wrapper
138   * class on the way from phpHtmlLib to Xyz.   * class on the way from phpHtmlLib to Xyz. <br>
139   *    ("Yyz" gets represented by Data::Storage by now.....)   *    ("Yyz" gets represented by Data::Storage by now.....)
140   *   *
141   * GenericDataSource interacts with an intermediate "proxy" object   * GenericDataSource interacts with an intermediate "proxy" object
142   * (e.g. Data::Driver::Proxy) when doing queries.   * (e.g. Data::Driver::Proxy) when doing queries. <br>
143     * <pre>
144   *    Don't mix this up with a persistent database handle which gets   *    Don't mix this up with a persistent database handle which gets
145   *    reused each and every time for different queries.   *    reused each and every time for different queries.
146   *    Here *is* a new instance of Data::Driver::Proxy for *each* query.   *    Here *is* a new instance of Data::Driver::Proxy for *each* query.
147     * </pre>
148   *   *
149   * But the point is: Caching! The actual *data* isn't read redundant!   * But the point is: Caching! The actual *data* isn't read redundant!
150   *   *
151     * <pre>
152     * --- snipped into here from above ---
153     * that may use arbitrary code modules as database handlers.
154     * It's aims are to get together:
155     *   o phpHtmlLibs "source-handlers"
156     *   o PEAR's database handlers
157     *   o custom ones (e.g. Data::Driver::Proxy object, which talks to a remote rpc server)
158     * --- snipped into here from above ---
159     * </pre>
160   *   *
161     * <p>
162   * !!!!!!  refactor this to Data::Driver::Proxy  !!!!!!     <-----------------   * !!!!!!  refactor this to Data::Driver::Proxy  !!!!!!     <-----------------
163   *   *
164   * Data::Driver::Proxy instantiates "handlers" below itself   * Data::Driver::Proxy instantiates "handlers" below itself
# Line 95  Line 168 
168   * One worker already implemented is Data::Driver::RPC::Remote, which   * One worker already implemented is Data::Driver::RPC::Remote, which
169   * talks to a RPC::XML server (todo: talk SOAP!) using PEAR::XML::RPC.   * talks to a RPC::XML server (todo: talk SOAP!) using PEAR::XML::RPC.
170   *   *
171     * <pre>
172   * ---  refactored here, but: redundant somehow  ---   * ---  refactored here, but: redundant somehow  ---
173   * Data::Driver::Proxy uses a PEAR XML::RPC object to actually   * Data::Driver::Proxy uses a PEAR XML::RPC object to actually
174   * talk HTTP and serialize data chunks to and from XML,   * talk HTTP and serialize data chunks to and from XML,
# Line 102  Line 176 
176   * It "proxies" arbitrary data chunks a) inside a native php4 session,   * It "proxies" arbitrary data chunks a) inside a native php4 session,
177   * b) by talking to a rdbms (single proxy-table) or c) [TODO] using PEAR::Cache.   * b) by talking to a rdbms (single proxy-table) or c) [TODO] using PEAR::Cache.
178   * ---  refactored here, but: redundant somehow  ---   * ---  refactored here, but: redundant somehow  ---
179     * </pre>
180   *   *
181   * !!!!!!  refactor this to Data::Driver::Proxy  !!!!!!     <-----------------   * !!!!!!  refactor this to Data::Driver::Proxy  !!!!!!     <-----------------
182     * </p>
183   *   *
184   *   *
185   * How to use?   * <p>
186     * <b>How to use?</b>
187   *   *
188   * Pass an array holding "locator metadata" to the constructor.   * Pass an array holding "locator metadata" to the constructor.
189   * GenericDataSource takes care of the rest.   * GenericDataSource takes care of the rest.
190   *   *
191     * <pre>
192   * Pass an array to the constructor: (e.g.)   * Pass an array to the constructor: (e.g.)
193   *   *
194   *  1. doing rpc-calls....   *  1. doing rpc-calls....
195     *    <code>
196   *    $locator = array(   *    $locator = array(
197   *      type => 'rpc',   *      type => 'rpc',
198   *      metadata => array( Host => 'localhost', Port => '8765' ),   *      metadata => array( Host => 'localhost', Port => '8765' ),
199   *    );   *    );
200   *    $source = new GenericDataSource($locator);   *    $source = ne w GenericDataSource($locator);
201   *    $this->set_data_source( &$source );   *    $this->set_data_source( &$source );
202     *    </code>
203   *   *
204   *  2. [proposal] common datahandles....   *  2. [proposal] common datahandles....
205     *    <code>
206   *    $locator = array(   *    $locator = array(
207   *      type => 'mysql',   *      type => 'mysql',
208   *      dsn => 'known dsn markup',   *      dsn => 'known dsn markup',
209   *    );   *    );
210   *    $source = new GenericDataSource($locator);   *    $source = ne w GenericDataSource($locator);
211   *    $this->set_data_source( &$source );   *    $this->set_data_source( &$source );
212     *    </code>
213     * </pre>
214     * </p>
215     *
216   *   *
217     * @link http://www.netfrag.org/~joko/
218   * @author Andreas Motl <andreas.motl@ilo.de>   * @author Andreas Motl <andreas.motl@ilo.de>
219     *
220     * @link http://www.netfrag.org/~jonen/
221   * @author Sebastian Utz <seut@tunemedia.de>   * @author Sebastian Utz <seut@tunemedia.de>
222     *
223   * @copyright (c) 2003 - All Rights reserved.   * @copyright (c) 2003 - All Rights reserved.
224     *
225     * @link http://www.gnu.org/licenses/lgpl.txt
226   * @license GNU LGPL (GNU Lesser General Public License)   * @license GNU LGPL (GNU Lesser General Public License)
227   *   *
  * @author-url http://www.netfrag.org/~joko/  
  * @author-url http://www.netfrag.org/~jonen/  
  * @license-url http://www.gnu.org/licenses/lgpl.txt  
228   *   *
229   * @package phpHtmlLib   * @package org.netfrag.glib
230   * @module GenericDataSource   * @subpackage DataSource
231     * @name DataSource::Generic
232     *
233     * @todo this:
234     * o mungle this to be able to be wrapped around phpHtmlLib's own storage-handles
235     * o implement another Data::Driver::Proxy container
236     *
237     * <pre>
238     *              !!!!!!!!          THIS IS THE PROBLEM          !!!!!!!!
239     *    !!!!!!!!   here is it where we have to break inheritance again   !!!!!!!!
240     *
241     *    THE CONFLICT: Beeing in phpHtmlLib *and* DesignPattern::TransparentProxy
242     *    inheritance trees at the same time, which is *not* possible at
243     *    declare-time. We *do* need some runtime-infrastructure to solve this!
244     *
245     *    TODO: move build- and check-locator stuff from ObjectList to this place!!!
246     *
247     *    ABOUT:
248     *      1. otherwhere: WebApp - scope:
249     *          x handles page vs. block vs. widget; dispatches MVC-View
250     *      2. here: DataSource - scope:
251     *          x handles bridge to frameworks (e.g. phpHtmlLib) vs. actual data driver libs (PEAR, etc.))
252     *          o clean implementation using a DesignPattern::AdapterProxy
253     * </pre>
254   *   *
  */  
   
 /**  
  * Todo:  
  *  
  *  o mungle this to be able to be wrapped around phpHtmlLib's own storage-handles  
  *  o implement another Data::Driver::Proxy container  
255   *   *
256   */   */
   
   
 //              !!!!!!!!          THIS IS THE PROBLEM          !!!!!!!!  
 //    !!!!!!!!   here is it where we have to break inheritance again   !!!!!!!!  
 //  
 //    THE CONFLICT: Beeing in phpHtmlLib *and* DesignPattern::TransparentProxy  
 //    inheritance trees at the same time, which is *not* possible at  
 //    declare-time. We *do* need some runtime-infrastructure to solve this!  
 //  
 //    TODO: move build- and check-locator stuff from ObjectList to this place!!!  
 //  
 //    ABOUT:  
 //      1. otherwhere: WebApp - scope:  
 //          x handles page vs. block vs. widget; dispatches MVC-View  
 //      2. here: DataSource - scope:  
 //          x handles bridge to frameworks (e.g. phpHtmlLib) vs. actual data driver libs (PEAR, etc.))  
 //          o clean implementation using a DesignPattern::AdapterProxy  
 //  
   
 loadModule('DesignPattern::Proxy');  
 loadModule('DesignPattern::TransparentProxy');  
 loadModule('DesignPattern::RemoteProxy');  
   
 //class GenericDataSource extends MemoryDataSource {  
 //class GenericDataSource extends DesignPattern_Proxy {  
 //class GenericDataSource extends DesignPattern_RemoteProxy {  
 //class GenericDataSource extends DesignPattern_TransparentProxy {  
   
 //class GenericDataSource extends MemoryDataSource {  
 //loadModule("DataSource::Proxy::Memory");  
 //class GenericDataSource extends DataSource_Proxy_Memory {  
 //class DataSource_GenericDataSource extends DataSource_Proxy_Memory {  
 //class DataSource_GenericDataSource extends DesignPattern_Proxy {  
 //class DataSource_GenericDataSource extends DataSource_Proxy_Memory {  
   
 // now independent!!! new class-inheritance-tree possible now!  
 // solved by having an Adapter *and* a Proxy here!  
 //class GenericDataSource {  
   
 loadModule('DesignPattern::AdapterProxy');  
257  class DataSource_Generic extends DesignPattern_AdapterProxy {  class DataSource_Generic extends DesignPattern_AdapterProxy {
258    
259  //    !!!!!!!!   here is it where we have to break inheritance again   !!!!!!!!  //    !!!!!!!!   here is it where we have to break inheritance again   !!!!!!!!
# Line 305  class DataSource_Generic extends DesignP Line 369  class DataSource_Generic extends DesignP
369                  //$this->_locator->_datasource_type = $query[datasource];                  //$this->_locator->_datasource_type = $query[datasource];
370                  //$locator[_datasource_type] = $query[datasource];                  //$locator[_datasource_type] = $query[datasource];
371                                    
372          /**
373           * <!-- Autodia -->
374           * can do: (this is metadata supplied for Autodia, don't delete!)
375           *  $this->_locator = new DataSource_Locator()
376           *
377           */
378    
379                  // build master locator                  // build master locator
380                  $this->_locator = php::mkComponent('DataSource::Locator', $locator, array( datasource_type => $query[datasource] ) );                  $this->_locator = php::mkComponent('DataSource::Locator', $locator, array( datasource_type => $query[datasource] ) );
381                  //exit;                  //exit;
# Line 351  class DataSource_Generic extends DesignP Line 422  class DataSource_Generic extends DesignP
422  //exit;  //exit;
423    
424    
425        /**
426         * <!-- Autodia -->
427         * can do: (this is metadata supplied for Autodia, don't delete!)
428         *  $proxy = new DesignPattern_RemoteProxy()
429         *
430         */
431    
432    
433      // --- Proxy selector/dispatcher ...      // --- Proxy selector/dispatcher ...
434            
435      switch ($this->_locator->_datasource_type) {      switch ($this->_locator->_datasource_type) {
# Line 399  class DataSource_Generic extends DesignP Line 478  class DataSource_Generic extends DesignP
478            case 'phpHtmlLib':            case 'phpHtmlLib':
479              //$adapter_arguments = $args[title];              //$adapter_arguments = $args[title];
480    
481              $this->set_adapter_module('DataSource::Adapter::phpHtmlLib::DataListSource');              $this->set_adapter_module('DataSource::Adapter::phpHtmlLib::DataSource');
482                            
483              // in order to let the Adapter communicate with the Proxy,              // in order to let the Adapter communicate with the Proxy,
484              // instantiate a wrapper method in a third namespace via              // instantiate a wrapper method in a third namespace via
# Line 446  class DataSource_Generic extends DesignP Line 525  class DataSource_Generic extends DesignP
525        // $this->create_handler();        // $this->create_handler();
526    
527    
528          /**
529           * <!-- Autodia -->
530           * can do: (this is metadata supplied for Autodia, don't delete!)
531           *  $adapter = new DataSource_Adapter_phpHtmlLib_DataListSource()
532           *
533           */
534    
535        // V1:        // V1:
536        //$this->create_adapter($adapter_module, $this->function, $this->arguments);        //$this->create_adapter($adapter_module, $this->function, $this->arguments);
537    
# Line 597  class DataSource_Generic extends DesignP Line 683  class DataSource_Generic extends DesignP
683        // FIXME: abstract this some more (e.g. via a CommandMapper|Registry)        // FIXME: abstract this some more (e.g. via a CommandMapper|Registry)
684          switch ($this->_query[metatype]) {          switch ($this->_query[metatype]) {
685            case 'data':            case 'data':
686              //$command = 'queryData';              $command = 'queryData';
687              $command = 'getObjects';   // FIXME!!!              
688                //$command = 'getObjects';   // FIXME!!!
689              //$this->_locator->set_option('metadata.command', $command);              //$this->_locator->set_option('metadata.command', $command);
690    /*
691              $args = array();              $args = array();
692              switch ($this->_query[vartype]) {              switch ($this->_query[vartype]) {
693                case 'object':                case 'objects':
694                  if (!$this->_query[classname]) {                  if (!$this->_query[classname]) {
695                    $msg = "_query[vartype] == 'object' requires _query[classname]";                    $msg = "_query[vartype] == 'objects' requires _query[classname]";
696                    user_error("GenericDataSource::query_data() - failed: " . $msg);                    user_error("GenericDataSource::query_data() - failed: " . $msg);
697                  }                  }
698                  array_push($args, $this->_query[classname]);                  array_push($args, $this->_query[classname]);
699                  break;                  break;
700              }              }
701              break;   */
702                $query_args = array();
703                 switch ($this->_query[abstract_type]) {
704                  case 'list':
705                    if (!$this->_query[classname]) {
706                      $msg = "_query[vartype] == 'objects' requires _query[classname]";
707                      user_error("GenericDataSource::query_data() - failed: " . $msg);
708                    }
709                    //array_push($query_args, $this->_query[classname]);
710                    $query_args[classname] = $this->_query[classname];
711                    break;
712                  case 'item':
713                    if (!$this->_query[classname]) {
714                      $msg = "_query[vartype] == 'objects' requires _query[classname]";
715                      user_error("GenericDataSource::query_data() - failed: " . $msg);
716                    }
717                    $query_args[guid] = $this->_query[ident];
718                    $query_args[classname] = $this->_query[classname];
719                    break;              
720                }
721                if($this->_query[action] == 'write') {
722                 $query_args[action] = $this->_query[action];
723                 $query_args[data] = $this->_query[data];
724                }
725                $args = array(
726                              'data_type' => $this->_query[abstract_type],
727                              'query_args' => $query_args
728                              );
729               break;
730    
731              // querySchema
732            case 'schema':            case 'schema':
733                //print "Testing schema:" . "<br>";
734              $command = 'querySchema';              $command = 'querySchema';
735              break;              break;
736          }          }
# Line 648  class DataSource_Generic extends DesignP Line 767  class DataSource_Generic extends DesignP
767                
768    }    }
769    
770    
771    
772          function &fetch_result() {          function &fetch_result() {
773                
774        $this->datasource_handler_buildoptions();        $this->datasource_handler_buildoptions();
775    
776        $method = $this->_locator->_call[_method];        $call = $this->_locator->get_call();
777        $args = $this->_locator->_call[_arguments];        //print Dumper($call);
778          
779        // pre-flight checks        // pre-flight checks
780          if (!$method) {          if (!$call[method]) {
781            $msg = "Remote command could not be resolved, please pass in or check configuration.";            $msg = "Remote method is empty, please pass in proper metadata or check configuration.";
782                          user_error("GenericDataSource::query_data() - failed: " . $msg);                          user_error("GenericDataSource::query_data() - failed: " . $msg);
783            return;            return;
784          }          }
# Line 666  class DataSource_Generic extends DesignP Line 787  class DataSource_Generic extends DesignP
787    
788        // do remote call here and get result        // do remote call here and get result
789        // FIXME: handle synchronous/asynchronous mode here!!!        // FIXME: handle synchronous/asynchronous mode here!!!
790          $this->datasource_handler_call($method, $args);          $this->datasource_handler_call($call[method], $call[args]);
791                    
792                    
793          // TODO: ... = $this->poll_handler_result  and  $this->get_handler_result          // TODO: ... = $this->poll_handler_result  and  $this->get_handler_result
# Line 677  class DataSource_Generic extends DesignP Line 798  class DataSource_Generic extends DesignP
798          $this->_result_count = sizeof($this->_result);          $this->_result_count = sizeof($this->_result);
799    
800        // trace        // trace
801          if ($this->_debug[notice]) {          //if (constants::get('VERBOSE') && $this->_debug[notice]) {
802            if (constants::get('VERBOSE') or constants::get('ERRORS_ONLY')) {
803            //print "_result = " . Dumper($this->_result);            //print "_result = " . Dumper($this->_result);
804            print "DataSource::Generic->_result_count = " . $this->_result_count . "<br/>";            //print "<div><b><font color=\"darkgreen\">Debug:</font></b> DataSource::Generic->_result_count = <b>" . $this->_result_count . "</b></div>";
805              $this->draw_status_box();
806          }          }
807            
808          return $this->_result;          return $this->_result;
809    
810          }          }
811    
812      function draw_status_box() {
813    
814        static $boxcount;
815        
816        $boxcount++;
817    
818        $box = container();
819    
820        // box client code (javascript)
821        $code_js = "
822        <script language=\"javascript\">
823        function toggle_vis(id) {
824          elem = eval('document.all.' + id);
825          status = elem.style.getAttribute('display');
826          if (status == 'none') {
827            elem.style.setAttribute('display', 'block');
828          } else {
829            elem.style.setAttribute('display', 'none');
830          }
831        }
832        </script>
833        ";
834        //$script = html_script($code_js);
835        //print $script->render();
836        $box->add( $code_js );
837        
838        // box style
839        $style = container(
840          html_style("text/css", '.boxlabel_darkgreen { color: darkgreen; font-weight:bold; }'),
841          html_style("text/css", '.box_dsg { background: #20ab39; color: white; border: 2px black groove; width:640px; padding:10px; margin:40px; }')
842        );
843        $box->add( $style );
844        
845        // box content
846        $statusbox = html_div('box_dsg');
847        $statusbox->add( html_b("DataSource::Generic"), html_br() );
848        $locatorbox = html_div('box_dsg');
849        $locatorbox->set_id("locatorbox_$boxcount");
850        $locatorbox->add( Dumper($this->_locator) );
851        //$locatorbox->set_style('visibility:false;');
852        $locatorbox->set_style('display:none;');
853        
854        $statusbox->add( html_span('boxlabel_darkgreen', "Locator:"), html_a("javascript:toggle_vis('locatorbox_$boxcount');", '[show]'), $locatorbox, html_br() );
855        $call = $this->_locator->get_call();
856        $statusbox->add( html_span('boxlabel_darkgreen', "Method:"), $call[method], html_br() );
857        if (sizeof($call[args])) {
858          $statusbox->add( html_span('boxlabel_darkgreen', "Arguments:"), Dumper($call[args]), html_br() );
859        }
860        $statusbox->add( html_span('boxlabel_darkgreen', "Count:"), $this->get_result_count(), html_br() );
861        $box->add( $statusbox );
862    
863        print $box->render();
864    
865      }
866    
867    
868    function &query_data() {    function &query_data() {
869      //print "query!<br/>";      //print "query!<br/>";
# Line 701  class DataSource_Generic extends DesignP Line 879  class DataSource_Generic extends DesignP
879    function &get_result() {    function &get_result() {
880      return $this->_result;      return $this->_result;
881    }    }
882      
883      function get_result_count() {
884        return $this->_result_count;
885      }
886                    
887  }  }
888    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.13

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