From 0fe810a2d0cc8739d56d58c04056a29bdd829450 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 24 Jul 2016 17:59:59 +0200 Subject: [PATCH] =?UTF-8?q?ORM:=20Refactor=20de=20la=20m=C3=A9thode=20'del?= =?UTF-8?q?ete'=20forme=20diff=C3=A9rente=20de=20'fetch'=20mais=20normalem?= =?UTF-8?q?ent=20m=C3=AAme=20r=C3=A9sultat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manager/ORM/Rows.php | 59 ++++++++++-------- manager/ORM/SQLBuilder.php | 124 +++++++++++++++++++++++++------------ test/automate.php | 2 +- 3 files changed, 117 insertions(+), 68 deletions(-) diff --git a/manager/ORM/Rows.php b/manager/ORM/Rows.php index 48d96f1..254c8e9 100644 --- a/manager/ORM/Rows.php +++ b/manager/ORM/Rows.php @@ -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 =========================================================*/ diff --git a/manager/ORM/SQLBuilder.php b/manager/ORM/SQLBuilder.php index bb4343c..c3ffbae 100644 --- a/manager/ORM/SQLBuilder.php +++ b/manager/ORM/SQLBuilder.php @@ -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 Table en question + * + * @return sql 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 diff --git a/test/automate.php b/test/automate.php index 959e287..270a35a 100755 --- a/test/automate.php +++ b/test/automate.php @@ -362,7 +362,7 @@ // ->unique() // unique result // SELECT - ->fetch(); // Result + ->delete(); // Result var_dump($myUser); /* [2] Modification