Browse Source

[spod] Fixed ID types equivalence (SInt -> SId);

* Fixed declaration of relations in same module
* Fixed invalid relation type error message
* Closes #3828
Cauê Waneck 10 years ago
parent
commit
7bc43d8536

+ 1 - 0
std/sys/db/RecordInfos.hx

@@ -69,6 +69,7 @@ typedef RecordRelation = {
 	var prop : String;
 	var prop : String;
 	var key : String;
 	var key : String;
 	var type : String;
 	var type : String;
+	var module : String;
 	var cascade : Bool;
 	var cascade : Bool;
 	var lock : Bool;
 	var lock : Bool;
 	var isNull : Bool;
 	var isNull : Bool;

+ 25 - 3
std/sys/db/RecordMacros.hx

@@ -122,8 +122,18 @@ class RecordMacros {
 		#end
 		#end
 	}
 	}
 
 
-	public dynamic function resolveType( name : String ) : haxe.macro.Type {
+	public dynamic function resolveType( name : String, ?module : String ) : haxe.macro.Type {
 		#if macro
 		#if macro
+		if (module != null)
+		{
+			var m = Context.getModule(module);
+			for (t in m)
+			{
+				if (t.toString() == name)
+					return t;
+			}
+		}
+
 		return Context.getType(name);
 		return Context.getType(name);
 		#else
 		#else
 		throw "not implemented";
 		throw "not implemented";
@@ -317,10 +327,12 @@ class RecordMacros {
 						isNull = false;
 						isNull = false;
 						var t = makeRecord(f.type);
 						var t = makeRecord(f.type);
 						if( t == null ) error("Relation type should be a sys.db.Object", f.pos);
 						if( t == null ) error("Relation type should be a sys.db.Object", f.pos);
+						var mod = t.get().module;
 						var r = {
 						var r = {
 							prop : f.name,
 							prop : f.name,
 							key : params.shift().i,
 							key : params.shift().i,
 							type : t.toString(),
 							type : t.toString(),
+							module : mod,
 							cascade : false,
 							cascade : false,
 							lock : false,
 							lock : false,
 							isNull : isNull,
 							isNull : isNull,
@@ -354,6 +366,16 @@ class RecordMacros {
 			default: fi.name == "id";
 			default: fi.name == "id";
 			}
 			}
 			if( isId ) {
 			if( isId ) {
+				switch(fi.t)
+				{
+					case DInt:
+						fi.t = DId;
+					case DUInt:
+						fi.t = DUId;
+					case DBigInt:
+						fi.t = DBigId;
+					case _:
+				}
 				if( i.key == null ) i.key = [fi.name] else error("Multiple table id declaration", f.pos);
 				if( i.key == null ) i.key = [fi.name] else error("Multiple table id declaration", f.pos);
 			}
 			}
 			i.fields.push(fi);
 			i.fields.push(fi);
@@ -363,7 +385,7 @@ class RecordMacros {
 		for( r in i.relations ) {
 		for( r in i.relations ) {
 			var field = fields.find(function(f) return f.name == r.prop);
 			var field = fields.find(function(f) return f.name == r.prop);
 			var f = i.hfields.get(r.key);
 			var f = i.hfields.get(r.key);
-			var relatedInf = getRecordInfos(makeRecord(resolveType(r.type)));
+			var relatedInf = getRecordInfos(makeRecord(resolveType(r.type, r.module)));
 			if (relatedInf.key.length > 1)
 			if (relatedInf.key.length > 1)
 				error('The relation ${r.prop} is invalid: Type ${r.type} has multiple keys, which is not supported',field.pos);
 				error('The relation ${r.prop} is invalid: Type ${r.type} has multiple keys, which is not supported',field.pos);
 			var relatedKey = relatedInf.key[0];
 			var relatedKey = relatedInf.key[0];
@@ -373,7 +395,7 @@ class RecordMacros {
 					case DUId: DUInt;
 					case DUId: DUInt;
 					case DBigId: DBigInt;
 					case DBigId: DBigInt;
 					case t = DString(_): t;
 					case t = DString(_): t;
-					case t: error("Unexpected id type $t for the relation. Use either SId, SInt, SUId, SUInt, SBigID, SBigInt or SString", field.pos);
+					case t: error('Unexpected id type $t for the relation. Use either SId, SInt, SUId, SUInt, SBigID, SBigInt or SString', field.pos);
 				}
 				}
 
 
 			if( f == null ) {
 			if( f == null ) {

+ 11 - 0
tests/unit/src/unit/MySpodClass.hx

@@ -82,3 +82,14 @@ abstract AbstractSpodTest<A>(A) from A
 	public var id:SId;
 	public var id:SId;
 	@:relation(ref_id) public var ref:ClassWithStringId;
 	@:relation(ref_id) public var ref:ClassWithStringId;
 }
 }
+
+
+//issue #3828
+@:keep @:skip class BaseIssueC3828 extends sys.db.Object {
+    public var id : SInt;
+    @:relation(ruid)
+    public var refUser : SNull<IssueC3828>;
+}
+
+@:keep class IssueC3828 extends BaseIssueC3828 {
+}

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

@@ -24,11 +24,13 @@ class TestSpod extends Test
 		try cnx.request('DROP TABLE NullableSpodClass') catch(e:Dynamic) {}
 		try cnx.request('DROP TABLE NullableSpodClass') catch(e:Dynamic) {}
 		try cnx.request('DROP TABLE ClassWithStringId') catch(e:Dynamic) {}
 		try cnx.request('DROP TABLE ClassWithStringId') catch(e:Dynamic) {}
 		try cnx.request('DROP TABLE ClassWithStringIdRef') catch(e:Dynamic) {}
 		try cnx.request('DROP TABLE ClassWithStringIdRef') catch(e:Dynamic) {}
+		try cnx.request('DROP TABLE IssueC3828') catch(e:Dynamic) {}
 		TableCreate.create(MySpodClass.manager);
 		TableCreate.create(MySpodClass.manager);
 		TableCreate.create(OtherSpodClass.manager);
 		TableCreate.create(OtherSpodClass.manager);
 		TableCreate.create(NullableSpodClass.manager);
 		TableCreate.create(NullableSpodClass.manager);
 		TableCreate.create(ClassWithStringId.manager);
 		TableCreate.create(ClassWithStringId.manager);
 		TableCreate.create(ClassWithStringIdRef.manager);
 		TableCreate.create(ClassWithStringIdRef.manager);
+		TableCreate.create(IssueC3828.manager);
 	}
 	}
 
 
 	private function setManager()
 	private function setManager()
@@ -60,6 +62,24 @@ class TestSpod extends Test
 		return scls;
 		return scls;
 	}
 	}
 
 
+	public function testIssue3828()
+	{
+		setManager();
+		var u1 = new IssueC3828();
+		u1.insert();
+		var u2 = new IssueC3828();
+		u2.refUser = u1;
+		u2.insert();
+		var u1id = u1.id, u2id = u2.id;
+		u1 = null; u2 = null;
+		Manager.cleanup();
+
+		var u1 = IssueC3828.manager.get(u1id);
+		var u2 = IssueC3828.manager.search($refUser == u1).first();
+		eq(u1.id, u1id);
+		eq(u2.id, u2id);
+	}
+
 	public function testStringIdRel()
 	public function testStringIdRel()
 	{
 	{
 		setManager();
 		setManager();