From d8a828f9a3200a05a52dedaa1eb6a1944248beaf Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 25 Jul 2016 00:26:58 +0200 Subject: [PATCH] =?UTF-8?q?ORM:=20Gestion=20des=20fonctions=20d'aggr=C3=A9?= =?UTF-8?q?gation=20(AVG,=20SUM,=20COUNT,=20MIN,=20MAX)=20+=20possibilit?= =?UTF-8?q?=C3=A9=20d'ajouter=20'DISTINCT',=20GROUP=20BY=20pas=20encore=20?= =?UTF-8?q?g=C3=A9r=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manager/ORM/Rows.php | 62 +++++++++++++---- manager/ORM/SQLBuilder.php | 21 ++++-- manager/View.php | 2 +- manager/{view => views}/group/groupChoice.php | 2 +- .../{view => views}/group/membersChoice.php | 2 +- manager/{view => views}/group/view.php | 2 +- manager/{view => views}/machine/groups.php | 2 +- manager/{view => views}/machine/view.php | 2 +- manager/{view => views}/user/groups.php | 2 +- manager/{view => views}/user/view.php | 2 +- test/automate.php | 68 +++++++------------ view/history.php | 10 ++- view/view.php | 2 + 13 files changed, 105 insertions(+), 74 deletions(-) rename manager/{view => views}/group/groupChoice.php (98%) rename manager/{view => views}/group/membersChoice.php (99%) rename manager/{view => views}/group/view.php (99%) rename manager/{view => views}/machine/groups.php (98%) rename manager/{view => views}/machine/view.php (98%) rename manager/{view => views}/user/groups.php (98%) rename manager/{view => views}/user/view.php (98%) diff --git a/manager/ORM/Rows.php b/manager/ORM/Rows.php index d8ca17b..0dc772d 100644 --- a/manager/ORM/Rows.php +++ b/manager/ORM/Rows.php @@ -10,6 +10,8 @@ /* CONSTANTES */ + + // {1} Conditions // const COND_EQUAL = '__=__'; const COND_NOTEQ = '__<>__'; const COND_INF = '__<__'; @@ -19,6 +21,16 @@ const COND_LIKE = '__LIKE__'; const COND_IN = '__IN__'; + // {2} Fonctions d'aggrégation // + const SEL_AVG = '__AVG__'; + const SEL_SUM = '__SUM__'; + const SEL_MAX = '__MAX__'; + const SEL_MIN = '__MIN__'; + const SEL_COUNT = '__COUNT__'; + + const SEL_DISTINCT = true; + + // {3} Constantes d'insertion // const INSERT_DEFAULT = '__DEFAULT__'; // Valeur DEFAULT (pour insertion) /* Attributs */ @@ -228,33 +240,42 @@ - /* SELECTIONNE UNIQUEMENT LES CHAMPS SELECTIONNES + /* SELECTIONNE UNIQUEMENT LE CHAMP SELECTIONNE * - * @fields Libellé du champ à afficher + * @field Libellé du champ à afficher + * @func Fonction d'aggrégation (ou NULL) + * @distinct Clause DISTINCT * * @return this Retourne l'object courant * */ - public function select($fields=[]){ + public function select($field=null, $func=null, $distinct=false){ /* [1] On formatte les champs =========================================================*/ - /* (1) On met en tableau quoi qu'il en soit */ - $fields = is_array($fields) ? $fields : [$fields]; + /* (1) On vérifie le type de @field */ + if( !is_string($field) ) + return $this; - /* (2) On vérifie que chaque champ existe, sinon on le retire */ - foreach($fields as $f=>$field) - if( !isset($this->schema['columns'][$field]) && $field != '*' ) - unset($fields[$f]); + /* (2) On vérifie que la colonne @field existe, sinon on quitte */ + if( !isset($this->schema['columns'][$field]) && $field != '*' ) + return $this; - /* (3) Permet d'avoir les indices de 0 à count-1 */ - sort($fields); + /* (3) On vérifie @func */ + $funcList = [self::SEL_AVG, self::SEL_SUM, self::SEL_MAX, self::SEL_MIN, self::SEL_COUNT]; + + // Si condition non nulle et pas référencée, on quitte + if( !is_null($func) && !in_array($func, $funcList) ) + return $this; + + /* (4) On met la valeur par défaut à @distinct si type mauvais */ + $distinct = !is_bool($distinct) ? false : $distinct; - /* [2] On enregistre la liste des champs + /* [2] On enregistre le champ =========================================================*/ - foreach($fields as $f=>$field) - if( !in_array($field, $this->select) ) // On ajoute si pas déja - $this->select[] = $field; + /* (1) Si aucun SELECT pour ce champ, on le crée */ + if( !isset($this->select[$field]) ) + $this->select[$field] = [$func, $distinct]; /* [3] On retourne l'object courant @@ -815,6 +836,16 @@ + + /* [6] Clause GROUP BY + =========================================================*/ + $nonAggregatedTables = []; + foreach($this->select as $field=>$sel){ + + } + + + /* [6] Clause LIMIT =========================================================*/ $requestS['LIMIT'] = ($this->unique) ? SQLBuilder::LIMIT(1) : SQLBuilder::LIMIT([]); @@ -828,6 +859,7 @@ /* (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 616071f..06fcbed 100644 --- a/manager/ORM/SQLBuilder.php +++ b/manager/ORM/SQLBuilder.php @@ -11,7 +11,7 @@ /* CONSTRUIT LA REQUETE FORMATTEE "SELECT" AVEC UNE LISTE DE CHAMPS * - * @sqlFields Liste de champ/tables contenant des champs + * @sqlFields Liste de champs : [table => field => func, alias] * * @return sql Renvoie un tableau formatté * @@ -24,9 +24,22 @@ /* [1] On construit la requête =========================================================*/ $c = 0; - foreach($sqlFields as $table=>$fields) - foreach($fields as $field){ - $sql[$c] = $table.'.'.$field; + foreach($sqlFields as $table=>$tableContent) + foreach($tableContent as $field=>$select){ + + /* (1) On construit le nom du champ */ + $fieldStr = "$table.$field"; + + /* (2) On ajout le DISTINCT s'il y a lieu */ + if( $select[1] ) + $fieldStr = "DISTINCT $fieldStr"; + + /* (3) On ajoute la fonction d'aggrégation s'il y a lieu */ + if( !is_null($select[0]) ) + $fieldStr = substr($select[0], 2, -2)."($fieldStr)"; + + /* (4) On ajoute l'alias */ + $sql[] = "$fieldStr as $field"; $c++; } diff --git a/manager/View.php b/manager/View.php index 822999c..8ddd782 100644 --- a/manager/View.php +++ b/manager/View.php @@ -105,7 +105,7 @@ /* [2] On vérifie que le template existe =========================================================*/ - $class = '\\manager\\view\\'.$match[1].'\\'.$match[2]; + $class = '\\manager\\views\\'.$match[1].'\\'.$match[2]; $method = 'view'; /* (1) On vérifie que la classe existe */ diff --git a/manager/view/group/groupChoice.php b/manager/views/group/groupChoice.php similarity index 98% rename from manager/view/group/groupChoice.php rename to manager/views/group/groupChoice.php index 57a4c73..0e525c7 100644 --- a/manager/view/group/groupChoice.php +++ b/manager/views/group/groupChoice.php @@ -1,6 +1,6 @@ whereName(['stef-montauban', Rows::COND_EQUAL]) // condition : name = 'stef-montauban' - ->select('name') // select : warehouse.name - ->unique(); // limit : 1 - - $myUser = - Table::get('user') // Table : USER - ->whereId([100, Rows::COND_INF]) // condition : clé primaire(id_user) < 100 - ->whereId([[94, 95, 96], Rows::COND_IN]) // condition : clé primaire(id_user) parmi les valeurs [94, 95, 96] - ->whereUsername(['%e%', Rows::COND_LIKE]) // condition : username LIKE '%e%' - ->select(['mail', 'username', 'firstname']) // select : user.mail, user.username, user.firstname - ->select('id_user') // Select : user.id_user - ->join('id_warehouse', $warehouse) // jointure la table WAREHOUSE (automatique, soit référence, soit primaire soit même référence) - - // SELECT - ->fetch(); - var_dump($myUser); + // $warehouse = + // Table::get('warehouse') // Table : WAREHOUSE + // ->whereName(['stef-montauban', Rows::COND_EQUAL]) // condition : name = 'stef-montauban' + // ->select('name') // select : warehouse.name + // ->unique(); // limit : 1 + // + // $myUser = + // Table::get('user') // Table : USER + // ->whereId([100, Rows::COND_INF]) // condition : clé primaire(id_user) < 100 + // ->whereId([[94, 95, 96], Rows::COND_IN]) // condition : clé primaire(id_user) parmi les valeurs [94, 95, 96] + // ->whereUsername(['%e%', Rows::COND_LIKE]) // condition : username LIKE '%e%' + // ->select('name') // Select : user.id_user + // ->select('id_user', Rows::SEL_COUNT) + // ->join('id_warehouse', $warehouse) // jointure la table WAREHOUSE (automatique, soit référence, soit primaire soit même référence) + // + // // SELECT + // ->fetch(); + // var_dump($myUser); // SELECT user.firstname, user.mail, user.username, user.id_user, warehouse.name @@ -447,32 +447,12 @@ - // $warehouse = Table::get('warehouse') - // ->whereName(['stef%', Rows::COND_LIKE]) - // ->select('id_warehouse'); - // - // $module_merge = Table::get('module_merge') - // ->join('id_warehouse', $warehouse) - // ->select('id_module_merge'); - // - // $module = Table::get('module') - // ->join('id_module', $module_merge) - // ->select('id_module'); - // - // $chip = Table::get('chip') - // ->select('id_chip') - // ->join('id_module', $module); - // - // var_dump($chip->fetch()); - - // SELECT module.id_module - // FROM chip, - // (SELECT * FROM module) as module, - // (SELECT * FROM module_merge) as module_merge, - // (SELECT * FROM warehouse WHERE warehouse.name LIKE 'stef%') as warehouse - // WHERE chip.id_module = module.id_module - // AND module.id_module = module_merge.id_module - // AND module_merge.id_warehouse = warehouse.id_warehouse + $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() ); + var_dump($selected); diff --git a/view/history.php b/view/history.php index b9ecd3e..b2bc452 100755 --- a/view/history.php +++ b/view/history.php @@ -4,6 +4,8 @@ use \manager\ManagerError; use \manager\Database; use \manager\Repo; + + use \manager\ORM\Table; use \manager\ORM\Rows; ?> @@ -55,12 +57,14 @@ /* (1) On récupère les données ---------------------------------------------------------*/ - $selReq = Database::getPDO()->query("select m.*, count(distinct h.id_user) as users, max(h.timestamp) as last + $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() ); + // $selected = Table::get('history'); + echo "
"; echo "Machine"; @@ -82,8 +86,8 @@ echo ""; - echo "".$mac['users']." conducteur(s)"; - echo "".$mac['users'].""; + echo "test conducteur(s)"; + echo "test"; echo ""; echo ""; diff --git a/view/view.php b/view/view.php index 347729e..73ccf92 100755 --- a/view/view.php +++ b/view/view.php @@ -1,3 +1,5 @@ + +