Mysql.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C)2005-2015 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. private class D {
  24. static function load(fun,args) : Dynamic {
  25. return cpp.Lib.load(lib,fun,args);
  26. }
  27. static var lib = "mysql5";
  28. public static var connect = load("mysql_connect",1);
  29. public static var select_db = load("select_db",2);
  30. public static var request = load("request",2);
  31. public static var close = load("close",1);
  32. public static var escape = load("escape", 2);
  33. public static var set_conv_funs = load("set_conv_funs", 4);
  34. public static var result_get_length = load("result_get_length",1);
  35. public static var result_get_nfields = load("result_get_nfields",1);
  36. public static var result_next = load("result_next",1);
  37. public static var result_get = load("result_get",2);
  38. public static var result_get_int = load("result_get_int",2);
  39. public static var result_get_float = load("result_get_float",2);
  40. public static var result_fields_names = cpp.Lib.loadLazy(lib,"result_get_fields_names",1);
  41. }
  42. private class MysqlResultSet implements sys.db.ResultSet {
  43. public var length(get,null) : Int;
  44. public var nfields(get,null) : Int;
  45. private var __r : Dynamic;
  46. private var cache : Dynamic;
  47. public function new(r) {
  48. __r = r;
  49. }
  50. private function get_length() {
  51. return D.result_get_length(__r);
  52. }
  53. private function get_nfields() {
  54. return D.result_get_nfields(__r);
  55. }
  56. public function hasNext() {
  57. if( cache == null )
  58. cache = next();
  59. return (cache != null);
  60. }
  61. public function next() : Dynamic {
  62. var c = cache;
  63. if( c != null ) {
  64. cache = null;
  65. return c;
  66. }
  67. c = D.result_next(__r);
  68. return c;
  69. }
  70. public function results() : List<Dynamic> {
  71. var l = new List();
  72. while( hasNext() )
  73. l.add(next());
  74. return l;
  75. }
  76. public function getResult( n : Int ) {
  77. return new String(D.result_get(__r,n));
  78. }
  79. public function getIntResult( n : Int ) : Int {
  80. return D.result_get_int(__r,n);
  81. }
  82. public function getFloatResult( n : Int ) : Float {
  83. return D.result_get_float(__r,n);
  84. }
  85. public function getFieldsNames() : Array<String> {
  86. var a = D.result_fields_names(__r);
  87. return a;
  88. }
  89. }
  90. private class MysqlConnection implements sys.db.Connection {
  91. private var __c : Dynamic;
  92. public function new(c) {
  93. __c = c;
  94. D.set_conv_funs(c, function(s) return new String(s), function(d) return untyped Date.new1(d), function(b) return haxe.io.Bytes.ofData(b));
  95. }
  96. public function request( s : String ) : sys.db.ResultSet {
  97. var r = D.request(this.__c, s);
  98. return new MysqlResultSet(r);
  99. }
  100. public function close() {
  101. D.close(__c);
  102. }
  103. public function escape( s : String ) {
  104. return new String(D.escape(__c,s));
  105. }
  106. public function quote( s : String ) {
  107. return "'"+escape(s)+"'";
  108. }
  109. public function addValue( s : StringBuf, v : Dynamic ) {
  110. if (v == null) {
  111. s.add(v);
  112. }
  113. else if (Std.is(v,Bool)) {
  114. s.add( v ? 1 : 0 );
  115. } else {
  116. var t:Int = untyped v.__GetType();
  117. if( t == 0xff )
  118. s.add(v);
  119. else if( t == 2 )
  120. s.add( untyped v.__GetInt() ? "1".code : "0".code );
  121. else {
  122. s.addChar("'".code);
  123. s.add(escape(Std.string(v)));
  124. s.addChar("'".code);
  125. }
  126. }
  127. }
  128. public function lastInsertId() {
  129. return request("SELECT LAST_INSERT_ID()").getIntResult(0);
  130. }
  131. public function dbName() {
  132. return "MySQL";
  133. }
  134. public function startTransaction() {
  135. request("START TRANSACTION");
  136. }
  137. public function commit() {
  138. request("COMMIT");
  139. }
  140. public function rollback() {
  141. request("ROLLBACK");
  142. }
  143. private static var __use_date = Date;
  144. }
  145. @:coreApi class Mysql {
  146. public static function connect( params : {
  147. host : String,
  148. ?port : Int,
  149. user : String,
  150. pass : String,
  151. ?socket : String,
  152. database : String
  153. } ) : sys.db.Connection {
  154. var o = {
  155. host : params.host,
  156. port : if( params.port == null ) 3306 else params.port,
  157. user : params.user,
  158. pass : params.pass,
  159. socket : if( params.socket == null ) null else params.socket
  160. };
  161. var c = D.connect(o);
  162. try {
  163. D.select_db(c,params.database);
  164. } catch( e : Dynamic ) {
  165. D.close(c);
  166. cpp.Lib.rethrow(e);
  167. }
  168. return new MysqlConnection(c);
  169. }
  170. }