Mysql.hx 6.7 KB

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