Browse Source

use interfaces, better for multidb support.

Nicolas Cannasse 19 years ago
parent
commit
b6ff14d169
3 changed files with 138 additions and 114 deletions
  1. 7 42
      std/neko/db/Connection.hx
  2. 122 1
      std/neko/db/Mysql.hx
  3. 9 71
      std/neko/db/ResultSet.hx

+ 7 - 42
std/neko/db/Connection.hx

@@ -24,47 +24,12 @@
  */
 package neko.db;
 
-class Connection {
+interface Connection {
 
-	private var __c : Void;
+	function selectDB( db : String ) : Void;
+	function request( s : String ) : ResultSet;
+	function close() : Void;
+	function escape( s : String ) : String;
+	function quote( s : String ) : String;
 
-	private function new(c) {
-		__c = c;
-	}
-
-	public function selectDB( db : String ) {
-		sql_select_db(this.__c,untyped db.__s);
-	}
-
-	public function request( s : String ) : ResultSet {
-		try {
-			var r = sql_request(this.__c,untyped s.__s);
-			untyped ResultSet.result_set_conv_date(r,function(d) { return Date.new1(d); });
-			return untyped new ResultSet(r);
-		} catch( e : Dynamic ) {
-			untyped if( __dollar__typeof(e) == __dollar__tobject && __dollar__typeof(e.msg) == __dollar__tstring )
-				e.msg = new String(e.msg);
-			untyped __dollar__rethrow(e);
-			return null;
-		}
-	}
-
-	public function close() {
-		sql_close(__c);
-	}
-
-	public function escape( s : String ) {
-		return new String(sql_escape(__c,untyped s.__s));
-	}
-
-	public function quote( s : String ) {
-		return "'"+escape(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);
-
-}
+}

+ 122 - 1
std/neko/db/Mysql.hx

@@ -24,6 +24,127 @@
  */
 package neko.db;
 
+private class MysqlResultSet implements ResultSet {
+
+	public var length(getLength,null) : Int;
+	public var nfields(getNFields,null) : Int;
+	private var __r : Void;
+	private var cache : Dynamic;
+
+	public function new(r) {
+		__r = r;
+	}
+
+	private function getLength() {
+		return result_get_length(__r);
+	}
+
+	private function getNFields() {
+		return result_get_nfields(__r);
+	}
+
+	public function hasNext() {
+		if( cache == null )
+			cache = next();
+		return (cache != null);
+	}
+
+	public function next() : Dynamic {
+		var c = cache;
+		if( c != null ) {
+			cache = null;
+			return c;
+		}
+		c = result_next(__r);
+		if( c == null )
+			return null;
+		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( hasNext() )
+			l.add(next());
+		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);
+	}
+
+	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);
+	public static var result_set_conv_date = neko.Lib.load("mysql","result_set_conv_date",2);
+
+}
+
+private class MysqlConnection implements Connection {
+
+	private var __c : Void;
+
+	public function new(c) {
+		__c = c;
+	}
+
+	public function selectDB( db : String ) {
+		sql_select_db(this.__c,untyped db.__s);
+	}
+
+	public function request( s : String ) : ResultSet {
+		try {
+			var r = sql_request(this.__c,untyped s.__s);
+			MysqlResultSet.result_set_conv_date(r,function(d) { return untyped Date.new1(d); });
+			return new MysqlResultSet(r);
+		} catch( e : Dynamic ) {
+			untyped if( __dollar__typeof(e) == __dollar__tobject && __dollar__typeof(e.msg) == __dollar__tstring )
+				e.msg = new String(e.msg);
+			untyped __dollar__rethrow(e);
+			return null;
+		}
+	}
+
+	public function close() {
+		sql_close(__c);
+	}
+
+	public function escape( s : String ) {
+		return new String(sql_escape(__c,untyped s.__s));
+	}
+
+	public function quote( s : String ) {
+		return "'"+escape(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);
+}
+
 class Mysql {
 
 	public static function connect( params : {
@@ -40,7 +161,7 @@ class Mysql {
 			pass : params.pass.__s,
 			socket : if( params.socket == null ) null else params.socket.__s
 		};
-		return untyped new Connection(sql_connect(o));
+		return new MysqlConnection(sql_connect(o));
 	}
 
 	static var sql_connect = neko.Lib.load("mysql","connect",1);

+ 9 - 71
std/neko/db/ResultSet.hx

@@ -24,79 +24,17 @@
  */
 package neko.db;
 
-class ResultSet {
+interface ResultSet {
 
-	public var length(getLength,null) : Int;
-	public var nfields(getNFields,null) : Int;
-	private var __r : Void;
-	private var cache : Dynamic;
+	var length(getLength,null) : Int;
+	var nfields(getNFields,null) : Int;
 
-	private function new(r) {
-		__r = r;
-	}
 
-	private function getLength() {
-		return result_get_length(__r);
-	}
-
-	private function getNFields() {
-		return result_get_nfields(__r);
-	}
-
-	public function hasNext() {
-		if( cache == null )
-			cache = next();
-		return (cache != null);
-	}
-
-	public function next() : Dynamic {
-		var c = cache;
-		if( c != null ) {
-			cache = null;
-			return c;
-		}
-		c = result_next(__r);
-		if( c == null )
-			return null;
-		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( hasNext() )
-			l.add(next());
-		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);
-	}
-
-	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);
+	function hasNext() : Bool;
+	function next() : Dynamic;
+	function results() : List<Dynamic>;
+	function getResult( n : Int ) : String;
+	function getIntResult( n : Int ) : Int;
+	function getFloatResult( n : Int ) : Float;
 
 }