0008914 - Abstraction to improve queries
authorFlávio Gomes da Silva Lisboa <flavio.lisboa@serpro.gov.br>
Thu, 12 Sep 2013 16:31:14 +0000 (13:31 -0300)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 19 Sep 2013 08:12:21 +0000 (10:12 +0200)
Change-Id: I6af2a5b1ca3bb1dff658cacbaa8369dc4ee7793c
Reviewed-on: https://gerrit.tine20.org/tine20/2323
Tested-by: jenkins user
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Tinebase/Backend/Sql/Command.php
tine20/Tinebase/Backend/Sql/Factory/Abstract.php [new file with mode: 0644]
tine20/Tinebase/Backend/Sql/Query/Abstract.php [new file with mode: 0644]

index b024e92..12ac62b 100755 (executable)
  * @package     Tinebase
  * @subpackage  Backend
  */
-class Tinebase_Backend_Sql_Command
+class Tinebase_Backend_Sql_Command extends Tinebase_Backend_Sql_Factory_Abstract
 {
-    protected static $_instances = array();
-    
-    /**
-     * @param Zend_Db_Adapter_Abstract $adapter
-     * @return Tinebase_Backend_Sql_Command_Interface
-     */
-    public static function factory(Zend_Db_Adapter_Abstract $adapter)
-    {
-        $className = __CLASS__ . '_' . self::_getClassName($adapter);\r
-        
-        // @todo find better array key (add loginname and host)
-        if (!isset(self::$_instances[$className])) {
-            self::$_instances[$className] = new $className($adapter);
-        }
-        
-        return self::$_instances[$className];\r
-    }
-    
-    /**
-     *
-     * @param Zend_Db_Adapter_Abstract $adapter
-     * @return string
-     */
-    private static function _getClassName($adapter)
-    {
-        $completeClassName = explode('_',get_class($adapter));
-        $className = $completeClassName[count($completeClassName)-1];
-        $className = str_replace('Oci','Oracle',$className);
-        
-        return $className;
-    }
 }
diff --git a/tine20/Tinebase/Backend/Sql/Factory/Abstract.php b/tine20/Tinebase/Backend/Sql/Factory/Abstract.php
new file mode 100644 (file)
index 0000000..7816c0a
--- /dev/null
@@ -0,0 +1,61 @@
+<?php
+/**
+ * Tine 2.0
+ *
+ * @package     Tinebase
+ * @subpackage  Backend
+ * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @author      Flávio Gomes da Silva Lisboa <flavio.lisboa@serpro.gov.br>
+ * @copyright   Copyright (c) 2013 Metaways Infosystems GmbH (http://www.metaways.de)
+ */
+
+/**
+ * Abstract factory for customized SQL statements
+ *
+ * @package     Tinebase
+ * @subpackage  Backend
+ */
+
+class Tinebase_Backend_Sql_Factory_Abstract 
+{
+    protected static $_instances = array();
+     
+    /**
+     * @param Zend_Db_Adapter_Abstract $adapter
+     * @return mixed
+    */
+    public static function factory(Zend_Db_Adapter_Abstract $adapter)
+    {
+        $className = get_called_class() . '_' . self::_getClassName($adapter);
+         
+        // @todo find better array key (add loginname and host)
+        if (!isset(self::$_instances[$className])) {
+            self::$_instances[$className] = new $className($adapter);
+        }
+         
+        return self::$_instances[$className];
+    }
+     
+    /**
+     *
+     * @return Zend_Db_Adapter_Abstract
+     */
+    public static function getDb()
+    {
+        return self::$_db;
+    }
+     
+    /**
+     *
+     * @param Zend_Db_Adapter_Abstract $adapter
+     * @return string
+     */
+    private static function _getClassName($adapter)
+    {
+        $completeClassName = explode('_',get_class($adapter));
+        $className = $completeClassName[count($completeClassName)-1];
+        $className = str_replace('Oci','Oracle',$className);
+         
+        return $className;
+    }    
+}
diff --git a/tine20/Tinebase/Backend/Sql/Query/Abstract.php b/tine20/Tinebase/Backend/Sql/Query/Abstract.php
new file mode 100644 (file)
index 0000000..aadd850
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+/**    
+ * Generalization for customized queries
+ *    
+ * @package Tinebase
+ * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @author Flávio Gomes da Silva Lisboa <flavio.lisboa@serpro.gov.br>
+ * @copyright Copyright (c) 2013 Metaways Infosystems GmbH (http://www.metaways.de)
+ */
+
+ /**
+ * Generalization for customized queries
+ * 
+ * How to use:
+ * 
+ * 1) Create a class in application folder following this pattern:
+ * 
+ * [Application]_Backend_Sql_Query extends Tinebase_Backend_Sql_Factory_Abstract
+ * 
+ * This class needs no code.
+ * 
+ * 2) Create a interface [Application]_Backend_Sql_Query_Interface with methods for creating and executing queries.
+ *
+ * 3) Create a class [Application]_Backend_Sql_Query_Abstract that extends [Application]_Backend_Sql_Query_Interface 
+ * 
+ * This class keeps the building of queries of same way, it's the default behavior.
+ *  
+ * 4) Create a class for each database Adapter: 
+ * [Application]_Backend_Sql_Query_Mysql
+ * [Application]_Backend_Sql_Query_Pgsql
+ * [Application]_Backend_Sql_Query_Oracle
+ * 
+ * If the adapter keeps the former query building, it extends [Application]_Backend_Sql_Query_Abstract
+ * If the adapter customizes the query, it only implements [Application]_Backend_Sql_Query_Interface 
+ * 
+ * The use of a generic query created with Zend_Db_Select must be replaced for call of method of one of these adapters.
+ * Instead of:
+ * 
+ * ...
+ * several lines of code with a generic query
+ * ...
+ * 
+ * you should use:
+ * 
+ * [Application]_Backend_Sql_Query::factory($this->_db)->[method]()
+ * 
+ * That works for every database supported, but can executes an optimized query for one determined database  
+ *
+ * @package Tinebase
+ */
+class Tinebase_Backend_Sql_Query_Abstract 
+{
+    /**
+     *
+     * @var Zend_Db_Adapter_Abstract
+     */
+    protected $_db;
+    /**
+     *
+     * @param Zend_Db_Adapter_Abstract $db
+     */
+    public function __construct(Zend_Db_Adapter_Abstract $db)
+    {
+        $this->_db = $db;
+    }    
+}