Permalink
Browse files

Fixes for SPOD macros

  • Loading branch information...
1 parent 9979a10 commit 30a6ff788711a1e4de5f6e073ae4a1069389bd07 @waneck committed Aug 30, 2012
Showing with 368 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. +237 −0 sys/db/Sqlite.hx
  3. +128 −0 sys/db/Types.hx
  4. +2 −2 tests/src/sqlite/tests/SpodTests.hx
View
@@ -1,3 +1,4 @@
.DS_Store
all_objs
obj
+bin
View
@@ -0,0 +1,237 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package sys.db;
+
+import sys.db.Connection;
+
+private class SqliteConnection implements Connection {
+
+ var c : Dynamic;
+
+ public function new( file : String ) {
+ c = _connect(file);
+ }
+
+ public function close() {
+ _close(c);
+ }
+
+ public function request( s : String ) : ResultSet {
+ try {
+ return new SqliteResultSet(_request(c, s));
+ } catch( e : String ) {
+ throw "Error while executing "+s+" ("+e+")";
+ }
+ }
+
+ public function escape( s : String ) {
+ return s.split("'").join("''");
+ }
+
+ public function quote( s : String ) {
+ if( s.indexOf("\000") >= 0 )
+ return "x'"+new String(_encode(s,"0123456789ABCDEF"))+"'";
+ return "'"+s.split("'").join("''")+"'";
+ }
+
+ public function addValue ( s : StringBuf, v : Dynamic ) {
+ if (Std.is (v, Int) || v == null) {
+ s.add (v);
+ } else if (Std.is (v, Bool)) {
+ s.add (if (v) 1 else 0);
+ } else {
+ s.add (quote (Std.string (v)));
+ }
+ /*var t = untyped __dollar__typeof(v);
+ if( untyped (t == __dollar__tint || t == __dollar__tnull) )
+ s.add(v);
+ else if( untyped t == __dollar__tbool )
+ s.add(if( v ) 1 else 0);
+ else
+ s.add(quote(Std.string(v)));*/
+ }
+
+ public function lastInsertId() {
+ return _last_id(c);
+ }
+
+ public function dbName() {
+ return "SQLite";
+ }
+
+ public function startTransaction() {
+ request("BEGIN TRANSACTION");
+ }
+
+ public function commit() {
+ request("COMMIT");
+ startTransaction(); // match mysql usage
+ }
+
+ public function rollback() {
+ request("ROLLBACK");
+ startTransaction(); // match mysql usage
+ }
+
+ static var _encode = cpp.Lib.load("std", "base_encode",2);
+ static var _connect = cpp.Lib.load("sqlite3", "sqlite_connect",1);
+ static var _close = cpp.Lib.load("sqlite3", "sqlite_close",1);
+ static var _request = cpp.Lib.load("sqlite3", "sqlite_request",2);
+ static var _last_id = cpp.Lib.load("sqlite3", "sqlite_last_insert_id",1);
+}
+
+
+private class SqliteResultSet implements ResultSet {
+
+ public var length(getLength,null) : Int;
+ public var nfields(getNFields,null) : Int;
+ var r : Dynamic;
+ var cache : List<Dynamic>;
+
+ public function new( r ) {
+ cache = new List();
+ this.r = r;
+ hasNext(); // execute the request
+ }
+
+ public function getFieldsNames() : Null<Array<String>>
+ {
+ return null;
+ }
+
+ function getLength () {
+ if( nfields != 0 ) {
+ while( true ) {
+ var c = doNext();
+ if( c == null )
+ break;
+ cache.add(c);
+ }
+ return cache.length;
+ }
+ return result_get_length(r);
+ }
+
+ function getNFields() {
+ return result_get_nfields(r);
+ }
+
+ public function hasNext() {
+ var c = next();
+ if( c == null )
+ return false;
+ cache.push(c);
+ return true;
+ }
+
+ public function next() : Dynamic {
+ var c = cache.pop();
+ if( c != null )
+ return c;
+ return doNext();
+ }
+
+ private function doNext () : Dynamic {
+
+ var c = result_next (r);
+
+ if( c == null ) {
+
+ return null;
+
+ } else {
+
+ var f = Reflect.fields (c);
+ var i = 0;
+ var l = f.length;
+ while (i < l) {
+ var v = Reflect.field (c, f[i]);
+ if (Type.getClassName (Type.getClass (v)) == "String") {
+ Reflect.setField (c, f[i], Std.string (v));
+ }
+ i = i + 1;
+ }
+
+
+
+
+ /*untyped {
+ var f = __dollar__objfields(c);
+ var i = 0;
+ var l = __dollar__asize(f);
+ while ( i < l ) {
+ var v = __dollar__objget (c, f [i]);
+ if( __dollar__typeof(v) == __dollar__tstring )
+ __dollar__objset(c,f[i],new String(v));
+ i = i + 1;
+ }
+ }*/
+
+ return c;
+
+ }
+
+
+ }
+
+ public function results() : List<Dynamic> {
+ var l = new List();
+ while( true ) {
+ var c = next();
+ if( c == null )
+ break;
+ l.add(c);
+ }
+ return l;
+ }
+
+ public function getResult( n : Int ) {
+ return new String(result_get(r,n));
+ }
+
+ public function getIntResult( n : Int ) : Int {
+ return result_get_int(r,n);
+ }
+
+ public function getFloatResult( n : Int ) : Float {
+ return result_get_float(r,n);
+ }
+
+ static var result_next = cpp.Lib.load("sqlite3", "sqlite_result_next",1);
+ static var result_get_length = cpp.Lib.load ("sqlite3", "sqlite_result_get_length",1);
+ static var result_get_nfields = cpp.Lib.load("sqlite3", "sqlite_result_get_nfields",1);
+ static var result_get = cpp.Lib.load("sqlite3", "sqlite_result_get",2);
+ static var result_get_int = cpp.Lib.load("sqlite3", "sqlite_result_get_int",2);
+ static var result_get_float = cpp.Lib.load("sqlite3", "sqlite_result_get_float",2);
+
+}
+
+class Sqlite {
+
+ public static function open( file : String ) : Connection {
+ return new SqliteConnection(file);
+ }
+
+}
View
@@ -0,0 +1,128 @@
+/*
+ * Copyright (c) 2005-2011, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package sys.db;
+
+// basic types
+
+/** int with auto increment **/
+typedef SId = Null<Int>
+
+/** int unsigned with auto increment **/
+typedef SUId = Null<Int>
+
+/** big int with auto increment **/
+typedef SBigId = Null<Float>
+
+typedef SInt = Null<Int>
+
+typedef SUInt = Null<Int>
+
+typedef SBigInt = Null<Float>
+
+/** single precision float **/
+typedef SSingle = Null<Float>
+
+/** double precision float **/
+typedef SFloat = Null<Float>
+
+/** use tinyint(1) to distinguish with int **/
+typedef SBool = Null<Bool>
+
+/** same as varchar(n) **/
+typedef SString<Const> = String
+
+/** date only, use SDateTime for date+time **/
+typedef SDate = Date
+
+/** mysql DateTime **/
+typedef SDateTime = Date
+
+/** mysql Timestamp **/
+typedef STimeStamp = Date
+
+/** TinyText (up to 255 bytes) **/
+typedef STinyText = String
+
+/** Text (up to 64KB) **/
+typedef SSmallText = String
+
+/** MediumText (up to 24MB) **/
+typedef SText = String
+
+/** Blob type (up to 64KB) **/
+typedef SSmallBinary = haxe.io.Bytes
+
+/** LongBlob type (up to 4GB) **/
+typedef SLongBinary = haxe.io.Bytes
+
+/** MediumBlob type (up to 24MB) **/
+typedef SBinary = haxe.io.Bytes
+
+/** same as binary(n) **/
+typedef SBytes<Const> = haxe.io.Bytes
+
+/** one byte signed [-128...127] **/
+typedef STinyInt = Null<Int>
+
+/** two bytes signed [-32768...32767] **/
+typedef SSmallInt = Null<Int>
+
+/** three bytes signed [-8388608...8388607] **/
+typedef SMediumInt = Null<Int>
+
+/** one byte [0...255] **/
+typedef STinyUInt = Null<Int>
+
+/** two bytes [0...65535] **/
+typedef SSmallUInt = Null<Int>
+
+/** three bytes [0...16777215] **/
+typedef SMediumUInt = Null<Int>
+
+// extra
+
+/** specify that this field is nullable **/
+typedef SNull<T> = Null<T>
+
+/** specify that the integer use custom encoding **/
+typedef SEncoded = Null<Int>
+
+/** haxe Serialized string **/
+typedef SSerialized = String
+
+/** native neko serialized bytes **/
+typedef SNekoSerialized = haxe.io.Bytes
+
+/** a set of bitflags of different enum values **/
+typedef SFlags<T:EnumValue> = haxe.EnumFlags<T>
+
+/** same as [SFlags] but will adapt the storage size to the number of flags **/
+typedef SSmallFlags<T:EnumValue> = SFlags<T>;
+
+/** allow to store any value in serialized form **/
+typedef SData<T> = Null<T>
+
+/** allow to store an enum value that does not have parameters as a simple int **/
+typedef SEnum<E:EnumValue> = Null<E>
@@ -116,13 +116,13 @@ class SpodTests
Manager.cleanup();
Assert.equals(2, MySpodClass.manager.all().length);
- var req = MySpodClass.manager.search( { relation: OtherSpodClass.manager.search( { name:"third spod" } ).first() } );
+ var req = MySpodClass.manager.search( { relation: OtherSpodClass.manager.search( { name:"third spod" }, null, false ).first() }, null, false );
Assert.equals(1, req.length);
scls = req.first();
scls.relation.name = "Test";
scls.relation.update();
- Assert.isNull(OtherSpodClass.manager.search( { name:"third spod" } ).first());
+ Assert.isNull(OtherSpodClass.manager.search( { name:"third spod" }, null, false ).first());
}
}

0 comments on commit 30a6ff7

Please sign in to comment.