Changeset 192

Show
Ignore:
Timestamp:
06/08/06 00:48:20 (3 years ago)
Author:
laurentj
Message:

implementation de l'interface Iterator dans jDbRecordset, les méthodes daos renvoi maintenant un recordset, et non plus un tableau, et suppression de jDbRecordSet::fetchInto, ajout de setFetchMode, s/jDAO/jDao dans testapp, amelioration template dao testapp

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/jelix/dao/jDaoBase.class.php

    r174 r192  
    2020 
    2121 
    22    protected $_properties
     22   protected $_properties=array()
    2323 
    2424   public function getProperties(){ return $this->_properties; } 
     
    117117 
    118118   public function findAll(){ 
    119       $dbw = new jDbWidget($this->_conn); 
    120       return $dbw->fetchAllInto($this->_selectClause.$this->_fromClause.$this->_whereClause , $this->_DaoRecordClassName); 
     119      $rs =  $this->_conn->query ($this->_selectClause.$this->_fromClause.$this->_whereClause); 
     120      $rs->setFetchMode(8,$this->_DaoRecordClassName); 
     121      return $rs; 
    121122   } 
    122123 
    123124   public function countAll(){ 
    124125     $query = 'SELECT COUNT(*) as c '.$this->_fromClause.$this->_whereClause; 
    125      $dbw = new jDbWidget ($this->_conn); 
    126      $res = $dbw->fetchFirst ($query, 'testapp~config'); 
     126     $rs  =  $this->_conn->query ($query); 
     127     $res = $rs->fetch (); 
    127128     return $res->c; 
    128129   } 
     
    139140      } 
    140141 
    141       $dbw =  new jDbWidget ($this->_conn); 
    142142      $q = $this->_selectClause.$this->_fromClause.$this->_whereClause; 
    143143      $q .= $this->_getPkWhereClauseForSelect($keys); 
    144144 
    145       $record = $dbw->fetchFirstInto($q, $this->_DaoRecordClassName); 
     145      $rs  =  $this->_conn->query ($q); 
     146      $rs->setFetchMode(8,$this->_DaoRecordClassName); 
     147      $record =  $rs->fetch (); 
    146148      return $record; 
    147149   } 
     
    174176         $query .= $this->_createConditionsClause($searchcond); 
    175177      } 
    176       $dbw = new jDBWidget ($this->_conn); 
    177       return $dbw->fetchAllInto ($query, $this->_DaoRecordClassName); 
     178 
     179      $rs  =  $this->_conn->query ($query); 
     180      $rs->setFetchMode(8,$this->_DaoRecordClassName); 
     181      return $rs; 
    178182   } 
    179183 
     
    188192      $c = $this->_DaoRecordClassName; 
    189193      $rec= new $c(); 
    190       $fields = & $rec->getProperties(); 
     194      $fields = $rec->getProperties(); 
    191195 
    192196      $sql = $this->_generateCondition ($daocond->condition, $fields, true); 
  • trunk/lib/jelix/dao/jDaoGenerator.class.php

    r181 r192  
    44* @subpackage dao 
    55* @version    $Id:$ 
    6 * @author     Croes G�ald, Laurent Jouanneau 
     6* @author     Croes G�ld, Laurent Jouanneau 
    77* @contributor Laurent Jouanneau 
    88* @copyright  2001-2005 CopixTeam, 2005-2006 Laurent Jouanneau 
     
    1414* il est sous Copyright 2001-2005 CopixTeam (licence LGPL) 
    1515* Auteurs initiaux : Gerald Croes et Laurent Jouanneau 
    16 * Adapt� et am�ior� pour Jelix par Laurent Jouanneau 
     16* Adapt�et am�or�our Jelix par Laurent Jouanneau 
    1717*/ 
    1818 
    1919/** 
    20 * G��ateur d'une classe PHP correspondant �un objet DAO d�init dans une fichier xml 
    21 * de d�inition 
     20* G�rateur d'une classe PHP correspondant �n objet DAO d�nit dans une fichier xml 
     21* de d�nition 
    2222*/ 
    2323 
     
    6767      $src = array(); 
    6868      $src[] = ' require_once ( JELIX_LIB_DAO_PATH .\'jDaoBase.class.php\');'; 
    69       $src[] = ' require_once ( JELIX_LIB_DB_PATH .\'jDbWidget.class.php\');'; 
    7069 
    7170      //----------------------- 
     
    318317               break; 
    319318               case 'count': 
    320                   $src[] = '    $dbw = new jDbWidget ($this->_conn);'; 
    321                   $src[] = '    $res = $dbw->fetchFirst ($query);'; 
     319                  $src[] = '    $rs = $this->_conn->query($query);'; 
     320                  $src[] = '    $res = $rs->fetch();'; 
    322321                  $src[] = '    return $res->c;'; 
    323322                  break; 
    324323               case 'selectfirst': 
    325                   $src[] = '    $dbw = new jDbWidget ($this->_conn);'; 
    326                   $src[] = '    return $dbw->fetchFirstInto ($query, \''.$this->_DaoRecordClassName.'\');'; 
     324                  $src[] = '    $rs = $this->_conn->query($query);'; 
     325                  $src[] = '    $rs->setFetchMode(8,\''.$this->_DaoRecordClassName.'\');'; 
     326                  $src[] = '    return $rs->fetch();'; 
    327327                  break; 
    328328               case 'select': 
    329329               default: 
    330                   $src[] = '    $dbw = new jDbWidget ($this->_conn);'; 
    331                   $src[] = '    return $dbw->fetchAllInto ($query, \''.$this->_DaoRecordClassName.'\''.$limit.');'; 
     330                  if($limit) 
     331                      $src[] = '    $rs = $this->_conn->limitQuery($query'.$limit.');'; 
     332                  else 
     333                      $src[] = '    $rs = $this->_conn->query($query);'; 
     334                  $src[] = '    $rs->setFetchMode(8,\''.$this->_DaoRecordClassName.'\');'; 
     335                  $src[] = '    return $rs;'; 
    332336         } 
    333337         $src[] = '}'; 
  • trunk/lib/jelix/db/drivers/mysql/jDbResultSet.mysql.class.php

    r1 r192  
    2929        return mysql_free_result ($this->_idResult); 
    3030    } 
     31    protected function _rewind (){ 
     32        return mysql_data_seek ( $this->_idResult, 0); 
     33    } 
    3134 
    3235    public function rowCount(){ 
  • trunk/lib/jelix/db/drivers/postgresql/jDbResultSet.postgresql.class.php

    r1 r192  
    2525        $this->_cnt = $cnt; 
    2626        } 
    27    protected function _fetch(){ 
    28       $toReturn = pg_fetch_object ($this->_idResult); 
    29       return $toReturn; 
    30    } 
    31    protected function _free (){ 
    32       return pg_free_result ($this->_idResult); 
     27    protected function _fetch(){ 
     28        $toReturn = pg_fetch_object ($this->_idResult); 
     29        return $toReturn; 
     30    } 
     31    protected function _free (){ 
     32        return pg_free_result ($this->_idResult); 
     33    } 
    3334 
    34    } 
    35    public  function rowCount(){ 
    36       return pg_num_rows($this->_idResult); 
    37    } 
     35    protected function _rewind (){ 
     36        return pg_result_seek ( $this->_idResult, 0 ); 
     37    } 
     38 
     39    public  function rowCount(){ 
     40        return pg_num_rows($this->_idResult); 
     41    } 
    3842 
    3943    public function bindColumn($column, &$param , $type=null ) 
     
    4347    public function bindValue($parameter, $value, $data_type) 
    4448       {throw new JException('jelix~db.error.feature.unsupported', array('pgsql','bindValue')); } 
     49 
    4550    public function columnCount(){ 
    4651      return pg_num_fields($this->_idResult); 
    47    } 
     52    } 
     53 
    4854    public function execute($parameters=array()){ 
    4955        $this->_idResult= pg_execute($this->_cnt,$this->_stmtId, $parameters); 
    5056        return true; 
    5157    } 
    52  
    53  
    5458} 
    5559?> 
  • trunk/lib/jelix/db/drivers/sqlite/jDbResultSet.sqlite.class.php

    r140 r192  
    55* @version    $Id:$ 
    66* @author     Loic Mathaud 
    7 * @contributor  
     7* @contributor 
    88* @copyright  2006 Loic Mathaud 
    99* @link      http://www.jelix.org 
     
    2525    } 
    2626 
     27    protected function _rewind (){ 
     28        return sqlite_rewind ( $this->_idResult ); 
     29    } 
     30 
    2731    public function rowCount(){ 
    2832        return sqlite_num_rows($this->_idResult); 
  • trunk/lib/jelix/db/jDbPDOConnection.class.php

    r60 r192  
    1616define('JPDO_FETCH_OBJ',5); // PDO::FETCH_OBJ 
    1717define('JPDO_FETCH_ORI_NEXT',0); // PDO::FETCH_ORI_NEXT 
     18define('JPDO_FETCH_ORI_FIRST',3); 
    1819define('JPDO_FETCH_CLASS',8); // PDO::FETCH_CLASS 
    1920define('JPDO_ATTR_STATEMENT_CLASS',13); //PDO::ATTR_STATEMENT_CLASS 
    2021define('JPDO_ATTR_AUTOCOMMIT',0); //PDO::ATTR_AUTOCOMMIT 
     22define('JPDO_ATTR_CURSOR',10); // PDO::ATTR_CURSOR 
     23define('JPDO_CURSOR_SCROLL',1); //PDO::CURSOR_SCROLL 
    2124 
    22 class jDbPDOResultSet extends PDOStatement
     25class jDbPDOResultSet extends PDOStatement implements Iterator
    2326 
    24    public function fetchAll ( $fetch_style = JPDO_FETCH_OBJ, $column_index=0 ){ 
    25       return parent::fetchAll( JPDO_FETCH_OBJ, $column_index); 
    26    } 
     27    const FETCH_CLASS = 8; 
    2728 
    28    public function fetch( $fetch_style= JPDO_FETCH_OBJ, $cur_or=JPDO_FETCH_ORI_NEXT, $cur_offset=0 ){ 
    29      return parent::fetch(JPDO_FETCH_OBJ,$cur_or,$cur_offset); 
    30    } 
    31     /** 
    32     * recupere un enregistrement et rempli les propri�s d'un objet existant avec 
    33     * les valeurs r�p�es. 
    34     * @param object/string  $object ou nom de la classe 
    35     * @return  boolean  indique si il y a eu des resultats ou pas. 
    36     */ 
    37    public function fetchInto ( $object){ 
    38       if(is_object($object)){ 
    39         if ($result = $this->fetch ()){ 
    40                 foreach (get_object_vars ($result) as $k=>$value){ 
    41                     $object->$k = $value; 
    42                 } 
    43                 return $object; 
    44             }else{ 
    45                 return false; 
    46             } 
     29    protected $_fetchMode = 0; 
    4730 
    48       }else{ 
    49          $this->setFetchMode( JPDO_FETCH_CLASS, $object ); 
    50          return $this->fetch (JPDO_FETCH_CLASS); 
    51       } 
    52    } 
     31    public function fetchAll ( $fetch_style = JPDO_FETCH_OBJ, $column_index=0 ){ 
     32        if($this->_fetchMode){ 
     33            return parent::fetchAll($this->_fetchModeSet, $column_index); 
     34        }else{ 
     35            return parent::fetchAll( JPDO_FETCH_OBJ, $column_index); 
     36        } 
     37    } 
     38 
     39    public function fetch( $fetch_style= null, $cur_or=JPDO_FETCH_ORI_NEXT, $cur_offset=0 ){ 
     40        if($this->_fetchMode){ 
     41            return parent::fetch($this->_fetchModeSet, $cur_or, $cur_offset); 
     42        }else{ 
     43            return parent::fetch(JPDO_FETCH_OBJ,$cur_or,$cur_offset); 
     44        } 
     45    } 
     46 
     47    public function setFetchMode($mode, $param=null){ 
     48        $this->_fetchMode = $mode; 
     49        return parent::setFetchMode($mode, $param); 
     50    } 
     51 
     52    //--------------- interface Iterator 
     53    protected $_currentRecord = false; 
     54    protected $_recordIndex = 0; 
     55 
     56    function current () { 
     57        return $this->_currentRecord; 
     58    } 
     59        function key () { 
     60          return $this->_recordIndex; 
     61        } 
     62 
     63        function next () { 
     64          $this->_currentRecord =  $this->fetch(JPDO_FETCH_OBJ,JPDO_FETCH_ORI_NEXT); 
     65          if($this->_currentRecord) 
     66              $this->_recordIndex++; 
     67        } 
     68 
     69        function rewind () { 
     70          $this->_rewind(); 
     71          $this->_recordIndex = 0; 
     72          $this->_currentRecord =  $this->fetch(JPDO_FETCH_OBJ,JPDO_FETCH_ORI_FIRST); 
     73        } 
     74 
     75        function valid () { 
     76          return ($this->_currentRecord != false); 
     77        } 
    5378 
    5479} 
     
    80105    } 
    81106 
     107    public function query ($queryString, $opt=false){ 
     108        if($opt) return parent::query($queryString); 
     109        // on passe par prepare, pour pouvoir specifier JPDO_CURSOR_SCROLL �ause de l'iterateur 
     110        $sth = $this->prepare($queryString, array(JPDO_ATTR_CURSOR=> JPDO_CURSOR_SCROLL)); 
     111        $sth->execute(); 
     112        return $sth; 
     113    } 
    82114 
    83115    public function limitQuery ($queryString, $limitOffset = null, $limitCount = null){ 
  • trunk/lib/jelix/db/jDbResultSet.class.php

    r4 r192  
    1111*/ 
    1212 
    13 abstract class jDbResultSet { 
     13abstract class jDbResultSet implements Iterator { 
     14 
     15    const FETCH_CLASS = 8; 
    1416 
    1517        protected $_idResult=null; 
     18        protected $_fetchMode = 0; 
     19        protected $_fetchModeParam = ''; 
    1620 
    1721        function __construct (  $idResult){ 
     
    2630    } 
    2731 
    28   /** 
    29     * fetch et renvoi les resultats sous forme d'un objet 
    30     * @return object l'objet contenant les champs r�p�s, ou false si le curseur est �a fin 
    31     */ 
     32    public function setFetchMode($fetchmode, $param=null){ 
     33        $this->_fetchMode = $fetchmode; 
     34        $this->_fetchModeParam =$param; 
     35    } 
     36    /** 
     37     * fetch et renvoi les resultats sous forme d'un objet 
     38     * @return object l'objet contenant les champs r�p�s, ou false si le curseur est �a fin 
     39     */ 
    3240        public function fetch(){ 
    3341                $result = $this->_fetch (); 
     42                if($result && $this->_fetchMode == self::FETCH_CLASS){ 
     43                    $object = $this->_fetchModeParam; 
     44                    $object = new $object(); 
     45            foreach (get_object_vars ($result) as $k=>$value){ 
     46                $object->$k = $value; 
     47            } 
     48            $result = $object; 
     49                } 
    3450                return $result; 
    3551        } 
     
    3854    public function fetchAll(){ 
    3955        $result=array(); 
    40         while($res =  $this->_fetch ()){ 
     56        while($res =  $this->fetch ()){ 
    4157            $result[] = $res; 
    4258        } 
    4359        return $result; 
    4460    } 
    45  
    46         /** 
    47     * recupere un enregistrement et rempli les propri�s d'un objet existant avec 
    48     * les valeurs r�p�es. 
    49     * @param object/string  $object ou nom de la classe 
    50     * @return  boolean  indique si il y a eu des resultats ou pas. 
    51     */ 
    52         public function fetchInto ( $object){ 
    53  
    54       if ($result = $this->_fetch ()){ 
    55          if(is_string($object)){ 
    56             $object = new $object(); 
    57          } 
    58          foreach (get_object_vars ($result) as $k=>$value){ 
    59             $object->$k = $value; 
    60          } 
    61                         return $object; 
    62                 }else{ 
    63                         return false; 
    64                 } 
    65         } 
    6661 
    6762    public function getAttribute($attr){return null;} 
     
    8681    abstract protected function _free (); 
    8782    abstract protected function _fetch (); 
     83    abstract protected function _rewind (); 
     84 
     85    //--------------- interface Iterator 
     86    protected $_currentRecord = false; 
     87    protected $_recordIndex = 0; 
     88 
     89    public function current () { 
     90        return $this->_currentRecord; 
     91    } 
     92 
     93        public function key () { 
     94          return $this->_recordIndex; 
     95        } 
     96 
     97        public function next () { 
     98          $this->_currentRecord =  $this->fetch (); 
     99          if($this->_currentRecord) 
     100              $this->_recordIndex++; 
     101        } 
     102 
     103        public function rewind () { 
     104          $this->_rewind(); 
     105          $this->_recordIndex = 0; 
     106          $this->_currentRecord =  $this->fetch (); 
     107        } 
     108 
     109        public function valid () { 
     110          return ($this->_currentRecord != false); 
     111        } 
     112 
    88113 
    89114} 
  • trunk/testapp/modules/testapp/controllers/main.classic.php

    r132 r192  
    4848 
    4949   function testdao(){ 
    50     $dao = jDAO::get('testnews'); 
     50    $dao = jDao::get('testnews'); 
    5151 
    5252    if( $id=$this->param('newid')){ 
    53         $dao = jDAO::get('config'); 
    54         $rec = jDAO::createRecord('config'); 
     53        $dao = jDao::get('config'); 
     54        $rec = jDao::createRecord('config'); 
    5555 
    5656        $rec->ckey = $id; 
  • trunk/testapp/modules/testapp/templates/testzone.tpl

    r132 r192  
    1  
     1<h3>R�ltat d'un findAll</h3> 
    22<table> 
    33   <tr><th>key</th><th>value</th></tr> 
     
    66  {/foreach} 
    77</table> 
    8 <p>Count = {$nombre}</p> 
    9 <p>Count of values that contains "value" = {$nombrevalue}</p> 
     8<p>CountAll donne : {$nombre}</p> 
     9<p>getCountValue donne : {$nombrevalue} (nombre de valeur contenant le mot "value")</p> 
    1010 
    11 <p>Selection de deux enregistrements:</p> 
     11<h3>Utilisation d'un findBy</h3> 
     12<p>cherchant les cl�foo ou bar</p> 
    1213<table> 
    1314   <tr><th>key</th><th>value</th></tr> 
     
    1617  {/foreach} 
    1718</table> 
     19<h3>R�ltat d'un get('foo')</h3> 
     20<p>key={$oneconf->ckey} value={$oneconf->cvalue}</p> 
    1821 
    19 <p>one conf key={$oneconf->ckey} value={$oneconf->cvalue}</p> 
    20  
     22<h3>Test insertion</h3> 
    2123<form action="{jurl 'testapp~main_testdao'}" method="POST"> 
    2224<fieldset><legend>Ajouter une nouvelle cl�legend> 
  • trunk/testapp/modules/testapp/zones/test.zone.php

    r88 r192  
    1717    protected function _prepareTpl(){ 
    1818 
    19         $dao = jDAO::get('config'); 
     19        $dao = jDao::get('config'); 
    2020 
    2121        $this->_tpl->assign('config',$dao->findAll()); 
     
    2424        $this->_tpl->assign('nombrevalue',$dao->getCountValue()); 
    2525 
    26         $cond = new jDAOConditions('or'); 
     26        $cond = new jDaoConditions('or'); 
    2727        $cond->addCondition('ckey','=','foo'); 
    2828        $cond->addCondition('ckey','=','bar'); 
Download in other formats: Unified Diff Zip Archive