Skip to content

Commit

Permalink
[spod] Fixed ID types equivalence (SInt -> SId);
Browse files Browse the repository at this point in the history
* Fixed declaration of relations in same module
* Fixed invalid relation type error message
* Closes #3828
  • Loading branch information
waneck committed Apr 14, 2015
1 parent 72172a7 commit 7bc43d8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions std/sys/db/RecordInfos.hx
Expand Up @@ -69,6 +69,7 @@ typedef RecordRelation = {
var prop : String;
var key : String;
var type : String;
var module : String;
var cascade : Bool;
var lock : Bool;
var isNull : Bool;
Expand Down
28 changes: 25 additions & 3 deletions std/sys/db/RecordMacros.hx
Expand Up @@ -122,8 +122,18 @@ class RecordMacros {
#end
}

public dynamic function resolveType( name : String ) : haxe.macro.Type {
public dynamic function resolveType( name : String, ?module : String ) : haxe.macro.Type {
#if macro
if (module != null)
{
var m = Context.getModule(module);
for (t in m)
{
if (t.toString() == name)
return t;
}
}

return Context.getType(name);
#else
throw "not implemented";
Expand Down Expand Up @@ -317,10 +327,12 @@ class RecordMacros {
isNull = false;
var t = makeRecord(f.type);
if( t == null ) error("Relation type should be a sys.db.Object", f.pos);
var mod = t.get().module;
var r = {
prop : f.name,
key : params.shift().i,
type : t.toString(),
module : mod,
cascade : false,
lock : false,
isNull : isNull,
Expand Down Expand Up @@ -354,6 +366,16 @@ class RecordMacros {
default: fi.name == "id";
}
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);
}
i.fields.push(fi);
Expand All @@ -363,7 +385,7 @@ class RecordMacros {
for( r in i.relations ) {
var field = fields.find(function(f) return f.name == r.prop);
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)
error('The relation ${r.prop} is invalid: Type ${r.type} has multiple keys, which is not supported',field.pos);
var relatedKey = relatedInf.key[0];
Expand All @@ -373,7 +395,7 @@ class RecordMacros {
case DUId: DUInt;
case DBigId: DBigInt;
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 ) {
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/src/unit/MySpodClass.hx
Expand Up @@ -82,3 +82,14 @@ abstract AbstractSpodTest<A>(A) from A
public var id:SId;
@: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 changes: 20 additions & 0 deletions tests/unit/src/unit/TestSpod.hx
Expand Up @@ -24,11 +24,13 @@ class TestSpod extends Test
try cnx.request('DROP TABLE NullableSpodClass') 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 IssueC3828') catch(e:Dynamic) {}
TableCreate.create(MySpodClass.manager);
TableCreate.create(OtherSpodClass.manager);
TableCreate.create(NullableSpodClass.manager);
TableCreate.create(ClassWithStringId.manager);
TableCreate.create(ClassWithStringIdRef.manager);
TableCreate.create(IssueC3828.manager);
}

private function setManager()
Expand Down Expand Up @@ -60,6 +62,24 @@ class TestSpod extends Test
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()
{
setManager();
Expand Down

0 comments on commit 7bc43d8

Please sign in to comment.