Browse Source

moved usage of null-safe comparison operator from compile-time to runtime (fixed issue #606)

Nicolas Cannasse 13 years ago
parent
commit
0672aaa78d
2 changed files with 13 additions and 6 deletions
  1. 9 0
      std/sys/db/Manager.hx
  2. 4 6
      std/sys/db/SpodMacros.hx

+ 9 - 0
std/sys/db/Manager.hx

@@ -387,6 +387,15 @@ class Manager<T : Object> {
 		return o == null ? null : Reflect.field(o, table_keys[0]);
 	}
 
+	public static function nullCompare( a : String, b : String, eq : Bool ) {
+		// we can't use a null-safe operator here
+		if( cnx.dbName() != "MySQL" )
+			return a + (eq ? " = " : " != ") + b;
+		var sql = a+" <=> "+b;
+		if( !eq ) sql = "NOT("+sql+")";
+		return sql;
+	}
+
 	function addCondition(s : StringBuf,x) {
 		var first = true;
 		if( x != null )

+ 4 - 6
std/sys/db/SpodMacros.hx

@@ -533,11 +533,9 @@ class SpodMacros {
 		}
 		var sql;
 		// use some different operators if there is a possibility for comparing two NULLs
-		if( r1.n || r2.n ) {
-			sql = makeOp(" <=> ", r1.sql, r2.sql, pos);
-			if( !eq )
-				sql = sqlAdd(makeString("NOT(", pos), sqlAddString(sql, ")"), pos);
-		} else
+		if( r1.n || r2.n )
+			sql = { expr : ECall({ expr : EField(manager,"nullCompare"), pos : pos },[r1.sql,r2.sql,{ expr : EConst(CIdent(eq?"true":"false")), pos : pos }]), pos : pos };
+		else
 			sql = makeOp(eq?" = ":" != ", r1.sql, r2.sql, pos);
 		return { sql : sql, t : DBool, n : r1.n || r2.n };
 	}
@@ -691,7 +689,7 @@ class SpodMacros {
 			case CString(s): return { sql : sqlQuoteValue(cond, DText), t : DString(s.length), n : false };
 			case CRegexp(_): error("Unsupported", p);
 			#if haxe3
-			case CIdent(n): 
+			case CIdent(n):
 			#else
 			case CIdent(n), CType(n):
 			#end