Browse Source

- database fields must be explicitly prefixed with $
- no longer need (dynamic,dynamic) for relation fields (autobuild)

Nicolas Cannasse 14 years ago
parent
commit
1f27f2c908
3 changed files with 23 additions and 6 deletions
  1. 1 1
      std/neko/db/MacroManager.hx
  2. 1 0
      std/neko/db/Object.hx
  3. 21 5
      std/neko/db/SpodData.hx

+ 1 - 1
std/neko/db/MacroManager.hx

@@ -110,7 +110,7 @@ class MacroManager<T : Object> {
 		return SpodData.macroCount(ethis, cond);
 	}
 
-	@:macro public function deleteCond(ethis, cond) : #if macro haxe.macro.Expr #else haxe.macro.Expr.ExprRequire<Void> #end {
+	@:macro public function delete(ethis, cond) : #if macro haxe.macro.Expr #else haxe.macro.Expr.ExprRequire<Void> #end {
 		return SpodData.macroDelete(ethis, cond);
 	}
 

+ 1 - 0
std/neko/db/Object.hx

@@ -28,6 +28,7 @@ package neko.db;
 	SPOD Object : the persistent object base type. See the tutorial on haXe
 	website to learn how to use SPOD.
 **/
+#if spod_macro @:autoBuild(neko.db.SpodData.macroBuild()) #end
 class Object #if spod_rtti implements haxe.rtti.Infos #end {
 
 /*

+ 21 - 5
std/neko/db/SpodData.hx

@@ -459,7 +459,7 @@ class SpodData {
 		}
 		var sql;
 		// use some different operators if there is a possibility for comparing two NULLs
-		if( r1.n && r2.n || (!eq && (r1.n || r2.n)) ) {
+		if( r1.n || r2.n ) {
 			sql = makeOp(" <=> ", r1.sql, r2.sql, pos);
 			if( !eq )
 				sql = sqlAdd(makeString("NOT(", pos), sqlAddString(sql, ")"), pos);
@@ -581,10 +581,10 @@ class SpodData {
 			case CString(s): return { sql : sqlQuoteValue(cond, DText), t : DString(s.length), n : false };
 			case CRegexp(_): error("Unsupported", p);
 			case CIdent(n), CType(n):
-				var f = current.hfields.get(n);
-				if( f != null ) {
-					if( (try typeof(cond) catch( e : Dynamic ) null) != null )
-						error("Possible conflict between variable and database field", p);
+				if( n.charCodeAt(0) == "$".code ) {
+					n = n.substr(1);
+					var f = current.hfields.get(n);
+					if( f == null ) error("Unknown database field '" + n + "'", p);
 					return { sql : makeString(f.name, p), t : f.t, n : f.isNull };
 				}
 				switch( n ) {
@@ -960,6 +960,22 @@ class SpodData {
 		var pos = Context.currentPos();
 		return { expr : ECall({ expr : EField(em,"unsafeDelete"), pos : pos },[sql]), pos : pos };
 	}
+	
+	public static function macroBuild() {
+		var fields = Context.getBuildFields();
+		for( f in fields )
+			for( m in f.meta )
+				if( m.name == ":relation" ) {
+					switch( f.kind ) {
+					case FVar(t, _):
+						f.kind = FProp("dynamic", "dynamic", t);
+					default:
+						Context.error("Invalid relation field type", f.pos);
+					}
+					break;
+				}
+		return fields;
+	}
 
 	#end