ORM: Refactor de la méthode 'delete' forme différente de 'fetch' mais normalement même résultat
This commit is contained in:
parent
e6c81685fb
commit
c503bf02f3
|
@ -633,66 +633,71 @@
|
|||
*
|
||||
*/
|
||||
public function delete(){
|
||||
/* [1] Rédaction de la clause DELETE FROM
|
||||
/* [0] Initialisation des paramètres
|
||||
=========================================================*/
|
||||
/* (1) On initialise la requête */
|
||||
$requestS = "DELETE FROM ".$this->schema['table']."\n";
|
||||
$requestS = [];
|
||||
|
||||
/* (2) On initialise les paramètres */
|
||||
$binded = [];
|
||||
|
||||
|
||||
/* [1] Clause DELETE FROM
|
||||
=========================================================*/
|
||||
$requestS['DELETE'] = SQLBuilder::DELETE($this->schema['table']);
|
||||
|
||||
|
||||
/* [2] On rédige la clause WHERE/AND
|
||||
=========================================================*/
|
||||
/* (0) On initialise les variables à "binder" */
|
||||
$binded = [];
|
||||
|
||||
/* (1) On met les conditions locales */
|
||||
$c = 0;
|
||||
foreach($this->where as $field=>$conditions)
|
||||
foreach($conditions as $cdt=>$value){
|
||||
|
||||
if( $value[1] == self::COND_IN ) // Si condition de type IN
|
||||
$requestS .= SQLBuilder::IN([$this->schema['table'], $field], $value[0], $c, $binded)."\n";
|
||||
$requestS['WHERE'][$c] = SQLBuilder::IN([$this->schema['table'], $field], $value[0], $c, $binded);
|
||||
else // Sinon
|
||||
$requestS .= SQLBuilder::WHERE([$this->schema['table'], $field], $value, $c, $binded)."\n";
|
||||
$requestS['WHERE'][$c] = SQLBuilder::WHERE([$this->schema['table'], $field], $value, $c, $binded);
|
||||
|
||||
$c++;
|
||||
}
|
||||
|
||||
|
||||
/* (2) On ajoute les jointures */
|
||||
foreach($this->joined as $localField=>$rows){
|
||||
if( $c == 0 ) $requestS .= 'WHERE ';
|
||||
else $requestS .= 'AND ';
|
||||
// Note: On ajoute les requêtes des tables de jointures dans la clause WHERE //
|
||||
foreach($this->joined as $field=>$data){
|
||||
// {1} On récupère la requête/les params de chaque jointure //
|
||||
$joinedFetched = $data['object']->fetch(false);
|
||||
|
||||
// {1} On inclut la requête de la jointure //
|
||||
$requestS .= $this->schema['table'].'.'.$localField." in (\n\t";
|
||||
// {2} On met la clé étrangère pour la clause SELECT //
|
||||
$joinedFetched['request']['SELECT'] = [ $data['object']->schema['table'].'.'.$data['field'] ];
|
||||
|
||||
// {2} On récupère la requête //
|
||||
$RowsFetched = $rows[0]->fetch(false);
|
||||
// {3} On construit la nouvelle requête //
|
||||
$joinedRequest = SQLBuilder::BUILD($joinedFetched['request']);
|
||||
|
||||
// {3} On ajoute la requête //
|
||||
$requestS .= implode("\n\t", explode("\n", $RowsFetched['request']));
|
||||
// {4} On supprime les retours à la ligne //
|
||||
$joinedRequest = str_replace("\n", " ", $joinedRequest);
|
||||
|
||||
// {4} On bind les params de la requête //
|
||||
$binded = array_merge($binded, $RowsFetched['binded']);
|
||||
// {5} On l'ajoute à la clause FROM avec comme alias le nom de la table de @data['object'] //
|
||||
$requestS['WHERE'][] = $this->schema['table'].".$field in ($joinedRequest)";
|
||||
|
||||
// {5} On précise qu'on veut un seul résultat //
|
||||
$requestS .= ")\n";
|
||||
|
||||
$c++;
|
||||
// {6} On ajoute les variables à la requête courante //
|
||||
$binded = array_merge($binded, $joinedFetched['binded']);
|
||||
}
|
||||
|
||||
|
||||
/* [3] Clause LIMIT
|
||||
=========================================================*/
|
||||
if( $this->unique )
|
||||
$requestS .= "LIMIT 1";
|
||||
$requestS['LIMIT'] = ($this->unique) ? SQLBuilder::LIMIT(1) : SQLBuilder::LIMIT([]);
|
||||
|
||||
|
||||
/* [4] On prépare la requête
|
||||
/* [4] On prépare et compose la requête
|
||||
=========================================================*/
|
||||
/* (0) On prépare la requête */
|
||||
$request = Database::getPDO()->prepare($requestS.';');
|
||||
/* (1) On compose la requête */
|
||||
$requestString = SQLBuilder::BUILD($requestS).';';
|
||||
|
||||
/* (2) On prépare la requête */
|
||||
$request = Database::getPDO()->prepare($requestString);
|
||||
|
||||
/* [5] On exécute la requête et retourne le résultat
|
||||
=========================================================*/
|
||||
|
|
|
@ -63,15 +63,23 @@
|
|||
*
|
||||
*/
|
||||
public static function UPDATE($table){
|
||||
/* [0] Initialisation
|
||||
=========================================================*/
|
||||
$sql = [];
|
||||
return $table;
|
||||
}
|
||||
|
||||
/* [1] On construit la requête
|
||||
=========================================================*/
|
||||
$sql[] = $table;
|
||||
|
||||
return $sql;
|
||||
|
||||
|
||||
|
||||
|
||||
/* CONSTRUIT LA REQUETE FORMATTEE "DELETE" AVEC LA TABLE EN QUESTION
|
||||
*
|
||||
* @table<String> Table en question
|
||||
*
|
||||
* @return sql<Array> Renvoie un tableau formatté
|
||||
*
|
||||
*/
|
||||
public static function DELETE($table){
|
||||
return $table;
|
||||
}
|
||||
|
||||
|
||||
|
@ -234,45 +242,81 @@
|
|||
*
|
||||
*/
|
||||
public static function BUILD($request){
|
||||
/* [1] Clause SELECT
|
||||
/* [0] On initialise le retour
|
||||
=========================================================*/
|
||||
$sql = 'SELECT ';
|
||||
$sql = '';
|
||||
|
||||
/* [1] Gestion dans l'ordre
|
||||
=========================================================*/
|
||||
foreach($request as $clause=>$statements){
|
||||
|
||||
switch($clause){
|
||||
|
||||
/* (1) Clause SELECT
|
||||
---------------------------------------------------------*/
|
||||
case 'SELECT':
|
||||
$sql = 'SELECT ';
|
||||
|
||||
$c = 0;
|
||||
foreach($statements as $field){
|
||||
$sql .= ($c==0) ? $field : ", $field";
|
||||
$c++;
|
||||
}
|
||||
|
||||
$sql .= "\n";
|
||||
break;
|
||||
|
||||
/* (2) Clause FROM
|
||||
---------------------------------------------------------*/
|
||||
case 'FROM':
|
||||
$sql .= 'FROM ';
|
||||
|
||||
$c = 0;
|
||||
foreach($statements as $field){
|
||||
$sql .= ($c==0) ? $field : ", $field";
|
||||
$c++;
|
||||
}
|
||||
|
||||
$sql .= "\n";
|
||||
break;
|
||||
|
||||
|
||||
/* (3) Clause WHERE
|
||||
---------------------------------------------------------*/
|
||||
case 'WHERE':
|
||||
$c = 0;
|
||||
foreach($statements as $field){
|
||||
$sql .= ($c==0) ? "WHERE $field\n" : "AND $field\n";
|
||||
$c++;
|
||||
}
|
||||
|
||||
$sql .= ($c==0) ? '' : "\n";
|
||||
break;
|
||||
|
||||
|
||||
|
||||
/* (4) Clause LIMIT
|
||||
---------------------------------------------------------*/
|
||||
case 'LIMIT':
|
||||
if( is_numeric($statements) )
|
||||
$sql .= 'LIMIT '.intval($statements);
|
||||
break;
|
||||
|
||||
|
||||
/* (5) Clause DELETE
|
||||
---------------------------------------------------------*/
|
||||
case 'DELETE':
|
||||
$sql .= "DELETE FROM $statements\n";
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
$c = 0;
|
||||
foreach($request['SELECT'] as $field){
|
||||
$sql .= ($c==0) ? $field : ", $field";
|
||||
$c++;
|
||||
}
|
||||
|
||||
$sql .= "\n";
|
||||
|
||||
/* [2] Clause FROM
|
||||
=========================================================*/
|
||||
$sql .= 'FROM ';
|
||||
|
||||
$c = 0;
|
||||
foreach($request['FROM'] as $field){
|
||||
$sql .= ($c==0) ? $field : ", $field";
|
||||
$c++;
|
||||
}
|
||||
|
||||
$sql .= "\n";
|
||||
|
||||
/* [3] Clause WHERE
|
||||
=========================================================*/
|
||||
$c = 0;
|
||||
foreach($request['WHERE'] as $field){
|
||||
$sql .= ($c==0) ? "WHERE $field\n" : "AND $field\n";
|
||||
$c++;
|
||||
}
|
||||
|
||||
$sql .= ($c==0) ? '' : "\n";
|
||||
|
||||
|
||||
/* [4] Clause LIMIT
|
||||
=========================================================*/
|
||||
if( is_numeric($request['LIMIT']) )
|
||||
$sql .= 'LIMIT '.intval($request['LIMIT']);
|
||||
|
||||
|
||||
|
||||
|
||||
/* [5] On retourne le résultat
|
||||
|
|
|
@ -362,7 +362,7 @@
|
|||
// ->unique() // unique result
|
||||
|
||||
// SELECT
|
||||
->fetch(); // Result
|
||||
->delete(); // Result
|
||||
var_dump($myUser);
|
||||
|
||||
/* [2] Modification
|
||||
|
|
Loading…
Reference in New Issue