ORM: GROUP BY automatique à vérifier avec des exemples plus complexes + gestion des fonctions d'aggrégation

This commit is contained in:
xdrm-brackets 2016-07-25 00:55:42 +02:00
parent 943e4e2106
commit 0ae20f3fd1
4 changed files with 94 additions and 16 deletions

View File

@ -839,12 +839,33 @@
/* [6] Clause GROUP BY /* [6] Clause GROUP BY
=========================================================*/ =========================================================*/
$nonAggregatedTables = []; /* (0) On initialise la liste des @rows non aggrégés */
foreach($this->select as $field=>$sel){ $notAggregatedRows = [];
/* (1) On cherche dans les champs locaux local */
foreach($this->select as $field=>$sel)
if( is_null($sel[0]) )
$notAggregatedRows[] = $this;
/* (2) On cherche dans les champs des jointures */
foreach($this->joined as $data)
foreach($data['object']->select as $field=>$sel)
if( is_null($sel[0]) )
$notAggregatedRows[] = $data['object'];
/* (3) Pour chaque table, on récupère les clés primaires */
$groupBy = [];
foreach($notAggregatedRows as $i=>$rows){
$groupBy[$rows->schema['table']] = [];
foreach($rows->schema['columns'] as $field=>$data)
if( $data['primary'] )
$groupBy[$rows->schema['table']][] = $field;
} }
/* (4) On rédige la clause GROUP BY */
$requestS['GROUPBY'] = SQLBuilder::GROUPBY($groupBy);
/* [6] Clause LIMIT /* [6] Clause LIMIT
=========================================================*/ =========================================================*/
@ -859,7 +880,6 @@
/* (2) On compose la requête */ /* (2) On compose la requête */
$requestString = SQLBuilder::BUILD($requestS).';'; $requestString = SQLBuilder::BUILD($requestS).';';
var_dump($requestString);
/* (3) On prépare la requête */ /* (3) On prépare la requête */
$request = Database::getPDO()->prepare($requestString); $request = Database::getPDO()->prepare($requestString);

View File

@ -38,9 +38,42 @@
if( !is_null($select[0]) ) if( !is_null($select[0]) )
$fieldStr = substr($select[0], 2, -2)."($fieldStr)"; $fieldStr = substr($select[0], 2, -2)."($fieldStr)";
/* (4) On ajoute l'alias */
$sql[] = "$fieldStr as $field";
/* (4) On ajoute l'alias */
if( $field != '*' ) $sql[$c] = "$fieldStr as $field";
else $sql[$c] = "$fieldStr";
$c++;
}
return $sql;
}
/* 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){
/* [0] Initialisation
=========================================================*/
$sql = [];
/* [1] On construit la requête
=========================================================*/
$c = 0;
foreach($tables as $table=>$fields)
foreach($fields as $field){
$sql[] = "$table.$field";
$c++; $c++;
} }
@ -266,7 +299,7 @@
/* (1) Clause SELECT /* (1) Clause SELECT
---------------------------------------------------------*/ ---------------------------------------------------------*/
case 'SELECT': case 'SELECT':
$sql = 'SELECT '; $sql .= 'SELECT ';
$c = 0; $c = 0;
foreach($statements as $field){ foreach($statements as $field){
@ -339,6 +372,20 @@
} }
$sql .= "\n"; $sql .= "\n";
break; break;
/* (8) Clause GROUP BY
---------------------------------------------------------*/
case 'GROUPBY':
$sql .= 'GROUP BY ';
$c = 0;
foreach($statements as $field){
$sql .= ($c==0) ? $field : ", $field";
$c++;
}
$sql .= "\n";
break;
} }

View File

@ -450,11 +450,21 @@
$selReq = Database::getPDO()->query("select m.*, max(h.timestamp) as last $selReq = Database::getPDO()->query("select m.*, max(h.timestamp) as last
from history as h, machine as m from history as h, machine as m
where h.id_machine = m.id_machine where h.id_machine = m.id_machine
group by h.id_machine"); group by m.id_machine");
$selected = Database::delNumeric( $selReq->fetchAll() ); $selected = Database::delNumeric( $selReq->fetchAll() );
var_dump($selected); var_dump($selected);
$mac = Table::get('machine')
->select('*');
$selected = Table::get('history')
->join('id_machine', $mac)
->select('timestamp', Rows::SEL_MAX);
var_dump($selected->fetch());
// $a = new ModuleRequest('authentificationDefault/warehouse', [ // $a = new ModuleRequest('authentificationDefault/warehouse', [
// 'name' => 'test', // 'name' => 'test',

View File

@ -57,13 +57,14 @@
/* (1) On récupère les données /* (1) On récupère les données
---------------------------------------------------------*/ ---------------------------------------------------------*/
$selReq = Database::getPDO()->query("select m.*, max(h.timestamp) as last $mac = Table::get('machine')
from history as h, machine as m ->select('*');
where h.id_machine = m.id_machine
group by h.id_machine");
$selected = Database::delNumeric( $selReq->fetchAll() );
// $selected = Table::get('history'); $selected = Table::get('history')
->join('id_machine', $mac)
->select('timestamp', Rows::SEL_MAX)
->select('id_history', Rows::SEL_COUNT, Rows::SEL_DISTINCT)
->fetch();
echo "<article class='inline-row' style='border: 0; box-shadow: none;background: transparent;'>"; echo "<article class='inline-row' style='border: 0; box-shadow: none;background: transparent;'>";
@ -86,7 +87,7 @@
echo "<span>"; echo "<span>";
echo "<span>test conducteur(s)</span>"; echo "<span>".$mac['id_history']." conducteur(s)</span>";
echo "<span>test</span>"; echo "<span>test</span>";
echo "</span>"; echo "</span>";