Sqlite.hx 4.9 KB

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