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