From 0ae20f3fd19b79d439c0ac7f99abd50a6c721b17 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 25 Jul 2016 00:55:42 +0200 Subject: [PATCH] =?UTF-8?q?ORM:=20GROUP=20BY=20automatique=20=C3=A0=20v?= =?UTF-8?q?=C3=A9rifier=20avec=20des=20exemples=20plus=20complexes=20+=20g?= =?UTF-8?q?estion=20des=20fonctions=20d'aggr=C3=A9gation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manager/ORM/Rows.php | 30 +++++++++++++++++---- manager/ORM/SQLBuilder.php | 53 +++++++++++++++++++++++++++++++++++--- test/automate.php | 12 ++++++++- view/history.php | 15 ++++++----- 4 files changed, 94 insertions(+), 16 deletions(-) diff --git a/manager/ORM/Rows.php b/manager/ORM/Rows.php index 0dc772d..030288b 100644 --- a/manager/ORM/Rows.php +++ b/manager/ORM/Rows.php @@ -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); diff --git a/manager/ORM/SQLBuilder.php b/manager/ORM/SQLBuilder.php index 06fcbed..65c61e2 100644 --- a/manager/ORM/SQLBuilder.php +++ b/manager/ORM/SQLBuilder.php @@ -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 Liste de champs : [table => fields] + * + * @return sql 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; } diff --git a/test/automate.php b/test/automate.php index e06540a..fe9561a 100755 --- a/test/automate.php +++ b/test/automate.php @@ -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', diff --git a/view/history.php b/view/history.php index b2bc452..62c6ba9 100755 --- a/view/history.php +++ b/view/history.php @@ -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 "
"; @@ -86,7 +87,7 @@ echo ""; - echo "test conducteur(s)"; + echo "".$mac['id_history']." conducteur(s)"; echo "test"; echo "";