[pLog-svn] r2204 - in plog/trunk/class/database: . pdb/drivers

ork at devel.plogworld.net ork at devel.plogworld.net
Sat Jun 11 22:18:49 GMT 2005


Author: ork
Date: 2005-06-11 22:18:48 +0000 (Sat, 11 Jun 2005)
New Revision: 2204

Modified:
   plog/trunk/class/database/db.class.php
   plog/trunk/class/database/pdb/drivers/pdbdriverbase.class.php
Log:
i moved the buildSelectQuery and the buildWhereCondition methods to oscars new PDB driver. 
i'm not sure if this is the best way to do it, but if it's defined at the driver, each driver can override the methods if one db build a query in a different way than most dbs will do.
i moved just these two methods to get your feedback. i think its good to move all buildXxxxQuery methods to the db drivers, what do you think?



Modified: plog/trunk/class/database/db.class.php
===================================================================
--- plog/trunk/class/database/db.class.php	2005-06-11 22:13:18 UTC (rev 2203)
+++ plog/trunk/class/database/db.class.php	2005-06-11 22:18:48 UTC (rev 2204)
@@ -118,30 +118,21 @@
                                    $limit         = null,
                                    $whereGlue     = ' AND ')
         {
+            $db = Db::getDb();
+
             $tableName = Db::addTablePrefixToTableName( $tableName );
 
-            $query = 'SELECT ';
-            if( $fieldsToFetch == array() ) {
-                $query .= '*';
-            } else {
-                $query .= implode(",", $fieldsToFetch);
-            }
-            $query .= ' FROM ' . $tableName;
+            $query = $db->buildSelectQuery( $tableName,
+                                            $fieldsToFetch,
+                                            $whereColumn,
+                                            $whereValue,
+                                            $orderColumn,
+                                            $limit,
+                                            $whereGlue );
 
-            if( $orderColumn != null ) {
-                $query .= Db::_buildOrderCondition( $orderColumn );
-            }
+            if( DEBUG_ENABLED && DEBUG_CHANNELS & DEBUG_CHANNEL_SQL )
+                $this->debug->log("SQL Query build: $query", LOGGER_PRIO_INFO );
 
-            if( $whereColumn != null ) {
-                $query .= Db::buildWhereConditions( $whereColumn, $whereValue, $whereGlue );
-            }
-
-            if( $limit != null ) {
-                $query .= ' LIMIT ' . $limit;
-            }
-
-            $query .= ';';
-
             return $query;
         }
 
@@ -210,25 +201,7 @@
             return $tableName;
         }
 
-        function _buildOrderCondition( $orderColumn )
-        {
-            preg_match( '/^(.)(.*)$/', $orderColumn, $matches );
 
-            $queryPart = ' ORDER BY '; 
-            switch( $firstValueCharacter ) {
-                case '-':
-                    $orderColumn = $matches[2];
-                    $direction = ' DESC';
-                case '+':
-                    $orderColumn = $matches[2];
-                default:
-                    $direction  = '';
-                    $queryPart .= $orderColumn . $direction;
-            }
-            return $queryPart;
-        }
-
-
         function buildWhereConditions( $whereColumn, $whereValue, $whereGlue )
         {
             $queryPart = ' WHERE ';
@@ -247,25 +220,25 @@
 
         function buildWhereCondition( $columnName, $columnValue )
         {
-            preg_match( '/^(.)(.*)$/', $columnValue, $matches );
-            $firstValueCharacter = $matches[1];
+            $db = Db::getDb();
+            return $db->buildWhereCondition( $columnName, $columnValue );
+        }
+
+        function _buildOrderCondition( $orderColumn )
+        {
+            preg_match( '/^(.)(.*)$/', $orderColumn, $matches );
+
+            $queryPart = ' ORDER BY '; 
             switch( $firstValueCharacter ) {
-                case '>':
-                    $operator = '>';
-                    $columnValue = $matches[2];
-                    break;
-                case '<':
-                    $operator = '<';
-                    $columnValue = $matches[2];
-                    break;
-                case '=':
-                    $columnValue = $matches[2];
-                    // no break, we want to continue to the default
+                case '-':
+                    $orderColumn = $matches[2];
+                    $direction = ' DESC';
+                case '+':
+                    $orderColumn = $matches[2];
                 default:
-                    $operator = '=';
-                }
-            $queryPart  = $columnName . ' ' . $operator . ' ';
-            $queryPart .= Db::quoteValue( $columnValue );
+                    $direction  = '';
+                    $queryPart .= $orderColumn . $direction;
+            }
             return $queryPart;
         }
 

Modified: plog/trunk/class/database/pdb/drivers/pdbdriverbase.class.php
===================================================================
--- plog/trunk/class/database/pdb/drivers/pdbdriverbase.class.php	2005-06-11 22:13:18 UTC (rev 2203)
+++ plog/trunk/class/database/pdb/drivers/pdbdriverbase.class.php	2005-06-11 22:18:48 UTC (rev 2204)
@@ -99,5 +99,89 @@
 		{
 		    $this->_debug = $debug;
 		}		
+
+
+
+        /**
+         * Start of DB dependent code, be sure to override these methods in
+         * your driver class, if the generic version are not good enough for
+         * your driver.
+         */ 
+
+
+
+        function buildSelectQuery( $tableName,
+                                   $fieldsToFetch,
+                                   $whereColumn,
+                                   $whereValue,
+                                   $orderColumn,
+                                   $limit,
+                                   $whereGlue )
+        {
+            $query = 'SELECT ';
+            if( $fieldsToFetch == array() ) {
+                $query .= '*';
+            } else {
+                $query .= implode(",", $fieldsToFetch);
+            }
+            $query .= ' FROM ' . $tableName;
+
+            if( $orderColumn != null ) {
+                $query .= Db::_buildOrderCondition( $orderColumn );
+            }
+
+            if( $whereColumn != null ) {
+                $query .= Db::buildWhereConditions( $whereColumn, $whereValue, $whereGlue );
+            }
+
+            if( $limit != null ) {
+                $query .= ' LIMIT ' . $limit;
+            }
+
+            $query .= ';';
+
+            return $query;
+        }
+
+
+         
+        /**
+         * Generic method to build where conditions. Column values might start
+         * with a keyword, each db driver may overwrite this method to implement
+         * db specific where conditions.
+         * 
+         * Please allow the following keywords:
+         * ~ - Column Value is a search string, add wildcards at the start
+         *     and the end of the searchstring. Use 'LIKE' or equivalent.
+         * > - Implement a 'greater then' search.
+         * < - Implement a 'less then' search.
+         * = - Equal, 
+         */
+        function buildWhereCondition( $columnName, $columnValue )
+        {
+            preg_match( '/^(.)(.*)$/', $columnValue, $matches );
+            $firstValueCharacter = $matches[1];
+            switch( $firstValueCharacter ) {
+                case '~':
+                    $operator = 'LIKE';
+                    $columnValue = '%' . $matches[2] . '%';
+                case '>':
+                    $operator = '>';
+                    $columnValue = $matches[2];
+                    break;
+                case '<':
+                    $operator = '<';
+                    $columnValue = $matches[2];
+                    break;
+                case '=':
+                    $columnValue = $matches[2];
+                    // no break, we want to continue to the default
+                default:
+                    $operator = '=';
+                }
+            $queryPart  = $columnName . ' ' . $operator . ' ';
+            $queryPart .= Db::quoteValue( $columnValue );
+            return $queryPart;
+        }
 	}
-?>
\ No newline at end of file
+?>




More information about the pLog-svn mailing list