Sqlite.hx 4.9 KB

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