2016-07-24 10:00:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace manager\ORM;
|
|
|
|
|
|
|
|
use \manager\Database;
|
|
|
|
use \manager\ORM\Rows;
|
|
|
|
|
|
|
|
|
|
|
|
class SQLBuilder{
|
|
|
|
|
|
|
|
|
2016-07-24 15:42:54 +00:00
|
|
|
/* CONSTRUIT LA REQUETE FORMATTEE "SELECT" AVEC UNE LISTE DE CHAMPS
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
2016-07-24 15:42:54 +00:00
|
|
|
* @sqlFields<Array> Liste de champ/tables contenant des champs
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
2016-07-24 15:42:54 +00:00
|
|
|
* @return sql<Array> Renvoie un tableau formatté
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-07-24 15:42:54 +00:00
|
|
|
public static function SELECT($sqlFields){
|
2016-07-24 10:00:49 +00:00
|
|
|
/* [0] Initialisation
|
|
|
|
=========================================================*/
|
2016-07-24 15:42:54 +00:00
|
|
|
$sql = [];
|
2016-07-24 10:00:49 +00:00
|
|
|
|
|
|
|
/* [1] On construit la requête
|
|
|
|
=========================================================*/
|
|
|
|
$c = 0;
|
2016-07-24 15:42:54 +00:00
|
|
|
foreach($sqlFields as $table=>$fields)
|
2016-07-24 10:00:49 +00:00
|
|
|
foreach($fields as $field){
|
2016-07-24 15:42:54 +00:00
|
|
|
$sql[$c] = $table.'.'.$field;
|
2016-07-24 10:00:49 +00:00
|
|
|
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-24 15:42:54 +00:00
|
|
|
/* CONSTRUIT LA REQUETE FORMATTEE "FROM" AVEC UNE LISTE DE TABLES
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
2016-07-24 15:42:54 +00:00
|
|
|
* @tables<Array> Liste de tables OU SQL PUR
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
2016-07-24 15:42:54 +00:00
|
|
|
* @return sql<Array> Renvoie un tableau formatté
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function FROM($tables){
|
2016-07-24 15:42:54 +00:00
|
|
|
return $tables;
|
2016-07-24 10:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-24 15:42:54 +00:00
|
|
|
/* CONSTRUIT LA REQUETE FORMATTEE "UPDATE" AVEC LA TABLE EN QUESTION
|
2016-07-24 10:31:54 +00:00
|
|
|
*
|
|
|
|
* @table<String> Table en question
|
|
|
|
*
|
2016-07-24 15:42:54 +00:00
|
|
|
* @return sql<Array> Renvoie un tableau formatté
|
2016-07-24 10:31:54 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function UPDATE($table){
|
|
|
|
/* [0] Initialisation
|
|
|
|
=========================================================*/
|
2016-07-24 15:42:54 +00:00
|
|
|
$sql = [];
|
2016-07-24 10:31:54 +00:00
|
|
|
|
|
|
|
/* [1] On construit la requête
|
|
|
|
=========================================================*/
|
2016-07-24 15:42:54 +00:00
|
|
|
$sql[] = $table;
|
2016-07-24 10:31:54 +00:00
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-24 10:00:49 +00:00
|
|
|
/* CONSTRUIT LA REQUETE TEXTUELLE "IN" AVEC UNE LISTE DE TABLES
|
|
|
|
*
|
|
|
|
* @field<Array> Tableau contenant [table, field]
|
|
|
|
* @array<Array> Valeurs de la clause IN
|
|
|
|
* @offset<int> Permet de rendre la condition unique (nommage des variables)
|
|
|
|
* @binded<Arary> Tableau associatif contenant les variables "bindés" -> ajout des champs
|
|
|
|
*
|
2016-07-24 15:42:54 +00:00
|
|
|
* @return sql<String> Renvoie le textuel formatté
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function IN($field, $array, $offset=0, &$binded){
|
|
|
|
/* [0] Initialisation
|
|
|
|
=========================================================*/
|
2016-07-24 15:42:54 +00:00
|
|
|
$sql = '';
|
2016-07-24 10:00:49 +00:00
|
|
|
|
|
|
|
/* [1] On construit la requête
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Champ */
|
|
|
|
$sql .= $field[0].'.'.$field[1].' IN (';
|
|
|
|
|
|
|
|
/* (2) Valeurs */
|
|
|
|
$c = 0;
|
|
|
|
foreach($array as $i=>$value){
|
|
|
|
if( $c > 0 ) $sql .= ', ';
|
|
|
|
|
|
|
|
$sql .= ':'.$field[0].'_x_'.$field[1].'_'.$offset.'_'.$i;
|
|
|
|
|
|
|
|
$binded[':'.$field[0].'_x_'.$field[1].'_'.$offset.'_'.$i] = $value;
|
|
|
|
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql.")";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* CONSTRUIT LA REQUETE TEXTUELLE "WHERE" AVEC UNE LISTE DE TABLES
|
|
|
|
*
|
|
|
|
* @field<Array> Tableau contenant [table, field]
|
|
|
|
* @valeur<Array> Valeurs de la clause WHERE [valeur, opérateur]
|
|
|
|
* @offset<int> Permet de rendre la condition unique (nommage des variables)
|
|
|
|
* @binded<Arary> Tableau associatif contenant les variables "bindés" -> ajout des champs
|
|
|
|
*
|
2016-07-24 15:42:54 +00:00
|
|
|
* @return sql<String> Renvoie le textuel formatté
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function WHERE($field, $value, $offset=0, &$binded){
|
|
|
|
/* [0] Initialisation
|
|
|
|
=========================================================*/
|
2016-07-24 15:42:54 +00:00
|
|
|
$sql = '';
|
2016-07-24 10:00:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [1] On construit la requête
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Chamo */
|
|
|
|
$sql .= $field[0].'.'.$field[1].' ';
|
|
|
|
|
|
|
|
/* (2) Opérateur */
|
|
|
|
$sql .= substr($value[1], 2, -2).' ';
|
|
|
|
|
|
|
|
/* (3) Variable */
|
|
|
|
$sql .= ':'.$field[0].'_x_'.$field[1].'_'.$offset;
|
|
|
|
|
|
|
|
$binded[':'.$field[0].'_x_'.$field[1].'_'.$offset] = $value[0];
|
|
|
|
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-24 10:31:54 +00:00
|
|
|
|
|
|
|
|
2016-07-24 15:42:54 +00:00
|
|
|
/* CONSTRUIT LA REQUETE FORMATTEE "SET" AVEC UNE LISTE DE TABLES
|
2016-07-24 10:31:54 +00:00
|
|
|
*
|
|
|
|
* @values<Array> Tableau de la forme [ field=>value, field2=>value2 ]
|
|
|
|
* @binded<Arary> Tableau associatif contenant les variables "bindés" -> ajout des champs
|
|
|
|
*
|
2016-07-24 15:42:54 +00:00
|
|
|
* @return sql<Array> Renvoie un tableau formatté
|
2016-07-24 10:31:54 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function SET($values, &$binded){
|
|
|
|
/* [0] Initialisation
|
|
|
|
=========================================================*/
|
2016-07-24 15:42:54 +00:00
|
|
|
$sql = [];
|
2016-07-24 10:31:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [1] On construit la requête
|
|
|
|
=========================================================*/
|
|
|
|
$c = 0;
|
|
|
|
foreach($values as $field=>$value){
|
|
|
|
/* (1) Champ */
|
2016-07-24 15:42:54 +00:00
|
|
|
$sql[$c] = ($c>0) ? "\n, " : '';
|
|
|
|
|
|
|
|
$sql[$c] .= $field.' = ';
|
2016-07-24 10:31:54 +00:00
|
|
|
|
|
|
|
/* (2) Variable */
|
2016-07-24 15:42:54 +00:00
|
|
|
$sql[$c] .= ':update_'.$field;
|
2016-07-24 10:31:54 +00:00
|
|
|
|
|
|
|
$binded[':update_'.$field] = $value;
|
|
|
|
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-24 15:42:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* CONSTRUIT LA REQUETE FORMATTEE "LIMIT" AVEC UN NOMBRE D'ENTREES
|
|
|
|
*
|
|
|
|
* @count<int> Nombre limite
|
|
|
|
*
|
|
|
|
* @return sql<Array> Renvoie un sql formatté
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function LIMIT($count=null){
|
|
|
|
/* [0] Initialisation
|
|
|
|
=========================================================*/
|
|
|
|
$sql = [];
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] On construit la requête
|
|
|
|
=========================================================*/
|
|
|
|
if( intval($count) == $count )
|
|
|
|
$sql = intval($count);
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* CONSTRUIT LA REQUETE A PARTIR D'UNE REQUETTE FORMATTEE
|
|
|
|
*
|
|
|
|
* @request<Arary> Requête formattée
|
|
|
|
*
|
|
|
|
* @return sql<String> Requête formattée en SQL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function BUILD($request){
|
|
|
|
/* [1] Clause SELECT
|
|
|
|
=========================================================*/
|
|
|
|
$sql = 'SELECT ';
|
|
|
|
|
|
|
|
$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
|
|
|
|
=========================================================*/
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-24 10:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|