Sqlite.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package sys.db;
  26. private class SqliteConnection implements Connection {
  27. var c : Void;
  28. var e : Void;
  29. public function new( file : String ) {
  30. c = untyped __call__("sqlite_open", file, 0666, e);
  31. }
  32. public function close() {
  33. untyped __call__("sqlite_close", c);
  34. untyped __call__("unset", c);
  35. }
  36. public function request( s : String ) : ResultSet {
  37. var h = untyped __call__("sqlite_query", c, s, __php__("SQLITE_BOTH"), e);
  38. if(untyped __physeq__(h, false))
  39. throw "Error while executing "+s+" ("+e+")";
  40. return new SqliteResultSet(cast h);
  41. }
  42. public function escape( s : String ) {
  43. return untyped __call__("sqlite_escape_string", s);
  44. }
  45. public function quote( s : String ) {
  46. if( s.indexOf("\000") >= 0 )
  47. return "x'"+base16_encode(s)+"'";
  48. return "'" + untyped __call__("sqlite_escape_string", s) + "'";
  49. }
  50. public function addValue( s : StringBuf, v : Dynamic ) {
  51. if( untyped __call__("is_int", v) || __call__("is_null", v))
  52. s.add(v);
  53. else if( untyped __call__("is_bool", v) )
  54. s.add(if( v ) 1 else 0);
  55. else
  56. s.add(quote(Std.string(v)));
  57. }
  58. public function lastInsertId() {
  59. return untyped __call__("sqlite_last_insert_rowid", c);
  60. }
  61. public function dbName() {
  62. return "SQLite";
  63. }
  64. public function startTransaction() {
  65. request("BEGIN TRANSACTION");
  66. }
  67. public function commit() {
  68. request("COMMIT");
  69. startTransaction(); // match mysql usage
  70. }
  71. public function rollback() {
  72. request("ROLLBACK");
  73. startTransaction(); // match mysql usage
  74. }
  75. function base16_encode(str : String) {
  76. str = untyped __call__("unpack", "H"+(2 * str.length), str);
  77. str = untyped __call__("chunk_split", untyped str[1]);
  78. return str;
  79. }
  80. }
  81. private class SqliteResultSet implements ResultSet {
  82. public var length(getLength,null) : Int;
  83. public var nfields(getNFields,null) : Int;
  84. var r : Void;
  85. var cache : Dynamic;
  86. public function new( r ) {
  87. this.r = r;
  88. }
  89. private function getLength() {
  90. if(untyped __physeq__(r, true))
  91. return untyped __call__("sqlite_changes", r);
  92. else if (untyped __physeq__(r, false))
  93. return 0;
  94. return untyped __call__("sqlite_num_rows", r);
  95. }
  96. private var _nfields : Int;
  97. private function getNFields() {
  98. if(_nfields == null)
  99. _nfields = untyped __call__("sqlite_num_fields", r);
  100. return _nfields;
  101. }
  102. public function hasNext() {
  103. if( cache == null )
  104. cache = next();
  105. return (cache != null);
  106. }
  107. private var cRow : ArrayAccess<String>;
  108. private function fetchRow() : Bool {
  109. cRow = untyped __call__("sqlite_fetch_array", r, __php__("SQLITE_ASSOC"));
  110. return ! untyped __physeq__(cRow, false);
  111. }
  112. public function next() : Dynamic {
  113. if( cache != null ) {
  114. var t = cache;
  115. cache = null;
  116. return t;
  117. }
  118. if(!fetchRow()) return null;
  119. return untyped __call__("_hx_anonymous", cRow);
  120. }
  121. public function results() : List<Dynamic> {
  122. var l = new List();
  123. while( true ) {
  124. var c = next();
  125. if( c == null )
  126. break;
  127. l.add(c);
  128. }
  129. return l;
  130. }
  131. public function getResult( n : Int ) : String {
  132. if(cRow == null && !fetchRow())
  133. return null;
  134. return untyped __call__("array_values", cRow)[n];
  135. }
  136. public function getIntResult( n : Int ) : Int {
  137. return untyped __call__("intval", getResult(n));
  138. }
  139. public function getFloatResult( n : Int ) : Float {
  140. return untyped __call__("floatval", getResult(n));
  141. }
  142. public function getFieldsNames() : Array<String> {
  143. return throw "Not implemented";
  144. }
  145. }
  146. @:core_api class Sqlite {
  147. public static function open( file : String ) : Connection {
  148. return new SqliteConnection(file);
  149. }
  150. }