ORM: Refactor de la méthode 'delete' forme différente de 'fetch' mais normalement même résultat

This commit is contained in:
xdrm-brackets 2016-07-24 17:59:59 +02:00
parent e6c81685fb
commit c503bf02f3
3 changed files with 117 additions and 68 deletions

View File

@ -633,66 +633,71 @@
* *
*/ */
public function delete(){ public function delete(){
/* [1] Rédaction de la clause DELETE FROM /* [0] Initialisation des paramètres
=========================================================*/ =========================================================*/
/* (1) On initialise la requête */ /* (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 /* [2] On rédige la clause WHERE/AND
=========================================================*/ =========================================================*/
/* (0) On initialise les variables à "binder" */
$binded = [];
/* (1) On met les conditions locales */ /* (1) On met les conditions locales */
$c = 0; $c = 0;
foreach($this->where as $field=>$conditions) foreach($this->where as $field=>$conditions)
foreach($conditions as $cdt=>$value){ foreach($conditions as $cdt=>$value){
if( $value[1] == self::COND_IN ) // Si condition de type IN 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 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++; $c++;
} }
/* (2) On ajoute les jointures */ /* (2) On ajoute les jointures */
foreach($this->joined as $localField=>$rows){ // Note: On ajoute les requêtes des tables de jointures dans la clause WHERE //
if( $c == 0 ) $requestS .= 'WHERE '; foreach($this->joined as $field=>$data){
else $requestS .= 'AND '; // {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 // // {2} On met la clé étrangère pour la clause SELECT //
$requestS .= $this->schema['table'].'.'.$localField." in (\n\t"; $joinedFetched['request']['SELECT'] = [ $data['object']->schema['table'].'.'.$data['field'] ];
// {2} On récupère la requête // // {3} On construit la nouvelle requête //
$RowsFetched = $rows[0]->fetch(false); $joinedRequest = SQLBuilder::BUILD($joinedFetched['request']);
// {3} On ajoute la requête // // {4} On supprime les retours à la ligne //
$requestS .= implode("\n\t", explode("\n", $RowsFetched['request'])); $joinedRequest = str_replace("\n", " ", $joinedRequest);
// {4} On bind les params de la requête // // {5} On l'ajoute à la clause FROM avec comme alias le nom de la table de @data['object'] //
$binded = array_merge($binded, $RowsFetched['binded']); $requestS['WHERE'][] = $this->schema['table'].".$field in ($joinedRequest)";
// {5} On précise qu'on veut un seul résultat // // {6} On ajoute les variables à la requête courante //
$requestS .= ")\n"; $binded = array_merge($binded, $joinedFetched['binded']);
$c++;
} }
/* [3] Clause LIMIT /* [3] Clause LIMIT
=========================================================*/ =========================================================*/
if( $this->unique ) $requestS['LIMIT'] = ($this->unique) ? SQLBuilder::LIMIT(1) : SQLBuilder::LIMIT([]);
$requestS .= "LIMIT 1";
/* [4] On prépare la requête /* [4] On prépare et compose la requête
=========================================================*/ =========================================================*/
/* (0) On prépare la requête */ /* (1) On compose la requête */
$request = Database::getPDO()->prepare($requestS.';'); $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 /* [5] On exécute la requête et retourne le résultat
=========================================================*/ =========================================================*/

View File

@ -63,15 +63,23 @@
* *
*/ */
public static function UPDATE($table){ public static function UPDATE($table){
/* [0] Initialisation return $table;
=========================================================*/ }
$sql = [];
/* [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){ public static function BUILD($request){
/* [1] Clause SELECT /* [0] On initialise le retour
=========================================================*/ =========================================================*/
$sql = '';
/* [1] Gestion dans l'ordre
=========================================================*/
foreach($request as $clause=>$statements){
switch($clause){
/* (1) Clause SELECT
---------------------------------------------------------*/
case 'SELECT':
$sql = 'SELECT '; $sql = 'SELECT ';
$c = 0; $c = 0;
foreach($request['SELECT'] as $field){ foreach($statements as $field){
$sql .= ($c==0) ? $field : ", $field"; $sql .= ($c==0) ? $field : ", $field";
$c++; $c++;
} }
$sql .= "\n"; $sql .= "\n";
break;
/* [2] Clause FROM /* (2) Clause FROM
=========================================================*/ ---------------------------------------------------------*/
case 'FROM':
$sql .= 'FROM '; $sql .= 'FROM ';
$c = 0; $c = 0;
foreach($request['FROM'] as $field){ foreach($statements as $field){
$sql .= ($c==0) ? $field : ", $field"; $sql .= ($c==0) ? $field : ", $field";
$c++; $c++;
} }
$sql .= "\n"; $sql .= "\n";
break;
/* [3] Clause WHERE
=========================================================*/ /* (3) Clause WHERE
---------------------------------------------------------*/
case 'WHERE':
$c = 0; $c = 0;
foreach($request['WHERE'] as $field){ foreach($statements as $field){
$sql .= ($c==0) ? "WHERE $field\n" : "AND $field\n"; $sql .= ($c==0) ? "WHERE $field\n" : "AND $field\n";
$c++; $c++;
} }
$sql .= ($c==0) ? '' : "\n"; $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;
}
}
/* [4] Clause LIMIT
=========================================================*/
if( is_numeric($request['LIMIT']) )
$sql .= 'LIMIT '.intval($request['LIMIT']);
/* [5] On retourne le résultat /* [5] On retourne le résultat

View File

@ -362,7 +362,7 @@
// ->unique() // unique result // ->unique() // unique result
// SELECT // SELECT
->fetch(); // Result ->delete(); // Result
var_dump($myUser); var_dump($myUser);
/* [2] Modification /* [2] Modification