PageRenderTime 29ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/std/neko/db/Sqlite.hx

http://github.com/Yoomee/clippy
Haxe | 200 lines | 143 code | 33 blank | 24 comment | 23 complexity | b878c699cc47f6d892d00086c78e36d7 MD5 | raw file
  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 */
 25package neko.db;
 26
 27import neko.db.Connection;
 28
 29private class SqliteConnection implements Connection {
 30
 31	var c : Void;
 32
 33	public function new( file : String ) {
 34		c = _connect(untyped file.__s);
 35	}
 36
 37	public function close() {
 38		_close(c);
 39	}
 40
 41	public function request( s : String ) : ResultSet {
 42		try {
 43			return new SqliteResultSet(_request(c,untyped s.__s));
 44		} catch( e : String ) {
 45			throw "Error while executing "+s+" ("+e+")";
 46		}
 47	}
 48
 49	public function escape( s : String ) {
 50		return s.split("'").join("''");
 51	}
 52
 53	public function quote( s : String ) {
 54		if( s.indexOf("\000") >= 0 )
 55			return "x'"+new String(untyped _encode(s.__s,"0123456789ABCDEF".__s))+"'";
 56		return "'"+s.split("'").join("''")+"'";
 57	}
 58
 59	public function addValue( s : StringBuf, v : Dynamic ) {
 60		var t = untyped __dollar__typeof(v);
 61		if( untyped (t == __dollar__tint || t == __dollar__tnull) )
 62			s.add(v);
 63		else if( untyped t == __dollar__tbool )
 64			s.add(if( v ) 1 else 0);
 65		else
 66			s.add(quote(Std.string(v)));
 67	}
 68
 69	public function lastInsertId() {
 70		return _last_id(c);
 71	}
 72
 73	public function dbName() {
 74		return "SQLite";
 75	}
 76
 77	public function startTransaction() {
 78		request("BEGIN TRANSACTION");
 79	}
 80
 81	public function commit() {
 82		request("COMMIT");
 83		startTransaction(); // match mysql usage
 84	}
 85
 86	public function rollback() {
 87		request("ROLLBACK");
 88		startTransaction(); // match mysql usage
 89	}
 90
 91	static var _encode = neko.Lib.load("std","base_encode",2);
 92	static var _connect = neko.Lib.load("sqlite","connect",1);
 93	static var _close = neko.Lib.load("sqlite","close",1);
 94	static var _request = neko.Lib.load("sqlite","request",2);
 95	static var _last_id = neko.Lib.load("sqlite","last_insert_id",1);
 96}
 97
 98
 99private class SqliteResultSet implements ResultSet {
100
101	public var length(getLength,null) : Int;
102	public var nfields(getNFields,null) : Int;
103	var r : Void;
104	var cache : List<Dynamic>;
105
106	public function new( r ) {
107		cache = new List();
108		this.r = r;
109		hasNext(); // execute the request
110	}
111
112	function getLength() {
113		if( nfields != 0 ) {
114			while( true ) {
115				var c = doNext();
116				if( c == null )
117					break;
118				cache.add(c);
119			}
120			return cache.length;
121		}
122		return result_get_length(r);
123	}
124
125	function getNFields() {
126		return result_get_nfields(r);
127	}
128
129	public function hasNext() {
130		var c = next();
131		if( c == null )
132			return false;
133		cache.push(c);
134		return true;
135	}
136
137	public function next() : Dynamic {
138		var c = cache.pop();
139		if( c != null )
140			return c;
141		return doNext();
142	}
143
144	private function doNext() : Dynamic {
145		var c = result_next(r);
146		if( c == null )
147			return null;
148		untyped {
149			var f = __dollar__objfields(c);
150			var i = 0;
151			var l = __dollar__asize(f);
152			while( i < l ) {
153				var v = __dollar__objget(c,f[i]);
154				if( __dollar__typeof(v) == __dollar__tstring )
155					__dollar__objset(c,f[i],new String(v));
156				i = i + 1;
157			}
158		}
159		return c;
160	}
161
162	public function results() : List<Dynamic> {
163		var l = new List();
164		while( true ) {
165			var c = next();
166			if( c == null )
167				break;
168			l.add(c);
169		}
170		return l;
171	}
172
173	public function getResult( n : Int ) {
174		return new String(result_get(r,n));
175	}
176
177	public function getIntResult( n : Int ) : Int {
178		return result_get_int(r,n);
179	}
180
181	public function getFloatResult( n : Int ) : Float {
182		return result_get_float(r,n);
183	}
184
185	static var result_next = neko.Lib.load("sqlite","result_next",1);
186	static var result_get_length = neko.Lib.load("sqlite","result_get_length",1);
187	static var result_get_nfields = neko.Lib.load("sqlite","result_get_nfields",1);
188	static var result_get = neko.Lib.load("sqlite","result_get",2);
189	static var result_get_int = neko.Lib.load("sqlite","result_get_int",2);
190	static var result_get_float = neko.Lib.load("sqlite","result_get_float",2);
191
192}
193
194class Sqlite {
195
196	public static function open( file : String ) : Connection {
197		return new SqliteConnection(file);
198	}
199
200}