Sqlite.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C)2005-2015 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.db;
  23. private class SqliteConnection implements Connection {
  24. var c : Dynamic;
  25. public function new( file : String ) {
  26. c = _connect(file);
  27. }
  28. public function close() {
  29. _close(c);
  30. }
  31. public function request( s : String ) : ResultSet {
  32. try {
  33. return new SqliteResultSet(_request(c,s));
  34. } catch( e : String ) {
  35. throw "Error while executing "+s+" ("+e+")";
  36. }
  37. }
  38. public function escape( s : String ) {
  39. return s.split("'").join("''");
  40. }
  41. public function quote( s : String ) {
  42. if( s.indexOf("\000") >= 0 )
  43. return "x'"+new String(_encode(s,"0123456789ABCDEF"))+"'";
  44. return "'"+s.split("'").join("''")+"'";
  45. }
  46. public function addValue( s : StringBuf, v : Dynamic ) {
  47. if (v == null) {
  48. s.add(v);
  49. }
  50. else if (Std.is(v,Bool)) {
  51. s.add( v ? 1 : 0 );
  52. } else {
  53. var t:Int = untyped v.__GetType();
  54. if( t == 0xff )
  55. s.add(v);
  56. else if( t == 2 )
  57. s.add( untyped v.__GetInt() );
  58. else
  59. s.add(quote(Std.string(v)));
  60. }
  61. }
  62. public function lastInsertId() {
  63. return _last_id(c);
  64. }
  65. public function dbName() {
  66. return "SQLite";
  67. }
  68. public function startTransaction() {
  69. request("BEGIN TRANSACTION");
  70. }
  71. public function commit() {
  72. request("COMMIT");
  73. startTransaction(); // match mysql usage
  74. }
  75. public function rollback() {
  76. request("ROLLBACK");
  77. startTransaction(); // match mysql usage
  78. }
  79. static var _encode = cpp.Lib.load("std","base_encode",2);
  80. static var _connect = cpp.Lib.load("sqlite","sqlite_connect",1);
  81. static var _close = cpp.Lib.load("sqlite","close",1);
  82. static var _request = cpp.Lib.load("sqlite","request",2);
  83. static var _last_id = cpp.Lib.load("sqlite","last_insert_id",1);
  84. }
  85. private class SqliteResultSet implements ResultSet {
  86. public var length(get,null) : Int;
  87. public var nfields(get,null) : Int;
  88. var r : Dynamic;
  89. var cache : List<Dynamic>;
  90. public function new( r ) {
  91. cache = new List();
  92. this.r = r;
  93. hasNext(); // execute the request
  94. }
  95. function get_length() {
  96. if( nfields != 0 ) {
  97. while( true ) {
  98. var c = result_next(r);
  99. if( c == null )
  100. break;
  101. cache.add(c);
  102. }
  103. return cache.length;
  104. }
  105. return result_get_length(r);
  106. }
  107. function get_nfields() {
  108. return result_get_nfields(r);
  109. }
  110. public function hasNext() {
  111. var c = next();
  112. if( c == null )
  113. return false;
  114. cache.push(c);
  115. return true;
  116. }
  117. public function next() : Dynamic {
  118. var c = cache.pop();
  119. if( c != null )
  120. return c;
  121. return result_next(r);
  122. }
  123. public function results() : List<Dynamic> {
  124. var l = new List();
  125. while( true ) {
  126. var c = next();
  127. if( c == null )
  128. break;
  129. l.add(c);
  130. }
  131. return l;
  132. }
  133. public function getResult( n : Int ) {
  134. return new String(result_get(r,n));
  135. }
  136. public function getIntResult( n : Int ) : Int {
  137. return result_get_int(r,n);
  138. }
  139. public function getFloatResult( n : Int ) : Float {
  140. return result_get_float(r,n);
  141. }
  142. public function getFieldsNames() : Array<String> {
  143. return null;
  144. }
  145. static var result_next = cpp.Lib.load("sqlite","result_next",1);
  146. static var result_get_length = cpp.Lib.load("sqlite","result_get_length",1);
  147. static var result_get_nfields = cpp.Lib.load("sqlite","result_get_nfields",1);
  148. static var result_get = cpp.Lib.load("sqlite","result_get",2);
  149. static var result_get_int = cpp.Lib.load("sqlite","result_get_int",2);
  150. static var result_get_float = cpp.Lib.load("sqlite","result_get_float",2);
  151. }
  152. @:coreApi class Sqlite {
  153. public static function open( file : String ) : Connection {
  154. return new SqliteConnection(file);
  155. }
  156. }