[pLog-svn] r2096 - plog/branches/plog-1.1-ben/class/database

ork at devel.plogworld.net ork at devel.plogworld.net
Sun May 29 19:15:02 GMT 2005


Author: ork
Date: 2005-05-29 19:15:02 +0000 (Sun, 29 May 2005)
New Revision: 2096

Modified:
   plog/branches/plog-1.1-ben/class/database/db.class.php
Log:
added some generic functions to build queries, where clauses and to quote values


Modified: plog/branches/plog-1.1-ben/class/database/db.class.php
===================================================================
--- plog/branches/plog-1.1-ben/class/database/db.class.php	2005-05-29 16:00:20 UTC (rev 2095)
+++ plog/branches/plog-1.1-ben/class/database/db.class.php	2005-05-29 19:15:02 UTC (rev 2096)
@@ -105,5 +105,65 @@
 
 			return $string;
 		}
-	}
+
+        function buildSelectQuery( $tableName, 
+                                   $fieldsToFetch = array(),
+                                   $whereColumn   = null, 
+                                   $whereValue    = null,
+                                   $orderColumn   = null,
+                                   $whereGlue     = ' AND ')
+        {
+            if ( !preg_match( "/^" . Db::getPrefix() . "/", $tableName) ) {
+                $tableName = Db::getPrefix() . $tableName;
+            }
+
+            $query = 'SELECT ';
+            if( $fieldsToFetch == array() ) {
+                $query .= '*';
+            } else {
+                $query .= implode(",", $fieldsToFetch);
+            }
+            $query .= ' FROM ' . $tableName;
+            if( is_array($whereColumn) ) {
+                $query .= ' WHERE ';
+                $conditions = array();
+                foreach( $whereColumn as $column => $value ) {
+                    $conditions[] = Db::_buildWhereCondition( $column, $value );
+                }
+                $query .= implode( $whereGlue , $conditions );
+            } elseif( !empty($whereColumn) ) {
+                $query .= ' WHERE ';
+                $query .= Db::_buildWhereCondition( $whereColumn, $whereValue );
+            }
+
+            if( $orderColumn != null ) {
+                $query .= ' ORDER BY ' . $orderColumn;
+            }
+            $query .= ';';
+
+            return $query;
+        }
+
+        function _buildWhereCondition( $columnName, $columnValue )
+        {
+            $queryPart  = $columnName . ' = ';
+            $queryPart .= Db::quoteValue( $columnValue );
+            return $queryPart;
+        }
+
+        function quoteValue( $value )
+        {
+            if( is_numeric($value) )
+                return $value;
+            elseif( is_object($value) ) {
+                switch (get_class($value)) {
+                    case 'timestamp':
+                        return Db::quoteValue($value->getIsoDate());
+                        break;
+                }
+            }
+            else
+                return '\'' . Db::qstr( $value ) . '\'';
+        }
+    }
 ?>




More information about the pLog-svn mailing list