X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=www%2Fecrire%2Fiterateur%2Fsql.php;fp=www%2Fecrire%2Fiterateur%2Fsql.php;h=622f88b0e7a07d8b5468feb8c554b54cc56d8d8d;hb=4f443dce95ff6f8221c189880a70c74ce1c1f238;hp=2a1c37e1a34389e86491b9c351b187d96dfef591;hpb=4a628e9b277d3617535f99d663ca79fa2e891177;p=lhc%2Fweb%2Fwww.git diff --git a/www/ecrire/iterateur/sql.php b/www/ecrire/iterateur/sql.php index 2a1c37e1..622f88b0 100644 --- a/www/ecrire/iterateur/sql.php +++ b/www/ecrire/iterateur/sql.php @@ -3,29 +3,41 @@ /***************************************************************************\ * SPIP, Systeme de publication pour l'internet * * * - * Copyright (c) 2001-2016 * + * Copyright (c) 2001-2017 * * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James * * * * Ce programme est un logiciel libre distribue sous licence GNU/GPL. * * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. * \***************************************************************************/ -if (!defined('_ECRIRE_INC_VERSION')) return; +/** + * Gestion de l'itérateur SQL + * + * @package SPIP\Core\Iterateur\SQL + **/ + +if (!defined('_ECRIRE_INC_VERSION')) { + return; +} /** - * Iterateur SQL + * Itérateur SQL + * + * Permet d'itérer sur des données en base de données */ class IterateurSQL implements Iterator { /** * ressource sql + * * @var resource|bool */ protected $sqlresult = false; /** * row sql courante + * * @var array|null */ protected $row = null; @@ -33,27 +45,29 @@ class IterateurSQL implements Iterator { protected $firstseek = false; /** - * Erreur presente ? + * Erreur presente ? * * @var bool - **/ + **/ public $err = false; /** - * Calcul du total des elements + * Calcul du total des elements * * @var int|null - **/ + **/ public $total = null; /** * selectionner les donnees, ie faire la requete SQL + * * @return void */ protected function select() { $this->row = null; $v = &$this->command; - $this->sqlresult = calculer_select($v['select'], $v['from'], $v['type'], $v['where'], $v['join'], $v['groupby'], $v['orderby'], $v['limit'], $v['having'], $v['table'], $v['id'], $v['connect'], $this->info); + $this->sqlresult = calculer_select($v['select'], $v['from'], $v['type'], $v['where'], $v['join'], $v['groupby'], + $v['orderby'], $v['limit'], $v['having'], $v['table'], $v['id'], $v['connect'], $this->info); $this->err = !$this->sqlresult; $this->firstseek = false; $this->pos = -1; @@ -66,8 +80,8 @@ class IterateurSQL implements Iterator { * array command: les commandes d'initialisation * array info: les infos sur le squelette */ - public function __construct($command, $info=array()) { - $this->type='SQL'; + public function __construct($command, $info = array()) { + $this->type = 'SQL'; $this->command = $command; $this->info = $info; $this->select(); @@ -75,6 +89,7 @@ class IterateurSQL implements Iterator { /** * Rembobiner + * * @return bool */ public function rewind() { @@ -85,18 +100,23 @@ class IterateurSQL implements Iterator { /** * Verifier l'etat de l'iterateur + * * @return bool */ public function valid() { - if ($this->err) + if ($this->err) { return false; - if (!$this->firstseek) + } + if (!$this->firstseek) { $this->next(); + } + return is_array($this->row); } /** * Valeurs sur la position courante + * * @return array */ public function current() { @@ -106,19 +126,20 @@ class IterateurSQL implements Iterator { public function key() { return $this->pos; } - + /** * Sauter a une position absolue + * * @param int $n * @param null|string $continue * @return bool */ - public function seek($n=0, $continue=null) { + public function seek($n = 0, $continue = null) { if (!sql_seek($this->sqlresult, $n, $this->command['connect'], $continue)) { // SQLite ne sait pas seek(), il faut relancer la query // si la position courante est apres la position visee // il faut relancer la requete - if ($this->pos>$n){ + if ($this->pos > $n) { $this->free(); $this->select(); $this->valid(); @@ -128,46 +149,56 @@ class IterateurSQL implements Iterator { return false; } $this->row = sql_fetch($this->sqlresult, $this->command['connect']); - $this->pos = min($n,$this->count()); + $this->pos = min($n, $this->count()); + return true; } /** * Avancer d'un cran + * * @return void */ - public function next(){ + public function next() { $this->row = sql_fetch($this->sqlresult, $this->command['connect']); - $this->pos ++; + $this->pos++; $this->firstseek |= true; } /** * Avancer et retourner les donnees pour le nouvel element + * * @return array|bool|null */ - public function fetch(){ + public function fetch() { if ($this->valid()) { $r = $this->current(); $this->next(); - } else + } else { $r = false; + } + return $r; } /** * liberer les ressources + * * @return bool */ - public function free(){ - if (!$this->sqlresult) return true; + public function free() { + if (!$this->sqlresult) { + return true; + } $a = sql_free($this->sqlresult, $this->command['connect']); - $this->sqlresult = null; - return $a; + $this->sqlresult = null; + + return $a; } - + /** * Compter le nombre de resultats + * * @return int */ public function count() { @@ -180,12 +211,12 @@ class IterateurSQL implements Iterator { $this->valid(); $s = $this->current(); $this->total = $s['count(*)']; - } else + } else { $this->total = sql_count($this->sqlresult, $this->command['connect']); + } } } + return $this->total; } } - -?>