Sqlite.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. private class SqliteConnection implements Connection {
  24. var c : Dynamic;
  25. public function new( file : String ) {
  26. c = _connect(file);
  27. }
  28. public function close() {
  29. _close(c);
  30. }
  31. public function request( s : String ) : ResultSet {
  32. try {
  33. return new SqliteResultSet(_request(c,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. {
  44. var hexChars = new Array<String>();
  45. for(i in 0...s.length)
  46. hexChars.push( StringTools.hex( StringTools.fastCodeAt(s,i),2 ) );
  47. return "x'"+ hexChars.join("") +"'";
  48. }
  49. return "'"+s.split("'").join("''")+"'";
  50. }
  51. public function addValue( s : StringBuf, v : Dynamic ) {
  52. if (v == null) {
  53. s.add(v);
  54. }
  55. else if (Std.is(v,Bool)) {
  56. s.add( v ? 1 : 0 );
  57. } else {
  58. var t:Int = untyped v.__GetType();
  59. if( t == 0xff )
  60. s.add(v);
  61. else if( t == 2 )
  62. s.add( untyped v.__GetInt() );
  63. else
  64. s.add(quote(Std.string(v)));
  65. }
  66. }
  67. public function lastInsertId() : Int{
  68. return _last_id(c);
  69. }
  70. public function dbName() {
  71. return "SQLite";
  72. }
  73. public function startTransaction() {
  74. request("BEGIN TRANSACTION");
  75. }
  76. public function commit() {
  77. request("COMMIT");
  78. startTransaction(); // match mysql usage
  79. }
  80. public function rollback() {
  81. request("ROLLBACK");
  82. startTransaction(); // match mysql usage
  83. }
  84. @:extern @:native("_hx_sqlite_connect")
  85. public static function _connect(filename:String):Dynamic return null;
  86. @:extern @:native("_hx_sqlite_request")
  87. public static function _request(handle:Dynamic,req:String):Dynamic return null;
  88. @:extern @:native("_hx_sqlite_close")
  89. public static function _close(handle:Dynamic):Void { };
  90. @:extern @:native("_hx_sqlite_last_insert_id")
  91. public static function _last_id(handle:Dynamic):Int return 0;
  92. }
  93. private class SqliteResultSet implements ResultSet {
  94. public var length(get,null) : Int;
  95. public var nfields(get,null) : Int;
  96. var r : Dynamic;
  97. var cache : List<Dynamic>;
  98. public function new( r:Dynamic ) {
  99. cache = new List();
  100. this.r = r;
  101. hasNext(); // execute the request
  102. }
  103. function get_length() {
  104. if( nfields != 0 ) {
  105. while( true ) {
  106. var c = result_next(r);
  107. if( c == null )
  108. break;
  109. cache.add(c);
  110. }
  111. return cache.length;
  112. }
  113. return result_get_length(r);
  114. }
  115. function get_nfields() {
  116. return result_get_nfields(r);
  117. }
  118. public function hasNext() {
  119. var c = next();
  120. if( c == null )
  121. return false;
  122. cache.push(c);
  123. return true;
  124. }
  125. public function next() : Dynamic {
  126. var c = cache.pop();
  127. if( c != null )
  128. return c;
  129. return result_next(r);
  130. }
  131. public function results() : List<Dynamic> {
  132. var l = new List();
  133. while( true ) {
  134. var c = next();
  135. if( c == null )
  136. break;
  137. l.add(c);
  138. }
  139. return l;
  140. }
  141. public function getResult( n : Int ) {
  142. return new String(result_get(r,n));
  143. }
  144. public function getIntResult( n : Int ) : Int {
  145. return result_get_int(r,n);
  146. }
  147. public function getFloatResult( n : Int ) : Float {
  148. return result_get_float(r,n);
  149. }
  150. public function getFieldsNames() : Array<String> {
  151. return null;
  152. }
  153. @:extern @:native("_hx_sqlite_result_next")
  154. public static function result_next(handle:Dynamic):Dynamic return null;
  155. @:extern @:native("_hx_sqlite_result_get_length")
  156. public static function result_get_length(handle:Dynamic):Int return 0;
  157. @:extern @:native("_hx_sqlite_result_get_nfields")
  158. public static function result_get_nfields(handle:Dynamic):Int return 0;
  159. @:extern @:native("_hx_sqlite_result_get")
  160. public static function result_get(handle:Dynamic,i:Int) : String return null;
  161. @:extern @:native("_hx_sqlite_result_get_int")
  162. public static function result_get_int(handle:Dynamic,i:Int) : Int return 0;
  163. @:extern @:native("_hx_sqlite_result_get_float")
  164. public static function result_get_float(handle:Dynamic,i:Int):Float return 0.0;
  165. }
  166. @:buildXml('<include name="${HXCPP}/src/hx/libs/sqlite/Build.xml"/>')
  167. @:coreApi class Sqlite {
  168. public static function open( file : String ) : Connection {
  169. return new SqliteConnection(file);
  170. }
  171. }