Mysql.hx 6.1 KB

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