home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / daogen / base.inc < prev    next >
Text File  |  2004-03-08  |  11KB  |  360 lines

  1. <?php
  2. echo "<h1>Value Object Base Class</h1>";
  3. print "<textarea name=\"valueObj\" cols=\"80\" rows=\"15\">";
  4. print htmlspecialchars("<?php\n");
  5. ?>
  6. class valueObject {
  7.         function toString() {
  8.                 $arr = get_object_vars($this);
  9.                 $str = get_class($this)." {\n";
  10.                 foreach($arr as $k => $v) {
  11.                         $str .= "\t$k: $v;\n";
  12.                 }
  13.                 $str .= "}\n";
  14.                 return $str;
  15.         }
  16.                                                                                                                                             
  17.         function serialize() {
  18.                 $arr = get_object_vars($this);
  19.                 return $arr;
  20.         }
  21.                                                                                                                                             
  22.         function unserialize($serializedValueObject) {
  23.                 if(is_array($serializedValueObject)) {
  24.                         foreach($serializedValueObject as $k => $v)
  25.                                 $this->$k = $v;
  26.                                                                                                                                             
  27.                         return $this;
  28.                 }
  29.                 else {
  30.                         return FALSE;
  31.                 }
  32.         }
  33. }
  34. <?php
  35. print htmlspecialchars("\n?>");
  36. echo '</textarea>';
  37. print "</pre>";
  38.  
  39. echo "<h1>Data Access Object Base Class</h1>";
  40. print "<pre>";
  41. print "<textarea name=\"valueObj\" cols=\"80\" rows=\"15\">";
  42. print htmlspecialchars("<?php\n");
  43. ?>
  44. /**
  45.   * Data Access Object (DAO).
  46.   * This class contains all database handling that is needed to
  47.   * permanently store and retrieve object instances.
  48.   */
  49.  
  50. /**
  51.  * Replace this with an inclusion of the database handler class
  52.  */
  53.  
  54. class Dao
  55. {
  56.  
  57.     /**
  58.      * populateVo-method.  This will take a database row and populate
  59.      * a Value Object with it's contents.
  60.      *
  61.      * @param row This is a row object from the db_mysql->query call
  62.      */
  63.     function populateVo(&$row) {
  64.         die("Must be implemented fully inside child class");
  65.     }
  66.     
  67.     /**
  68.      * doQuery-method.  This method serves as a wrapper to the database
  69.      * class used behind this DAO.  The purpose is twofold: one, don't
  70.      * duplicate code; two, make it easy to change out the database backend
  71.      * three, keep all database error checking in one place
  72.      *
  73.      * @param sql SQL string to use as query
  74.      * @return result Database result object
  75.      */
  76.     function doQuery($sql) {
  77.         $db = db_mysql::instance();
  78.  
  79.         $dbresult = $db->query($sql);
  80.  
  81.         if($db->geterrorstatus() != 0) {
  82.             //return and raise an error
  83.             trigger_error("Database Error".$sql);
  84.             $return = FALSE;
  85.         }
  86.         else {
  87.             $return =& $dbresult; 
  88.         }
  89.  
  90.         return $return;
  91.     }
  92.  
  93.     /**
  94.      * sanitize-method.  This method sanitizes database variables.
  95.      *
  96.      * @param var Unclean variable
  97.      * @return var Clean variable
  98.      */
  99.     function sanitize($var) {
  100.         $db = db_mysql::instance();
  101.         return $db->sanitize($var);
  102.     }
  103.  
  104.     /**
  105.      * checkPrimaryKey-method.  This method makes sure that the primary keys
  106.      * of the value object being passed are defined because they must be used
  107.      * in the SQL query.
  108.      *
  109.      * @param vo Value Object to check
  110.      * @return boolean TRUE/FALSE depending on whether it passed the test
  111.      */
  112.     function checkPrimaryKeys(&$vo) {
  113.         die("Must be implemented fully inside child class");
  114.     }
  115.  
  116.     /**
  117.      * exists-method.  this will execute a query and return
  118.      * true or false if the passed valueobject exists in the
  119.      * data store.
  120.      *
  121.      * @param vo  this parameter contains the class instance to be loaded.
  122.      */
  123.     function exists(&$vo) {
  124.         if($this->checkPrimaryKeys($vo)) {
  125.             $sql = $this->selectAll_SQLHOOK().$this->where_SQLHOOK(&$vo);
  126.             
  127.             $dbresult = $this->doQuery($sql);
  128.  
  129.             if($dbresult->total_rows() == 0 || !$dbresult) {
  130.                 $return FALSE;
  131.             }
  132.             else {
  133.                 $return TRUE;
  134.             }
  135.         }
  136.         else {
  137.             $return FALSE;
  138.         }
  139.         return $return;
  140.     }
  141.  
  142.     /**
  143.      * load-method. this will load vo contents from database using
  144.      * primary-key as identifier. upper layer should use this so that vo
  145.      * instance is created and only primary-key should be specified. then call
  146.      * this method to complete other persistent information. this method will
  147.      * overwrite all other fields except primary-key and possible runtime variables.
  148.      *
  149.      * @param vo  this parameter contains the class instance to be loaded.
  150.      *                     primary-key field must be set for this to work properly.
  151.      */
  152.     function load(&$vo) 
  153.     {
  154.         if($this->checkPrimaryKeys($vo)) {
  155.             $sql = $this->selectAll_SQLHOOK().$this->where_SQLHOOK(&$vo);
  156.             unset($vo);
  157.             $dbresult = $this->doQuery($sql);
  158.             $row = $dbresult->next_obj();
  159.  
  160.             $result = $this->populateVo($row);
  161.         }
  162.         else {
  163.             $result = FALSE;
  164.         }
  165.  
  166.         return $result;
  167.     }
  168.  
  169.  
  170.     /**
  171.      * loadall-method. this will read all contents from database table and
  172.      * build an vector containing vos. please note, that this method
  173.      * will consume huge amounts of resources if table has lot's of rows.
  174.      * this should only be used when target tables have only small amounts
  175.      * of data.
  176.      *
  177.      */
  178.     function loadAll() 
  179.     {
  180.           $sql = $this->selectAll_SQLHOOK().$this->orderBy_SQLHOOK;
  181.           $dbresult = $this->doQuery($sql);
  182.  
  183.           $searchresults = array();
  184.           while($row = $dbresult->next_obj()) 
  185.             $searchresults[] = $this->populateVo($row);
  186.  
  187.           return $searchresults;
  188.     }
  189.  
  190.     /**
  191.      * loadlimit-method. this will read all contents from database table and
  192.      * build an vector containing vos. please note, that this method
  193.      * will consume huge amounts of resources if table has lot's of rows.
  194.      * this should only be used when target tables have only small amounts
  195.      * of data.
  196.      *
  197.      */
  198.     function loadLimit($limit=10,$offset=0) 
  199.     {
  200.           if($offset == 0)
  201.               $limit = "LIMIT $limit";
  202.           else
  203.               $limit = "LIMIT $offset,$limit";
  204.  
  205.           $sql = $this->selectAll_SQLHOOK().$this->orderBy_SQLHOOK." $limit";
  206.  
  207.           $dbresult = $this->doQuery($sql);
  208.  
  209.           $searchresults = array();
  210.           while($row = $dbresult->next_obj()) 
  211.             $searchresults[] = $this->populateVo($row);
  212.  
  213.           return $searchresults;
  214.     }
  215.  
  216.     /**
  217.      * create-method. this will create new row in database according to supplied
  218.      * vo contents. make sure that values for all not null columns are
  219.      * correctly specified. also, if this table does not use automatic surrogate-keys
  220.      * the primary-key must be specified. after insert command this method will
  221.      * read the generated primary-key back to vo if automatic surrogate-keys
  222.      * were used.
  223.      *
  224.      * @param vo  this parameter contains the class instance to be created.
  225.      *                  if automatic surrogate-keys are not used the primary-key
  226.      *                  field must be set for this to work properly.
  227.      */
  228.     function create(&$vo) 
  229.     {
  230.         $db = db_mysql::instance();
  231.         $sql = $this->insert_SQLHOOK(&$vo);
  232.         $this->doQuery($sql);
  233.         $result = $db->getlatestinsertid();
  234.         return $result;
  235.     }
  236.  
  237.     /**
  238.      * save-method. this method will discover the current state of the vo in the
  239.      * database.  it will decide whether it already exists and update the record
  240.      * or it will insert it as a new record.
  241.      *
  242.      * @param vo  this parameter contains the class instance to be updated.
  243.      *                     primary-key field must be set for this to work properly.
  244.      */
  245.  
  246.     function save(&$vo) {
  247.         if($this->exists($vo)) {
  248.             return $this->update($vo);
  249.         }
  250.         else {
  251.             return $this->create($vo);
  252.         }
  253.     }
  254.  
  255.     /**
  256.      * update-method. this method will update the current state of vo to database.
  257.      * save can not be used to create new instances in database, so upper layer must
  258.      * make sure that the primary-key is correctly specified. primary-key will indicate
  259.      * which instance is going to be updated in database.
  260.      *
  261.      * @param vo  this parameter contains the class instance to be updated.
  262.      *                     primary-key field must be set for this to work properly.
  263.      */
  264.     function update(&$vo) 
  265.     {
  266.         $sql = $this->update_SQLHOOK(&$vo);
  267.  
  268.         $this->doQuery($sql);
  269.  
  270.         return true;
  271.     }
  272.  
  273.  
  274.     /**
  275.      * delete-method. this method will remove the information from database as identified by
  276.      * by primary-key in supplied vo. once vo has been deleted it can not
  277.      * be restored by calling save. restoring can only be done using create method but if
  278.      * database is using automatic surrogate-keys, the resulting object will have different
  279.      * primary-key than what it was in the deleted object.
  280.      *
  281.      * @param vo  this parameter contains the class instance to be deleted.
  282.      *                     primary-key field must be set for this to work properly.
  283.      */
  284.     function delete(&$vo) 
  285.     {
  286.         if($this->checkPrimaryKeys($vo)) {
  287.             $sql = $this->delete_SQLHOOK().$this->where_SQLHOOK(&$vo);
  288.             $this->doQuery($sql);
  289.             $result = true;
  290.         }
  291.         else {
  292.             $result = false;
  293.         }
  294.  
  295.         return $result;
  296.     }
  297.  
  298.     /**
  299.     * deleteall-method. this method will remove all information from the table that matches
  300.     * this dao and valueobject couple. this should be the most efficient way to clear table.
  301.     * once deleteall has been called, no vo that has been created before can be
  302.     * restored by calling save. Restoring can only be done using create method but if database
  303.     * is using automatic surrogate-keys, the resulting object will have different primary-key
  304.     * than what it was in the deleted object. (Note, the implementation of this method should
  305.     * be different with different DB backends.)
  306.     */
  307.     function deleteAll() 
  308.     {
  309.         $sql = $this->delete_SQLHOOK();
  310.  
  311.         $this->doQuery($sql);
  312.  
  313.         return TRUE;
  314.     }
  315.  
  316.     /**
  317.     * coutAll-method. This method will return the number of all rows from table that matches
  318.     * this Dao. The implementation will simply execute "select count(primarykey) from table".
  319.     * If table is empty, the return value is 0. This method should be used before calling
  320.     * loadAll, to make sure table has not too many rows.
  321.     *
  322.     */
  323.     function countAll() 
  324.     {
  325.         $result = $this->doQuery($this->countAll_SQLHOOK());
  326.  
  327.         $row = $result->next_row();
  328.  
  329.         return $row[0];
  330.     }
  331.  
  332.     /**
  333.      * searchMatching-Method. This method provides searching capability to
  334.      * get matching vos from database. It works by searching all
  335.      * objects that match permanent instance variables of this objects.
  336.      * Upper layer should use this by setting some parameters in vo
  337.      * and then  call searchMatching. The result will be 0-N objects in vector,
  338.      * all matching those criteria you specified. Those instance-variables that
  339.      * have NULL values are excluded in search-criteria.
  340.      *
  341.      * @param vo  This parameter contains the class instance where search will be based.
  342.      *            Primary-key field should not be set.
  343.      */
  344.     function searchMatching(&$vo) 
  345.     {
  346.         $dbresult = $this->doQuery($this->searchMatching_SQLHOOK(&$vo));
  347.  
  348.         $searchresults = array();
  349.         while($row = $dbresult->next_obj()) 
  350.             $searchresults[] = $this->populateVo($row);
  351.  
  352.         return $searchresults;
  353.     }
  354. }
  355. <?php
  356. print htmlspecialchars("\n?>");
  357. echo '</textarea>';
  358. print "</pre>";
  359. ?>
  360.