Sqlite.hx 4.8 KB

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