From d0584130b59a070439d28e64c5ebb51ff73bf294 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 24 Jul 2016 12:18:30 +0200 Subject: [PATCH] ORM: Delete avec condition 'IN' --- manager/ORM/Rows.php | 42 +++++++++++++++++----------------- manager/repo/chip.php | 12 ++++++++-- test/automate.php | 52 +++++++++++++++++++++++++++---------------- 3 files changed, 64 insertions(+), 42 deletions(-) diff --git a/manager/ORM/Rows.php b/manager/ORM/Rows.php index 9cb658b..a32fc53 100644 --- a/manager/ORM/Rows.php +++ b/manager/ORM/Rows.php @@ -611,59 +611,59 @@ /* [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( $c == 0 ) $requestS .= 'WHERE '.$this->schema['table'].'.'.$field.' '.substr($value[1], 2, -2).' :'.$this->schema['table'].'_x_'.$field.'_'.$cdt."\n"; - else $requestS .= 'AND '.$this->schema['table'].'.'.$field.' '.substr($value[1], 2, -2).' :'.$this->schema['table'].'_x_'.$field.'_'.$cdt."\n"; + + if( $value[1] == self::COND_IN ) // Si condition de type IN + $requestS .= SQLBuilder::IN([$this->schema['table'], $field], $value[0], $c, $binded)."\n"; + else // Sinon + $requestS .= SQLBuilder::WHERE([$this->schema['table'], $field], $value, $c, $binded)."\n"; $c++; } + /* (2) On ajoute les jointures */ foreach($this->joined as $localField=>$rows){ if( $c == 0 ) $requestS .= 'WHERE '; else $requestS .= 'AND '; // {1} Clause SELECT interne // - $requestS .= $this->schema['table'].'.'.$localField." = (\n\tSELECT "; + $requestS .= $this->schema['table'].'.'.$localField." = (\n\t"; // Jointure du SELECT, champ joint lié au champ local - $requestS .= $rows->schema['table'].'.'.$this->schema['columns'][$localField]['references'][1]."\n"; + $requestS .= SQLBuilder::SELECT([ + $rows->schema['table'] => [ $this->schema['columns'][$localField]['references'][1] ] + ])."\n"; + // {2} Clause FROM interne // - $requestS .= "\tFROM ".$this->schema['columns'][$localField]['references'][0]."\n"; + $requestS .= "\t".SQLBuilder::FROM([$this->schema['columns'][$localField]['references'][0]])."\n"; // {3} Clause WHERE/AND interne // $c2 = 0; foreach($rows->where as $field=>$conditions) foreach($conditions as $cdt=>$value){ - if( $c2 == 0 ) $requestS .= "\tWHERE ".$rows->schema['table'].'.'.$field.' '.substr($value[1], 2, -2).' :'.$rows->schema['table'].'_x_'.$field.'_'.$cdt."\n"; - else $requestS .= "\tAND ".$rows->schema['table'].'.'.$field.' '.substr($value[1], 2, -2).' :'.$rows->schema['table'].'_x_'.$field.'_'.$cdt."\n"; + if( $value[1] == self::COND_IN ) // Si condition de type IN + $requestS .= "\t".SQLBuilder::IN([$rows->schema['table'], $field], $value[0], $c2, $binded)."\n"; + else // Sinon + $requestS .= "\t".SQLBuilder::WHERE([$rows->schema['table'], $field], $value, $c2, $binded)."\n"; + $c2++; } $requestS .= "\tLIMIT 1)\n"; } - - /* [3] On exécute la requête et 'bind' les paramètres + /* [3] On prépare la requête =========================================================*/ /* (0) On prépare la requête */ $request = Database::getPDO()->prepare($requestS.';'); - $binded = []; - - /* (1) On bind tous les paramètres locaux */ - foreach($this->where as $field=>$conditions) - foreach($conditions as $cdt=>$value) - $binded[':'.$this->schema['table'].'_x_'.$field.'_'.$cdt] = $value[0]; - - /* (3) On bind tous les paramètres des jointures */ - foreach($this->joined as $rows) - foreach($rows->where as $field=>$conditions) - foreach($conditions as $cdt=>$value) - $binded[':'.$rows->schema['table'].'_x_'.$field.'_'.$cdt] = $value[0]; /* [4] On exécute la requête et retourne le résultat diff --git a/manager/repo/chip.php b/manager/repo/chip.php index a2434e2..8d86e61 100644 --- a/manager/repo/chip.php +++ b/manager/repo/chip.php @@ -20,8 +20,16 @@ public static function getAll($id_warehouse){ /* [1] On récupère les modules de l'entrepot courant =========================================================*/ - $module = Table::get('module'); - // ->where + $module = Table::get('module') + ->whereName([ $_SESSION['WAREHOUSE']['modules'], Rows::COND_IN ]); + + $chip = Table::get('chip') + ->select('*'); + + $module_merge = Table::get('module_merge') + ->join('id_module', $module); + + var_dump($module_merge->fetch()); /* [2] On retourne le résultat diff --git a/test/automate.php b/test/automate.php index 020991a..ecaefbe 100755 --- a/test/automate.php +++ b/test/automate.php @@ -345,24 +345,24 @@ /* [1] FETCH =========================================================*/ - $warehouse = - Table::get('warehouse') // Access to table 'warehouse' - ->whereName(['stef-montauban', Rows::COND_EQUAL]); // condition : name = 'my-warehouse' - - - $myUser = - Table::get('user') // Access to table 'user' - ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field) - ->whereId([[1,4,6,9], Rows::COND_IN]) // PRIMARY KEY (other condition on same field) - ->whereUsername(['%e%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' - ->select(['mail', 'username', 'firstname']) // Select clause - ->select('id_user') // Select clause (added) - ->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse' - // ->unique() // unique result - - // SELECT - ->fetch(); // Result - var_dump($myUser); + // $warehouse = + // Table::get('warehouse') // Access to table 'warehouse' + // ->whereName(['stef-montauban', Rows::COND_EQUAL]); // condition : name = 'my-warehouse' + // + // + // $myUser = + // Table::get('user') // Access to table 'user' + // ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field) + // ->whereId([[1,4,6,9], Rows::COND_IN]) // PRIMARY KEY (other condition on same field) + // ->whereUsername(['%e%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' + // ->select(['mail', 'username', 'firstname']) // Select clause + // ->select('id_user') // Select clause (added) + // ->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse' + // // ->unique() // unique result + // + // // SELECT + // ->fetch(); // Result + // var_dump($myUser); /* [2] Modification =========================================================*/ @@ -374,6 +374,7 @@ // $myUser = // Table::get('user') // Access to table 'user' // ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field) + // ->whereId([[1,4,6,9], Rows::COND_IN]) // PRIMARY KEY (other condition on same field) // ->whereUsername(['jo%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' // ->select(['mail', 'username', 'firstname']) // Select clause // ->select('id_user') // Select clause (added) @@ -420,13 +421,26 @@ // $myUser = // Table::get('user') // Access to table 'user' // ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field) - // ->whereUsername(['no%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' + // ->whereId([[1,4,6,9], Rows::COND_IN]) // PRIMARY KEY (other condition on same field) + // ->whereUsername(['n%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' // ->select(['mail', 'username', 'firstname']) // Select clause // ->select('id_user') // Select clause (added) // ->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse' // // ->unique() // unique result // // ->delete(); + // + // $module = Table::get('module') + // ->whereName([ $_SESSION['WAREHOUSE']['modules'], Rows::COND_IN ]); + // + // $chip = Table::get('chip') + // ->select('*'); + // + // $module_merge = Table::get('module_merge') + // ->join('id_module', $module) + // ->join('id_chip', $chip); + // + // var_dump($module_merge->fetch()); ?>