Sqlite.hx 4.5 KB

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