2
0

Mysql.hx 6.3 KB

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