Sqlite.hx 5.0 KB

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