Sqlite.hx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 lastInsertId() {
  52. return untyped __call__("sqlite_last_insert_rowid", c);
  53. }
  54. public function dbName() {
  55. return "SQLite";
  56. }
  57. public function startTransaction() {
  58. request("BEGIN TRANSACTION");
  59. }
  60. public function commit() {
  61. request("COMMIT");
  62. startTransaction(); // match mysql usage
  63. }
  64. public function rollback() {
  65. request("ROLLBACK");
  66. startTransaction(); // match mysql usage
  67. }
  68. function base16_encode(str : String) {
  69. str = untyped __call__("unpack", "H"+(2 * str.length), str);
  70. str = untyped __call__("chunk_split", untyped str[1]);
  71. return str;
  72. }
  73. }
  74. private class SqliteResultSet implements ResultSet {
  75. public var length(getLength,null) : Int;
  76. public var nfields(getNFields,null) : Int;
  77. var r : Void;
  78. var cache : List<Dynamic>;
  79. public function new( r ) {
  80. cache = new List();
  81. this.r = r;
  82. hasNext(); // execute the request
  83. }
  84. function getLength() {
  85. if( nfields != 0 ) {
  86. while( true ) {
  87. var c = doNext();
  88. if( c == null )
  89. break;
  90. cache.add(c);
  91. }
  92. return cache.length;
  93. }
  94. return untyped __call__("sqlite_num_rows", r);
  95. }
  96. function getNFields() {
  97. return untyped __call__("sqlite_num_fields", r);
  98. }
  99. public function hasNext() {
  100. var c = next();
  101. if( c == null )
  102. return false;
  103. cache.push(c);
  104. return true;
  105. }
  106. public function next() : Dynamic {
  107. var c = cache.pop();
  108. if( c != null )
  109. return c;
  110. return doNext();
  111. }
  112. private function doNext() : Dynamic {
  113. var c = untyped __call__("sqlite_fetch_array", r, __php__("SQLITE_BOTH"));
  114. if(untyped __physeq__(c, false))
  115. return null;
  116. return untyped __call__("_hx_anonymous", c);
  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. return Reflect.field(next(), cast n);
  130. }
  131. public function getIntResult( n : Int ) : Int {
  132. return untyped __call__("intval", Reflect.field(next(), cast n));
  133. }
  134. public function getFloatResult( n : Int ) : Float {
  135. return untyped __call__("floatval", Reflect.field(next(), cast n));
  136. }
  137. }
  138. class Sqlite {
  139. public static function open( file : String ) : Connection {
  140. return new SqliteConnection(file);
  141. }
  142. }