Browse Source

[spod] Search non-const SEnum. Closes #1199

Cauê Waneck 10 years ago
parent
commit
a0f9dfff68
2 changed files with 58 additions and 2 deletions
  1. 7 2
      std/sys/db/RecordMacros.hx
  2. 51 0
      tests/unit/src/unit/TestSpod.hx

+ 7 - 2
std/sys/db/RecordMacros.hx

@@ -574,7 +574,6 @@ class RecordMacros {
 									if( c == null ) {
 										if( n == "null" )
 											return { sql : sqlAddString(r1.sql, eq ? " IS NULL" : " IS NOT NULL"), t : DBool, n : false };
-										error("Unknown constructor " + n, e2.pos);
 									} else {
 										return { sql : makeOp(eq?" = ":" != ", r1.sql, { expr : EConst(CInt(Std.string(c.index))), pos : e2.pos }, pos), t : DBool, n : r1.n };
 									}
@@ -585,7 +584,13 @@ class RecordMacros {
 						default:
 						}
 						if( !ok )
-							error("Should be a constant constructor", e2.pos);
+						{
+							var epath = e.split('.');
+							var ename = epath.pop();
+							var etype = TPath({ name:ename, pack:epath });
+							var expr = macro std.Type.enumIndex( @:pos(e2.pos) ( $e2 : $etype ) ); //make sure we have the correct type
+							return { sql: makeOp(eq?" = ":" != ", r1.sql, expr, pos), t : DBool, n : r1.n };
+						}
 					default:
 					}
 				}

+ 51 - 0
tests/unit/src/unit/TestSpod.hx

@@ -54,6 +54,57 @@ class TestSpod extends Test
 		return scls;
 	}
 
+	public function testEnum()
+	{
+		setManager();
+		var c1 = new OtherSpodClass("first spod");
+		c1.insert();
+		var c2 = new OtherSpodClass("second spod");
+		c2.insert();
+
+		var scls = getDefaultClass();
+		scls.relation = c1;
+		scls.insert();
+		var id1 = scls.theId;
+		scls = getDefaultClass();
+		scls.relation = c1;
+		scls.insert();
+		var id2 = scls.theId;
+		scls = getDefaultClass();
+		scls.relation = c1;
+		scls.anEnum = FirstValue;
+		scls.insert();
+		var id3 = scls.theId;
+		scls = null;
+
+		Manager.cleanup();
+		var r1s = [ for (c in MySpodClass.manager.search($anEnum == SecondValue,{orderBy:theId})) c.theId ];
+		eq([id1,id2].join(','),r1s.join(','));
+		var r2s = MySpodClass.manager.search($anEnum == FirstValue);
+		eq(r2s.length,1);
+		eq(r2s.first().theId,id3);
+
+		var fv = getSecond();
+		var r1s = [ for (c in MySpodClass.manager.search($anEnum == fv,{orderBy:theId})) c.theId ];
+		eq([id1,id2].join(','),r1s.join(','));
+		var r2s = MySpodClass.manager.search($anEnum == getFirst());
+		eq(r2s.length,1);
+		eq(r2s.first().theId,id3);
+
+		r2s.first().delete();
+		for (v in MySpodClass.manager.search($anEnum == fv)) v.delete();
+	}
+
+	public function getFirst()
+	{
+		return FirstValue;
+	}
+
+	public function getSecond()
+	{
+		return SecondValue;
+	}
+
 	public function testUpdate()
 	{
 		setManager();