Mysql.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package php.db;
  26. import php.db.Connection;
  27. private class MysqlConnection implements Connection {
  28. var c : Void;
  29. public function new( c : Void) {
  30. this.c = c;
  31. }
  32. public function close() {
  33. untyped __call__("mysql_close", c);
  34. untyped __call__("unset", c);
  35. }
  36. public function request( s : String ) : ResultSet {
  37. var h = untyped __call__("mysql_query", s, c);
  38. if(untyped __physeq__(h, false))
  39. throw "Error while executing "+s+" ("+untyped __call__("mysql_error", c)+")";
  40. return new MysqlResultSet(cast h);
  41. }
  42. public function escape( s : String ) {
  43. return untyped __call__("mysql_real_escape_string", s, c);
  44. }
  45. public function quote( s : String ) {
  46. return "'" + untyped __call__("mysql_real_escape_string", s, c) + "'";
  47. }
  48. public function addValue( s : StringBuf, v : Dynamic ) {
  49. if( untyped __call__("is_int", v) || __call__("is_null", v))
  50. s.add(v);
  51. else if( untyped __call__("is_bool", v) )
  52. s.add(if( v ) 1 else 0);
  53. else
  54. s.add(quote(Std.string(v)));
  55. }
  56. public function lastInsertId() {
  57. return untyped __call__("mysql_insert_id", c);
  58. }
  59. public function dbName() {
  60. return "MySQL";
  61. }
  62. public function startTransaction() {
  63. request("START TRANSACTION");
  64. }
  65. public function commit() {
  66. request("COMMIT");
  67. }
  68. public function rollback() {
  69. request("ROLLBACK");
  70. }
  71. }
  72. private class MysqlResultSet implements ResultSet {
  73. public var length(getLength,null) : Int;
  74. public var nfields(getNFields,null) : Int;
  75. private var __r : Void;
  76. private var cache : Dynamic;
  77. public function new(r) {
  78. __r = r;
  79. }
  80. private function getLength() {
  81. if(untyped __physeq__(__r, true))
  82. return untyped __call__("mysql_affected_rows");
  83. else if (untyped __physeq__(__r, false))
  84. return 0;
  85. return untyped __call__("mysql_num_rows", __r);
  86. }
  87. private var _nfields : Int;
  88. private function getNFields() {
  89. if(_nfields == null)
  90. _nfields = untyped __call__("mysql_num_fields", __r);
  91. return _nfields;
  92. }
  93. private var _fieldsDesc : Array<Dynamic>;
  94. private function getFieldsDescription() {
  95. if(_fieldsDesc == null) {
  96. _fieldsDesc = [];
  97. for(i in 0...nfields) {
  98. _fieldsDesc.push({
  99. name : untyped __call__("mysql_field_name", __r, i),
  100. type : untyped __call__("mysql_field_type", __r, i)
  101. });
  102. }
  103. }
  104. return _fieldsDesc;
  105. }
  106. private function convert(v : String, type : String) : Dynamic {
  107. if(v == null) return v;
  108. switch(type) {
  109. case "year", "int":
  110. return untyped __call__("intval", v);
  111. case "real":
  112. return untyped __call__("floatval", v);
  113. case "datetime", "date":
  114. return Date.fromString(v);
  115. default:
  116. return v;
  117. }
  118. }
  119. public function hasNext() {
  120. if( cache == null )
  121. cache = next();
  122. return (cache != null);
  123. }
  124. private var cRow : ArrayAccess<String>;
  125. private function fetchRow() : Bool {
  126. cRow = untyped __call__("mysql_fetch_array", __r, __php__("MYSQL_NUM"));
  127. return ! untyped __physeq__(cRow, false);
  128. }
  129. public function next() : Dynamic {
  130. if( cache != null ) {
  131. var t = cache;
  132. cache = null;
  133. return t;
  134. }
  135. if(!fetchRow()) return null;
  136. var o : Dynamic = {};
  137. var descriptions = getFieldsDescription();
  138. for(i in 0...nfields)
  139. Reflect.setField(o, descriptions[i].name, convert(cRow[i], descriptions[i].type));
  140. return o;
  141. }
  142. public function results() : List<Dynamic> {
  143. var l = new List();
  144. while( hasNext() )
  145. l.add(next());
  146. return l;
  147. }
  148. public function getResult( n : Int ) : String {
  149. if(cRow == null)
  150. if(!fetchRow())
  151. return null;
  152. return cRow[n];
  153. }
  154. public function getIntResult( n : Int ) : Int {
  155. return untyped __call__("intval", getResult(n));
  156. }
  157. public function getFloatResult( n : Int ) : Float {
  158. return untyped __call__("floatval", getResult(n));
  159. }
  160. }
  161. class Mysql {
  162. public static function connect( params : {
  163. host : String,
  164. port : Int,
  165. user : String,
  166. pass : String,
  167. socket : String,
  168. database : String
  169. } ) : php.db.Connection {
  170. var c = untyped __call__("mysql_connect",
  171. params.host + (params.port == null ? '' : ':'+params.port) + (params.socket == null ? '' : ':'+params.socket),
  172. params.user,
  173. params.pass);
  174. if(!untyped __call__("mysql_select_db", params.database, c))
  175. throw "Unable to connect to " + params.database;
  176. return new MysqlConnection(c);
  177. }
  178. }