Line 33... |
Line 33... |
33 |
protected $limite_requete = array( 'depart' => 0, 'limite' => 100);
|
33 |
protected $limite_requete = array( 'depart' => 0, 'limite' => 100);
|
34 |
/** Stocke le nombre total de résultats de la requete principale. Est calculée lors de l'assemblage de la requete */
|
34 |
/** Stocke le nombre total de résultats de la requete principale. Est calculée lors de l'assemblage de la requete */
|
35 |
protected $total_resultat;
|
35 |
protected $total_resultat;
|
36 |
protected $retour_format = 'max';
|
36 |
protected $retour_format = 'max';
|
Line -... |
Line 37... |
- |
|
37 |
|
- |
|
38 |
// beaucoup plus flexible dans le cas de requêtes SQL complexes
|
- |
|
39 |
protected $mesChamps = '';
|
- |
|
40 |
protected $mesJoinsEtConditions = '';
|
37 |
|
41 |
|
38 |
public function traiterParametres() {
|
42 |
public function traiterParametres() {
|
39 |
if (isset($this->parametres) && !empty($this->parametres)) {
|
43 |
if (isset($this->parametres) && !empty($this->parametres)) {
|
40 |
foreach ($this->parametres as $param => $valeur) {
|
44 |
foreach ($this->parametres as $param => $valeur) {
|
41 |
switch ($param) {
|
45 |
switch ($param) {
|
Line 110... |
Line 114... |
110 |
if (is_numeric($this->ressources[0])) {
|
114 |
if (is_numeric($this->ressources[0])) {
|
111 |
$this->requete_condition[] = ' id = '.$this->getBdd()->proteger($this->ressources[0]);
|
115 |
$this->requete_condition[] = ' id = '.$this->getBdd()->proteger($this->ressources[0]);
|
112 |
$this->format_reponse .= '/id';
|
116 |
$this->format_reponse .= '/id';
|
113 |
//requete : /ontologies/#classe:#code (ex : /ontologies/rangTaxo:290)
|
117 |
//requete : /ontologies/#classe:#code (ex : /ontologies/rangTaxo:290)
|
114 |
} elseif (strrpos($this->ressources[0], ':') !== false) {
|
118 |
} elseif (strrpos($this->ressources[0], ':') !== false) {
|
- |
|
119 |
// plusieurs couples #classe:#code séparés par des virgules
|
- |
|
120 |
if(strrpos($this->ressources[0], ',') !== false) {
|
- |
|
121 |
$this->traiterMultipleRessourceId();
|
- |
|
122 |
return;
|
- |
|
123 |
}
|
- |
|
124 |
|
- |
|
125 |
// ou un unique couple #classe:#code
|
115 |
$this->format_reponse .= '/id';
|
126 |
$this->format_reponse .= '/id';
|
116 |
preg_match('/^([^:]+):([^:]+)$/', $this->ressources[0], $match);
|
127 |
preg_match('/^([^:]+):([^:]+)$/', $this->ressources[0], $match);
|
117 |
$this->requete_condition[] =
|
128 |
$this->requete_condition[] =
|
118 |
' id IN (SELECT id FROM '.$this->table.' WHERE code = '.$this->getBdd()->proteger($match[2])
|
129 |
' id IN (SELECT id FROM '.$this->table.' WHERE code = '.$this->getBdd()->proteger($match[2])
|
119 |
.' AND classe_id = (SELECT id FROM '.$this->table.' WHERE code = '.$this->getBdd()->proteger($match[1]).'))';
|
130 |
.' AND classe_id = (SELECT id FROM '.$this->table.' WHERE code = '.$this->getBdd()->proteger($match[1]).'))';
|
Line 128... |
Line 139... |
128 |
.$this->ressources[0].' " n\'existe pas.';
|
139 |
.$this->ressources[0].' " n\'existe pas.';
|
129 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
|
140 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
|
130 |
}
|
141 |
}
|
131 |
}
|
142 |
}
|
Line -... |
Line 143... |
- |
|
143 |
|
- |
|
144 |
// Requète : /ontologies/#classe:#code,[...] (ex : /ontologies/numStatus:2,numStatus:3,genreNombre:10)
|
- |
|
145 |
public function traiterMultipleRessourceId() {
|
- |
|
146 |
$this->format_reponse .= '/ids'; // noter le "s"
|
- |
|
147 |
$this->mesChamps = Array(
|
- |
|
148 |
// 'a.*' // pourquoi pas, mais alors des unset() seront nécessaire
|
- |
|
149 |
'a.id',
|
- |
|
150 |
'a.classe_id AS `classe.id`',
|
- |
|
151 |
'a.nom',
|
- |
|
152 |
'a.description',
|
- |
|
153 |
'a.code',
|
- |
|
154 |
'a.complements',
|
- |
|
155 |
'c.nom AS classe', // évite très simplement (un très couteux) ajouterClasseCorrespondante()
|
- |
|
156 |
'concat(c.code,":", b.code) AS requete'); // permet aux appelants de récupérer la valeur qu'ils recherchent
|
- |
|
157 |
$this->mesChamps = implode(', ', $this->mesChamps);
|
- |
|
158 |
|
- |
|
159 |
$this->mesJoinsEtConditions =
|
- |
|
160 |
// alias de la table première
|
- |
|
161 |
" a "
|
- |
|
162 |
. " LEFT JOIN {$this->table} b ON a.id = b.id LEFT JOIN {$this->table} c ON b.classe_id = c.id"
|
- |
|
163 |
. " WHERE ";
|
- |
|
164 |
|
- |
|
165 |
$or_stack = false;
|
- |
|
166 |
$tab = explode(',', $this->ressources[0]);
|
- |
|
167 |
foreach($tab as $couple) {
|
- |
|
168 |
preg_match('/^([^:]+):([^:]+)$/', $couple, $match);
|
- |
|
169 |
if($or_stack) {
|
- |
|
170 |
// une fois qu'un set de condition et présent,
|
- |
|
171 |
// les autres sont `OR`-ed.
|
- |
|
172 |
$this->mesJoinsEtConditions .= " OR ";
|
- |
|
173 |
}
|
- |
|
174 |
$this->mesJoinsEtConditions .=
|
- |
|
175 |
sprintf("(b.code = %s AND c.code = %s)",
|
- |
|
176 |
$this->getBdd()->proteger($match[2]),
|
- |
|
177 |
$this->getBdd()->proteger($match[1]));
|
- |
|
178 |
$or_stack = true;
|
- |
|
179 |
}
|
Line 132... |
Line 180... |
132 |
|
180 |
}
|
133 |
|
181 |
|
134 |
public function traiterRessourceRelations() {
|
182 |
public function traiterRessourceRelations() {
|
135 |
//requete = /ontologies/#id/relations :
|
183 |
//requete = /ontologies/#id/relations :
|
Line 149... |
Line 197... |
149 |
.$this->formerRequeteLimite(); //print_r($requete);
|
197 |
.$this->formerRequeteLimite(); //print_r($requete);
|
150 |
return $requete;
|
198 |
return $requete;
|
151 |
}
|
199 |
}
|
Line 152... |
Line 200... |
152 |
|
200 |
|
- |
|
201 |
public function formerRequeteChamp() {
|
- |
|
202 |
if($this->mesChamps) return $this->mesChamps;
|
153 |
public function formerRequeteChamp() {
|
203 |
|
154 |
$champ[] = 'id';
|
204 |
$champ[] = 'id';
|
155 |
if ($this->format_reponse == 'ontologies') {
|
205 |
if ($this->format_reponse == 'ontologies') {
|
156 |
$champ[] = 'nom, code ';
|
206 |
$champ[] = 'nom, code ';
|
157 |
}
|
207 |
}
|
Line 192... |
Line 242... |
192 |
}
|
242 |
}
|
193 |
return $validite;
|
243 |
return $validite;
|
194 |
}
|
244 |
}
|
Line 195... |
Line 245... |
195 |
|
245 |
|
- |
|
246 |
public function retournerRequeteCondition() {
|
- |
|
247 |
if($this->mesJoinsEtConditions) return $this->mesJoinsEtConditions;
|
196 |
public function retournerRequeteCondition() {
|
248 |
|
197 |
$condition = '';
|
249 |
$condition = '';
|
198 |
if ($this->requete_condition !== null) {
|
250 |
if ($this->requete_condition !== null) {
|
199 |
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
|
251 |
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
|
200 |
}
|
252 |
}
|
Line 237... |
Line 289... |
237 |
// determine en fct du service appelé (/ontologies | /ontologies/#id | /ontologies/#id/champ |
|
289 |
// determine en fct du service appelé (/ontologies | /ontologies/#id | /ontologies/#id/champ |
|
238 |
// /ontologies/#id/relations) le format du tableau à retourner. Encode en json
|
290 |
// /ontologies/#id/relations) le format du tableau à retourner. Encode en json
|
239 |
switch ($this->format_reponse) {
|
291 |
switch ($this->format_reponse) {
|
240 |
case 'ontologies' : $reponse = $this->formaterOntologies($resultat); break;
|
292 |
case 'ontologies' : $reponse = $this->formaterOntologies($resultat); break;
|
241 |
case 'ontologies/id' : $reponse = $this->formaterOntologiesId($resultat[0]); break;
|
293 |
case 'ontologies/id' : $reponse = $this->formaterOntologiesId($resultat[0]); break;
|
- |
|
294 |
case 'ontologies/ids' : $reponse = $this->formaterMultipleOntologiesId($resultat); break;
|
242 |
case 'ontologies/id/relations' : $reponse = $this->formaterOntologiesIdRelations($resultat); break;
|
295 |
case 'ontologies/id/relations' : $reponse = $this->formaterOntologiesIdRelations($resultat); break;
|
243 |
default : break;
|
296 |
default : break;
|
244 |
}
|
297 |
}
|
245 |
return $reponse;
|
298 |
return $reponse;
|
246 |
}
|
299 |
}
|
Line 286... |
Line 339... |
286 |
$table_retour = array_filter($resultat, function($val) { return $val != ''; });
|
339 |
$table_retour = array_filter($resultat, function($val) { return $val != ''; });
|
287 |
$this->calculerClassID($table_retour);
|
340 |
$this->calculerClassID($table_retour);
|
288 |
return $table_retour;
|
341 |
return $table_retour;
|
289 |
}
|
342 |
}
|
Line -... |
Line 343... |
- |
|
343 |
|
- |
|
344 |
public function formaterMultipleOntologiesId($resultats) {
|
- |
|
345 |
$result = Array();
|
- |
|
346 |
foreach($resultats as $k => $resultat) {
|
- |
|
347 |
$id = $resultat['requete'];
|
- |
|
348 |
$result[$id] = array_filter($resultat, function($val) { return $val != ''; });
|
- |
|
349 |
unset($result[$id]['requete']);
|
- |
|
350 |
$this->calculerClassID($result[$id]);
|
- |
|
351 |
}
|
- |
|
352 |
return $result;
|
- |
|
353 |
}
|
290 |
|
354 |
|
291 |
public function calculerClassID(&$resultat) {
|
355 |
public function calculerClassID(&$resultat) {
|
292 |
// commenté: pourquoi restreindre le choix des champs au format "max",
|
356 |
// commenté: pourquoi restreindre le choix des champs au format "max",
|
293 |
// ça ne semble pas logique...
|
357 |
// ça ne semble pas logique...
|