Procházet zdrojové kódy

[spod] Add abstract types support for SPOD. SText may be sent as bytes; Convert them to String. Closes #2886

Cauê Waneck před 11 roky
rodič
revize
e73cac9046

+ 2 - 0
std/sys/db/Manager.hx

@@ -360,6 +360,8 @@ class Manager<T : Object> {
 						}
 					case DSmallBinary, DLongBinary, DBinary, DBytes(_), DData if (Std.is(val, String)):
 						val = haxe.io.Bytes.ofString(val);
+					case DString(_) | DTinyText | DSmallText | DText if(!Std.is(val,String)):
+						val = val + "";
 					case DBool if (!Std.is(val,Bool)):
 						if (Std.is(val,Int))
 							val = val != 0;

+ 5 - 1
std/sys/db/RecordMacros.hx

@@ -25,6 +25,7 @@ import haxe.macro.Expr;
 import haxe.macro.Type.VarAccess;
 #if macro
 import haxe.macro.Context;
+using haxe.macro.TypeTools;
 #end
 
 private typedef SqlFunction = {
@@ -193,12 +194,15 @@ class RecordMacros {
 			case "haxe.io.Bytes": DBinary;
 			default: throw "Unsupported Record Type " + name;
 			}
-		case TAbstract(a, _):
+		case TAbstract(a, p):
 			var name = a.toString();
 			return switch( name ) {
 			case "Int": DInt;
 			case "Float": DFloat;
 			case "Bool": DBool;
+			case _ if (!a.get().meta.has(':coreType')):
+				var a = a.get();
+				makeType(a.type.applyTypeParameters(a.params, p));
 			default: throw "Unsupported Record Type " + name;
 			}
 		case TEnum(e, _):

+ 1 - 1
std/sys/db/TableCreate.hx

@@ -106,4 +106,4 @@ class TableCreate {
 			return false;
 		}
 	}
-}
+}

+ 10 - 0
tests/unit/MySpodClass.hx

@@ -11,6 +11,7 @@ import sys.db.Types;
   public var string:SString<255>;
   public var date:SDateTime;
   public var binary:SBinary;
+	public var abstractType:AbstractSpodTest<String>;
 
   public var nullInt:SNull<Int>;
   public var enumFlags:SFlags<SpodEnum>;
@@ -27,6 +28,7 @@ import sys.db.Types;
 	public var theId:SId;
   @:relation(rnid) public var relationNullable:Null<OtherSpodClass>;
   public var data:Null<SData<Array<ComplexClass>>>;
+	public var abstractType:Null<AbstractSpodTest<String>>;
 }
 
 @:keep class ComplexClass
@@ -57,3 +59,11 @@ import sys.db.Types;
 	SecondValue;
 	ThirdValue;
 }
+
+abstract AbstractSpodTest<A>(A) from A
+{
+	public function get():A
+	{
+		return this;
+	}
+}

+ 6 - 2
tests/unit/TestSpod.hx

@@ -40,8 +40,9 @@ class TestSpod extends Test
 		scls.boolean = true;
 		scls.string = "some string";
 		scls.date = new Date(2012, 7, 30, 0, 0, 0);
+		scls.abstractType = "other string";
 
-		var bytes = Bytes.ofString("\x01\n\r\x02");
+		var bytes = Bytes.ofString("\x01\n\r'\x02");
 		scls.binary = bytes;
 		scls.enumFlags = EnumFlags.ofInt(0);
 		scls.enumFlags.set(FirstValue);
@@ -91,6 +92,7 @@ class TestSpod extends Test
 		var scls = new NullableSpodClass();
 		scls.data = null;
 		scls.relationNullable = null;
+		scls.abstractType = null;
 		scls.insert();
 
 		var id = scls.theId;
@@ -139,12 +141,14 @@ class TestSpod extends Test
 		eq(cls1.boolean, true,pos());
 		t(Std.is(cls1.string, String),pos());
 		eq(cls1.string, "some string",pos());
+		t(Std.is(cls1.abstractType, String),pos());
+		eq(cls1.abstractType.get(), "other string",pos());
 		t(cls1.date != null,pos());
 		t(Std.is(cls1.date, Date),pos());
 		eq(cls1.date.getTime(), new Date(2012, 7, 30, 0, 0, 0).getTime(),pos());
 
 		t(Std.is(cls1.binary, Bytes),pos());
-		eq(cls1.binary.compare(Bytes.ofString("\x01\n\r\x02")), 0,pos());
+		eq(cls1.binary.compare(Bytes.ofString("\x01\n\r'\x02")), 0,pos());
 		t(cls1.enumFlags.has(FirstValue),pos());
 		f(cls1.enumFlags.has(SecondValue),pos());
 		t(cls1.enumFlags.has(ThirdValue),pos());