389 lines
8.2 KiB
PHP
389 lines
8.2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace manager\ORM;
|
||
|
|
||
|
use \manager\Database;
|
||
|
use \manager\ORM\Rows;
|
||
|
|
||
|
|
||
|
class SQLBuilder{
|
||
|
|
||
|
|
||
|
/* CONSTRUIT LA REQUETE FORMATTEE "SELECT" AVEC UNE LISTE DE CHAMPS
|
||
|
*
|
||
|
* @sqlFields<Array> Liste de champs : [table => field => [func, alias] ]
|
||
|
*
|
||
|
* @return sql<Array> Renvoie un tableau formatté
|
||
|
*
|
||
|
*/
|
||
|
public static function SELECT($sqlFields){
|
||
|
return $sqlFields;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* CONSTRUIT LA REQUETE FORMATTEE "GROUP BY" AVEC UNE LISTE DE CHAMPS
|
||
|
*
|
||
|
* @tables<Array> Liste de champs : [table => fields]
|
||
|
*
|
||
|
* @return sql<Array> Renvoie un tableau formatté
|
||
|
*
|
||
|
*/
|
||
|
public static function GROUPBY($tables){
|
||
|
return $tables;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* CONSTRUIT LA REQUETE FORMATTEE "FROM" AVEC UNE LISTE DE TABLES
|
||
|
*
|
||
|
* @tables<Array> Liste de tables OU SQL PUR
|
||
|
*
|
||
|
* @return sql<Array> Renvoie un tableau formatté
|
||
|
*
|
||
|
*/
|
||
|
public static function FROM($tables){
|
||
|
return $tables;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* CONSTRUIT LA REQUETE FORMATTEE "UPDATE" AVEC LA TABLE EN QUESTION
|
||
|
*
|
||
|
* @table<String> Table en question
|
||
|
*
|
||
|
* @return sql<Array> Renvoie un tableau formatté
|
||
|
*
|
||
|
*/
|
||
|
public static function UPDATE($table){
|
||
|
return $table;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* 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;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* 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)
|
||
|
* @bound<Arary> Tableau associatif contenant les variables "bindés" -> ajout des champs
|
||
|
*
|
||
|
* @return sql<String> Renvoie le textuel formatté
|
||
|
*
|
||
|
*/
|
||
|
public static function IN($field, $array, $offset=0, &$bound){
|
||
|
/* [0] Initialisation
|
||
|
=========================================================*/
|
||
|
$sql = '';
|
||
|
|
||
|
/* [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;
|
||
|
|
||
|
$bound[':'.$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)
|
||
|
* @bound<Arary> Tableau associatif contenant les variables "bindés" -> ajout des champs
|
||
|
*
|
||
|
* @return sql<String> Renvoie le textuel formatté
|
||
|
*
|
||
|
*/
|
||
|
public static function WHERE($field, $value, $offset=0, &$bound){
|
||
|
/* [0] Initialisation
|
||
|
=========================================================*/
|
||
|
$sql = '';
|
||
|
|
||
|
|
||
|
/* [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;
|
||
|
|
||
|
$bound[':'.$field[0].'_x_'.$field[1].'_'.$offset] = $value[0];
|
||
|
|
||
|
|
||
|
return $sql;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* CONSTRUIT LA REQUETE FORMATTEE "SET" AVEC UNE LISTE DE TABLES
|
||
|
*
|
||
|
* @values<Array> Tableau de la forme [ field=>value, field2=>value2 ]
|
||
|
* @bound<Arary> Tableau associatif contenant les variables "bindés" -> ajout des champs
|
||
|
*
|
||
|
* @return sql<Array> Renvoie un tableau formatté
|
||
|
*
|
||
|
*/
|
||
|
public static function SET($values, &$bound){
|
||
|
/* [0] Initialisation
|
||
|
=========================================================*/
|
||
|
$sql = [];
|
||
|
|
||
|
|
||
|
/* [1] On construit la requête
|
||
|
=========================================================*/
|
||
|
$c = 0;
|
||
|
foreach($values as $field=>$value){
|
||
|
/* (1) Champ */
|
||
|
$sql[$c] = $field.' = ';
|
||
|
|
||
|
/* (2) Variable */
|
||
|
$sql[$c] .= ':update_'.$field;
|
||
|
|
||
|
$bound[':update_'.$field] = $value;
|
||
|
|
||
|
$c++;
|
||
|
}
|
||
|
|
||
|
return $sql;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* 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){
|
||
|
/* [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 ";
|
||
|
$c = 0;
|
||
|
foreach($statements as $table=>$fields)
|
||
|
foreach($fields as $field=>$select){
|
||
|
|
||
|
/* (1) On construit le nom du champ */
|
||
|
$fieldStr = "$table.$field";
|
||
|
|
||
|
/* (2) On ajout le DISTINCT s'il y a lieu */
|
||
|
if( isset($select[1]) && $select[1] )
|
||
|
$fieldStr = "DISTINCT $fieldStr";
|
||
|
|
||
|
/* (3) On ajoute la fonction d'aggrégation s'il y a lieu */
|
||
|
if( isset($select[0]) && !is_null($select[0]) )
|
||
|
$fieldStr = substr($select[0], 2, -2)."($fieldStr)";
|
||
|
|
||
|
|
||
|
/* (4) On ajoute l'alias */
|
||
|
if( isset($select[0]) && !is_null($select[0]) )
|
||
|
$fieldStr = "$fieldStr as agg_$field";
|
||
|
else
|
||
|
$fieldStr = "$fieldStr";
|
||
|
|
||
|
$sql .= ($c==0) ? "$fieldStr" : ", $fieldStr";
|
||
|
|
||
|
$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;
|
||
|
|
||
|
|
||
|
/* (6) Clause UPDATE
|
||
|
---------------------------------------------------------*/
|
||
|
case 'UPDATE':
|
||
|
$sql .= "UPDATE $statements\n";
|
||
|
break;
|
||
|
break;
|
||
|
|
||
|
|
||
|
/* (7) Clause SET
|
||
|
---------------------------------------------------------*/
|
||
|
case 'SET':
|
||
|
$c = 0;
|
||
|
foreach($statements as $field){
|
||
|
$sql .= ($c>0) ? "\n, $field" : "SET $field";
|
||
|
$c++;
|
||
|
}
|
||
|
$sql .= "\n";
|
||
|
break;
|
||
|
|
||
|
/* (8) Clause GROUP BY
|
||
|
---------------------------------------------------------*/
|
||
|
case 'GROUPBY':
|
||
|
$sql .= 'GROUP BY ';
|
||
|
|
||
|
$c = 0;
|
||
|
foreach($statements as $table=>$fields)
|
||
|
foreach($fields as $field){
|
||
|
$sql .= ($c==0) ? "$table.$field" : ", $table.$field";
|
||
|
$c++;
|
||
|
}
|
||
|
|
||
|
$sql .= "\n";
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* [2] On retourne le résultat
|
||
|
=========================================================*/
|
||
|
return $sql;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
?>
|