~ubuntu-branches/ubuntu/oneiric/haxe/oneiric

« back to all changes in this revision

Viewing changes to haxe/std/php/db/Mysql.hx

  • Committer: Bazaar Package Importer
  • Author(s): Jens Peter Secher
  • Date: 2009-03-18 23:09:50 UTC
  • mfrom: (3.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090318230950-pgfuxg2ucolps74t
Tags: 1:2.2-2
* Use ocamlfind to locate and use the libraries xml-light and extlib
  which already exist in Debian as separate packages.
  (Closes: #519630)
* Fixed compile error with camlp4 3.11, thanks to Stéphane Glondu.
  (Closes: #519627)
* Use quilt instead of dpatch for patches, and describe how to use
  quilt in Debian.source (thanks to Russ Allbery).
* Added a Vcs-Hg control filed to indicate the location of the public
  repository.
* Bumped Standards-Version to 3.8.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005, The haXe Project Contributors
 
3
 * All rights reserved.
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
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.
 
12
 *
 
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
 
23
 * DAMAGE.
 
24
 */
 
25
package php.db;
 
26
 
 
27
import php.db.Connection;
 
28
 
 
29
private class MysqlConnection implements Connection {
 
30
 
 
31
        var c : Void;
 
32
 
 
33
        public function new( c : Void) {
 
34
                this.c = c;
 
35
        }
 
36
 
 
37
        public function close() {
 
38
                untyped __call__("mysql_close", c);
 
39
                untyped __call__("unset", c);
 
40
        }
 
41
 
 
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);
 
47
        }
 
48
 
 
49
        public function escape( s : String ) {
 
50
                return untyped __call__("mysql_real_escape_string", s, c);
 
51
        }
 
52
 
 
53
        public function quote( s : String ) {
 
54
                return "'" + untyped __call__("mysql_real_escape_string", s, c) + "'";
 
55
        }
 
56
 
 
57
        public function lastInsertId() {
 
58
                return untyped __call__("mysql_insert_id", c);
 
59
        }
 
60
 
 
61
        public function dbName() {
 
62
                return "MySQL";
 
63
        }
 
64
 
 
65
        public function startTransaction() {
 
66
                request("START TRANSACTION");
 
67
        }
 
68
 
 
69
        public function commit() {
 
70
                request("COMMIT");
 
71
        }
 
72
 
 
73
        public function rollback() {
 
74
                request("ROLLBACK");
 
75
        }
 
76
}
 
77
 
 
78
 
 
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;
 
84
 
 
85
        public function new(r) {
 
86
                __r = r;
 
87
        }
 
88
 
 
89
        private function getLength() {
 
90
                if(untyped __physeq__(__r, true))
 
91
                        return untyped __call__("mysql_affected_rows");
 
92
                else if (untyped __physeq__(__r, false))
 
93
                        return 0;
 
94
                return untyped __call__("mysql_num_rows", __r);
 
95
        }
 
96
        
 
97
        private var _nfields : Int;
 
98
        private function getNFields() {
 
99
                if(_nfields == null)
 
100
                        _nfields = untyped __call__("mysql_num_fields", __r);
 
101
                return _nfields;
 
102
        }
 
103
 
 
104
        private var _fieldsDesc : Array<Dynamic>;
 
105
        private function getFieldsDescription() {
 
106
                if(_fieldsDesc == null) {
 
107
                        _fieldsDesc = [];
 
108
                        for(i in 0...nfields) {
 
109
                                _fieldsDesc.push({
 
110
                                        name : untyped __call__("mysql_field_name", __r, i),
 
111
                                        type : untyped __call__("mysql_field_type", __r, i)
 
112
                                });
 
113
                        }
 
114
                }
 
115
                return _fieldsDesc;
 
116
        }
 
117
 
 
118
        private function convert(v : String, type : String) : Dynamic {
 
119
                if(v == null) return v;
 
120
                switch(type) {
 
121
                        case "year", "int":
 
122
                                return untyped __call__("intval", v);
 
123
                        case "real":
 
124
                                return untyped __call__("floatval", v);
 
125
                        case "datetime", "date":
 
126
                                return Date.fromString(v);
 
127
                        default:
 
128
                                return v;
 
129
                }
 
130
        }
 
131
 
 
132
        public function hasNext() {
 
133
                if( cache == null )
 
134
                        cache = next();
 
135
                return (cache != null);
 
136
        }
 
137
 
 
138
        public function next() : Dynamic {
 
139
                if( cache != null ) {
 
140
                        var t = cache;
 
141
                        cache = null;
 
142
                        return t;
 
143
                }
 
144
                var c = untyped __call__("mysql_fetch_array", __r, __php__("MYSQL_NUM"));
 
145
                if(untyped __physeq__(c, false))
 
146
                        return null;
 
147
 
 
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));
 
152
                return o;
 
153
        }
 
154
 
 
155
        public function results() : List<Dynamic> {
 
156
                var l = new List();
 
157
                while( hasNext() )
 
158
                        l.add(next());
 
159
                return l;
 
160
        }
 
161
 
 
162
        public function getResult( n : Int ) : String {
 
163
                var a = untyped __call__("mysql_fetch_row", __r);
 
164
                return a[n];
 
165
        }
 
166
 
 
167
        public function getIntResult( n : Int ) : Int {
 
168
                return untyped __call__("intval", getResult(n));
 
169
        }
 
170
 
 
171
        public function getFloatResult( n : Int ) : Float {
 
172
                return untyped __call__("floatval", getResult(n));
 
173
        }
 
174
}
 
175
 
 
176
class Mysql {
 
177
 
 
178
        public static function connect( params : {
 
179
                host : String,
 
180
                port : Int,
 
181
                user : String,
 
182
                pass : String,
 
183
                socket : String,
 
184
                database : String
 
185
        } ) : php.db.Connection {
 
186
                var c = untyped __call__("mysql_connect",
 
187
                        params.host + (params.port == null ? '' : ':'+params.port) + (params.socket == null ? '' : ':'+params.socket),
 
188
                        params.user,
 
189
                        params.pass);
 
190
                if(!untyped __call__("mysql_select_db", params.database, c))
 
191
                        throw "Unable to connect to " + params.database;
 
192
                return new MysqlConnection(c);
 
193
        }
 
194
 
 
195
}