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