Sqlite.hx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. import php.*;
  24. import php.Global.*;
  25. import php.db.*;
  26. import sys.db.*;
  27. @:coreApi class Sqlite {
  28. public static function open(file:String):Connection {
  29. return new SQLiteConnection(file);
  30. }
  31. }
  32. private class SQLiteConnection implements Connection {
  33. var db:SQLite3;
  34. public function new(file:String) {
  35. db = new SQLite3(file);
  36. db.enableExceptions(true);
  37. }
  38. public function request(s:String):ResultSet {
  39. var result = db.query(s);
  40. return new SQLiteResultSet(result);
  41. }
  42. public function close():Void {
  43. db.close();
  44. }
  45. public function escape(s:String):String {
  46. return SQLite3.escapeString(s);
  47. }
  48. public function quote(s:String):String {
  49. if (s.indexOf("\000") >= 0)
  50. return "x'" + Global.bin2hex(s) + "'";
  51. return "'" + SQLite3.escapeString(s) + "'";
  52. }
  53. public function addValue(s:StringBuf, v:Dynamic):Void {
  54. if (Global.is_int(v) || Global.is_null(v)) {
  55. s.add(v);
  56. } else if (Global.is_bool(v)) {
  57. s.add(v ? 1 : 0);
  58. } else {
  59. s.add(quote(Std.string(v)));
  60. }
  61. }
  62. public function lastInsertId():Int {
  63. return Syntax.int(db.lastInsertRowID());
  64. }
  65. public function dbName():String {
  66. return 'SQLite';
  67. }
  68. public function startTransaction():Void {
  69. db.query('BEGIN TRANSACTION');
  70. }
  71. public function commit():Void {
  72. db.query('COMMIT');
  73. }
  74. public function rollback():Void {
  75. db.query('ROLLBACK');
  76. }
  77. }
  78. private class SQLiteResultSet implements ResultSet {
  79. public var length(get, null):Int;
  80. public var nfields(get, null):Int;
  81. var cache = new NativeIndexedArray<{}>();
  82. var result:SQLite3Result;
  83. var resultIsDepleted = false;
  84. var fieldsInfo:NativeAssocArray<Int>;
  85. public function new(result:SQLite3Result) {
  86. this.result = result;
  87. }
  88. public function hasNext():Bool {
  89. return switch next() {
  90. case null: false;
  91. case row:
  92. array_unshift(cache, row);
  93. row;
  94. }
  95. }
  96. public function next():Dynamic {
  97. return switch array_shift(cache) {
  98. case null: fetchNext();
  99. case row: row;
  100. }
  101. }
  102. function fetchNext():Null<{}> {
  103. return resultIsDepleted ? null : switch result.fetchArray(Const.SQLITE3_ASSOC) {
  104. case false:
  105. resultIsDepleted = true;
  106. result.finalize();
  107. null;
  108. case row:
  109. Boot.createAnon(correctArrayTypes(row));
  110. }
  111. }
  112. public function cacheAll():NativeIndexedArray<{}> {
  113. var row = fetchNext();
  114. while(row != null) {
  115. cache.push(row);
  116. row = fetchNext();
  117. }
  118. return cache;
  119. }
  120. public function results():List<Dynamic> {
  121. var list = new List();
  122. for(row in cacheAll()) {
  123. list.add(row);
  124. }
  125. return list;
  126. }
  127. function getColumn(n:Int):Any {
  128. return array_values(Syntax.array(current()))[n];
  129. }
  130. public function getResult(n:Int):String {
  131. return Syntax.string(getColumn(n));
  132. }
  133. public function getIntResult(n:Int):Int {
  134. return Syntax.int(getColumn(n));
  135. }
  136. public function getFloatResult(n:Int):Float {
  137. return Syntax.float(getColumn(n));
  138. }
  139. public function getFieldsNames():Null<Array<String>> {
  140. var fieldsInfo = getFieldsInfo();
  141. return Global.array_keys(fieldsInfo);
  142. }
  143. function current():Null<{}> {
  144. return switch reset(cache) {
  145. case false:
  146. switch next() {
  147. case null: null;
  148. case row:
  149. cache.push(row);
  150. row;
  151. }
  152. case row: row;
  153. }
  154. }
  155. function correctArrayTypes(row:NativeAssocArray<String>):NativeAssocArray<Scalar> {
  156. var fieldsInfo = getFieldsInfo();
  157. Syntax.foreach(row, function(field:String, value:String) {
  158. row[field] = correctType(value, fieldsInfo[field]);
  159. });
  160. return cast row;
  161. }
  162. inline function getFieldsInfo():NativeAssocArray<Int> {
  163. if (fieldsInfo == null) {
  164. fieldsInfo = cast Syntax.arrayDecl();
  165. for (i in 0...nfields) {
  166. fieldsInfo[result.columnName(i)] = result.columnType(i);
  167. }
  168. }
  169. return fieldsInfo;
  170. }
  171. function correctType(value:String, type:Int):Scalar {
  172. if (value == null)
  173. return null;
  174. if (type == Const.SQLITE3_INTEGER)
  175. return Syntax.int(value);
  176. if (type == Const.SQLITE3_FLOAT)
  177. return Syntax.float(value);
  178. return value;
  179. }
  180. function get_length():Int
  181. return count(cacheAll());
  182. function get_nfields():Int
  183. return result.numColumns();
  184. }