Browse Source

added missing int types + SSmallFlags (auto size depending of flags count)

Nicolas Cannasse 13 years ago
parent
commit
df15d5ad54
5 changed files with 59 additions and 23 deletions
  1. 1 1
      std/sys/db/Manager.hx
  2. 6 1
      std/sys/db/SpodInfos.hx
  3. 23 18
      std/sys/db/SpodMacros.hx
  4. 8 2
      std/sys/db/TableCreate.hx
  5. 21 1
      std/sys/db/Types.hx

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

@@ -140,7 +140,7 @@ class Manager<T : Object> {
 				// if the field is not defined, give it a default value on insert
 				// if the field is not defined, give it a default value on insert
 				if( !inf.isNull )
 				if( !inf.isNull )
 					switch( inf.t ) {
 					switch( inf.t ) {
-					case DUInt, DTinyInt, DInt, DSingle, DFloat, DFlags(_), DBigInt:
+					case DUInt, DTinyInt, DInt, DSingle, DFloat, DFlags(_), DBigInt, DTinyUInt, DSmallInt, DSmallUInt, DMediumInt, DMediumUInt:
 						Reflect.setField(x, f, 0);
 						Reflect.setField(x, f, 0);
 					case DBool:
 					case DBool:
 						Reflect.setField(x, f, false);
 						Reflect.setField(x, f, false);

+ 6 - 1
std/sys/db/SpodInfos.hx

@@ -48,8 +48,13 @@ enum SpodType {
 	DEncoded;
 	DEncoded;
 	DSerialized;
 	DSerialized;
 	DNekoSerialized;
 	DNekoSerialized;
-	DFlags( flags : Array<String> );
+	DFlags( flags : Array<String>, autoSize : Bool );
 	DTinyInt;
 	DTinyInt;
+	DTinyUInt;
+	DSmallInt;
+	DSmallUInt;
+	DMediumInt;
+	DMediumUInt;
 	// specific for intermediate calculus
 	// specific for intermediate calculus
 	DInterval;
 	DInterval;
 	DNull;
 	DNull;

+ 23 - 18
std/sys/db/SpodMacros.hx

@@ -155,6 +155,25 @@ class SpodMacros {
 		}
 		}
 		return null;
 		return null;
 	}
 	}
+	
+	function getFlags( t : haxe.macro.Type ) {
+		switch( t ) {
+		case TEnum(e,_):
+			var cl = e.get().names;
+			if( cl.length > 1 ) {
+				var prefix = cl[0];
+				for( c in cl )
+					while( prefix.length > 0 && c.substr(0, prefix.length) != prefix )
+						prefix = prefix.substr(0, -1);
+				for( i in 0...cl.length )
+					cl[i] = cl[i].substr(prefix.length);
+			}
+			if( cl.length > 31 ) throw "Too many flags";
+			return cl;
+		default:
+			throw "Flags parameter should be an enum";
+		}
+	}
 
 
 	function makeType( t : haxe.macro.Type ) {
 	function makeType( t : haxe.macro.Type ) {
 		switch( t ) {
 		switch( t ) {
@@ -166,22 +185,7 @@ class SpodMacros {
 			case "String": DText;
 			case "String": DText;
 			case "Date": DDateTime;
 			case "Date": DDateTime;
 			case "haxe.io.Bytes": DBinary;
 			case "haxe.io.Bytes": DBinary;
-			case "sys.db.SFlags":
-				switch( p[0] ) {
-				case TEnum(e,_):
-					var cl = e.get().names;
-					if( cl.length > 1 ) {
-						var prefix = cl[0];
-						for( c in cl )
-							while( prefix.length > 0 && c.substr(0, prefix.length) != prefix )
-								prefix = prefix.substr(0, -1);
-						for( i in 0...cl.length )
-							cl[i] = cl[i].substr(prefix.length);
-					}
-					return DFlags(cl);
-				default:
-					throw "Flags parameter should be an enum";
-				}
+			case "sys.db.SFlags": DFlags(getFlags(p[0]),false);
 			default: throw "Unsupported " + name;
 			default: throw "Unsupported " + name;
 			}
 			}
 		case TEnum(e, p):
 		case TEnum(e, p):
@@ -201,6 +205,7 @@ class SpodMacros {
 				case "SString": return DString(makeInt(p[0]));
 				case "SString": return DString(makeInt(p[0]));
 				case "SBytes": return DBytes(makeInt(p[0]));
 				case "SBytes": return DBytes(makeInt(p[0]));
 				case "SNull", "Null": isNull = true; return makeType(p[0]);
 				case "SNull", "Null": isNull = true; return makeType(p[0]);
+				case "SSmallFlags": return DFlags(getFlags(p[0]),true);
 				default:
 				default:
 				}
 				}
 			throw "Unsupported " + name;
 			throw "Unsupported " + name;
@@ -412,7 +417,7 @@ class SpodMacros {
 
 
 	function unifyClass( t : SpodType ) {
 	function unifyClass( t : SpodType ) {
 		return switch( t ) {
 		return switch( t ) {
-		case DId, DInt, DUId, DUInt, DEncoded, DFlags(_), DTinyInt: 0;
+		case DId, DInt, DUId, DUInt, DEncoded, DFlags(_), DTinyInt, DTinyUInt, DSmallInt, DSmallUInt, DMediumInt, DMediumUInt: 0;
 		case DBigId, DBigInt, DSingle, DFloat: 1;
 		case DBigId, DBigInt, DSingle, DFloat: 1;
 		case DBool: 2;
 		case DBool: 2;
 		case DString(_), DTinyText, DSmallText, DText, DSerialized: 3;
 		case DString(_), DTinyText, DSmallText, DText, DSerialized: 3;
@@ -738,7 +743,7 @@ class SpodMacros {
 					if( pl.length == 1 ) {
 					if( pl.length == 1 ) {
 						var r = buildCond(e);
 						var r = buildCond(e);
 						switch( r.t ) {
 						switch( r.t ) {
-						case DFlags(vals):
+						case DFlags(vals,_):
 							var id = makeIdent(pl[0]);
 							var id = makeIdent(pl[0]);
 							var idx = Lambda.indexOf(vals,id);
 							var idx = Lambda.indexOf(vals,id);
 							if( idx < 0 ) error("Flag should be "+vals.join(","), pl[0].pos);
 							if( idx < 0 ) error("Flag should be "+vals.join(","), pl[0].pos);

+ 8 - 2
std/sys/db/TableCreate.hx

@@ -36,9 +36,14 @@ class TableCreate {
 		return switch( t ) {
 		return switch( t ) {
 		case DId: "INTEGER "+autoInc(dbName);
 		case DId: "INTEGER "+autoInc(dbName);
 		case DUId: "INTEGER UNSIGNED "+autoInc(dbName);
 		case DUId: "INTEGER UNSIGNED "+autoInc(dbName);
-		case DInt, DEncoded, DFlags(_): "INTEGER";
-		case DTinyInt: "TINYINT";
+		case DInt, DEncoded: "INTEGER";
 		case DUInt: "INTEGER UNSIGNED";
 		case DUInt: "INTEGER UNSIGNED";
+		case DTinyInt: "TINYINT";
+		case DTinyUInt: "TINYINT UNSIGNED";
+		case DSmallInt: "SMALLINT";
+		case DSmallUInt: "SMALLINT UNSIGNED";
+		case DMediumInt: "MEDIUMINT";
+		case DMediumUInt: "MEDIUMINT UNSIGNED";
 		case DSingle: "FLOAT";
 		case DSingle: "FLOAT";
 		case DFloat: "DOUBLE";
 		case DFloat: "DOUBLE";
 		case DBool: "TINYINT(1)";
 		case DBool: "TINYINT(1)";
@@ -55,6 +60,7 @@ class TableCreate {
 		case DBigInt: "BIGINT";
 		case DBigInt: "BIGINT";
 		case DBigId: "BIGINT "+autoInc(dbName);
 		case DBigId: "BIGINT "+autoInc(dbName);
 		case DBytes(n): "BINARY(" + n + ")";
 		case DBytes(n): "BINARY(" + n + ")";
+		case DFlags(fl, auto): getTypeSQL(auto ? (fl.length <= 8 ? DTinyUInt : (fl.length <= 16 ? DSmallUInt : (fl.length <= 24 ? DMediumUInt : DInt))) : DInt, dbName);
 		case DNull, DInterval: throw "assert";
 		case DNull, DInterval: throw "assert";
 		};
 		};
 	}
 	}

+ 21 - 1
std/sys/db/Types.hx

@@ -83,9 +83,24 @@ typedef SBinary = haxe.io.Bytes
 /** same as binary(n) **/
 /** same as binary(n) **/
 typedef SBytes<Const> = haxe.io.Bytes
 typedef SBytes<Const> = haxe.io.Bytes
 
 
-/** TinyInt [-128...127] **/
+/** one byte signed [-128...127] **/
 typedef STinyInt = Int
 typedef STinyInt = Int
 
 
+/** two bytes signed [-32768...32767] **/
+typedef SSmallInt = Int;
+
+/** three bytes signed [-8388608...8388607] **/
+typedef SMediumInt = Int;
+
+/** one byte [0...255] **/
+typedef STinyUInt = Int
+
+/** two bytes [0...65535] **/
+typedef SSmallUInt = Int;
+
+/** three bytes [0...16777215] **/
+typedef SMediumUInt = Int;
+
 // extra
 // extra
 
 
 /** specify that this field is nullable **/
 /** specify that this field is nullable **/
@@ -100,6 +115,7 @@ typedef SSerialized = String
 /** native neko serialized bytes **/
 /** native neko serialized bytes **/
 typedef SNekoSerialized = haxe.io.Bytes
 typedef SNekoSerialized = haxe.io.Bytes
 
 
+/** a set of bitflags of different enum values **/
 @:native("Int")
 @:native("Int")
 extern class SFlags<T> {
 extern class SFlags<T> {
 	public inline function init() : Void {
 	public inline function init() : Void {
@@ -121,3 +137,7 @@ extern class SFlags<T> {
 		return cast this;
 		return cast this;
 	}
 	}
 }
 }
+
+/** same as [SFlags] but will adapt the storage size to the number of flags **/
+typedef SSmallFlags<T> = SFlags<T>;
+