Mysql.hx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. import sys.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. case "blob":
  119. return haxe.io.Bytes.ofData(cast v);
  120. default:
  121. return v;
  122. }
  123. }
  124. public function hasNext() {
  125. if( cache == null )
  126. cache = next();
  127. return (cache != null);
  128. }
  129. private var cRow : ArrayAccess<String>;
  130. private function fetchRow() : Bool {
  131. cRow = untyped __call__("mysql_fetch_array", __r, __php__("MYSQL_NUM"));
  132. return ! untyped __physeq__(cRow, false);
  133. }
  134. public function next() : Dynamic {
  135. if( cache != null ) {
  136. var t = cache;
  137. cache = null;
  138. return t;
  139. }
  140. if(!fetchRow()) return null;
  141. var o : Dynamic = {};
  142. var descriptions = getFieldsDescription();
  143. for(i in 0...nfields)
  144. Reflect.setField(o, descriptions[i].name, convert(cRow[i], descriptions[i].type));
  145. return o;
  146. }
  147. public function results() : List<Dynamic> {
  148. var l = new List();
  149. while( hasNext() )
  150. l.add(next());
  151. return l;
  152. }
  153. public function getResult( n : Int ) : String {
  154. if(cRow == null)
  155. if(!fetchRow())
  156. return null;
  157. return cRow[n];
  158. }
  159. public function getIntResult( n : Int ) : Int {
  160. return untyped __call__("intval", getResult(n));
  161. }
  162. public function getFloatResult( n : Int ) : Float {
  163. return untyped __call__("floatval", getResult(n));
  164. }
  165. public function getFieldsNames() : Array<String> {
  166. var fields = [];
  167. for( i in 0...nfields )
  168. fields.push(untyped __call__("mysql_field_name", __r, i));
  169. return fields;
  170. }
  171. }
  172. @:core_api class Mysql {
  173. public static function connect( params : {
  174. host : String,
  175. ?port : Int,
  176. user : String,
  177. pass : String,
  178. ?socket : String,
  179. database : String
  180. } ) : sys.db.Connection {
  181. var c = untyped __call__("mysql_connect",
  182. params.host + (params.port == null ? '' : ':'+params.port) + (params.socket == null ? '' : ':'+params.socket),
  183. params.user,
  184. params.pass);
  185. if(!untyped __call__("mysql_select_db", params.database, c))
  186. throw "Unable to connect to " + params.database;
  187. return new MysqlConnection(c);
  188. }
  189. }