2
* Copyright (c) 2005, The haXe Project Contributors
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* - Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* - Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
* DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
17
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27
import php.db.Connection;
29
private class MysqlConnection implements Connection {
33
public function new( c : Void) {
37
public function close() {
38
untyped __call__("mysql_close", c);
39
untyped __call__("unset", c);
42
public function request( s : String ) : ResultSet {
43
var h = untyped __call__("mysql_query", s, c);
44
if(untyped __physeq__(h, false))
45
throw "Error while executing "+s+" ("+untyped __call__("mysql_error", c)+")";
46
return new MysqlResultSet(cast h);
49
public function escape( s : String ) {
50
return untyped __call__("mysql_real_escape_string", s, c);
53
public function quote( s : String ) {
54
return "'" + untyped __call__("mysql_real_escape_string", s, c) + "'";
57
public function lastInsertId() {
58
return untyped __call__("mysql_insert_id", c);
61
public function dbName() {
65
public function startTransaction() {
66
request("START TRANSACTION");
69
public function commit() {
73
public function rollback() {
79
private class MysqlResultSet implements ResultSet {
80
public var length(getLength,null) : Int;
81
public var nfields(getNFields,null) : Int;
82
private var __r : Void;
83
private var cache : Dynamic;
85
public function new(r) {
89
private function getLength() {
90
if(untyped __physeq__(__r, true))
91
return untyped __call__("mysql_affected_rows");
92
else if (untyped __physeq__(__r, false))
94
return untyped __call__("mysql_num_rows", __r);
97
private var _nfields : Int;
98
private function getNFields() {
100
_nfields = untyped __call__("mysql_num_fields", __r);
104
private var _fieldsDesc : Array<Dynamic>;
105
private function getFieldsDescription() {
106
if(_fieldsDesc == null) {
108
for(i in 0...nfields) {
110
name : untyped __call__("mysql_field_name", __r, i),
111
type : untyped __call__("mysql_field_type", __r, i)
118
private function convert(v : String, type : String) : Dynamic {
119
if(v == null) return v;
122
return untyped __call__("intval", v);
124
return untyped __call__("floatval", v);
125
case "datetime", "date":
126
return Date.fromString(v);
132
public function hasNext() {
135
return (cache != null);
138
public function next() : Dynamic {
139
if( cache != null ) {
144
var c = untyped __call__("mysql_fetch_array", __r, __php__("MYSQL_NUM"));
145
if(untyped __physeq__(c, false))
148
var o : Dynamic = {};
149
var descriptions = getFieldsDescription();
150
for(i in 0...nfields)
151
Reflect.setField(o, descriptions[i].name, convert(c[i], descriptions[i].type));
155
public function results() : List<Dynamic> {
162
public function getResult( n : Int ) : String {
163
var a = untyped __call__("mysql_fetch_row", __r);
167
public function getIntResult( n : Int ) : Int {
168
return untyped __call__("intval", getResult(n));
171
public function getFloatResult( n : Int ) : Float {
172
return untyped __call__("floatval", getResult(n));
178
public static function connect( params : {
185
} ) : php.db.Connection {
186
var c = untyped __call__("mysql_connect",
187
params.host + (params.port == null ? '' : ':'+params.port) + (params.socket == null ? '' : ':'+params.socket),
190
if(!untyped __call__("mysql_select_db", params.database, c))
191
throw "Unable to connect to " + params.database;
192
return new MysqlConnection(c);