|
@@ -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);
|