public
Description: The ultra-lightweight ultra-flexible blogging engine with a fetish for birds and misspellings.
Homepage: http://chyrp.net/
Clone URL: git://github.com/vito/chyrp.git
Click here to lend your support to: chyrp and make a donation at www.pledgie.com !
* More SQL interfacing improvements
  * Use condition building for update/insert queries
  * $tables arg is now optional in QueryBuilder::build_conditions
vito (author)
Mon Sep 01 10:39:40 -0700 2008
commit  5563f88b0bc792af119d65e58943ccc949705f19
tree    41b198d7cce6475afd6ff99065cd5555a2b527ed
parent  0ab14775db15b0805c1f2192b79edcc6247ad196
...
8
9
10
11
12
13
14
15
16
 
 
17
18
19
...
71
72
73
74
 
75
76
77
...
185
186
187
188
 
189
190
191
192
193
194
195
196
197
198
199
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
 
 
 
218
 
 
 
 
 
 
 
 
 
219
220
221
222
223
 
 
 
224
225
226
...
8
9
10
 
 
 
 
 
 
11
12
13
14
15
...
67
68
69
 
70
71
72
73
...
181
182
183
 
184
185
186
187
188
 
 
 
 
 
 
 
 
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
 
223
224
225
226
227
228
0
@@ -8,12 +8,8 @@
0
      * Function: build_update_values
0
      * Creates an update data part.
0
      */
0
-    public static function build_update_values($data) {
0
-      $set = array();
0
-
0
-      foreach ($data as $field => $val)
0
-        array_push($set, "$field = $val");
0
-
0
+    public static function build_update_values($data, &$params = array()) {
0
+      $set = self::build_conditions($data, $params);
0
       return implode(", ", $set);
0
     }
0
 
0
@@ -71,7 +67,7 @@
0
     public static function build_update($table, $conds, $data, &$params = array()) {
0
       return "
0
         UPDATE __$table
0
-        SET ".self::build_update_values($data)."
0
+        SET ".self::build_update_values($data, $params)."
0
         ".($conds ? "WHERE ".self::build_where($conds, $table, $params) : "")."
0
       ";
0
     }
0
@@ -185,42 +181,48 @@
0
       return implode(", ", $order);
0
     }
0
 
0
-    public static function build_conditions($conds, &$params, $tables) {
0
+    public static function build_conditions($conds, &$params, $tables = null) {
0
       foreach ($conds as $key => $val) {
0
         if (is_numeric($key)) # Full expression
0
           $cond = $val;
0
         else { # Key => Val expression
0
-          if (substr($key, -4) == " not") { # Negation
0
-            $key = substr($key, 0, -4);
0
-            if (is_array($val))
0
-              $cond = $key." NOT IN ".self::build_in($val);
0
-            elseif ($val === null)
0
-              $cond = $key." IS NOT NULL";
0
-            else {
0
-              $cond = $key." != :".$key;
0
+          if (is_string($val) and $val[0] == ":")
0
+            $cond = $key." = ".$val;
0
+          else {
0
+            if (substr($key, -4) == " not") { # Negation
0
+              $key = substr($key, 0, -4);
0
+              if (is_array($val))
0
+                $cond = $key." NOT IN ".self::build_in($val);
0
+              elseif ($val === null)
0
+                $cond = $key." IS NOT NULL";
0
+              else {
0
+                $cond = $key." != :".$key;
0
+                $params[":".$key] = $val;
0
+              }
0
+            } elseif (substr($key, -5) == " like") { # LIKE
0
+              $key = substr($key, 0, -5);
0
+              $cond = $key." LIKE :".$key;
0
               $params[":".$key] = $val;
0
-            }
0
-          } elseif (substr($key, -5) == " like") { # LIKE
0
-            $key = substr($key, 0, -5);
0
-            $cond = $key." LIKE :".$key;
0
-            $params[":".$key] = $val;
0
-          } elseif (substr($key, -9) == " not like") { # NOT LIKE
0
-            $key = substr($key, 0, -9);
0
-            $cond = $key." NOT LIKE :".$key;
0
-            $params[":".$key] = $val;
0
-          } else { # Equation
0
-            if (is_array($val))
0
-              $cond = $key." IN ".self::build_in($val);
0
-            elseif ($val === null)
0
-              $cond = $key." IS NULL";
0
-            else {
0
-              $cond = $key." = :".$key;
0
+            } elseif (substr($key, -9) == " not like") { # NOT LIKE
0
+              $key = substr($key, 0, -9);
0
+              $cond = $key." NOT LIKE :".$key;
0
               $params[":".$key] = $val;
0
+            } else { # Equation
0
+              if (is_array($val))
0
+                $cond = $key." IN ".self::build_in($val);
0
+              elseif ($val === null)
0
+                $cond = $key." IS NULL";
0
+              else {
0
+                $cond = $key." = :".$key;
0
+                $params[":".$key] = $val;
0
+              }
0
             }
0
           }
0
         }
0
 
0
-        self::tablefy($cond, $tables);
0
+        if ($tables)
0
+          self::tablefy($cond, $tables);
0
+
0
         $conditions[] = $cond;
0
       }
0
 
...
314
315
316
317
318
319
320
321
322
 
 
 
323
324
325
...
314
315
316
 
 
 
 
 
 
317
318
319
320
321
322
0
@@ -314,12 +314,9 @@
0
           $sql->delete("tags", array("id" => $tag["id"]));
0
         else
0
           $sql->update("tags",
0
-                       "id = :id",
0
-                       array("tags" => ":tags",
0
-                             "clean" => ":clean"),
0
-                       array(":id" => $tag["id"],
0
-                             ":tags" => join(",", $names),
0
-                             ":clean" => join(",", $cleans)));
0
+                       array("id" => $tag["id"]),
0
+                       array("tags" => join(",", $names),
0
+                             "clean" => join(",", $cleans)));
0
       }
0
 
0
       Flash::notice(__("Tag deleted.", "tags"), "/admin/?action=manage_tags");

Comments