Sqlite.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 lastInsertId() {
  51. return _last_id(c);
  52. }
  53. public function dbName() {
  54. return "SQLite";
  55. }
  56. public function startTransaction() {
  57. request("BEGIN TRANSACTION");
  58. }
  59. public function commit() {
  60. request("COMMIT");
  61. startTransaction(); // match mysql usage
  62. }
  63. public function rollback() {
  64. request("ROLLBACK");
  65. startTransaction(); // match mysql usage
  66. }
  67. static var _encode = neko.Lib.load("std","base_encode",2);
  68. static var _connect = neko.Lib.load("sqlite","connect",1);
  69. static var _close = neko.Lib.load("sqlite","close",1);
  70. static var _request = neko.Lib.load("sqlite","request",2);
  71. static var _last_id = neko.Lib.load("sqlite","last_insert_id",1);
  72. }
  73. private class SqliteResultSet implements ResultSet {
  74. public var length(getLength,null) : Int;
  75. public var nfields(getNFields,null) : Int;
  76. var r : Void;
  77. var cache : List<Dynamic>;
  78. public function new( r ) {
  79. cache = new List();
  80. this.r = r;
  81. hasNext(); // execute the request
  82. }
  83. function getLength() {
  84. if( nfields != 0 ) {
  85. while( true ) {
  86. var c = doNext();
  87. if( c == null )
  88. break;
  89. cache.add(c);
  90. }
  91. return cache.length;
  92. }
  93. return result_get_length(r);
  94. }
  95. function getNFields() {
  96. return result_get_nfields(r);
  97. }
  98. public function hasNext() {
  99. var c = next();
  100. if( c == null )
  101. return false;
  102. cache.push(c);
  103. return true;
  104. }
  105. public function next() : Dynamic {
  106. var c = cache.pop();
  107. if( c != null )
  108. return c;
  109. return doNext();
  110. }
  111. private function doNext() : Dynamic {
  112. var c = result_next(r);
  113. if( c == null )
  114. return null;
  115. untyped {
  116. var f = __dollar__objfields(c);
  117. var i = 0;
  118. var l = __dollar__asize(f);
  119. while( i < l ) {
  120. var v = __dollar__objget(c,f[i]);
  121. if( __dollar__typeof(v) == __dollar__tstring )
  122. __dollar__objset(c,f[i],new String(v));
  123. i = i + 1;
  124. }
  125. }
  126. return c;
  127. }
  128. public function results() : List<Dynamic> {
  129. var l = new List();
  130. while( true ) {
  131. var c = next();
  132. if( c == null )
  133. break;
  134. l.add(c);
  135. }
  136. return l;
  137. }
  138. public function getResult( n : Int ) {
  139. return new String(result_get(r,n));
  140. }
  141. public function getIntResult( n : Int ) : Int {
  142. return result_get_int(r,n);
  143. }
  144. public function getFloatResult( n : Int ) : Float {
  145. return result_get_float(r,n);
  146. }
  147. static var result_next = neko.Lib.load("sqlite","result_next",1);
  148. static var result_get_length = neko.Lib.load("sqlite","result_get_length",1);
  149. static var result_get_nfields = neko.Lib.load("sqlite","result_get_nfields",1);
  150. static var result_get = neko.Lib.load("sqlite","result_get",2);
  151. static var result_get_int = neko.Lib.load("sqlite","result_get_int",2);
  152. static var result_get_float = neko.Lib.load("sqlite","result_get_float",2);
  153. }
  154. class Sqlite {
  155. public static function open( file : String ) : Connection {
  156. return new SqliteConnection(file);
  157. }
  158. }