Browse Source

[spod] Improve error messages and allow SInt / SUInt / SBigInt with the @:id metadata. Closes #3828

Cauê Waneck 10 years ago
parent
commit
c08dc31820
2 changed files with 10 additions and 6 deletions
  1. 8 4
      std/sys/db/RecordMacros.hx
  2. 2 2
      tests/unit/src/unit/MySpodClass.hx

+ 8 - 4
std/sys/db/RecordMacros.hx

@@ -27,6 +27,7 @@ import haxe.macro.Type.VarAccess;
 import haxe.macro.Context;
 using haxe.macro.TypeTools;
 #end
+using Lambda;
 
 private typedef SqlFunction = {
 	var name : String;
@@ -359,16 +360,19 @@ class RecordMacros {
 		}
 		// create fields for undeclared relations keys :
 		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)));
+			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];
 			var relatedKeyType = switch(relatedInf.hfields.get(relatedKey).t)
 				{
-					case DId: DInt;
-					case DUId: DUInt;
-					case DBigId: DBigInt;
+					case DId | DInt: DInt;
+					case DUId | DUInt: DUInt;
+					case DBigId | DBigId: DBigInt;
 					case t = DString(_): t;
-					default: throw "Unexpected id type, use either SId, SUId, SBigID or SString";
+					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 ) {

+ 2 - 2
tests/unit/src/unit/MySpodClass.hx

@@ -42,9 +42,9 @@ import sys.db.Types;
 	}
 }
 
-@:keep class OtherSpodClass extends Object
+@:id(theid) @:keep class OtherSpodClass extends Object
 {
-	public var theid:SId;
+	public var theid:SInt;
 	public var name:SString<255>;
 
 	public function new(name:String)