Mysql.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 lastInsertId() {
  49. return untyped __call__("mysql_insert_id", c);
  50. }
  51. public function dbName() {
  52. return "MySQL";
  53. }
  54. public function startTransaction() {
  55. request("START TRANSACTION");
  56. }
  57. public function commit() {
  58. request("COMMIT");
  59. }
  60. public function rollback() {
  61. request("ROLLBACK");
  62. }
  63. }
  64. private class MysqlResultSet implements ResultSet {
  65. public var length(getLength,null) : Int;
  66. public var nfields(getNFields,null) : Int;
  67. private var __r : Void;
  68. private var cache : Dynamic;
  69. public function new(r) {
  70. __r = r;
  71. }
  72. private function getLength() {
  73. return untyped __call__("mysql_num_rows", __r);
  74. }
  75. private var _nfields : Int;
  76. private function getNFields() {
  77. if(_nfields == null)
  78. _nfields = untyped __call__("mysql_num_fields", __r);
  79. return _nfields;
  80. }
  81. private var _fieldsDesc : Array<Dynamic>;
  82. private function getFieldsDescription() {
  83. if(_fieldsDesc == null) {
  84. _fieldsDesc = [];
  85. for(i in 0...nfields) {
  86. _fieldsDesc.push({
  87. name : untyped __call__("mysql_field_name", __r, i),
  88. type : untyped __call__("mysql_field_type", __r, i)
  89. });
  90. }
  91. }
  92. return _fieldsDesc;
  93. }
  94. private function convert(v : String, type : String) : Dynamic {
  95. if(v == null) return v;
  96. switch(type) {
  97. case "year", "int":
  98. return untyped __call__("intval", v);
  99. case "real":
  100. return untyped __call__("floatval", v);
  101. case "datetime", "date":
  102. return Date.fromString(v);
  103. default:
  104. return v;
  105. }
  106. }
  107. public function hasNext() {
  108. if( cache == null )
  109. cache = next();
  110. return (cache != null);
  111. }
  112. public function next() : Dynamic {
  113. if( cache != null ) {
  114. var t = cache;
  115. cache = null;
  116. return t;
  117. }
  118. var c = untyped __call__("mysql_fetch_array", __r, __php__("MYSQL_NUM"));
  119. if(untyped __physeq__(c, false))
  120. return null;
  121. var o : Dynamic = {};
  122. var descriptions = getFieldsDescription();
  123. for(i in 0...nfields)
  124. Reflect.setField(o, descriptions[i].name, convert(c[i], descriptions[i].type));
  125. return o;
  126. }
  127. public function results() : List<Dynamic> {
  128. var l = new List();
  129. while( hasNext() )
  130. l.add(next());
  131. return l;
  132. }
  133. public function getResult( n : Int ) : String {
  134. var a : Array<String> = untyped __call__("mysql_fetch_row", __r);
  135. return a[n];
  136. }
  137. public function getIntResult( n : Int ) : Int {
  138. return untyped __call__("intval", getResult(n));
  139. }
  140. public function getFloatResult( n : Int ) : Float {
  141. return untyped __call__("floatval", getResult(n));
  142. }
  143. }
  144. class Mysql {
  145. public static function connect( params : {
  146. host : String,
  147. port : Int,
  148. user : String,
  149. pass : String,
  150. socket : String,
  151. database : String
  152. } ) : php.db.Connection {
  153. var c = untyped __call__("mysql_connect",
  154. params.host + (params.port == null ? '' : ':'+params.port) + (params.socket == null ? '' : ':'+params.socket),
  155. params.user,
  156. params.pass);
  157. if(!untyped __call__("mysql_select_db", params.database, c))
  158. throw "Unable to connect to " + params.database;
  159. return new MysqlConnection(c);
  160. }
  161. }