Sqlite.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C)2005-2012 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. var e : Dynamic;
  26. public function new( file : String ) {
  27. c = untyped __call__("sqlite_open", file, 0666, e);
  28. }
  29. public function close() {
  30. untyped __call__("sqlite_close", c);
  31. untyped __call__("unset", c);
  32. }
  33. public function request( s : String ) : ResultSet {
  34. var h = untyped __call__("sqlite_query", c, s, __php__("SQLITE_BOTH"), e);
  35. if(untyped __physeq__(h, false))
  36. throw "Error while executing "+s+" ("+e+")";
  37. return new SqliteResultSet(cast h);
  38. }
  39. public function escape( s : String ) {
  40. return untyped __call__("sqlite_escape_string", s);
  41. }
  42. public function quote( s : String ) {
  43. if( s.indexOf("\000") >= 0 )
  44. return "x'"+base16_encode(s)+"'";
  45. return "'" + untyped __call__("sqlite_escape_string", s) + "'";
  46. }
  47. public function addValue( s : StringBuf, v : Dynamic ) {
  48. if( untyped __call__("is_int", v) || __call__("is_null", v))
  49. s.add(v);
  50. else if( untyped __call__("is_bool", v) )
  51. s.add(if( v ) 1 else 0);
  52. else
  53. s.add(quote(Std.string(v)));
  54. }
  55. public function lastInsertId() {
  56. return untyped __call__("sqlite_last_insert_rowid", c);
  57. }
  58. public function dbName() {
  59. return "SQLite";
  60. }
  61. public function startTransaction() {
  62. request("BEGIN TRANSACTION");
  63. }
  64. public function commit() {
  65. request("COMMIT");
  66. startTransaction(); // match mysql usage
  67. }
  68. public function rollback() {
  69. request("ROLLBACK");
  70. startTransaction(); // match mysql usage
  71. }
  72. function base16_encode(str : String) {
  73. str = untyped __call__("unpack", "H"+(2 * str.length), str);
  74. str = untyped __call__("chunk_split", untyped str[1]);
  75. return str;
  76. }
  77. }
  78. private class SqliteResultSet implements ResultSet {
  79. public var length(get,null) : Int;
  80. public var nfields(get,null) : Int;
  81. var r : Dynamic;
  82. var cache : Dynamic;
  83. public function new( r ) {
  84. this.r = r;
  85. }
  86. private function get_length() {
  87. if(untyped __physeq__(r, true))
  88. return untyped __call__("sqlite_changes", r);
  89. else if (untyped __physeq__(r, false))
  90. return 0;
  91. return untyped __call__("sqlite_num_rows", r);
  92. }
  93. private var _nfields : Int;
  94. private function get_nfields() {
  95. if(_nfields == null)
  96. _nfields = untyped __call__("sqlite_num_fields", r);
  97. return _nfields;
  98. }
  99. public function hasNext() {
  100. if( cache == null )
  101. cache = next();
  102. return (cache != null);
  103. }
  104. private var cRow : ArrayAccess<String>;
  105. private function fetchRow() : Bool {
  106. cRow = untyped __call__("sqlite_fetch_array", r, __php__("SQLITE_ASSOC"));
  107. return ! untyped __physeq__(cRow, false);
  108. }
  109. public function next() : Dynamic {
  110. if( cache != null ) {
  111. var t = cache;
  112. cache = null;
  113. return t;
  114. }
  115. if(!fetchRow()) return null;
  116. return untyped __call__("_hx_anonymous", cRow);
  117. }
  118. public function results() : List<Dynamic> {
  119. var l = new List();
  120. while( true ) {
  121. var c = next();
  122. if( c == null )
  123. break;
  124. l.add(c);
  125. }
  126. return l;
  127. }
  128. public function getResult( n : Int ) : String {
  129. if(cRow == null && !fetchRow())
  130. return null;
  131. return untyped __call__("array_values", cRow)[n];
  132. }
  133. public function getIntResult( n : Int ) : Int {
  134. return untyped __call__("intval", getResult(n));
  135. }
  136. public function getFloatResult( n : Int ) : Float {
  137. return untyped __call__("floatval", getResult(n));
  138. }
  139. public function getFieldsNames() : Array<String> {
  140. return throw "Not implemented";
  141. }
  142. }
  143. @:coreApi class Sqlite {
  144. public static function open( file : String ) : Connection {
  145. return new SqliteConnection(file);
  146. }
  147. }