sql.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * SQL class. Used for handling SQL connections
  4. *
  5. * @module sql
  6. * @class SQL
  7. * @constructor
  8. */
  9. class SQL {
  10. /**
  11. * This is the mysqli connection beneath everything
  12. *
  13. * @property sql
  14. * @type {mysqli}
  15. * @private
  16. * @required
  17. */
  18. private $sql;
  19. public $insert_id;
  20. private $emsg;
  21. public function __construct($server,$user,$pass,$db){
  22. $this->sql = new mysqli($server,$user,$pass,$db) or die('Unable to connect to mysql');
  23. }
  24. public function __invoke(){
  25. return $this->sql;
  26. }
  27. public function __get($name){
  28. switch($name){
  29. case 'error':
  30. return empty($this->sql->error)?$this->emsg:$this->sql->error;
  31. break;
  32. }
  33. }
  34. public function __set($name,$value){
  35. switch($name){
  36. case 'error':
  37. $this->emsg = $value;
  38. break;
  39. }
  40. }
  41. /**
  42. * Returns a Query object based on inputs
  43. *
  44. * @method query
  45. * @param {String} sql The sql expression to run
  46. * @param {String=null} [types] A string containing all the types of arguments being passed
  47. * @param {Mixed} [bindings]* The bindings to use in the sql statement
  48. * @return {Query} Returns the query object
  49. */
  50. public function query(){
  51. $reflect = new ReflectionClass('Query');
  52. $args = array_merge(array($this),func_get_args());
  53. return $reflect->newInstanceArgs($args);
  54. }
  55. public function escape($s){
  56. return $this->sql->escape_string($s);
  57. }
  58. public function charset($charset){
  59. return $this->sql->set_charset($charset);
  60. }
  61. }
  62. /**
  63. * Query class. Returned by SQL::query()
  64. *
  65. * @class Query
  66. * @constructor
  67. */
  68. class Query {
  69. private $query;
  70. private $sql;
  71. private $parent;
  72. public function __construct($sql,$source,$types=null){
  73. $args = func_get_args();
  74. $args = array_splice($args,2);
  75. $this->parent = $sql;
  76. $this->sql = $sql();
  77. $this->query = $sql()->prepare($source);
  78. if($this->query){
  79. if(!is_null($types)){
  80. call_user_func_array(array($this->query, 'bind_param'),make_referenced($args)) or trigger_error($sql()->error);
  81. }
  82. }else{
  83. trigger_error($sql->error);
  84. }
  85. }
  86. public function __invoke(){
  87. return $this->query;
  88. }
  89. public function execute(){
  90. if($this->query){
  91. $r = $this->query->execute();
  92. $this->parent->insert_id = $this->sql->insert_id;
  93. $this->parent->error = $this->error;
  94. $this->sql->commit();
  95. return $r;
  96. }else{
  97. return false;
  98. }
  99. }
  100. public function __get($name){
  101. switch($name){
  102. /**
  103. * Returns the mysqli::results object for the
  104. * query
  105. *
  106. * @property results
  107. * @type {mysqli::results}
  108. * @public
  109. */
  110. case 'results':
  111. if($this->query){
  112. $this->execute();
  113. $result = $this->query->get_result();
  114. $this->query->close();
  115. return $result;
  116. }else{
  117. return false;
  118. }
  119. break;
  120. /**
  121. * Returns an associative array of the query resulsts
  122. *
  123. * @property assoc_results
  124. * @type {Array}
  125. * @public
  126. */
  127. /**
  128. * Returns an associative array of the query resulsts
  129. *
  130. * @property resulsts_assoc
  131. * @type {Array}
  132. * @public
  133. */
  134. case 'assoc_results':case 'results_assoc':
  135. if($this->query){
  136. $a = array();
  137. $r = $this->results;
  138. while($row = $r->fetch_assoc()){
  139. array_push($a,$row);
  140. }
  141. return $a;
  142. }else{
  143. return false;
  144. }
  145. break;
  146. /**
  147. * Returns a numbered array of the query results
  148. *
  149. * @property num_results
  150. * @type {Array}
  151. * @public
  152. */
  153. /**
  154. * Returns a numbered array of the query results
  155. *
  156. * @property resulsts_num
  157. * @type {Array}
  158. * @public
  159. */
  160. case 'num_results':case 'results_num':
  161. if($this->query){
  162. $a = array();
  163. $r = $this->results;
  164. while($row = $r->fetch_num()){
  165. array_push($a,$row);
  166. }
  167. return $a;
  168. }else{
  169. return false;
  170. }
  171. break;
  172. case 'assoc_result':case 'result_assoc':
  173. if($this->query){
  174. $r = $this->results;
  175. return $r?$r->fetch_assoc():false;
  176. }else{
  177. return false;
  178. }
  179. break;
  180. case 'num_result':case 'result_num':
  181. if($this->query){
  182. $r = $this->results;
  183. return $r?$r->fetch_row():false;
  184. }else{
  185. return false;
  186. }
  187. break;
  188. case 'insert_id':
  189. return $this->parent->insert_id;
  190. break;
  191. case 'error':
  192. return $this->query->error;
  193. break;
  194. }
  195. }
  196. }
  197. function make_referenced(&$arr){
  198. $refs = array();
  199. foreach($arr as $key => $value){
  200. $refs[$key] = &$arr[$key];
  201. }
  202. return $refs;
  203. }
  204. ?>