123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- require_once('query.class.php');
- /**
- * SQL class. Used for handling SQL connections
- *
- * @module sql
- * @class SQL
- * @constructor
- */
- class SQL {
- /**
- * This is the mysqli connection beneath everything
- *
- * @property sql
- * @type {mysqli}
- * @private
- * @required
- */
- private $sql;
- public $queries = [];
- private static $connections = [];
- public function __construct($server,$user,$pass,$db){
- $this->sql = new mysqli('p:'.$server,$user,$pass,$db) or die('Unable to connect to mysql');
- self::$connections[] = $sql;
- }
- public function __destruct(){
- $this->sql->rollback();
- $this->sql->close();
- foreach($this->queries as $query){
- unset($query);
- }
- self::$connections = array_diff(self::$connections, [$this]);
- }
- public function __invoke(){
- return $this->sql;
- }
- public function __get($name){
- switch($name){
- case 'error':
- return $this->sql->error;
- break;
- case 'insert_id':
- return $this->sql->insert_id;
- break;
- }
- }
- /**
- * Returns a Query object based on inputs
- *
- * @method query
- * @param {String} sql The sql expression to run
- * @param {String=null} [types] A string containing all the types of arguments being passed
- * @param {Mixed} [bindings]* The bindings to use in the sql statement
- * @return {Query} Returns the query object
- */
- public function query(...$args){
- return new SQL\Query(...array_merge([$this], $args));
- }
- public function escape($s){
- return $this->sql->escape_string($s);
- }
- public function charset($charset){
- return $this->sql->set_charset($charset);
- }
- public static function make_referenced(&$arr){
- $refs = [];
- foreach($arr as $key => $value){
- $refs[$key] = &$arr[$key];
- }
- return $refs;
- }
- public static function shutdown(){
- foreach(self::$connections as $sql){
- unset($sql);
- }
- }
- }
- register_shutdown_function(function(){
- SQL::shutdown();
- });
- ?>
|