Sqlite.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. }
  77. public function rollback() {
  78. request("ROLLBACK");
  79. }
  80. @:native("_hx_sqlite_connect")
  81. extern public static function _connect(filename:String):Dynamic;
  82. @:native("_hx_sqlite_request")
  83. extern public static function _request(handle:Dynamic, req:String):Dynamic;
  84. @:native("_hx_sqlite_close")
  85. extern public static function _close(handle:Dynamic):Void;
  86. @:native("_hx_sqlite_last_insert_id")
  87. extern public static function _last_id(handle:Dynamic):Int;
  88. }
  89. private class SqliteResultSet implements ResultSet {
  90. public var length(get, null):Int;
  91. public var nfields(get, null):Int;
  92. var r:Dynamic;
  93. var cache:List<Dynamic>;
  94. public function new(r:Dynamic) {
  95. cache = new List();
  96. this.r = r;
  97. hasNext(); // execute the request
  98. }
  99. function get_length() {
  100. if (nfields != 0) {
  101. while (true) {
  102. var c = result_next(r);
  103. if (c == null)
  104. break;
  105. cache.add(c);
  106. }
  107. return cache.length;
  108. }
  109. return result_get_length(r);
  110. }
  111. function get_nfields() {
  112. return result_get_nfields(r);
  113. }
  114. public function hasNext() {
  115. var c = next();
  116. if (c == null)
  117. return false;
  118. cache.push(c);
  119. return true;
  120. }
  121. public function next():Dynamic {
  122. var c = cache.pop();
  123. if (c != null)
  124. return c;
  125. return result_next(r);
  126. }
  127. public function results():List<Dynamic> {
  128. var l = new List();
  129. while (true) {
  130. var c = next();
  131. if (c == null)
  132. break;
  133. l.add(c);
  134. }
  135. return l;
  136. }
  137. public function getResult(n:Int) {
  138. return new String(result_get(r, n));
  139. }
  140. public function getIntResult(n:Int):Int {
  141. return result_get_int(r, n);
  142. }
  143. public function getFloatResult(n:Int):Float {
  144. return result_get_float(r, n);
  145. }
  146. public function getFieldsNames():Array<String> {
  147. throw new haxe.exceptions.NotImplementedException();
  148. }
  149. @:native("_hx_sqlite_result_next")
  150. extern public static function result_next(handle:Dynamic):Dynamic;
  151. @:native("_hx_sqlite_result_get_length")
  152. extern public static function result_get_length(handle:Dynamic):Int;
  153. @:native("_hx_sqlite_result_get_nfields")
  154. extern public static function result_get_nfields(handle:Dynamic):Int;
  155. @:native("_hx_sqlite_result_get")
  156. extern public static function result_get(handle:Dynamic, i:Int):String;
  157. @:native("_hx_sqlite_result_get_int")
  158. extern public static function result_get_int(handle:Dynamic, i:Int):Int;
  159. @:native("_hx_sqlite_result_get_float")
  160. extern public static function result_get_float(handle:Dynamic, i:Int):Float;
  161. }
  162. @:buildXml('<include name="${HXCPP}/src/hx/libs/sqlite/Build.xml"/>')
  163. @:coreApi class Sqlite {
  164. public static function open(file:String):Connection {
  165. return new SqliteConnection(file);
  166. }
  167. }