Ver código fonte

applied patch from porfirioribeiro, issue #1641

Franco Ponticelli 12 anos atrás
pai
commit
0f971d3358
2 arquivos alterados com 11 adições e 339 exclusões
  1. 10 189
      std/php/_std/sys/db/Mysql.hx
  2. 1 150
      std/php/_std/sys/db/Sqlite.hx

+ 10 - 189
std/php/_std/sys/db/Mysql.hx

@@ -23,188 +23,6 @@ package sys.db;
 
 import sys.db.Connection;
 
-private class MysqlConnection implements Connection {
-
-	var c : Dynamic;
-
-	public function new( c ) {
-		this.c = c;
-	}
-
-	public function close() {
-		untyped __call__("mysql_close", c);
-		untyped __call__("unset", c);
-	}
-
-	public function request( s : String ) : ResultSet {
-		var h = untyped __call__("mysql_query", s, c);
-		if(untyped __physeq__(h, false))
-			throw "Error while executing "+s+" ("+untyped __call__("mysql_error", c)+")";
-		return new MysqlResultSet(cast h, cast c);
-	}
-
-	public function escape( s : String ) {
-		return untyped __call__("mysql_real_escape_string", s, c);
-	}
-
-	public function quote( s : String ) {
-		return "'" + untyped __call__("mysql_real_escape_string", s, c) + "'";
-	}
-
-	public function addValue( s : StringBuf, v : Dynamic ) {
-		if( untyped __call__("is_int", v) || __call__("is_null", v))
-			s.add(v);
-		else if( untyped __call__("is_bool", v) )
-			s.add(if( v ) 1 else 0);
-		else
-			s.add(quote(Std.string(v)));
-	}
-
-	public function lastInsertId() {
-		return untyped __call__("mysql_insert_id", c);
-	}
-
-	public function dbName() {
-		return "MySQL";
-	}
-
-	public function startTransaction() {
-		request("START TRANSACTION");
-	}
-
-	public function commit() {
-		request("COMMIT");
-	}
-
-	public function rollback() {
-		request("ROLLBACK");
-	}
-}
-
-
-private class MysqlResultSet implements ResultSet {
-	public var length(get,null) : Int;
-	public var nfields(get,null) : Int;
-	private var __r : Dynamic;
-	private var __c : Dynamic;
-	private var cache : Dynamic;
-
-	public function new(r, c) {
-		__r = r;
-		__c = c;
-	}
-
-	private function get_length() {
-		if(untyped __physeq__(__r, true))
-			return untyped __call__("mysql_affected_rows", __c);
-		else if (untyped __physeq__(__r, false))
-			return 0;
-		return untyped __call__("mysql_num_rows", __r);
-	}
-
-	private var _nfields : Int;
-	private function get_nfields() {
-		if(_nfields == null)
-			_nfields = untyped __call__("mysql_num_fields", __r);
-		return _nfields;
-	}
-
-	private var _fieldsDesc : Array<Dynamic>;
-	private function getFieldsDescription() {
-		if(_fieldsDesc == null) {
-			_fieldsDesc = [];
-			for (i in 0...nfields) {
-				var type = untyped __call__("mysql_field_type", __r, i);
-				if ( type == "blob" ) {
-					var flags : String = untyped __call__("mysql_field_flags", __r, i);
-					if ( flags.indexOf("blobs") == -1 && flags.indexOf("binary") == -1 ) {
-						type = "text";
-					}
-				}
-				var item = {
-					name : untyped __call__("mysql_field_name", __r, i),
-					type : type,
-				};
-				_fieldsDesc.push(item);
-			}
-		}
-		return _fieldsDesc;
-	}
-
-	private function convert(v : String, type : String) : Dynamic {
-		if (v == null) return v;
-		switch(type) {
-			case "int", "year":
-				return untyped __call__("intval", v);
-			case "real":
-				return untyped __call__("floatval", v);
-			case "datetime", "date", "timestamp":
-				return Date.fromString(v);
-			case "blob":
-				return haxe.io.Bytes.ofData(cast v);
-			default:
-				return v;
-		}
-	}
-
-	public function hasNext() {
-		if( cache == null )
-			cache = next();
-		return (cache != null);
-	}
-
-	private var cRow : ArrayAccess<String>;
-	private function fetchRow() : Bool {
-		cRow = untyped __call__("mysql_fetch_array", __r, __php__("MYSQL_NUM"));
-		return ! untyped __physeq__(cRow, false);
-	}
-
-	public function next() : Dynamic {
-		if( cache != null ) {
-			var t = cache;
-			cache = null;
-			return t;
-		}
-		if(!fetchRow()) return null;
-
-		var o : Dynamic = {};
-		var descriptions = getFieldsDescription();
-		for(i in 0...nfields)
-			Reflect.setField(o, descriptions[i].name, convert(cRow[i], descriptions[i].type));
-		return o;
-	}
-
-	public function results() : List<Dynamic> {
-		var l = new List();
-		while( hasNext() )
-			l.add(next());
-		return l;
-	}
-
-	public function getResult( n : Int ) : String {
-		if(cRow == null)
-			if(!fetchRow())
-				return null;
-		return cRow[n];
-	}
-
-	public function getIntResult( n : Int ) : Int {
-		return untyped __call__("intval", getResult(n));
-	}
-
-	public function getFloatResult( n : Int ) : Float {
-		return untyped __call__("floatval", getResult(n));
-	}
-
-	public function getFieldsNames() : Array<String> {
-		var fields = [];
-		for( i in 0...nfields )
-			fields.push(untyped __call__("mysql_field_name", __r, i));
-		return fields;
-	}
-
-}
-
 @:coreApi class Mysql {
 
 	public static function connect( params : {
@@ -215,13 +33,16 @@ private class MysqlResultSet implements ResultSet {
 		?socket : String,
 		database : String
 	} ) : sys.db.Connection {
-		var c = untyped __call__("mysql_connect",
-			params.host + (params.port == null ? '' : ':'+params.port) + (params.socket == null ? '' : ':'+params.socket),
-			params.user,
-			params.pass);
-		if(!untyped __call__("mysql_select_db", params.database, c))
-			throw "Unable to connect to " + params.database;
-		return new MysqlConnection(c);
+		var dsn="mysql:";
+		if (params.socket !=null)
+			dsn+="unix_socket="+params.socket+";";
+		else{
+			dsn+="host="+params.host+";"; 
+			if (params.port!=null)
+				dsn+='port='+params.port+";";
+		}
+		dsn+="dbname="+params.database;
+		return php.db.PDO.open(dsn,params.user,params.pass);
 	}
 
 }

+ 1 - 150
std/php/_std/sys/db/Sqlite.hx

@@ -21,159 +21,10 @@
  */
 package sys.db;
 
-private class SqliteConnection implements Connection {
-
-	var c : Dynamic;
-	var e : Dynamic;
-
-	public function new( file : String ) {
-		c = untyped __call__("sqlite_open", file, 0666, e);
-	}
-
-	public function close() {
-		untyped __call__("sqlite_close", c);
-		untyped __call__("unset", c);
-	}
-
-	public function request( s : String ) : ResultSet {
-		var h = untyped __call__("sqlite_query", c, s, __php__("SQLITE_BOTH"), e);
-		if(untyped __physeq__(h, false))
-			throw "Error while executing "+s+" ("+e+")";
-		return new SqliteResultSet(cast h);
-	}
-
-	public function escape( s : String ) {
-		return untyped __call__("sqlite_escape_string", s);
-	}
-
-	public function quote( s : String ) {
-		if( s.indexOf("\000") >= 0 )
-			return "x'"+base16_encode(s)+"'";
-		return "'" + untyped __call__("sqlite_escape_string", s) + "'";
-	}
-
-	public function addValue( s : StringBuf, v : Dynamic ) {
-		if( untyped __call__("is_int", v) || __call__("is_null", v))
-			s.add(v);
-		else if( untyped __call__("is_bool", v) )
-			s.add(if( v ) 1 else 0);
-		else
-			s.add(quote(Std.string(v)));
-	}
-
-	public function lastInsertId() {
-		return untyped __call__("sqlite_last_insert_rowid", 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
-	}
-
-	function base16_encode(str : String) {
-		str = untyped __call__("unpack", "H"+(2 * str.length), str);
-		str = untyped __call__("chunk_split", untyped str[1]);
-		return str;
-	}
-}
-
-
-private class SqliteResultSet implements ResultSet {
-
-	public var length(get,null) : Int;
-	public var nfields(get,null) : Int;
-	var r : Dynamic;
-	var cache : Dynamic;
-
-	public function new( r ) {
-		this.r = r;
-	}
-
-	private function get_length() {
-		if(untyped __physeq__(r, true))
-			return untyped __call__("sqlite_changes", r);
-		else if (untyped __physeq__(r, false))
-			return 0;
-		return untyped __call__("sqlite_num_rows", r);
-	}
-
-	private var _nfields : Int;
-	private function get_nfields() {
-		if(_nfields == null)
-			_nfields = untyped __call__("sqlite_num_fields", r);
-		return _nfields;
-	}
-
-	public function hasNext() {
-		if( cache == null )
-			cache = next();
-		return (cache != null);
-	}
-
-	private var cRow : ArrayAccess<String>;
-	private function fetchRow() : Bool {
-		cRow = untyped __call__("sqlite_fetch_array", r, __php__("SQLITE_ASSOC"));
-		return ! untyped __physeq__(cRow, false);
-	}
-
-	public function next() : Dynamic {
-		if( cache != null ) {
-			var t = cache;
-			cache = null;
-			return t;
-		}
-		if(!fetchRow()) return null;
-		return untyped __call__("_hx_anonymous", cRow);
-	}
-
-	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 ) : String {
-		if(cRow == null && !fetchRow())
-			return null;
-		return untyped __call__("array_values", cRow)[n];
-	}
-
-	public function getIntResult( n : Int ) : Int {
-		return untyped __call__("intval", getResult(n));
-	}
-
-	public function getFloatResult( n : Int ) : Float {
-		return untyped __call__("floatval", getResult(n));
-	}
-
-	public function getFieldsNames() : Array<String> {
-		return throw "Not implemented";
-	}
-
-}
-
 @:coreApi class Sqlite {
 
 	public static function open( file : String ) : Connection {
-		return new SqliteConnection(file);
+		return php.db.PDO.open("sqlite:"+file);
 	}
 
 }