1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- require_once('SQL/query.class.php');
-
- class SQL {
-
- private $guid;
- private $sql;
- public $queries = [];
- private static $connections = [];
- public function __construct($server,$user,$pass,$db){
- $this->guid = uniqid();
- $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;
- }
- }
- public function __toString(){
- return $this->guid;
- }
-
- 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();
- });
- ?>
|