Sqlite.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C)2005-2019 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. var hexChars = new Array<String>();
  44. for (i in 0...s.length)
  45. hexChars.push(StringTools.hex(StringTools.fastCodeAt(s, i), 2));
  46. return "x'" + hexChars.join("") + "'";
  47. }
  48. return "'" + s.split("'").join("''") + "'";
  49. }
  50. public function addValue(s:StringBuf, v:Dynamic) {
  51. if (v == null) {
  52. s.add(v);
  53. } else if (Std.isOfType(v, Bool)) {
  54. s.add(v ? 1 : 0);
  55. } else {
  56. var t:Int = untyped v.__GetType();
  57. if (t == 0xff)
  58. s.add(v);
  59. else if (t == 2)
  60. s.add(untyped v.__GetInt());
  61. else
  62. s.add(quote(Std.string(v)));
  63. }
  64. }
  65. public function lastInsertId():Int {
  66. return _last_id(c);
  67. }
  68. public function dbName() {
  69. return "SQLite";
  70. }
  71. public function startTransaction() {
  72. request("BEGIN TRANSACTION");
  73. }
  74. public function commit() {
  75. request("COMMIT");
  76. startTransaction(); // match mysql usage
  77. }
  78. public function rollback() {
  79. request("ROLLBACK");
  80. startTransaction(); // match mysql usage
  81. }
  82. @:native("_hx_sqlite_connect")
  83. extern public static function _connect(filename:String):Dynamic;
  84. @:native("_hx_sqlite_request")
  85. extern public static function _request(handle:Dynamic, req:String):Dynamic;
  86. @:native("_hx_sqlite_close")
  87. extern public static function _close(handle:Dynamic):Void;
  88. @:native("_hx_sqlite_last_insert_id")
  89. extern public static function _last_id(handle:Dynamic):Int;
  90. }
  91. private class SqliteResultSet implements ResultSet {
  92. public var length(get, null):Int;
  93. public var nfields(get, null):Int;
  94. var r:Dynamic;
  95. var cache:List<Dynamic>;
  96. public function new(r:Dynamic) {
  97. cache = new List();
  98. this.r = r;
  99. hasNext(); // execute the request
  100. }
  101. function get_length() {
  102. if (nfields != 0) {
  103. while (true) {
  104. var c = result_next(r);
  105. if (c == null)
  106. break;
  107. cache.add(c);
  108. }
  109. return cache.length;
  110. }
  111. return result_get_length(r);
  112. }
  113. function get_nfields() {
  114. return result_get_nfields(r);
  115. }
  116. public function hasNext() {
  117. var c = next();
  118. if (c == null)
  119. return false;
  120. cache.push(c);
  121. return true;
  122. }
  123. public function next():Dynamic {
  124. var c = cache.pop();
  125. if (c != null)
  126. return c;
  127. return result_next(r);
  128. }
  129. public function results():List<Dynamic> {
  130. var l = new List();
  131. while (true) {
  132. var c = next();
  133. if (c == null)
  134. break;
  135. l.add(c);
  136. }
  137. return l;
  138. }
  139. public function getResult(n:Int) {
  140. return new String(result_get(r, n));
  141. }
  142. public function getIntResult(n:Int):Int {
  143. return result_get_int(r, n);
  144. }
  145. public function getFloatResult(n:Int):Float {
  146. return result_get_float(r, n);
  147. }
  148. public function getFieldsNames():Array<String> {
  149. return null;
  150. }
  151. @:native("_hx_sqlite_result_next")
  152. extern public static function result_next(handle:Dynamic):Dynamic;
  153. @:native("_hx_sqlite_result_get_length")
  154. extern public static function result_get_length(handle:Dynamic):Int;
  155. @:native("_hx_sqlite_result_get_nfields")
  156. extern public static function result_get_nfields(handle:Dynamic):Int;
  157. @:native("_hx_sqlite_result_get")
  158. extern public static function result_get(handle:Dynamic, i:Int):String;
  159. @:native("_hx_sqlite_result_get_int")
  160. extern public static function result_get_int(handle:Dynamic, i:Int):Int;
  161. @:native("_hx_sqlite_result_get_float")
  162. extern public static function result_get_float(handle:Dynamic, i:Int):Float;
  163. }
  164. @:buildXml('<include name="${HXCPP}/src/hx/libs/sqlite/Build.xml"/>')
  165. @:coreApi class Sqlite {
  166. public static function open(file:String):Connection {
  167. return new SqliteConnection(file);
  168. }
  169. }