ORM: GROUP BY automatique à vérifier avec des exemples plus complexes + gestion des fonctions d'aggrégation
This commit is contained in:
parent
943e4e2106
commit
0ae20f3fd1
|
@ -839,12 +839,33 @@
|
|||
|
||||
/* [6] Clause GROUP BY
|
||||
=========================================================*/
|
||||
$nonAggregatedTables = [];
|
||||
foreach($this->select as $field=>$sel){
|
||||
/* (0) On initialise la liste des @rows non aggrégés */
|
||||
$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
|
||||
=========================================================*/
|
||||
|
@ -859,7 +880,6 @@
|
|||
|
||||
/* (2) On compose la requête */
|
||||
$requestString = SQLBuilder::BUILD($requestS).';';
|
||||
var_dump($requestString);
|
||||
|
||||
/* (3) On prépare la requête */
|
||||
$request = Database::getPDO()->prepare($requestString);
|
||||
|
|
|
@ -38,9 +38,42 @@
|
|||
if( !is_null($select[0]) )
|
||||
$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++;
|
||||
}
|
||||
|
||||
|
@ -266,7 +299,7 @@
|
|||
/* (1) Clause SELECT
|
||||
---------------------------------------------------------*/
|
||||
case 'SELECT':
|
||||
$sql = 'SELECT ';
|
||||
$sql .= 'SELECT ';
|
||||
|
||||
$c = 0;
|
||||
foreach($statements as $field){
|
||||
|
@ -339,6 +372,20 @@
|
|||
}
|
||||
$sql .= "\n";
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -450,11 +450,21 @@
|
|||
$selReq = Database::getPDO()->query("select m.*, max(h.timestamp) as last
|
||||
from history as h, machine as m
|
||||
where h.id_machine = m.id_machine
|
||||
group by h.id_machine");
|
||||
group by m.id_machine");
|
||||
$selected = Database::delNumeric( $selReq->fetchAll() );
|
||||
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', [
|
||||
// 'name' => 'test',
|
||||
|
|
|
@ -57,13 +57,14 @@
|
|||
|
||||
/* (1) On récupère les données
|
||||
---------------------------------------------------------*/
|
||||
$selReq = Database::getPDO()->query("select m.*, max(h.timestamp) as last
|
||||
from history as h, machine as m
|
||||
where h.id_machine = m.id_machine
|
||||
group by h.id_machine");
|
||||
$selected = Database::delNumeric( $selReq->fetchAll() );
|
||||
$mac = Table::get('machine')
|
||||
->select('*');
|
||||
|
||||
// $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;'>";
|
||||
|
@ -86,7 +87,7 @@
|
|||
|
||||
|
||||
echo "<span>";
|
||||
echo "<span>test conducteur(s)</span>";
|
||||
echo "<span>".$mac['id_history']." conducteur(s)</span>";
|
||||
echo "<span>test</span>";
|
||||
echo "</span>";
|
||||
|
||||
|
|
Loading…
Reference in New Issue