sql.class.php 3.9 KB

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