ORM: Edit avec condition 'IN'

This commit is contained in:
xdrm-brackets 2016-07-24 12:31:54 +02:00
parent 4b25bbe008
commit bc95bad2b3
3 changed files with 99 additions and 44 deletions

View File

@ -377,21 +377,18 @@
/* [1] Rédaction de la clause UPDATE
=========================================================*/
/* (1) On initialise la requête */
$requestS = "UPDATE ".$this->schema['table']."\n";
$requestS = SQLBuilder::UPDATE($this->schema['table'])."\n";
/* [2] Rédaction de la clause SET
=========================================================*/
/* (5) On met tout les champs à modifier */
$c = 0;
foreach($cleared as $field=>$value){
if( $c == 0 ) $requestS .= 'SET '.$field.' = :update_'.$field;
else $requestS .= "\n, ".$field.' = :update_'.$field;
$c++;
}
/* (0) On initialise les variables à "binder" */
$binded = [];
$requestS .= "\n";
/* (1) On met tout les champs à modifier */
$requestS .= SQLBuilder::SET($cleared, $binded)."\n";
@ -401,8 +398,11 @@
$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++;
}
@ -413,20 +413,26 @@
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++;
}
@ -434,28 +440,13 @@
}
/* [4] On exécute la requête et 'bind' les paramètres
/* [4] 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 bind les nouvelles valeurs */
foreach($cleared as $field=>$value)
$binded[':update_'.$field] = $value;
/* [6] On exécute la requête et retourne le résultat
/* [5] On exécute la requête et retourne le résultat
=========================================================*/
/* (1) On exécute la requête */
$updated = $request->execute($binded);

View File

@ -72,6 +72,30 @@
/* CONSTRUIT LA REQUETE TEXTUELLE "UPDATE" AVEC LA TABLE EN QUESTION
*
* @table<String> Table en question
*
* @return sql<String> Renvoie la clause remplie
*
*/
public static function UPDATE($table){
/* [0] Initialisation
=========================================================*/
$sql = 'UPDATE ';
/* [1] On construit la requête
=========================================================*/
$sql .= $table;
return $sql;
}
/* CONSTRUIT LA REQUETE TEXTUELLE "IN" AVEC UNE LISTE DE TABLES
*
* @field<Array> Tableau contenant [table, field]
@ -146,6 +170,43 @@
/* CONSTRUIT LA REQUETE TEXTUELLE "SET" AVEC UNE LISTE DE TABLES
*
* @values<Array> Tableau de la forme [ field=>value, field2=>value2 ]
* @binded<Arary> Tableau associatif contenant les variables "bindés" -> ajout des champs
*
* @return sql<String> Renvoie la clause remplie
*
*/
public static function SET($values, &$binded){
/* [0] Initialisation
=========================================================*/
$sql = 'SET ';
/* [1] On construit la requête
=========================================================*/
$c = 0;
foreach($values as $field=>$value){
/* (1) Champ */
if( $c > 0 ) $sql .= "\n, ";
$sql .= $field.' = ';
/* (2) Variable */
$sql .= ':update_'.$field;
$binded[':update_'.$field] = $value;
$c++;
}
return $sql;
}
}

View File

@ -372,18 +372,21 @@
//
//
// $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)
// ->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse'
// ->unique() // unique result
// 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
//
// // ->fetch();
// ->edit([
// 'id_warehouse' => Table::get('warehouse')->whereName(['stef-montauban', Rows::COND_EQUAL])->unique()->select('id_warehouse')->fetch()['id_warehouse']
// ]);
// ->edit([
// 'id_warehouse' => Table::get('warehouse')->whereName('stef-montauban')->unique()->select('id_warehouse')->fetch()['id_warehouse']
// ]);
// var_dump($myUser);
/* [3] INSERTION