More dataaccess stuff.
authorCameron Ball <cameron@getapproved.com.au>
Fri, 19 Sep 2014 09:07:02 +0000 (17:07 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Fri, 19 Sep 2014 09:07:02 +0000 (17:07 +0800)
Controllers/IndexController.php
DataAccess/DataMapper/DataMapper.php
DataAccess/DataMapper/IDataMapper.php
DataAccess/IRepository.php
DataAccess/Queries/IQueryConstraints.php [new file with mode: 0644]
DataAccess/Queries/QueryConstraints.php [new file with mode: 0644]
DataAccess/Queries/StepMania/ISimfileQueryConstraints.php [new file with mode: 0644]
DataAccess/Queries/StepMania/SimfileQueryConstraints.php [new file with mode: 0644]
DataAccess/StepMania/ISimfileRepository.php
DataAccess/StepMania/SimfileRepository.php
public_html/index.php

index 5f86846..8b4ba2a 100644 (file)
@@ -5,12 +5,10 @@ namespace Controllers;
 use DataAccess\StepMania\ISimfileRepository;
 use Services\Http\IHttpResponse;
 use Services\Http\IHttpRequest;
-use Controllers\AbstractBaseController;
+use DataAccess\Queries\StepMania\SimfileQueryConstraints;
 
 class IndexController implements IDivineController
 {
-    
-    private $_content;
     private $_simfileRepository;
     private $_response;
     private $_request;
@@ -26,9 +24,19 @@ class IndexController implements IDivineController
         $this->_simfileRepository = $repository;
     }
         
-    public function indexAction() {      
-        $this->_response->setHeader('Content-Type', 'application/json')
-                        ->setBody(json_encode(array('message' => 'nothing to see here')))
-                        ->sendResponse();
+    public function indexAction() { 
+        $queryConstraints = new SimfileQueryConstraints();
+        $queryConstraints->stepsHaveRating(15);
+
+        $simfiles = $this->_simfileRepository->findByTitle('a', $queryConstraints);
+
+        foreach($simfiles as $simfile)
+        {
+            echo $simfile->getTitle();
+        }
+        
+//        $this->_response->setHeader('Content-Type', 'application/json')
+//                        ->setBody(json_encode(array('message' => 'nothing to see here')))
+//                        ->sendResponse();
     }
 }
index 1e49010..9d1611b 100644 (file)
@@ -22,15 +22,14 @@ class DataMapper implements IDataMapper
         $options = array(PDO::ATTR_EMULATE_PREPARES => false,\r
                          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);\r
         \r
-        $this->_db = new PDO($dsn, $username, $password, $options);        \r
+        $this->_db = new PDO($dsn, $username, null, $options);        \r
         $this->_maps = include $maps;\r
     }\r
     \r
-    public function find($entityName, $queryString)\r
+    public function map($entityName, $queryString = 'SELECT * FROM %s')\r
     {\r
-        $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE %s',\r
-            $this->_maps[$entityName]['table'],\r
-            $queryString\r
+        $statement = $this->_db->prepare(sprintf($queryString,\r
+            $this->_maps[$entityName]['table']\r
         ));\r
         \r
         $statement->execute();\r
@@ -55,23 +54,9 @@ class DataMapper implements IDataMapper
             $entities[$row['id']] = $class;\r
         }\r
         \r
-        return count($entities) > 1 ? $entities : reset($entities);\r
-        \r
-        return $this->findRange($id, $entityName, 1);\r
+        return $entities;\r
     }\r
