Mysql.hx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C)2005-2019 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 MysqlParams {
  24. public var host:hl.Bytes;
  25. public var user:hl.Bytes;
  26. public var pass:hl.Bytes;
  27. public var socket:hl.Bytes;
  28. public var port:Int;
  29. public function new() {}
  30. }
  31. private typedef ConnectionHandler = hl.Abstract<"mysql_cnx">;
  32. private typedef ResultHandler = hl.Abstract<"mysql_result">;
  33. @:hlNative("mysql")
  34. private class MysqlResultSet implements sys.db.ResultSet {
  35. public var length(get, null):Int;
  36. public var nfields(get, null):Int;
  37. private var r:ResultHandler;
  38. private var cache:Dynamic;
  39. function new(r) {
  40. this.r = r;
  41. }
  42. private function get_length() {
  43. return result_get_length(r);
  44. }
  45. private function get_nfields() {
  46. return result_get_nfields(r);
  47. }
  48. public function hasNext() {
  49. if (cache == null)
  50. cache = next();
  51. return (cache != null);
  52. }
  53. public function next():Dynamic {
  54. var c = cache;
  55. if (c != null) {
  56. cache = null;
  57. return c;
  58. }
  59. c = result_next(r);
  60. return c;
  61. }
  62. public function results():List<Dynamic> {
  63. var l = new List();
  64. while (hasNext())
  65. l.add(next());
  66. return l;
  67. }
  68. public function getResult(n:Int) {
  69. var v = result_get(r, n);
  70. if (v == null)
  71. return null;
  72. return @:privateAccess String.fromUTF8(v);
  73. }
  74. public function getIntResult(n:Int):Int {
  75. return result_get_int(r, n);
  76. }
  77. public function getFloatResult(n:Int):Float {
  78. return result_get_float(r, n);
  79. }
  80. public function getFieldsNames():Array<String> {
  81. var a = result_get_fields_names(r);
  82. return [for (v in a) @:privateAccess String.fromUTF8(v)];
  83. }
  84. static function result_get_length(r:ResultHandler):Int {
  85. return 0;
  86. }
  87. static function result_get_nfields(r:ResultHandler):Int {
  88. return 0;
  89. }
  90. static function result_next(r:ResultHandler):Dynamic {
  91. return null;
  92. }
  93. static function result_get(r:ResultHandler, n:Int):hl.Bytes {
  94. return null;
  95. }
  96. static function result_get_int(r:ResultHandler, n:Int):Int {
  97. return 0;
  98. }
  99. static function result_get_float(r:ResultHandler, n:Int):Float {
  100. return 0.;
  101. }
  102. static function result_get_fields_names(r:ResultHandler):hl.NativeArray<hl.Bytes> {
  103. return null;
  104. }
  105. }
  106. @:hlNative("mysql")
  107. private class MysqlConnection implements Connection {
  108. var h:ConnectionHandler;
  109. function new(h) {
  110. this.h = h;
  111. }
  112. public function close() {
  113. if (h != null)
  114. close_wrap(h);
  115. h = null;
  116. }
  117. public function request(s:String) @:privateAccess {
  118. var len = 0;
  119. var b = s.bytes.utf16ToUtf8(0, len);
  120. return new MysqlResultSet(request_wrap(h, b, len));
  121. }
  122. public function escape(s:String) @:privateAccess {
  123. var len = 0;
  124. var utf = s.bytes.utf16ToUtf8(0, len);
  125. return String.fromUTF8(escape_wrap(h, utf, len));
  126. }
  127. public function quote(s:String) {
  128. return "'" + escape(s) + "'";
  129. }
  130. public function addValue(s:StringBuf, v:Dynamic) {
  131. if (v == null) {
  132. s.add(null);
  133. return;
  134. }
  135. var t = hl.Type.getDynamic(v).kind;
  136. if (t == HI32 || t == HF64)
  137. s.add(v);
  138. else if (t == HBool)
  139. s.addChar(if (v) "1".code else "0".code);
  140. else {
  141. s.addChar("'".code);
  142. s.add(escape(Std.string(v)));
  143. s.addChar("'".code);
  144. }
  145. }
  146. public function lastInsertId() {
  147. return request("SELECT LAST_INSERT_ID()").getIntResult(0);
  148. }
  149. public function dbName() {
  150. return "MySQL";
  151. }
  152. public function startTransaction() {
  153. request("START TRANSACTION");
  154. }
  155. public function commit() {
  156. request("COMMIT");
  157. }
  158. public function rollback() {
  159. request("ROLLBACK");
  160. }
  161. static function close_wrap(h:ConnectionHandler) {}
  162. static function connect_wrap(p:MysqlParams):ConnectionHandler {
  163. return null;
  164. }
  165. static function select_db_wrap(h:ConnectionHandler, db:hl.Bytes):Bool {
  166. return false;
  167. }
  168. @:hlNative("mysql", "request")
  169. static function request_wrap(h:ConnectionHandler, rq:hl.Bytes, rqLen:Int):ResultHandler {
  170. return null;
  171. }
  172. @:hlNative("mysql", "escape")
  173. static function escape_wrap(h:ConnectionHandler, str:hl.Bytes, len:Int):hl.Bytes {
  174. return null;
  175. }
  176. static function setConvFuns(fstring:Dynamic, fbytes:Dynamic, fdate:Dynamic, fjson:Dynamic) {};
  177. }
  178. class Mysql {
  179. static var INIT_DONE = false;
  180. public static function connect(params:{
  181. host:String,
  182. ?port:Int,
  183. user:String,
  184. pass:String,
  185. ?socket:String,
  186. ?database:String
  187. }):sys.db.Connection@:privateAccess {
  188. if (!INIT_DONE) {
  189. INIT_DONE = true;
  190. MysqlConnection.setConvFuns(function(v:hl.Bytes) return @:privateAccess String.fromUTF8(v),
  191. function(v:hl.Bytes, len:Int) return new haxe.io.Bytes(v, len), function(t) return Date.fromTime(1000. * t),
  192. function(v:hl.Bytes) return haxe.Json.parse(@:privateAccess String.fromUTF8(v)));
  193. }
  194. var p = new MysqlParams();
  195. p.host = params.host == null ? null : params.host.toUtf8();
  196. p.user = params.user.toUtf8();
  197. p.pass = params.pass.toUtf8();
  198. p.socket = params.socket == null ? null : params.socket.toUtf8();
  199. p.port = params.port == null ? 3306 : params.port;
  200. var cnx = new MysqlConnection(MysqlConnection.connect_wrap(p));
  201. if (params.database != null && !MysqlConnection.select_db_wrap(cnx.h, params.database.toUtf8())) {
  202. cnx.close();
  203. throw "Failed to select database " + params.database;
  204. }
  205. return cnx;
  206. }
  207. }