home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / daogen / actions / generateDao.php next >
PHP Script  |  2004-03-08  |  6KB  |  235 lines

  1. <?php
  2. /*
  3. Copyright T & M Web Enterprises 2003
  4. Author: Mike Hostetler <mike@tm-web.com>
  5. Version: 1.0 Release date: 01 November 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12.  
  13. /**
  14.  *
  15.  *@NAME:        generateDao
  16.  *@PURPOSE:     To take an array of input parameters and generate a php Data Access Object
  17.  *@INPUT:       
  18.  *                @param $classname The name of the Dao class
  19.  *                @param $tablename The name of the database table to use with the Dao
  20.  *                @param $pk_num Integer, number of primary keys
  21.  *                @param $col_num Integer, number of columns
  22.  *                @param $colname Array of Database column names 
  23.  *                @param $varname Array of variable names
  24.  *                @param $type_colname Datatype of colname rows
  25.  *                @param $ordering Index of colname that is to be used with sql ORDER BY statments
  26.  *@ACTION:      Generate the code in ASCII text and return it
  27.  *@OUTPUT:      ASCII text of Vo code
  28.  *@EXCEPTIONS:  None
  29.  *@REMARKS:     None
  30.  *
  31.  */
  32.  
  33. function generateDao($classname,$tablename,$pk_num,$col_num,$colname,$varname,$type_colname,$ordering) {
  34.     //We want to buffer all of this to return it
  35.     ob_start();
  36. ?>
  37. /**
  38.   * <?php echo $classname;?> Data Access Object (DAO).
  39.   * This class contains all database handling that is needed to
  40.   * permanently store and retrieve <?php echo $classname;?>  object instances.
  41.   */
  42.  
  43. /**
  44.  * Replace this with an inclusion of the DAO parent class
  45.  * Replace this with an inclusion of the associated Vo class
  46.  */
  47.  
  48. class <?php echo $classname;?>Dao extends Dao
  49. {
  50.  
  51.     /**
  52.      * populateVo-method.  This will take a database row and populate
  53.      * a Value Object with it's contents.
  54.      *
  55.      * @param row This is a row object from the db_mysql->query call
  56.      */
  57.     function populateVo(&$row) {
  58.         $vo =& new <?php echo $classname;?>Vo();
  59. <?php
  60.     for($i=0;$i<$col_num;$i++)
  61.         printf("\t\t\$vo->%s = \$row->%s;\n",$varname[$i],$varname[$i]);
  62.     ?>
  63.         return $vo;
  64.     }
  65.  
  66.     /**
  67.      * checkPrimaryKeys-method.  Checks to make sure primary keys of
  68.      * the VO are set and valid.
  69.      *
  70.      * @param Object vo Value Object to check
  71.      * @return Boolean var Clean variable
  72.      */
  73.     function checkPrimaryKeys(&$vo) {
  74.         $return = TRUE;
  75. <?php
  76. $b= "";
  77. for($i=0;$i<$pk_num;$i++)
  78. {
  79.     echo "\n\t\t{$b}if (\$vo->{$varname[$i]} == \"\")
  80.         {
  81.             //print \"can not check existance without primary-key!\";
  82.             trigger_error('Value Object Error'".'.$vo->toString'.");
  83.             \$return FALSE;
  84.         }";
  85.     $b = "else";
  86. }?>
  87.  
  88.         return $return;
  89.     }
  90.  
  91.     /**
  92.      * selectAll-method. Creates 'SELECT * FROM db_table'
  93.      */
  94.     function selectAll_SQLHOOK() {
  95.         return "SELECT * FROM <?php echo $tablename;?> ";
  96.     }
  97.  
  98.     /**
  99.      * where-method. Creates a SQL 'WHERE' clause for the table.
  100.      *
  101.      * @param Object vo  Value Object that contains the primary keys.
  102.      */
  103.     function where_SQLHOOK(&$vo) 
  104.     {
  105.         $sql = "WHERE 1";
  106. <?php
  107.         for( $i = 0; $i < $pk_num ; $i ++ )
  108.         {
  109.             printf("\t\t\t".'$sql .= " AND %s = ".$this->sanitize($vo->%s);'."\n",$varname[$i],$varname[$i]);
  110.         }
  111. ?>
  112.  
  113.         return $sql;
  114.     }
  115.  
  116.     /**
  117.      * insert-method. Creates a SQL 'Insert' statement from the 
  118.      * supplied Value Object.
  119.      *
  120.      * @param Object vo  Value Object that contains the primary keys.
  121.      */
  122.     function insert_SQLHOOK(&$vo) 
  123.     {
  124.         $sql = "INSERT INTO <?php echo $tablename;?> ( <?php 
  125. $a = "";
  126. for( $i = 0; $i < $col_num ; $i ++ )
  127. {
  128.     $a .= sprintf('%s, ',$varname[$i]);
  129. }
  130. $a = substr_replace ( $a, "", -2 );
  131. echo $a;
  132.  
  133. ?> ) values (";<?php
  134. $a = "";
  135. for( $i = 0; $i < $col_num ; $i ++ )
  136. {
  137.         $a .= sprintf("\n\t\t".'$sql .= $this->sanitize($vo->%s).", ";',$varname[$i]); 
  138. }
  139. $a = substr_replace ( $a, " )\";", -4 );
  140. echo $a;
  141. ?>      
  142.  
  143.         return $sql;
  144.     }
  145.  
  146.     /**
  147.      * update-method. This method creates a SQL 'Update' statement from
  148.      * the supplied Value Object.
  149.      *
  150.      * @param Object vo  Value Object that contains the primary keys.
  151.      */
  152.     function update_SQLHOOK(&$vo) 
  153.     {
  154.  
  155.         $sql = "UPDATE <?php echo $tablename;?> SET ";<?php
  156. $a = "";
  157. for( $i = 0; $i < $col_num ; $i ++ )
  158. {
  159.         $a .= sprintf("\n\t\t".'$sql .= "%s = ".$this->sanitize($vo->%s).", ";',$varname[$i],$varname[$i]); 
  160. }
  161. $a = substr_replace ( $a, ' ";', -4 );
  162. print $a;
  163. ?>
  164.  
  165.         $sql .= "WHERE 1";<?php
  166. for( $i = 0; $i < $pk_num ; $i ++ )
  167. {
  168.     printf("\n\t\t".'$sql .= " AND %s = ".$this->sanitize($vo->%s);',$varname[$i],$varname[$i]);
  169. }?>          
  170.  
  171.         return $sql;
  172.     }
  173.  
  174.  
  175.     /**
  176.      * delete-method. Creates a SQL 'Delete' statement;
  177.      */
  178.     function delete_SQLHOOK() 
  179.     {
  180.         return "DELETE FROM <?php echo $tablename;?> ";
  181.     }
  182.  
  183.     /**
  184.     * coutAll-method. Creates a SQL SELECT special case that counts the number of rows.
  185.     */
  186.     function countAll_SQLHOOK() 
  187.     {
  188.         return "SELECT count(<?php echo $varname[0];?>) AS count FROM <?php echo $tablename;?>";
  189.     }
  190.  
  191.     /**
  192.      * searchMatching-Method. Creates a specialized SQL statment that searches
  193.      * based upon any value in the supplied Value Object.
  194.      *
  195.      * @param Object vo  Value Object that contains the primary keys.
  196.      */
  197.     function searchMatching_SQLHOOK(&$vo) 
  198.     {
  199.  
  200.         $sql = "SELECT * FROM <?php echo $tablename;?> WHERE 1 ";
  201.  
  202. <?php
  203. $a = "";
  204. for( $i = 0; $i < $col_num ; $i ++ )
  205. {
  206.     $op = '=';
  207.     $mod = '';
  208.  
  209.     if( $type_colname[$i] == 'varchar' )
  210.     {
  211.         $op = 'LIKE';
  212.         $mod = '%';
  213.     }
  214.  
  215.     ?>
  216.         if ($vo-><?php echo $varname[$i]; ?> != "") 
  217.         {
  218.             $sql .= " AND <?php echo $varname[$i].' '.$op; ?> ".$this->sanitize('<?php echo $mod;?>'.$vo-><?php echo $varname[$i];?>.'<?php echo $mod;?>');
  219.         }
  220. <?php
  221. }
  222. ?>     
  223.         $sql .= " ORDER BY <?php echo $colname[$ordering] ?> ";
  224.  
  225.         return $sql;
  226.     }
  227. }
  228. <?php
  229.  
  230.     $contents = ob_get_contents();
  231.     ob_end_clean();
  232.     return $contents;
  233. }
  234. ?>
  235.