Mysql.hx 5.6 KB

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