From 0e09b4dbea9402cf2a568ec4abe3eadecea93869 Mon Sep 17 00:00:00 2001
From: Martin Stjernholm <mast@lysator.liu.se>
Date: Sun, 10 Jun 2012 02:33:27 +0200
Subject: [PATCH] Handle null objects correctly in emulate_bindings.

Note that fixing this opens up a bug compat issue, described in
comment #7 in [bug 5900].
---
 lib/modules/Sql.pmod/sql_util.pmod | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/lib/modules/Sql.pmod/sql_util.pmod b/lib/modules/Sql.pmod/sql_util.pmod
index 23fa663f9e..0b704ea227 100644
--- a/lib/modules/Sql.pmod/sql_util.pmod
+++ b/lib/modules/Sql.pmod/sql_util.pmod
@@ -47,8 +47,14 @@ string emulate_bindings(string query, mapping(string|int:mixed)|void bindings,
   function my_quote=(driver&&driver->quote?driver->quote:quote);
   v=map(values(bindings),
 	lambda(mixed m) {
-          if(zero_type(m))
-            return "NULL";
+	  if(zero_type(m))
+	    return "NULL";
+	  if (objectp (m) && m->is_val_null)
+	    // Note: Could need bug compatibility here - in some cases
+	    // we might be passed a null object that can be cast to
+	    // "", and before this it would be. This is an observed
+	    // compat issue in comment #7 in [bug 5900].
+	    return "NULL";
 	  if(multisetp(m))
 	    return sizeof(m) ? indices(m)[0] : "";
 	  return "'"+(intp(m)?(string)m:my_quote((string)m))+"'";
-- 
GitLab