Nicolas Cannasse 19 years ago
parent
commit
9ffea8c9a7
3 changed files with 120 additions and 0 deletions
  1. 35 0
      std/neko/db/Connection.hx
  2. 24 0
      std/neko/db/Mysql.hx
  3. 61 0
      std/neko/db/Result.hx

+ 35 - 0
std/neko/db/Connection.hx

@@ -0,0 +1,35 @@
+package neko.db;
+
+class Connection {
+
+	private var __c : Void;
+
+	private function new(c) {
+		__c = c;
+	}
+
+	public function selectDB( db : String ) {
+		sql_select_db(this.__c,untyped db.__s);
+	}
+
+	public function request( s : String ) : Result {
+		var r = sql_request(this.__c,untyped s.__s);
+		//Result.set_conv_date(r,function(d) { return new Date(d); });
+		return untyped new Result(r);
+	}
+
+	public function close() {
+		sql_close(__c);
+	}
+
+	public function escape( s : String ) {
+		return new String(sql_escape(__c,untyped s.__s));
+	}
+
+	private static var __use_date = Date;
+	private static var sql_select_db = neko.Lib.load("mysql","select_db",2);
+	private static var sql_request = neko.Lib.load("mysql","request",2);
+	private static var sql_close = neko.Lib.load("mysql","close",1);
+	private static var sql_escape = neko.Lib.load("mysql","escape",2);
+
+}

+ 24 - 0
std/neko/db/Mysql.hx

@@ -0,0 +1,24 @@
+package neko.db;
+
+class Mysql {
+
+	public static function connect( params : {
+		host : String,
+		port : Int,
+		user : String,
+		pass : String,
+		socket : String
+	} ) : neko.db.Connection {
+		var o = untyped {
+			host : params.host.__s,
+			port : params.port,
+			user : params.user.__s,
+			pass : params.pass.__s,
+			socket : if( params.socket == null ) null else params.socket.__s
+		};
+		return untyped new Connection(sql_connect(o));
+	}
+
+	static var sql_connect = neko.Lib.load("mysql","connect",1);
+
+}

+ 61 - 0
std/neko/db/Result.hx

@@ -0,0 +1,61 @@
+package neko.db;
+
+class Result {
+
+	public var length : Int;
+	public var nfields : Int;
+	public var current : { };
+	private var __r : Void;
+
+	private function new(r) {
+		__r = r;
+	}
+
+
+	public function next() : Bool {
+		var c = result_next(__r);
+		current = c;
+		if( c == null )
+			return false;
+		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 true;
+	}
+
+	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);
+	}
+
+	public function results() : Array<{}> {
+		var a = new Array();
+		while( next() )
+			a.push(current);
+		return a;
+	}
+
+	private static var result_get_length = neko.Lib.load("mysql","result_get_length",1);
+	private static var result_get_nfields = neko.Lib.load("mysql","result_get_nfields",1);
+	private static var result_next = neko.Lib.load("mysql","result_next",1);
+	private static var result_get = neko.Lib.load("mysql","result_get",2);
+	private static var result_get_int = neko.Lib.load("mysql","result_get_int",2);
+	private static var result_get_float = neko.Lib.load("mysql","result_get_float",2);
+	private static var result_set_conv_date = neko.Lib.load("mysql","result_set_conv_date",2);
+
+}