Selaa lähdekoodia

[php] Fix PDOClass and complete it. (#5210)

* Overload for function prepare and query on PDOClass

* Add overload for constructor

* Use PDO attribute

* Add attributes

* Add overload for functions on PDOStatement

* Fix type.

* Add PDOException

* Fix constructor

* Add getPrevious function

* Fix type (see php doc)

* Fix indentation.

* Fix FETCH_ORI_NEXT inline.

* Add the mysql driver constants.

* Move and rename PDOClass

* Extract classes.

* Temporary fix for default params.

* Fix package.

* Try Fix.

* Try to launch unit test with new PDO reference.

* Change class names.

* Fix import.

* Update copyright

* update lib

* update submodule libs

* Fix package path

* Array<String> to NativeArray in Exception file

* Add inTransaction method on PDOClass + remove empty lines

* package php to php.db for PDO

* PDOClass and PDOStatement in PDO module

* Fix package

* Move PDO and PDOException in db directory
ilicos - Théo S 8 vuotta sitten
vanhempi
commit
96d4ae2ed3

+ 1 - 1
extra/haxelib_src

@@ -1 +1 @@
-Subproject commit b0b6057d24cac4fc4464b259f269e6d80bd9bf9d
+Subproject commit 031b0ea06102968a23b7f2c89c1810286264474c

+ 19 - 15
std/php/Exception.hx

@@ -22,19 +22,23 @@
 package php;
 
 extern class Exception {
-  public function new(?message : String, ?code : Int) : Void;
-
-  private var message : String;
-  private var code : Int;
-  private var file : String;
-  private var line : Int;
-
-  public function getMessage() : String;       // message of the exception
-  public function getCode() : Int;             // code of the exception
-  public function getFile() : String;          // source filename
-  public function getLine() : Int;             // source line
-  public function getTrace() : Array<String>;  // an array of the backtrace()
-  public function getTraceAsString() : String; // formated string of trace
-
-  public function __toString() : String;       // formated string for display
+    @:overload(function():Void { } )
+    @:overload(function(message : String):Void { } )
+    @:overload(function(message : String, code : Int):Void { } )
+	public function new(message : String, code : Int, previous:Exception) : Void;
+	
+	private var message : String;
+	private var code : Int;
+	private var file : String;
+	private var line : Int;
+	
+	public function getMessage() : String;       // message of the exception
+	public function getPrevious() : Exception;   // last exception
+	public function getCode() : Dynamic;             // code of the exception
+	public function getFile() : String;          // source filename
+	public function getLine() : Int;             // source line
+	public function getTrace() : NativeArray;  // an array of the backtrace()
+	public function getTraceAsString() : String; // formated string of trace
+	
+	public function __toString() : String;       // formated string for display
 }

+ 27 - 0
std/php/RuntimeException.hx

@@ -0,0 +1,27 @@
+/*
+ * Copyright (C)2005-2012 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package php;
+import php.Exception;
+
+extern class RuntimeException extends Exception {
+	
+}

+ 3 - 1
std/php/_std/sys/db/Mysql.hx

@@ -33,7 +33,8 @@ import sys.db.Connection;
 		?socket : String,
 		?database : String
 	} ) : sys.db.Connection {
-		var dsn="mysql:";
+		var dsn = "mysql:";
+        
 		if (params.socket !=null)
 			dsn+="unix_socket="+params.socket+";";
 		else{
@@ -44,6 +45,7 @@ import sys.db.Connection;
 		if (params.database != null) {
 			dsn+="dbname="+params.database;
 		}
+		
 		return php.db.PDO.open(dsn,params.user,params.pass);
 	}
 

+ 190 - 22
std/php/db/PDO.hx

@@ -22,20 +22,12 @@
 package php.db;
 
 import php.NativeArray;
-
 import sys.db.Connection;
+
 import sys.db.ResultSet;
 
 import php.Lib;
 
-/**
- * PDO::FETCH_COLUMN = 7
- * PDO::FETCH_CLASS = 8
- * PDO::FETCH_INTO = 9
- * PDO::PARAM_STR = 2
- * PDO::FETCH_BOTH = 4
- * PDO::FETCH_ORI_NEXT = 0
- */
 class PDO
 {
 	public static function open(dsn : String, ?user : String, ?password : String, ?options : Dynamic) : Connection {
@@ -43,9 +35,146 @@ class PDO
 	}
 }
 
+@:native("PDO")
 extern class PDOClass
 {
-//	public function new(dns : String, ?username : String, ?password : String, ?driver_options : NativeArray) : Void;
+	public static var ATTR_CASE(get, never):Int;
+	private static inline function get_ATTR_CASE() : Int return untyped __php__("PDO::ATTR_CASE"); 
+	
+	public static var CASE_LOWER(get, never):Int;
+	private static inline function get_CASE_LOWER() : Int return untyped __php__("PDO::CASE_LOWER"); 
+	
+	public static var CASE_NATURAL(get, never):Int;
+	private static inline function get_CASE_NATURAL() : Int return untyped __php__("PDO::CASE_NATURAL"); 
+	
+	public static var CASE_UPPER(get, never):Int;
+	private static inline function get_CASE_UPPER() : Int return untyped __php__("PDO::CASE_UPPER"); 
+	
+	public static var ATTR_ERRMODE(get, never):Int;
+	private static inline function get_ATTR_ERRMODE() : Int return untyped __php__("PDO::ATTR_ERRMODE"); 
+	
+	public static var ERRMODE_SILENT(get, never):Int;
+	private static inline function get_ERRMODE_SILENT() : Int return untyped __php__("PDO::ERRMODE_SILENT"); 
+	
+	public static var ERRMODE_WARNING(get, never):Int;
+	private static inline function get_ERRMODE_WARNING() : Int return untyped __php__("PDO::ERRMODE_WARNING"); 
+	
+	public static var ERRMODE_EXCEPTION(get, never):Int;
+	private static inline function get_ERRMODE_EXCEPTION() : Int return untyped __php__("PDO::ERRMODE_EXCEPTION"); 
+	
+	public static var ATTR_ORACLE_NULLS(get, never):Int;
+	private static inline function get_ATTR_ORACLE_NULLS() : Int return untyped __php__("PDO::ATTR_ORACLE_NULLS"); 
+	
+	public static var NULL_NATURAL(get, never):Int;
+	private static inline function get_NULL_NATURAL() : Int return untyped __php__("PDO::NULL_NATURAL"); 
+	
+	public static var NULL_EMPTY_STRING(get, never):Int;
+	private static inline function get_NULL_EMPTY_STRING() : Int return untyped __php__("PDO::NULL_EMPTY_STRING"); 
+	
+	public static var NULL_TO_STRING(get, never):Int;
+	private static inline function get_NULL_TO_STRING() : Int return untyped __php__("PDO::NULL_TO_STRING"); 
+	
+	public static var ATTR_STRINGIFY_FETCHES(get, never):Int;
+	private static inline function get_ATTR_STRINGIFY_FETCHES() : Int return untyped __php__("PDO::ATTR_STRINGIFY_FETCHES"); 
+	
+	public static var ATTR_STATEMENT_CLASS(get, never):Int;
+	private static inline function get_ATTR_STATEMENT_CLASS() : Int return untyped __php__("PDO::ATTR_STATEMENT_CLASS"); 
+	
+	public static var ATTR_TIMEOUT(get, never):Int;
+	private static inline function get_ATTR_TIMEOUT() : Int return untyped __php__("PDO::ATTR_TIMEOUT"); 
+	
+	public static var ATTR_AUTOCOMMIT(get, never):Int;
+	private static inline function get_ATTR_AUTOCOMMIT() : Int return untyped __php__("PDO::ATTR_AUTOCOMMIT"); 
+	
+	public static var ATTR_EMULATE_PREPARES(get, never):Int;
+	private static inline function get_ATTR_EMULATE_PREPARES() : Int return untyped __php__("PDO::ATTR_EMULATE_PREPARES"); 
+	
+	public static var ATTR_DEFAULT_FETCH_MODE(get, never):Int;
+	private static inline function get_ATTR_DEFAULT_FETCH_MODE() : Int return untyped __php__("PDO::ATTR_DEFAULT_FETCH_MODE"); 
+	
+	public static var FETCH_ASSOC(get, never):Int;
+	private static inline function get_FETCH_ASSOC() : Int return untyped __php__("PDO::FETCH_ASSOC"); 
+	
+	public static var FETCH_BOTH(get, never):Int;
+	private static inline function get_FETCH_BOTH() : Int return untyped __php__("PDO::FETCH_BOTH"); 
+	
+	public static var FETCH_BOUND(get, never):Int;
+	private static inline function get_FETCH_BOUND() : Int return untyped __php__("PDO::FETCH_BOUND"); 
+	
+	public static var FETCH_INTO(get, never):Int;
+	private static inline function get_FETCH_INTO() : Int return untyped __php__("PDO::FETCH_INTO"); 
+	
+	public static var FETCH_LAZY(get, never):Int;
+	private static inline function get_FETCH_LAZY() : Int return untyped __php__("PDO::FETCH_LAZY"); 
+	
+	public static var FETCH_NAMED(get, never):Int;
+	private static inline function get_FETCH_NAMED() : Int return untyped __php__("PDO::FETCH_NAMED"); 
+	
+	public static var FETCH_NUM(get, never):Int;
+	private static inline function get_FETCH_NUM() : Int return untyped __php__("PDO::FETCH_NUM"); 
+	
+	public static var FETCH_OBJ(get, never):Int;
+	private static inline function get_FETCH_OBJ() : Int return untyped __php__("PDO::FETCH_OBJ"); 
+	
+	public static var FETCH_ORI_NEXT(get, never):Int;
+	private static inline function get_FETCH_ORI_NEXT() : Int return untyped __php__("PDO::FETCH_ORI_NEXT"); 
+	
+	public static var PARAM_STR(get, never):Int;
+	private static inline function get_PARAM_STR():Int return untyped __php__("PDO::PARAM_STR");
+    
+    public static var MYSQL_ATTR_USE_BUFFERED_QUERY(get, never):Int;
+	private static inline function get_MYSQL_ATTR_USE_BUFFERED_QUERY():Int return untyped __php__("PDO::MYSQL_ATTR_USE_BUFFERED_QUERY");
+    
+    public static var MYSQL_ATTR_LOCAL_INFILE(get, never):Int;
+	private static inline function get_MYSQL_ATTR_LOCAL_INFILE():Int return untyped __php__("PDO::MYSQL_ATTR_LOCAL_INFILE");
+    
+    public static var MYSQL_ATTR_INIT_COMMAND(get, never):Int;
+	private static inline function get_MYSQL_ATTR_INIT_COMMAND():Int return untyped __php__("PDO::MYSQL_ATTR_INIT_COMMAND");
+    
+    public static var MYSQL_ATTR_READ_DEFAULT_FILE(get, never):Int;
+	private static inline function get_MYSQL_ATTR_READ_DEFAULT_FILE():Int return untyped __php__("PDO::MYSQL_ATTR_READ_DEFAULT_FILE");
+    
+    public static var MYSQL_ATTR_READ_DEFAULT_GROUP(get, never):Int;
+	private static inline function get_MYSQL_ATTR_READ_DEFAULT_GROUP():Int return untyped __php__("PDO::MYSQL_ATTR_READ_DEFAULT_GROUP");
+    
+    public static var MYSQL_ATTR_MAX_BUFFER_SIZE(get, never):Int;
+	private static inline function get_MYSQL_ATTR_MAX_BUFFER_SIZE():Int return untyped __php__("PDO::MYSQL_ATTR_MAX_BUFFER_SIZE");
+    
+    public static var MYSQL_ATTR_DIRECT_QUERY(get, never):Int;
+	private static inline function get_MYSQL_ATTR_DIRECT_QUERY():Int return untyped __php__("PDO::MYSQL_ATTR_DIRECT_QUERY");
+    
+    public static var MYSQL_ATTR_FOUND_ROWS(get, never):Int;
+	private static inline function get_MYSQL_ATTR_FOUND_ROWS():Int return untyped __php__("PDO::MYSQL_ATTR_FOUND_ROWS");
+    
+    public static var MYSQL_ATTR_IGNORE_SPACE(get, never):Int;
+	private static inline function get_MYSQL_ATTR_IGNORE_SPACE():Int return untyped __php__("PDO::MYSQL_ATTR_IGNORE_SPACE");
+    
+    public static var MYSQL_ATTR_COMPRESS(get, never):Int;
+	private static inline function get_MYSQL_ATTR_COMPRESS():Int return untyped __php__("PDO::MYSQL_ATTR_COMPRESS");
+    
+    public static var MYSQL_ATTR_SSL_CA(get, never):Int;
+	private static inline function get_MYSQL_ATTR_SSL_CA():Int return untyped __php__("PDO::MYSQL_ATTR_SSL_CA");
+    
+    public static var MYSQL_ATTR_SSL_CAPATH(get, never):Int;
+	private static inline function get_MYSQL_ATTR_SSL_CAPATH():Int return untyped __php__("PDO::MYSQL_ATTR_SSL_CAPATH");
+    
+    public static var MYSQL_ATTR_SSL_CERT(get, never):Int;
+	private static inline function get_MYSQL_ATTR_SSL_CERT():Int return untyped __php__("PDO::MYSQL_ATTR_SSL_CERT");
+    
+    public static var MYSQL_ATTR_SSL_CIPHER(get, never):Int;
+	private static inline function get_MYSQL_ATTR_SSL_CIPHER():Int return untyped __php__("PDO::MYSQL_ATTR_SSL_CIPHER");
+    
+    public static var MYSQL_ATTR_SSL_KEY(get, never):Int;
+	private static inline function get_MYSQL_ATTR_SSL_KEY():Int return untyped __php__("PDO::MYSQL_ATTR_SSL_KEY");
+    
+    public static var MYSQL_ATTR_MULTI_STATEMENTS(get, never):Int;
+	private static inline function get_MYSQL_ATTR_MULTI_STATEMENTS():Int return untyped __php__("PDO::MYSQL_ATTR_MULTI_STATEMENTS");
+    
+	@:overload(function(dns : String):Void{})
+	@:overload(function(dns : String, username : String):Void{})
+	@:overload(function(dns : String, username : String, password : String):Void { } )
+	public function new(dns : String, username : String, password : String, driver_options : NativeArray):Void;
+	
 	public function beginTransaction() : Bool;
 	public function commit() : Bool;
 	public function errorCode() : Dynamic;
@@ -54,34 +183,73 @@ extern class PDOClass
 	public function getAttribute(attribute : Int) : Dynamic;
 	public function getAvailableDrivers() : NativeArray;
 	public function lastInsertId(?name : String) : String;
+	
+	@:overload(function(statement : String):PDOStatement { })
 	public function prepare(statement : String, driver_options : NativeArray) : PDOStatement;
-	public function query(statement : String, mode : Int) : PDOStatement;
-	public function quote(String : String, ?parameter_type : Int = 2) : String;
+	
+	@:overload(function(statement : String, mode : Int):PDOStatement { })
+	@:overload(function(statement : String, mode : Int, colno:Int):PDOStatement { })
+	@:overload(function(statement : String, mode : Int, object:Dynamic):PDOStatement { })
+	@:overload(function(statement : String, mode : Int, classname:String, ctorargs:NativeArray):PDOStatement { })
+	public function query(statement : String) : PDOStatement;
+	
+    @:overload(function(string:String):String {})
+	public function quote(string : String, parameter_type : Int) : String;
 	public function rollBack() : Bool;
 	public function setAttribute(attribute : Int, value : Dynamic) : Bool;
+    public function inTransaction():Bool;
 }
 
 extern class PDOStatement
 {
-	public function bindColumn(column : Dynamic, param : Dynamic, ?type : Int, ?maxlen : Int, ?driverdata : Dynamic) : Bool;
-	public function bindParam(parameter : Dynamic, variable : Dynamic, ?data_type : Int, ?length : Int, ?driver_options : Dynamic) : Bool;
-	public function bindValue(parameter : Dynamic, value : Dynamic, ?data_type : Int) : Bool;
+	public var queryString(default, null):String;
+	
+	@:overload(function(column : Dynamic, param : Dynamic):Bool { })
+	@:overload(function(column : Dynamic, param : Dynamic, type : Int):Bool { })
+	@:overload(function(column : Dynamic, param : Dynamic, type : Int, maxlen : Int):Bool { })
+	public function bindColumn(column : Dynamic, param : Dynamic, type : Int, maxlen : Int, driverdata : Dynamic) : Bool;
+	
+	@:overload(function(parameter : Dynamic, variable : Dynamic, data_type : Int, length : Int):Bool{})
+	@:overload(function(parameter : Dynamic, variable : Dynamic, data_type : Int, length : Int, driver_options : Dynamic):Bool{})
+	public function bindParam(parameter : Dynamic, variable : Dynamic, ?data_type : Int = PDOClass.PARAM_STR) : Bool;
+	
+    @:overload(function(parameter : Dynamic, value : Dynamic) : Bool{})
+	public function bindValue(parameter : Dynamic, value : Dynamic, data_type : Int) : Bool;
 	public function closeCursor() : Bool;
 	public function columnCount() : Int;
 	public function debugDumpParams() : Bool;
 	public function errorCode() : String;
 	public function errorInfo() : NativeArray;
+	
+	@:overload(function():Bool{})
 	public function execute(input_parameters : NativeArray) : Bool;
-	public function fetch(?fetch_style : Int = 4, ?cursor_orientation : Int = 0, ?cursor_offset : Int = 0) : Dynamic;
-	public function fetchAll(?fetch_style : Int) : NativeArray;
-	public function fetchColumn(?column_number : Int = 0) : String;
-	public function fetchObject(?class_name : String, ?ctor_args : NativeArray) : Dynamic;
+	
+	@:overload(function():Dynamic{})
+	@:overload(function(fetch_style : Int):Dynamic{})
+	@:overload(function(fetch_style : Int, cursor_orientation : Int):Dynamic{})
+	public function fetch(fetch_style : Int, cursor_orientation : Int, cursor_offset : Int) : Dynamic;
+	
+	@:overload(function():Dynamic{})
+	@:overload(function(fetch_style : Int):Dynamic{})
+	@:overload(function(fetch_style : Int, fetch_argument:Dynamic):Dynamic{})
+	public function fetchAll(fetch_style : Int, fetch_argument:Dynamic, ctor_args:NativeArray) : NativeArray;
+	
+    @:overload(function():String{})
+	public function fetchColumn(column_number : Int) : String;
+	
+	@:overload(function(): Dynamic{})
+	@:overload(function(class_name : String): Dynamic{})
+	public function fetchObject(class_name : String, ctor_args : NativeArray) : Dynamic;
+	
 	public function getAttribute(attribute : Int) : Dynamic;
 	public function getColumnMeta(column : Int) : NativeArray;
 	public function nextRowset() : Bool;
 	public function rowCount() : Int;
 	public function setAttribute(attribute : Int, value : Dynamic) : Bool;
-	public function setFetchMode(mode : Int, ?fetch : Dynamic, ?ctorargs : NativeArray) : Bool;
+	
+	@:overload(function(mode : Int) : Bool{})
+	@:overload(function(mode : Int, fetch : Dynamic) : Bool{})
+	public function setFetchMode(mode : Int, fetch : Dynamic, ctorargs : NativeArray) : Bool;
 }
 
 private class PDOConnection implements Connection {
@@ -91,13 +259,13 @@ private class PDOConnection implements Connection {
 
 	public function new(dsn : String, ?user : String, ?password : String, ?options : Dynamic) {
 		if(null == options)
-			pdo = untyped __call__("new PDO", dsn, user, password);
+			pdo = new PDOClass(dsn, user, password);
 		else
 		{
 			var arr : NativeArray = untyped __call__("array");
 			for (key in Reflect.fields(options))
 				arr[untyped key] = Reflect.field(options, key);
-			pdo = untyped __call__("new PDO", dsn, user, password, arr);
+			pdo = new PDOClass(dsn, user, password, arr);
 		}
 		dbname = dsn.split(':').shift();
 		switch(dbname.toLowerCase())

+ 28 - 0
std/php/db/PDOException.hx

@@ -0,0 +1,28 @@
+/*
+ * Copyright (C)2005-2012 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package php.db;
+import php.RuntimeException;
+
+extern class PDOException extends RuntimeException{
+	public var errorInfo:NativeArray;
+	//private var code:String;
+}

+ 0 - 1
tests/sys/lua

@@ -1 +0,0 @@
-/Users/jdonaldson/bin/lua

+ 1 - 0
tests/sys/lua

@@ -0,0 +1 @@
+/Users/jdonaldson/bin/lua