Mysql.hx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 MysqlConnection implements Connection {
  28. var c : Void;
  29. public function new( c : Void) {
  30. this.c = c;
  31. }
  32. public function close() {
  33. untyped __call__("mysql_close", c);
  34. untyped __call__("unset", c);
  35. }
  36. public function request( s : String ) : ResultSet {
  37. var h = untyped __call__("mysql_query", s, c);
  38. if(untyped __physeq__(h, false))
  39. throw "Error while executing "+s+" ("+untyped __call__("mysql_error", c)+")";
  40. return new MysqlResultSet(cast h, cast c);
  41. }
  42. public function escape( s : String ) {
  43. return untyped __call__("mysql_real_escape_string", s, c);
  44. }
  45. public function quote( s : String ) {
  46. return "'" + untyped __call__("mysql_real_escape_string", s, c) + "'";
  47. }
  48. public function addValue( s : StringBuf, v : Dynamic ) {
  49. if( untyped __call__("is_int", v) || __call__("is_null", v))
  50. s.add(v);
  51. else if( untyped __call__("is_bool", v) )
  52. s.add(if( v ) 1 else 0);
  53. else
  54. s.add(quote(Std.string(v)));
  55. }
  56. public function lastInsertId() {
  57. return untyped __call__("mysql_insert_id", c);
  58. }
  59. public function dbName() {
  60. return "MySQL";
  61. }
  62. public function startTransaction() {
  63. request("START TRANSACTION");
  64. }
  65. public function commit() {
  66. request("COMMIT");
  67. }
  68. public function rollback() {
  69. request("ROLLBACK");
  70. }
  71. }
  72. private class MysqlResultSet implements ResultSet {
  73. public var length(getLength,null) : Int;
  74. public var nfields(getNFields,null) : Int;
  75. private var __r : Void;
  76. private var __c : Void;
  77. private var cache : Dynamic;
  78. public function new(r, c) {
  79. __r = r;
  80. __c = c;
  81. }
  82. private function getLength() {
  83. if(untyped __physeq__(__r, true))
  84. return untyped __call__("mysql_affected_rows", __c);
  85. else if (untyped __physeq__(__r, false))
  86. return 0;
  87. return untyped __call__("mysql_num_rows", __r);
  88. }
  89. private var _nfields : Int;
  90. private function getNFields() {
  91. if(_nfields == null)
  92. _nfields = untyped __call__("mysql_num_fields", __r);
  93. return _nfields;
  94. }
  95. private var _fieldsDesc : Array<Dynamic>;
  96. private function getFieldsDescription() {
  97. if(_fieldsDesc == null) {
  98. _fieldsDesc = [];
  99. for (i in 0...nfields) {
  100. var item = {
  101. name : untyped __call__("mysql_field_name", __r, i),
  102. type : untyped __call__("mysql_field_type", __r, i)
  103. };
  104. _fieldsDesc.push(item);
  105. }
  106. }
  107. return _fieldsDesc;
  108. }
  109. private function convert(v : String, type : String) : Dynamic {
  110. if (v == null) return v;
  111. switch(type) {
  112. case "int", "year":
  113. return untyped __call__("intval", v);
  114. case "real":
  115. return untyped __call__("floatval", v);
  116. case "datetime", "date":
  117. return Date.fromString(v);
  118. default:
  119. return v;
  120. }
  121. }
  122. public function hasNext() {
  123. if( cache == null )
  124. cache = next();
  125. return (cache != null);
  126. }
  127. private var cRow : ArrayAccess<String>;
  128. private function fetchRow() : Bool {
  129. cRow = untyped __call__("mysql_fetch_array", __r, __php__("MYSQL_NUM"));
  130. return ! untyped __physeq__(cRow, false);
  131. }
  132. public function next() : Dynamic {
  133. if( cache != null ) {
  134. var t = cache;
  135. cache = null;
  136. return t;
  137. }
  138. if(!fetchRow()) return null;
  139. var o : Dynamic = {};
  140. var descriptions = getFieldsDescription();
  141. for(i in 0...nfields)
  142. Reflect.setField(o, descriptions[i].name, convert(cRow[i], descriptions[i].type));
  143. return o;
  144. }
  145. public function results() : List<Dynamic> {
  146. var l = new List();
  147. while( hasNext() )
  148. l.add(next());
  149. return l;
  150. }
  151. public function getResult( n : Int ) : String {
  152. if(cRow == null)
  153. if(!fetchRow())
  154. return null;
  155. return cRow[n];
  156. }
  157. public function getIntResult( n : Int ) : Int {
  158. return untyped __call__("intval", getResult(n));
  159. }
  160. public function getFloatResult( n : Int ) : Float {
  161. return untyped __call__("floatval", getResult(n));
  162. }
  163. public function getFieldsNames() : Array<String> {
  164. var fields = [];
  165. for( i in 0...nfields )
  166. fields.push(untyped __call__("mysql_field_name", __r, i));
  167. return fields;
  168. }
  169. }
  170. class Mysql {
  171. public static function connect( params : {
  172. host : String,
  173. port : Int,
  174. user : String,
  175. pass : String,
  176. socket : String,
  177. database : String
  178. } ) : php.db.Connection {
  179. var c = untyped __call__("mysql_connect",
  180. params.host + (params.port == null ? '' : ':'+params.port) + (params.socket == null ? '' : ':'+params.socket),
  181. params.user,
  182. params.pass);
  183. if(!untyped __call__("mysql_select_db", params.database, c))
  184. throw "Unable to connect to " + params.database;
  185. return new MysqlConnection(c);
  186. }
  187. }