Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /****************************************************************************************
- * MySQL Database library by ZeusAFK Technologies
- * Version 0.1a | 2017-07-27
- ****************************************************************************************/
- namespace ZeusAFK;
- if(!class_exists('ZeusAFK\Database')){
- class Database{
- private static $instance;
- private $connection;
- private $results;
- private $handlers;
- private $error;
- private $on_error;
- private $has_errors;
- private $charset;
- public static function getInstance(){
- if(!self::$instance instanceof self){
- self::$instance = new self;
- }
- return self::$instance;
- }
- function __construct(){
- $this->results = array();
- $this->handlers = array();
- $this->has_errors = false;
- $this->charset = 'utf8';
- $this->on_error = function($type, $description){
- return false;
- };
- $that = $this;
- $this->RegisterHandler('FETCH_ROWS_HANDLER', function($results){
- $row = array();
- foreach($results as $key => $value) $row[$key] = $value;
- $this->AppendResults($row);
- });
- $this->RegisterHandler('FETCH_ROW_HANDLER', function($results){
- $row = array();
- foreach($results as $key => $value) $row[$key] = $value;
- $this->SetResults($row);
- });
- $this->RegisterHandler('FETCH_FIELD_HANDLER', function($results){
- foreach($results as $key => $value) $this->SetResults($value);
- });
- }
- public function CreateConnection($host, $user, $password, $database, $port = 3306){
- @$this->connection = new \mysqli($host, $user, $password, $database, $port);
- if($this->connection->connect_errno){
- $this->has_errors = true;
- $this->error = $connection->connect_errno;
- $this->on_error && $this->on_error('connection_creation', $this->error);
- }else{
- $this->connection->set_charset($this->charset);
- }
- return $this;
- }
- public function SetConnection($connection){
- $this->connection = $connection;
- return $this;
- }
- public function GetConnection(){
- return $this->connection;
- }
- function RegisterHandler($name, $function){
- $this->handlers[$name] = $function;
- return $this;
- }
- function GetHandler($name){
- return $this->handlers[$name];
- }
- function GetResults(){
- return $this->results;
- }
- function SetResults($results){
- $this->results = $results;
- return $this;
- }
- function AppendResults($results){
- $this->results[] = $results;
- return $this;
- }
- function ClearResults(){
- $this->results = array();
- return $this;
- }
- function SetErrorHandler($on_error){
- $this->on_error = $on_error;
- return $this;
- }
- function Success(){
- return $this->has_errors == false;
- }
- function GetError(){
- return $this->error;
- }
- public function Query($query, $types = false, $params = false, $results = false, $callback = false, &$fetch = false, $connection = false){
- $this->ClearResults();
- if(!$connection){
- $connection = $this->connection;
- }
- $new_prepared_statement = true;
- if($query instanceof mysqli_stmt){
- $stmt = $query;
- $new_prepared_statement = false;
- }else if(!($stmt = $connection->prepare($query))){
- $this->has_errors = true;
- $this->error = $connection->error;
- $error_handler = $this->on_error;
- $error_handler && $error_handler('statement_prepare_error', $this->error);
- return $this;
- }
- if($types && $params){
- if(!is_array($params)){
- $params = array($params);
- }
- $referencedParams = array();
- foreach($params as $k => $param){
- $referencesParams[$k] = &$params[$k];
- }
- if(sizeof($params) > 0){
- call_user_func_array(array($stmt, "bind_param"), array_merge(array($types), $referencesParams));
- }
- }
- $executed = $stmt->execute();
- if(!$executed){
- $this->has_errors = true;
- $error_handler = $this->on_error;
- $error_handler && $error_handler('execute_error', $this->error);
- return false;
- }else if(!$results){
- $result = $stmt->result_metadata();
- if($result){
- $info_fields = $result->fetch_fields();
- $results = array();
- foreach ($info_fields as $field){
- $count = 0;
- $field_name = $field->name;
- while(in_array($field_name, $results)){
- $count++;
- $field_name = $field->name.$count;
- }
- $results[] = $field->name.($count > 0 ? $count : '');
- }
- }
- }
- if($results){
- $formattedResults = array();
- $valuesContainer = array();
- foreach($results as $k => $value){
- $valuesContainer[$k] = null;
- $formattedResults[$value] = &$valuesContainer[$k];
- }
- if(sizeof($results) > 0){
- call_user_func_array(array($stmt, "bind_result"), $formattedResults);
- }
- }
- if(function_exists('mysqli_fetch_all')){
- do {
- $stmt->store_result();
- $rows = array();
- $row = array();
- if(!$callback){
- if($stmt->num_rows == 1 && sizeof($results) == 1)
- $callback = $this->GetHandler('FETCH_FIELD_HANDLER');
- else if($stmt->num_rows == 1 && sizeof($results) > 1)
- $callback = $this->GetHandler('FETCH_ROW_HANDLER');
- else
- $callback = $this->GetHandler('FETCH_ROWS_HANDLER');
- }
- while($stmt->fetch()){
- if($fetch){
- $row = array();
- foreach($formattedResults as $key => $value) $row[$key] = $value;
- $rows[] = $row;
- }
- if($callback) $callback($formattedResults);
- }
- } while ($stmt->more_results() && $stmt->next_result());
- }else{
- $stmt->store_result();
- $rows = array();
- $row = array();
- if(!$callback){
- if($stmt->num_rows == 1 && sizeof($results) == 1)
- $callback = $this->GetHandler('FETCH_FIELD_HANDLER');
- else if($stmt->num_rows == 1 && sizeof($results) > 1)
- $callback = $this->GetHandler('FETCH_ROW_HANDLER');
- else
- $callback = $this->GetHandler('FETCH_ROWS_HANDLER');
- }
- while($stmt->fetch()){
- if($fetch){
- $row = array();
- foreach($formattedResults as $key => $value) $row[$key] = $value;
- $rows[] = $row;
- }
- if($callback) $callback($formattedResults);
- }
- $stmt->free_result();
- if($new_prepared_statement){
- $stmt->close();
- }
- while ($connection->more_results()){
- $connection->next_result();
- $result = $connection->use_result();
- if ($result instanceof mysqli_result) {
- $result->free();
- }
- }
- }
- return $this;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement