Mysql.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C)2005-2017 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 php.*;
  24. import sys.db.*;
  25. import php.db.*;
  26. import php.db.Mysqli_result;
  27. @:coreApi class Mysql {
  28. public static function connect(
  29. params : {
  30. host : String,
  31. ?port : Int,
  32. user : String,
  33. pass : String,
  34. ?socket : String,
  35. database : String
  36. }
  37. ) : Connection {
  38. return new MysqlConnection(params);
  39. }
  40. }
  41. private class MysqlConnection implements Connection {
  42. var db : Mysqli;
  43. public function new(
  44. params : {
  45. host : String,
  46. ?port : Int,
  47. user : String,
  48. pass : String,
  49. ?socket : String,
  50. database : String
  51. }
  52. ) : Void {
  53. if (params.port == null) params.port = Std.parseInt(Global.ini_get('mysqli.default_port'));
  54. if (params.socket == null) params.socket = Global.ini_get('mysqli.default_socket');
  55. db = new Mysqli(params.host, params.user, params.pass, params.database, params.port, params.socket);
  56. }
  57. public function request( s : String ) : ResultSet {
  58. var result = db.query(s);
  59. if (result == false) throw 'Failed to perform db query';
  60. if (result == true) return null;
  61. return new MysqlResultSet(result);
  62. }
  63. public function close() : Void {
  64. db.close();
  65. }
  66. public function escape( s : String ) : String {
  67. return db.escape_string(s);
  68. }
  69. public function quote( s : String ) : String {
  70. if (s.indexOf("\000") >= 0) return "x'" + Global.bin2hex(s) + "'";
  71. return "'" + db.escape_string(s) + "'";
  72. }
  73. public function addValue( s : StringBuf, v : Dynamic ) : Void {
  74. if (Global.is_int(v)
  75. || Global.is_null(v)) {
  76. s.add(v);
  77. } else if (Global.is_bool(v)) {
  78. s.add(v ? 1 : 0);
  79. } else {
  80. s.add(quote(Std.string(v)));
  81. }
  82. }
  83. public function lastInsertId() : Int {
  84. return db.insert_id;
  85. }
  86. public function dbName() : String {
  87. return 'MySQL';
  88. }
  89. public function startTransaction() : Void {
  90. var success = db.begin_transaction();
  91. if (!success) throw 'Failed to start transaction';
  92. }
  93. public function commit() : Void {
  94. var success = db.commit();
  95. if (!success) throw 'Failed to commit transaction';
  96. }
  97. public function rollback() : Void {
  98. var success = db.rollback();
  99. if (!success) throw 'Failed to rollback transaction';
  100. }
  101. }
  102. private class MysqlResultSet implements ResultSet {
  103. static var hxAnonClassName = Boot.getHxAnon().phpClassName;
  104. public var length(get,null) : Int;
  105. public var nfields(get,null) : Int;
  106. var result:Mysqli_result;
  107. var fetchedRow:NativeAssocArray<Scalar>;
  108. var fieldsInfo:NativeAssocArray<MysqliFieldInfo>;
  109. public function new( result:Mysqli_result ) {
  110. this.result = result;
  111. }
  112. public function hasNext() : Bool {
  113. if (fetchedRow == null) fetchNext();
  114. return fetchedRow != null;
  115. }
  116. public function next() : Dynamic {
  117. if (fetchedRow == null) fetchNext();
  118. return withdrawFetched();
  119. }
  120. public function results() : List<Dynamic> {
  121. var list = new List();
  122. result.data_seek(0);
  123. var row = result.fetch_object(hxAnonClassName);
  124. while (row != null) {
  125. row = correctObjectTypes(row);
  126. list.add(row);
  127. row = result.fetch_object(hxAnonClassName);
  128. }
  129. return list;
  130. }
  131. public function getResult( n : Int ) : String {
  132. if (fetchedRow == null) fetchNext();
  133. return Global.array_values(fetchedRow)[n];
  134. }
  135. public function getIntResult( n : Int ) : Int {
  136. return Syntax.int(getResult(n));
  137. }
  138. public function getFloatResult( n : Int ) : Float {
  139. return Syntax.float(getResult(n));
  140. }
  141. public function getFieldsNames() : Null<Array<String>> {
  142. var fields = result.fetch_fields();
  143. return [for (field in fields) field.name];
  144. }
  145. function fetchNext() {
  146. var row = result.fetch_assoc();
  147. if (row != null) fetchedRow = correctArrayTypes(row);
  148. }
  149. function withdrawFetched() : Dynamic {
  150. if (fetchedRow == null) return null;
  151. var row = fetchedRow;
  152. fetchedRow = null;
  153. return Boot.createAnon(row);
  154. }
  155. function correctArrayTypes(row:NativeAssocArray<String>):NativeAssocArray<Scalar> {
  156. var fieldsInfo = getFieldsInfo();
  157. Syntax.foreach(row, function(field:String, value:String) {
  158. row[field] = correctType(value, fieldsInfo[field].type);
  159. });
  160. return cast row;
  161. }
  162. function correctObjectTypes(row:{}):{} {
  163. var fieldsInfo = getFieldsInfo();
  164. Syntax.foreach(row, function(field:String, value:String) {
  165. value = correctType(value, fieldsInfo[field].type);
  166. Syntax.setField(row, field, value);
  167. });
  168. return row;
  169. }
  170. inline function getFieldsInfo():NativeAssocArray<MysqliFieldInfo> {
  171. if (fieldsInfo == null) {
  172. fieldsInfo = cast Syntax.arrayDecl();
  173. Syntax.foreach(result.fetch_fields(), function(_, info) {
  174. fieldsInfo[info.name] = info;
  175. });
  176. }
  177. return fieldsInfo;
  178. }
  179. function correctType(value:String, type:Int):Scalar {
  180. if (value == null) return null;
  181. if (
  182. type == Const.MYSQLI_TYPE_BIT
  183. || type == Const.MYSQLI_TYPE_TINY
  184. || type == Const.MYSQLI_TYPE_SHORT
  185. || type == Const.MYSQLI_TYPE_LONG
  186. || type == Const.MYSQLI_TYPE_INT24
  187. || type == Const.MYSQLI_TYPE_CHAR
  188. ) {
  189. return Syntax.int(value);
  190. }
  191. if (
  192. type == Const.MYSQLI_TYPE_DECIMAL
  193. || type == Const.MYSQLI_TYPE_NEWDECIMAL
  194. || type == Const.MYSQLI_TYPE_FLOAT
  195. || type == Const.MYSQLI_TYPE_DOUBLE
  196. ) {
  197. return Syntax.float(value);
  198. }
  199. return value;
  200. }
  201. function get_length() return result.num_rows;
  202. function get_nfields() return result.field_count;
  203. }