215,62 → 215,65 |
|
//+------------------------------------------------------------------------------------------------------+ |
public function assemblerLaRequete() { |
$requete = ' SELECT '.$this->formerRequeteChamp(). |
', CASE num_statut WHEN "" THEN 1 ELSE 0 END AS is_null '. |
' FROM '.$this->table |
.$this->formerRequeteCondition(). |
' ORDER BY is_null ASC, num_statut ASC ' |
.$this->formerRequeteLimite(); |
return $requete; |
$nolimit = in_array( |
$this->format_reponse, |
array($this->service.'/id', $this->service.'/id/champs')); |
if(!$nolimit) { |
$count = $this->recupererTotalResultat(); |
$limiteClause = self::formerRequeteLimite( // LIMIT |
$this->limite_requete['depart'], |
$count, |
$this->limite_requete['limite']); |
} |
|
public function formerRequeteChamp() { |
if (in_array('*', $this->requete_champ)) { |
$champ = ' * '; |
} else { |
$champ = implode(', ', $this->requete_champ); |
} |
return $champ; |
} |
return sprintf( |
'SELECT %s, IF(num_statut="",1,0) AS is_null' . |
' FROM %s WHERE %s ORDER BY is_null ASC, num_statut ASC %s -- %s:%d', |
|
public function formerRequeteCondition() { |
$condition = ''; |
if ($this->requete_condition != null) { |
$condition = ' WHERE '.implode(' AND ', $this->requete_condition); |
in_array('*', $this->requete_champ) ? ' * ' : implode(', ', $this->requete_champ), |
$this->table, |
$this->requete_condition ? implode(' AND ', $this->requete_condition) : 'TRUE', |
$nolimit ? '' : $limiteClause, |
__FILE__, __LINE__); |
} |
return $condition; |
} |
|
//ajout d'une limite seulement pour les listes (pas plus de 100 resultats retournés pr les requetes |
// suivantes : /noms-vernaculaires et /noms-vernaculaires/#id/relations) |
public function formerRequeteLimite() { |
if (in_array($this->format_reponse , array($this->service.'/id', $this->service.'/id/champs'))) { |
$this->requete_limite = ''; |
} elseif (($depart = $this->limite_requete['depart']) > ($this->total_resultat = $this->recupererTotalResultat())) { |
$this->limite_requete['depart'] = |
(($this->total_resultat - $this->limite_requete['limite']) < 0) ? 0 : ($this->total_resultat - $this->limite_requete['limite']); |
$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite']; |
} else { |
$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite']; |
static function formerRequeteLimite(&$depart, $total, $limite) { |
if ($depart > $total) { |
$depart = $total - $limite < 0 ? 0 : ($total - $limite); |
return ' LIMIT ' . $depart . ', ' . $limite; |
} |
return $this->requete_limite; |
return ' LIMIT ' . $depart . ', ' . $limite; |
} |
|
//on récupère le nombre total de résultats de la requete (ex : le nombre d'id contenu dans la liste /noms-vernaculaires) |
public function recupererTotalResultat() { |
$distinct = ($this->format_reponse == 'noms-vernaculaires/attributions') ? 'id' : 'distinct(id)'; |
$requete = 'SELECT count('.$distinct.') as nombre FROM ' |
.$this->table |
.$this->formerRequeteCondition(); |
$res = $this->getBdd()->recuperer($requete); |
$res = $this->getBdd()->recuperer(sprintf( |
'SELECT COUNT(%s) AS nombre FROM %s WHERE %s -- %s:%d', |
|
if ($res) { |
$total = $res['nombre']; |
} else { |
$t = 'Fonction recupererTotalResultat() : <br/>Données introuvables dans la base '.$requete; |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $t); |
$this->format_reponse == 'noms-vernaculaires/attributions' ? 'id' : 'distinct(id)', |
$this->table, |
$this->requete_condition ? implode(' AND ', $this->requete_condition) : 'TRUE', |
__FILE__, __LINE__)); |
|
if (! $res) |
throw new Exception('Données introuvables', RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE); |
if($res['nombre'] == 0) { |
print json_encode( |
array( |
"entete" => array( |
"depart" => $this->limite_requete['depart'], |
"limite" => $this->limite_requete['limite'], |
"masque" => $this->recupererMasque(), |
"total" => 0 |
), |
"resultat" => array() |
)); |
die; // die() très dommage (pour phpunit), mais la stack d'imbrication ne nous permet pas de retourner proprement |
} |
return $total; |
|
return $res['nombre']; |
} |
|
//+------------------------------------------------------------------------------------------------------+ |
333,10 → 336,10 |
public function hierarchiserResultat($resultat) { |
//tri recherche floue |
if (isset($this->parametres['masque.nv'])) { |
$resultat = $this->trierRechercheFloue($this->parametres['masque.nv'], $resultat, 'nom_vernaculaire'); |
return $this->trierRechercheFloue($this->parametres['masque.nv'], $resultat, 'nom_vernaculaire'); |
} |
if (isset($this->parametres['masque'])) { |
$resultat = $this->trierRechercheFloue($this->parametres['masque'], $resultat, 'nom_vernaculaire'); |
return $this->trierRechercheFloue($this->parametres['masque'], $resultat, 'nom_vernaculaire'); |
} |
return $resultat; |
} |
348,8 → 351,7 |
$tab_masque[] = $param.'='.$valeur; |
} |
} |
$masque = implode('&', $tab_masque); |
return $masque; |
return implode('&', $tab_masque); |
} |
|
public function formaterEnOss($resultat) { |
365,8 → 367,7 |
} |
if (isset($this->masque)) $masque = implode('&', $this->masque); |
else $masque = 'Pas de masque'; |
$table_retour_oss = array($masque, $oss); |
return $table_retour_oss; |
return array($masque, $oss); |
} |
|
public function formaterNomsVernaculairesAttributions($resultat) { |