Changeset 192
- Timestamp:
- 06/08/06 00:48:20 (3 years ago)
- Files:
-
- trunk/lib/jelix/dao/jDaoBase.class.php (modified) (5 diffs)
- trunk/lib/jelix/dao/jDaoGenerator.class.php (modified) (4 diffs)
- trunk/lib/jelix/db/drivers/mysql/jDbResultSet.mysql.class.php (modified) (1 diff)
- trunk/lib/jelix/db/drivers/postgresql/jDbResultSet.postgresql.class.php (modified) (2 diffs)
- trunk/lib/jelix/db/drivers/sqlite/jDbResultSet.sqlite.class.php (modified) (2 diffs)
- trunk/lib/jelix/db/jDbPDOConnection.class.php (modified) (2 diffs)
- trunk/lib/jelix/db/jDbResultSet.class.php (modified) (4 diffs)
- trunk/testapp/modules/testapp/controllers/main.classic.php (modified) (1 diff)
- trunk/testapp/modules/testapp/templates/testzone.tpl (modified) (3 diffs)
- trunk/testapp/modules/testapp/zones/test.zone.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/jelix/dao/jDaoBase.class.php
r174 r192 20 20 21 21 22 protected $_properties ;22 protected $_properties=array(); 23 23 24 24 public function getProperties(){ return $this->_properties; } … … 117 117 118 118 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; 121 122 } 122 123 123 124 public function countAll(){ 124 125 $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 (); 127 128 return $res->c; 128 129 } … … 139 140 } 140 141 141 $dbw = new jDbWidget ($this->_conn);142 142 $q = $this->_selectClause.$this->_fromClause.$this->_whereClause; 143 143 $q .= $this->_getPkWhereClauseForSelect($keys); 144 144 145 $record = $dbw->fetchFirstInto($q, $this->_DaoRecordClassName); 145 $rs = $this->_conn->query ($q); 146 $rs->setFetchMode(8,$this->_DaoRecordClassName); 147 $record = $rs->fetch (); 146 148 return $record; 147 149 } … … 174 176 $query .= $this->_createConditionsClause($searchcond); 175 177 } 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; 178 182 } 179 183 … … 188 192 $c = $this->_DaoRecordClassName; 189 193 $rec= new $c(); 190 $fields = &$rec->getProperties();194 $fields = $rec->getProperties(); 191 195 192 196 $sql = $this->_generateCondition ($daocond->condition, $fields, true); trunk/lib/jelix/dao/jDaoGenerator.class.php
r181 r192 4 4 * @subpackage dao 5 5 * @version $Id:$ 6 * @author Croes G� ald, Laurent Jouanneau6 * @author Croes G�ld, Laurent Jouanneau 7 7 * @contributor Laurent Jouanneau 8 8 * @copyright 2001-2005 CopixTeam, 2005-2006 Laurent Jouanneau … … 14 14 * il est sous Copyright 2001-2005 CopixTeam (licence LGPL) 15 15 * Auteurs initiaux : Gerald Croes et Laurent Jouanneau 16 * Adapt� et am�ior� pour Jelix par Laurent Jouanneau16 * Adapt�et am�or�our Jelix par Laurent Jouanneau 17 17 */ 18 18 19 19 /** 20 * G� �ateur d'une classe PHP correspondant �un objet DAO d�init dans une fichier xml21 * de d� inition20 * G�rateur d'une classe PHP correspondant �n objet DAO d�nit dans une fichier xml 21 * de d�nition 22 22 */ 23 23 … … 67 67 $src = array(); 68 68 $src[] = ' require_once ( JELIX_LIB_DAO_PATH .\'jDaoBase.class.php\');'; 69 $src[] = ' require_once ( JELIX_LIB_DB_PATH .\'jDbWidget.class.php\');';70 69 71 70 //----------------------- … … 318 317 break; 319 318 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();'; 322 321 $src[] = ' return $res->c;'; 323 322 break; 324 323 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();'; 327 327 break; 328 328 case 'select': 329 329 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;'; 332 336 } 333 337 $src[] = '}'; trunk/lib/jelix/db/drivers/mysql/jDbResultSet.mysql.class.php
r1 r192 29 29 return mysql_free_result ($this->_idResult); 30 30 } 31 protected function _rewind (){ 32 return mysql_data_seek ( $this->_idResult, 0); 33 } 31 34 32 35 public function rowCount(){ trunk/lib/jelix/db/drivers/postgresql/jDbResultSet.postgresql.class.php
r1 r192 25 25 $this->_cnt = $cnt; 26 26 } 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 } 33 34 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 } 38 42 39 43 public function bindColumn($column, &$param , $type=null ) … … 43 47 public function bindValue($parameter, $value, $data_type) 44 48 {throw new JException('jelix~db.error.feature.unsupported', array('pgsql','bindValue')); } 49 45 50 public function columnCount(){ 46 51 return pg_num_fields($this->_idResult); 47 } 52 } 53 48 54 public function execute($parameters=array()){ 49 55 $this->_idResult= pg_execute($this->_cnt,$this->_stmtId, $parameters); 50 56 return true; 51 57 } 52 53 54 58 } 55 59 ?> trunk/lib/jelix/db/drivers/sqlite/jDbResultSet.sqlite.class.php
r140 r192 5 5 * @version $Id:$ 6 6 * @author Loic Mathaud 7 * @contributor 7 * @contributor 8 8 * @copyright 2006 Loic Mathaud 9 9 * @link http://www.jelix.org … … 25 25 } 26 26 27 protected function _rewind (){ 28 return sqlite_rewind ( $this->_idResult ); 29 } 30 27 31 public function rowCount(){ 28 32 return sqlite_num_rows($this->_idResult); trunk/lib/jelix/db/jDbPDOConnection.class.php
r60 r192 16 16 define('JPDO_FETCH_OBJ',5); // PDO::FETCH_OBJ 17 17 define('JPDO_FETCH_ORI_NEXT',0); // PDO::FETCH_ORI_NEXT 18 define('JPDO_FETCH_ORI_FIRST',3); 18 19 define('JPDO_FETCH_CLASS',8); // PDO::FETCH_CLASS 19 20 define('JPDO_ATTR_STATEMENT_CLASS',13); //PDO::ATTR_STATEMENT_CLASS 20 21 define('JPDO_ATTR_AUTOCOMMIT',0); //PDO::ATTR_AUTOCOMMIT 22 define('JPDO_ATTR_CURSOR',10); // PDO::ATTR_CURSOR 23 define('JPDO_CURSOR_SCROLL',1); //PDO::CURSOR_SCROLL 21 24 22 class jDbPDOResultSet extends PDOStatement {25 class jDbPDOResultSet extends PDOStatement implements Iterator { 23 26 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; 27 28 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; 47 30 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 } 53 78 54 79 } … … 80 105 } 81 106 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 } 82 114 83 115 public function limitQuery ($queryString, $limitOffset = null, $limitCount = null){ trunk/lib/jelix/db/jDbResultSet.class.php
r4 r192 11 11 */ 12 12 13 abstract class jDbResultSet { 13 abstract class jDbResultSet implements Iterator { 14 15 const FETCH_CLASS = 8; 14 16 15 17 protected $_idResult=null; 18 protected $_fetchMode = 0; 19 protected $_fetchModeParam = ''; 16 20 17 21 function __construct ( $idResult){ … … 26 30 } 27 31 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 */ 32 40 public function fetch(){ 33 41 $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 } 34 50 return $result; 35 51 } … … 38 54 public function fetchAll(){ 39 55 $result=array(); 40 while($res = $this-> _fetch ()){56 while($res = $this->fetch ()){ 41 57 $result[] = $res; 42 58 } 43 59 return $result; 44 60 } 45 46 /**47 * recupere un enregistrement et rempli les propri�s d'un objet existant avec48 * les valeurs r�p�es.49 * @param object/string $object ou nom de la classe50 * @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 }66 61 67 62 public function getAttribute($attr){return null;} … … 86 81 abstract protected function _free (); 87 82 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 88 113 89 114 } trunk/testapp/modules/testapp/controllers/main.classic.php
r132 r192 48 48 49 49 function testdao(){ 50 $dao = jD AO::get('testnews');50 $dao = jDao::get('testnews'); 51 51 52 52 if( $id=$this->param('newid')){ 53 $dao = jD AO::get('config');54 $rec = jD AO::createRecord('config');53 $dao = jDao::get('config'); 54 $rec = jDao::createRecord('config'); 55 55 56 56 $rec->ckey = $id; trunk/testapp/modules/testapp/templates/testzone.tpl
r132 r192 1 1 <h3>R�ltat d'un findAll</h3> 2 2 <table> 3 3 <tr><th>key</th><th>value</th></tr> … … 6 6 {/foreach} 7 7 </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> 10 10 11 <p>Selection de deux enregistrements:</p> 11 <h3>Utilisation d'un findBy</h3> 12 <p>cherchant les cl�foo ou bar</p> 12 13 <table> 13 14 <tr><th>key</th><th>value</th></tr> … … 16 17 {/foreach} 17 18 </table> 19 <h3>R�ltat d'un get('foo')</h3> 20 <p>key={$oneconf->ckey} value={$oneconf->cvalue}</p> 18 21 19 <p>one conf key={$oneconf->ckey} value={$oneconf->cvalue}</p> 20 22 <h3>Test insertion</h3> 21 23 <form action="{jurl 'testapp~main_testdao'}" method="POST"> 22 24 <fieldset><legend>Ajouter une nouvelle cl�legend> trunk/testapp/modules/testapp/zones/test.zone.php
r88 r192 17 17 protected function _prepareTpl(){ 18 18 19 $dao = jD AO::get('config');19 $dao = jDao::get('config'); 20 20 21 21 $this->_tpl->assign('config',$dao->findAll()); … … 24 24 $this->_tpl->assign('nombrevalue',$dao->getCountValue()); 25 25 26 $cond = new jD AOConditions('or');26 $cond = new jDaoConditions('or'); 27 27 $cond->addCondition('ckey','=','foo'); 28 28 $cond->addCondition('ckey','=','bar');
