1
0

sql.class.php 4.0 KB

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