2016-07-24 10:00:49 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-18 14:03:03 +00:00
|
|
|
namespace orm\core;
|
2016-07-24 10:00:49 +00:00
|
|
|
|
2016-11-05 13:57:35 +00:00
|
|
|
use \database\core\DatabaseDriver;
|
2016-10-18 14:03:03 +00:00
|
|
|
use \orm\core\Rows;
|
2016-07-24 10:00:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
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-25 08:34:06 +00:00
|
|
|
* @sqlFields<Array> Liste de champs : [table => field => [func, alias] ]
|
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-25 08:34:06 +00:00
|
|
|
return $sqlFields;
|
2016-07-24 22:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:07:20 +00:00
|
|
|
/* CONSTRUIT LA REQUETE FORMATTEE "ORDER BY" AVEC UNE LISTE DE CHAMPS
|
|
|
|
*
|
|
|
|
* @tables<Array> Liste de champs : [table => fields]
|
|
|
|
*
|
|
|
|
* @return sql<Array> Renvoie un tableau formatté
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function ORDERBY($tables){
|
|
|
|
return $tables;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-24 22:55:42 +00:00
|
|
|
/* 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){
|
2016-07-25 08:34:06 +00:00
|
|
|
return $tables;
|
2016-07-24 10:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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){
|
2016-07-24 15:59:59 +00:00
|
|
|
return $table;
|
|
|
|
}
|
2016-07-24 10:31:54 +00:00
|
|
|
|
|
|
|
|
2016-07-24 15:59:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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;
|
2016-07-24 10:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2016-07-24 16:27:52 +00:00
|
|
|
* @bound<Arary> Tableau associatif contenant les variables "bindés" -> ajout des champs
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
2016-07-24 15:42:54 +00:00
|
|
|
* @return sql<String> Renvoie le textuel formatté
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-07-24 16:27:52 +00:00
|
|
|
public static function IN($field, $array, $offset=0, &$bound){
|
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
|
|
|
|
=========================================================*/
|
|
|
|
/* (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;
|
|
|
|
|
2016-07-24 16:27:52 +00:00
|
|
|
$bound[':'.$field[0].'_x_'.$field[1].'_'.$offset.'_'.$i] = $value;
|
2016-07-24 10:00:49 +00:00
|
|
|
|
|
|
|
$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)
|
2016-07-24 16:27:52 +00:00
|
|
|
* @bound<Arary> Tableau associatif contenant les variables "bindés" -> ajout des champs
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
2016-07-24 15:42:54 +00:00
|
|
|
* @return sql<String> Renvoie le textuel formatté
|
2016-07-24 10:00:49 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-07-24 16:27:52 +00:00
|
|
|
public static function WHERE($field, $value, $offset=0, &$bound){
|
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
|
|
|
|
=========================================================*/
|
|
|
|
/* (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;
|
|
|
|
|
2016-07-24 16:27:52 +00:00
|
|
|
$bound[':'.$field[0].'_x_'.$field[1].'_'.$offset] = $value[0];
|
2016-07-24 10:00:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
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 ]
|
2016-07-24 16:27:52 +00:00
|
|
|
* @bound<Arary> Tableau associatif contenant les variables "bindés" -> ajout des champs
|
2016-07-24 10:31:54 +00:00
|
|
|
*
|
2016-07-24 15:42:54 +00:00
|
|
|
* @return sql<Array> Renvoie un tableau formatté
|
2016-07-24 10:31:54 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-07-24 16:27:52 +00:00
|
|
|
public static function SET($values, &$bound){
|
2016-07-24 10:31:54 +00:00
|
|
|
/* [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 16:11:46 +00:00
|
|
|
$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
|
|
|
|
2016-07-24 16:27:52 +00:00
|
|
|
$bound[':update_'.$field] = $value;
|
2016-07-24 10:31:54 +00:00
|
|
|
|
|
|
|
$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
|
|
|
|
=========================================================*/
|
2016-07-25 08:34:06 +00:00
|
|
|
$sql = '';
|
2016-07-24 15:42:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [1] On construit la requête
|
|
|
|
=========================================================*/
|
|
|
|
if( intval($count) == $count )
|
2016-07-25 08:34:06 +00:00
|
|
|
$sql = intval($count);
|
2016-07-24 15:42:54 +00:00
|
|
|
|
|
|
|
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){
|
2016-07-24 15:59:59 +00:00
|
|
|
/* [0] On initialise le retour
|
2016-07-24 15:42:54 +00:00
|
|
|
=========================================================*/
|
2016-07-24 15:59:59 +00:00
|
|
|
$sql = '';
|
2016-07-24 15:42:54 +00:00
|
|
|
|
2016-07-24 15:59:59 +00:00
|
|
|
/* [1] Gestion dans l'ordre
|
|
|
|
=========================================================*/
|
|
|
|
foreach($request as $clause=>$statements){
|
2016-07-24 15:42:54 +00:00
|
|
|
|
2016-07-24 15:59:59 +00:00
|
|
|
switch($clause){
|
2016-07-24 15:42:54 +00:00
|
|
|
|
2016-07-24 15:59:59 +00:00
|
|
|
/* (1) Clause SELECT
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
case 'SELECT':
|
2016-07-25 08:34:06 +00:00
|
|
|
$sql .= "SELECT ";
|
|
|
|
$c = 0;
|
|
|
|
foreach($statements as $table=>$fields)
|
|
|
|
foreach($fields as $field=>$select){
|
|
|
|
|
|
|
|
/* (1) On construit le nom du champ */
|
2016-07-25 09:25:25 +00:00
|
|
|
$fieldStr = "$table.$field";
|
2016-07-25 08:34:06 +00:00
|
|
|
|
|
|
|
/* (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 */
|
2016-07-25 08:53:24 +00:00
|
|
|
if( isset($select[0]) && !is_null($select[0]) )
|
2016-07-25 09:25:25 +00:00
|
|
|
$fieldStr = "$fieldStr as agg_$field";
|
2016-07-25 08:53:24 +00:00
|
|
|
else
|
2016-07-25 09:25:25 +00:00
|
|
|
$fieldStr = "$fieldStr";
|
2016-07-25 08:34:06 +00:00
|
|
|
|
2016-07-25 09:25:25 +00:00
|
|
|
$sql .= ($c==0) ? "$fieldStr" : ", $fieldStr";
|
2016-07-24 15:42:54 +00:00
|
|
|
|
2016-07-24 15:59:59 +00:00
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= "\n";
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* (2) Clause FROM
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
case 'FROM':
|
|
|
|
$sql .= 'FROM ';
|
|
|
|
|
|
|
|
$c = 0;
|
|
|
|
foreach($statements as $field){
|
2016-07-25 08:34:06 +00:00
|
|
|
$sql .= ($c==0) ? "$field" : ", $field";
|
2016-07-24 15:59:59 +00:00
|
|
|
$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;
|
2016-07-24 16:11:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (6) Clause UPDATE
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
case 'UPDATE':
|
|
|
|
$sql .= "UPDATE $statements\n";
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
/* (7) Clause SET
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
case 'SET':
|
|
|
|
$c = 0;
|
|
|
|
foreach($statements as $field){
|
|
|
|
$sql .= ($c>0) ? "\n, $field" : "SET $field";
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
$sql .= "\n";
|
|
|
|
break;
|
2016-07-24 22:55:42 +00:00
|
|
|
|
|
|
|
/* (8) Clause GROUP BY
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
case 'GROUPBY':
|
|
|
|
$sql .= 'GROUP BY ';
|
|
|
|
|
|
|
|
$c = 0;
|
2016-07-25 08:34:06 +00:00
|
|
|
foreach($statements as $table=>$fields)
|
|
|
|
foreach($fields as $field){
|
|
|
|
$sql .= ($c==0) ? "$table.$field" : ", $table.$field";
|
|
|
|
$c++;
|
|
|
|
}
|
2016-07-24 22:55:42 +00:00
|
|
|
|
|
|
|
$sql .= "\n";
|
|
|
|
break;
|
2016-10-03 10:07:20 +00:00
|
|
|
|
|
|
|
/* (9) Clause ORDER BY
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
case 'ORDERBY':
|
|
|
|
|
|
|
|
// si aucun ORDER BY, on quitte
|
|
|
|
if( count($statements) == 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$sql .= 'ORDER BY ';
|
|
|
|
|
|
|
|
$c = 0;
|
|
|
|
foreach($statements as $table=>$fields)
|
|
|
|
foreach($fields as $field=>$order){
|
|
|
|
|
|
|
|
if( $c > 0 ) $sql .= ', ';
|
|
|
|
|
|
|
|
$sql .= "$table.$field ". substr($order, 2, -2);
|
|
|
|
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= "\n";
|
|
|
|
break;
|
2016-07-24 15:59:59 +00:00
|
|
|
}
|
2016-07-24 15:42:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-24 15:59:59 +00:00
|
|
|
|
|
|
|
|
2016-07-24 15:42:54 +00:00
|
|
|
|
|
|
|
|
2016-07-24 16:11:46 +00:00
|
|
|
/* [2] On retourne le résultat
|
2016-07-24 15:42:54 +00:00
|
|
|
=========================================================*/
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-24 10:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|