-    \r
-    public function findById($id, $entity)\r
-    {\r
-         $queryString = sprintf('id=%u', $id);\r
-         return $this->find($entity, $queryString);\r
-    }\r
-    \r
-    public function findRange($id, $entity, $limit)\r
-    {\r
-        $queryString = sprintf('id>=%u LIMIT %u', $id, $limit);\r
-        return $this->find($entity, $queryString);\r
-    }\r
-    \r
+        \r
     public function save(IDivineEntity $entity)\r
     {\r
         $queries = AbstractPopulationHelper::generateUpdateSaveQuery($this->_maps, $entity, $entity->getId(), $this->_db);\r
index ee90de0..001db54 100644 (file)
@@ -2,18 +2,15 @@
 \r
 namespace DataAccess\DataMapper;\r
 \r
+use DataAccess\Queries\IQueryConstraints;\r
 use Domain\Entities\IDivineEntity;\r
 \r
 interface IDataMapper\r
 {\r
     //TODO: Table is the wrong name. We actually give the implementation the entity name and it finds the table from the maps.\r
     \r
-    //find in table based on criteria in queryString\r
-    public function find($entityName, $queryString);\r
-    //find id in table and return it as an entity\r
-    public function findById($id, $entityName);\r
-    //find rows with id >= id and stop at limit\r
-    public function findRange($id, $entityName, $limit);\r
+    //find in table based on constraints and return it as entity\r
+    public function map($entityName, $queryString);\r
     //insert/update entity in table\r
     public function save(IDivineEntity $entity);\r
     //remove entity from table\r
index 34765cb..af3a435 100644 (file)
@@ -8,6 +8,4 @@ interface IRepository
 {\r
     public function findById($id);\r
     public function findRange($id, $limit);\r
-    public function save(IDivineEntity $entity);\r
-    public function remove(IDivineEntity $entity);\r
 }
\ No newline at end of file
diff --git a/DataAccess/Queries/IQueryConstraints.php b/DataAccess/Queries/IQueryConstraints.php
new file mode 100644 (file)
index 0000000..38128d6
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+/* 
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+namespace DataAccess\Queries;
+
+interface IQueryConstraints
+{
+    public function applyTo($queryString);
+    public function limit($start, $end);
+    public function where($column, $operator, $value);
+    public function join($type, $tableA, $columnA, $tableB, $columnB);
+}
\ No newline at end of file
diff --git a/DataAccess/Queries/QueryConstraints.php b/DataAccess/Queries/QueryConstraints.php
new file mode 100644 (file)
index 0000000..415baf8
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+
+namespace DataAccess\Queries;
+
+use DataAccess\Queries\IQueryConstraints;
+
+class QueryConstraints implements IQueryConstraints
+{
+    private $_queryString;
+    
+    protected $_whereClauses = array();
+    protected $_limitClause;
+    protected $_joinClause;
+    
+    public function applyTo($queryString)
+    {
+        $this->_queryString = $queryString;
+        
+        $this->applyJoinClause()
+             ->applyWhereClauses()
+             ->applyLimitClause();
+        
+        return $this->_queryString;
+    }
+    
+    public function where($columnName, $operator, $value)
+    {
+        $this->_whereClauses[$columnName] = array('operator' => $operator, 'value' => $value);
+        return $this;
+    }
+    
+    public function limit($start, $end = null)
+    {
+        if($end) 
+        {
+            $this->_limitClause = sprintf(' LIMIT %u,%u', $start, $end);
+            return $this;
+        }
+        
+        $this->_limitClause = sprintf(' LIMIT %u', $start);
+        
+        return $this;
+    }
+    
+    public function join($type, $tableA, $columnA, $tableB, $columnB)
+    {
+        $this->_joinClause = sprintf(' %s JOIN %s ON %s.%s = %s.%s', $type, $tableB, $tableA, $columnA, $tableB, $columnB);
+        return $this;
+    }
+    
+    private function applyJoinClause()
+    {
+        $this->_queryString .= $this->_joinClause;
+        return $this;
+    }
+    
+    private function applyWhereClauses()
+    {
+        $this->_queryString .= ' WHERE ';
+        
+        foreach($this->_whereClauses as $columnName => $columnValue)
+        {
+            switch(gettype($columnValue['value']))
+            {
+                case 'integer':
+                    $this->_queryString .= sprintf("%s%s%u", $columnName, $columnValue['operator'], $columnValue['value']) . ' AND ';
+                    break;
+                case 'string':
+                    $this->_queryString .= sprintf("%s %s '%s'", $columnName, $columnValue['operator'], $columnValue['value']) . ' AND ';
+                    break;
+            }
+            
+        }
+        
+        $this->_queryString = rtrim($this->_queryString, ' AND ');
+        return $this;
+    }
+    
+    private function applyLimitClause()
+    {
+        $this->_queryString .= $this->_limitClause;
+        return $this;
+    }
+    
+}
\ No newline at end of file
diff --git a/DataAccess/Queries/StepMania/ISimfileQueryConstraints.php b/DataAccess/Queries/StepMania/ISimfileQueryConstraints.php
new file mode 100644 (file)
index 0000000..3f59a87
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+namespace DataAccess\Queries\StepMania;
+
+use DataAccess\Queries\IQueryConstraints;
+
+interface ISimfileQueryConstraints extends IQueryConstraints
+{
+    public function hasFgChanges($bool);
+    public function hasBgChanges($bool);
+    public function stepsHaveRating($rating);
+    public function hasDifficulty($difficulty);
+}
\ No newline at end of file
diff --git a/DataAccess/Queries/StepMania/SimfileQueryConstraints.php b/DataAccess/Queries/StepMania/SimfileQueryConstraints.php
new file mode 100644 (file)
index 0000000..1876634
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+namespace DataAccess\Queries\StepMania;
+
+use DataAccess\Queries\QueryConstraints;
+use DataAccess\Queries\StepMania\ISimfileQueryConstraints;
+
+class SimfileQueryConstraints extends QueryConstraints implements ISimfileQueryConstraints
+{        
+    public function hasFgChanges($bool)
+    {
+        return $this->where('fg_changes', '=', (int)$bool);
+    }
+    public function hasBgChanges($bool)
+    {
+        return $this->where('bg_changes', '=', (int)$bool);
+    }
+    
+    public function stepsHaveRating($rating)
+    {
+        return $this->join('INNER', 'simfiles', 'id', 'steps', 'simfile_id')
+                    ->where('steps.rating', '=', $rating);
+    }
+    public function hasDifficulty($difficulty){}
+    
+    public function bpm($bpm)
+    {
+        return;
+    }
+}
index eea84c5..940bcc0 100644 (file)
@@ -3,11 +3,12 @@
 namespace DataAccess\StepMania;
 
 use DataAccess\IRepository;
+use DataAccess\Queries\StepMania\ISimfileQueryConstraints;
 use Domain\Entities\StepMania\ISimfile;
 
 interface ISimfileRepository extends IRepository
 {
-    public function findByTitle($title);
+    public function findByTitle($title, ISimfileQueryConstraints $constraints);
     public function findByArtist($artist);
     public function findByBpm($high, $low);
     public function findByStepArtist($artistName);
index 7e1a8fa..02d7c29 100644 (file)
@@ -4,32 +4,72 @@ namespace DataAccess\StepMania;
 \r
 use DataAccess\StepMania\ISimfileRepository;\r
 use DataAccess\DataMapper\IDataMapper;\r
+use DataAccess\Queries\StepMania\ISimfileQueryConstraints;\r
 use Domain\Entities\StepMania\ISimfile;\r
 \r
 //TODO: Implement some sort of caching. Probably OK for now not to worry.\r
 class SimfileRepository implements ISimfileRepository\r
 {\r
-    private $dataMapper;\r
+    private $_dataMapper;\r
     \r
     public function __construct(IDataMapper $dataMapper) {\r
-        $this->dataMapper = $dataMapper;\r
+        $this->_dataMapper = $dataMapper;\r
     }\r
     \r
     public function findById($id) {\r
-        return $this->dataMapper->findById($id, 'Simfile');\r
+        return $this->_dataMapper->map(\r
+            'Simfile',\r
+            'SELECT * FROM %s WHERE id=' . $id\r
+        );\r
     }\r
     \r
     public function findRange($id, $limit)\r
     {\r
-        return $this->dataMapper->findRange($id, 'Simfile', $limit);\r
+        return $this->_dataMapper->findRange(\r
+            'Simfile',\r
+            'SELECT * FROM %s WHERE id>=' . $id . ' LIMIT ' . $limit\r
+        );\r
     }\r
     \r
     public function save(ISimfile $entity) {\r
-        $this->dataMapper->save($entity);\r
+        $this->_dataMapper->save($entity);\r
     }\r
     \r
     //TODO: Implement\r
     public function remove(ISimfile $entity) {\r
         ;\r
     }\r
+    \r
+    public function findByTitle($title, ISimfileQueryConstraints $constraints = NULL)\r
+    {\r
+        //TODO: Should I inject a factory, and then make $constraints if it isn't given?\r
+        if($constraints)\r
+        {\r
+            $queryString = $constraints->where('title', 'LIKE', "%%$title%%") //TODO: Should I make a like method that handles adding the %% ?\r
+                                       ->applyTo('SELECT * from %s');\r
+        } else {\r
+            //It would avoid this, or rather I could put this in the constraints class\r
+            $queryString = "SELECT * FROM %s WHERE title LIKE '%$title%'";\r
+        }\r
+        \r
+        //is it better to pass in constraints object?\r
+        //could have a default "select * from %s" in the constraints object which could be overwritten via a method.\r
+        //-no more need for applyTo, just go $constratints->getQuery\r
+        //maybe it should no longer be constraints but instead queryBuilder\r
+        \r
+        /**\r
+         * have this class contain a queryBuilderFactory and then have constraintsClass \r
+         * go in through methods which act on the query, adding in constraints.\r
+         */\r
+        return $this->_dataMapper->map('Simfile', $queryString);\r
+    }\r
+    \r
+    public function findByArtist($artist){}\r
+    public function findByBpm($high, $low){}\r
+    public function findByStepArtist($artistName){}\r
+    public function findByLightMeter($feet){}\r
+    public function findByBeginnerMeter($feet){}\r
+    public function findByMediumMeter($feet){}\r
+    public function findByHardMeter($feet){}\r
+    public function findByExpertMeter($feet){}\r
 }\r
index 1f6b8a1..0118b64 100644 (file)
@@ -15,7 +15,7 @@ $controllerName= $router->getControllerName();
 $controllerAction = $router->getActionName();\r
 $controllerActionArgs = $router->getActionArgs();\r
 \r
-$controller = $container->get('Controllers\\' . $controllerName . 'Controller' );\r
+$controller = $container->get('Controllers\\' . ucfirst($controllerName) . 'Controller' );\r
 \r
 // Last thing to do, call the action on the specified controller.\r
 call_user_func(array($controller, $controllerAction . 'Action'), $controllerActionArgs);\r