Index: includes/handlers/geo_sql.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/geo/includes/handlers/geo_sql.inc,v
retrieving revision 1.10
diff -u -p -r1.10 geo_sql.inc
--- includes/handlers/geo_sql.inc	27 Apr 2010 02:18:22 -0000	1.10
+++ includes/handlers/geo_sql.inc	23 Aug 2010 15:00:47 -0000
@@ -71,32 +71,65 @@ class GeoSQL implements GeoInterface {
    * Take a sql query as returned by a function and expand it.
    */
   function sql($sql, $args = array()) {
-    if (is_array($args)) {
-      foreach ($args as $i => $g) {
-        $srid = GEO_SRID_DEFAULT;
-        $wkt = NULL;
-        if (is_object($g)) {
-
-          // If the backend is the same as this one, query the column directly.
-          if ($g->geoCompatible($this)) {
-            $sql = preg_replace('/\%g/', $g->sqlTableAlias(), $sql, 1);
-          }
+    if (!is_array($args)) {
+      return $sql;
+    }
 
-          // Otherwise, get the WKT representation and call GeomFomText on it.
-          else {
-            $wkt = GeoSimple::asText($g);
-          }
-        }
-        elseif(is_string($g)) {
-          $wkt = $g;
-        }
-        if (isset($wkt)) {
-          $sql = preg_replace('/\%g/', "GeomFromText('$wkt', $srid)", $sql, 1);
+    $args = array_values($args);
+
+    $output_sql = '';
+    foreach (preg_split('/%./', $sql, -1, PREG_SPLIT_OFFSET_CAPTURE) as $match_idx => $match) {
+      list($match_string, $match_offset) = $match;
+      if ($match_offset) {
+        $g = $args[$match_idx-1];
+        $format_spec=substr($sql, $match_offset-1, 1);
+        switch ($format_spec) {
+          case 'g':
+            $srid = GEO_SRID_DEFAULT;
+            $wkt = NULL;
+            if (is_object($g)) {
+
+              // If the backend is the same as this one, query the column directly.
+              if ($g->geoCompatible($this)) {
+                $output_sql .= $g->sqlTableAlias();
+              }
+
+              // Otherwise, get the WKT representation and call GeomFomText on it.
+              else {
+                $wkt = GeoSimple::asText($g);
+              }
+            }
+            elseif(is_string($g)) {
+              $wkt = $g;
+            }
+            if (isset($wkt)) {
+              $output_sql .= "GeomFromText('$wkt', $srid)";
+            }
+            break;
+
+          case 'b':
+            $output_sql .= db_encode_blob($g);
+            break;
+
+          case 's':
+            $output_sql .= $g;
+            break;
+
+          case 'd':
+            $output_sql .= (int)$g;
+            break;
+
+          case 'f':
+            $output_sql .= (float)$g;
+            break;
+
+          default:
+            $output_sql .= $format_spec;
         }
       }
+      $output_sql .= $match_string;
     }
-
-    return $sql;
+    return $output_sql;
   }
 
   function sqlTableAlias() {
