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

ork at devel.plogworld.net ork at devel.plogworld.net
Tue May 31 13:54:29 GMT 2005


Author: ork
Date: 2005-05-31 13:54:29 +0000 (Tue, 31 May 2005)
New Revision: 2135

Modified:
   plog/branches/plog-1.1-ben/class/database/db.class.php
Log:
further tweaked the Db::build(Insert|Update|..)Query methods


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-31 13:29:45 UTC (rev 2134)
+++ plog/branches/plog-1.1-ben/class/database/db.class.php	2005-05-31 13:54:29 UTC (rev 2135)
@@ -100,22 +100,15 @@
                 $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 .= Db::_buildOrderCondition( $orderColumn );
             }
 
+            if( $whereColumn != null ) {
+                $query .= Db::buildWhereConditions( $whereColumn, $whereValue, $whereGlue );
+            }
+
             if( $limit != null ) {
                 $query .= ' LIMIT ' . $limit;
             }
@@ -144,6 +137,45 @@
             return $query;
         }
 
+        function buildUpdateQuery( $tableName,
+                                   $keyValuePairs,
+                                   $whereColumn   = null, 
+                                   $whereValue    = null,
+                                   $whereGlue     = ' AND ')
+        {
+            $tableName = Db::addTablePrefixToTableName( $tableName );
+
+            $query  = 'UPDATE ' . $tableName . ' SET ';
+
+            $valuesToSet = array();
+            foreach( $keyValuePairs as $key => $value ) {
+                $queryPart  = $key . ' = ';
+                $queryPart .= Db::quoteValue( $value );
+                $valuesToSet[] = $queryPart;
+            }
+            $query .= implode( ",", $valuesToSet );
+            
+            if( $whereColumn != null )
+                $query .= Db::buildWhereConditions( $whereColumn, $whereValue, $whereGlue );
+
+            return $query;
+        }
+
+        function buildDeleteQuery( $tableName,
+                                   $whereColumn = null,
+                                   $whereValue  = null,
+                                   $whereGlue   = ' AND ')
+        {
+            $tableName = Db::addTablePrefixToTableName( $tableName );
+
+            $query  = 'DELETE FROM ' . $tableName;
+
+            if( $whereColumn != null )
+                $query .= Db::buildWhereConditions( $whereColumn, $whereValue, $whereGlue );
+
+            return $query;
+        }
+
         function addTablePrefixToTableName( $tableName ) {
             if ( !preg_match( "/^" . Db::getPrefix() . "/", $tableName) ) {
                 $tableName = Db::getPrefix() . $tableName;
@@ -170,8 +202,24 @@
         }
 
 
-        function _buildWhereCondition( $columnName, $columnValue )
+        function buildWhereConditions( $whereColumn, $whereValue, $whereGlue )
         {
+            $queryPart = ' WHERE ';
+            if( is_array($whereColumn) ) {
+                $conditions = array();
+                foreach( $whereColumn as $column => $value ) {
+                    $conditions[] = Db::buildWhereCondition( $column, $value );
+                }
+                $queryPart .= implode( $whereGlue , $conditions );
+            } elseif( !empty($whereColumn) ) {
+                $queryPart .= Db::buildWhereCondition( $whereColumn, $whereValue );
+            }
+
+            return $queryPart;
+        }
+
+        function buildWhereCondition( $columnName, $columnValue )
+        {
             preg_match( '/^(.)(.*)$/', $columnValue, $matches );
             $firstValueCharacter = $matches[1];
             switch( $firstValueCharacter ) {
@@ -194,10 +242,29 @@
             return $queryPart;
         }
 
+        /**
+         * Quote a value for a SQL query. Depending on the type of the
+         * value, this method will return the correct string to store
+         * the value in the database.
+         *
+         * - if the value is numeric, it will return the numeric value
+         * - if the value is a Timestamp(), it will return an isoDate
+         * - if the value is a string, it will return a quoted string
+         * - if the value starts with an '@', it will return the
+         *   unquoted value. this is used e.g. for adding something
+         *   like "date = date" in Articles()->deleteArticle().
+         *
+         * @param value a value to be stored in the database
+         * @return string the correctly quoted value
+         * @access public
+         */
+
         function quoteValue( $value )
         {
             if( is_numeric($value) )
                 return $value;
+            if( preg_match('/^@(.*)/', $value, $matches) )
+                return $matches[1];
             elseif( is_object($value) ) {
                 switch (get_class($value)) {
                     case 'timestamp':




More information about the pLog-svn mailing list