Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. <?php
  2. /****************************************************************************************
  3. * MySQL Database library by ZeusAFK Technologies
  4. * Version 0.1a | 2017-07-27
  5. ****************************************************************************************/
  6.  
  7. namespace ZeusAFK;
  8.  
  9. if(!class_exists('ZeusAFK\Database')){
  10. class Database{
  11.  
  12. private static $instance;
  13.  
  14. private $connection;
  15. private $results;
  16. private $handlers;
  17. private $error;
  18. private $on_error;
  19. private $has_errors;
  20. private $charset;
  21.  
  22. public static function getInstance(){
  23. if(!self::$instance instanceof self){
  24. self::$instance = new self;
  25. }
  26. return self::$instance;
  27. }
  28.  
  29. function __construct(){
  30. $this->results = array();
  31. $this->handlers = array();
  32. $this->has_errors = false;
  33. $this->charset = 'utf8';
  34. $this->on_error = function($type, $description){
  35. return false;
  36. };
  37.  
  38. $that = $this;
  39.  
  40. $this->RegisterHandler('FETCH_ROWS_HANDLER', function($results){
  41. $row = array();
  42. foreach($results as $key => $value) $row[$key] = $value;
  43. $this->AppendResults($row);
  44. });
  45.  
  46. $this->RegisterHandler('FETCH_ROW_HANDLER', function($results){
  47. $row = array();
  48. foreach($results as $key => $value) $row[$key] = $value;
  49. $this->SetResults($row);
  50. });
  51.  
  52. $this->RegisterHandler('FETCH_FIELD_HANDLER', function($results){
  53. foreach($results as $key => $value) $this->SetResults($value);
  54. });
  55. }
  56.  
  57. public function CreateConnection($host, $user, $password, $database, $port = 3306){
  58. @$this->connection = new \mysqli($host, $user, $password, $database, $port);
  59.  
  60. if($this->connection->connect_errno){
  61. $this->has_errors = true;
  62. $this->error = $connection->connect_errno;
  63. $this->on_error && $this->on_error('connection_creation', $this->error);
  64. }else{
  65. $this->connection->set_charset($this->charset);
  66. }
  67.  
  68. return $this;
  69. }
  70.  
  71. public function SetConnection($connection){
  72. $this->connection = $connection;
  73. return $this;
  74. }
  75.  
  76. public function GetConnection(){
  77. return $this->connection;
  78. }
  79.  
  80. function RegisterHandler($name, $function){
  81. $this->handlers[$name] = $function;
  82. return $this;
  83. }
  84.  
  85. function GetHandler($name){
  86. return $this->handlers[$name];
  87. }
  88.  
  89. function GetResults(){
  90. return $this->results;
  91. }
  92.  
  93. function SetResults($results){
  94. $this->results = $results;
  95. return $this;
  96. }
  97.  
  98. function AppendResults($results){
  99. $this->results[] = $results;
  100. return $this;
  101. }
  102.  
  103. function ClearResults(){
  104. $this->results = array();
  105. return $this;
  106. }
  107.  
  108. function SetErrorHandler($on_error){
  109. $this->on_error = $on_error;
  110. return $this;
  111. }
  112.  
  113. function Success(){
  114. return $this->has_errors == false;
  115. }
  116.  
  117. function GetError(){
  118. return $this->error;
  119. }
  120.  
  121. public function Query($query, $types = false, $params = false, $results = false, $callback = false, &$fetch = false, $connection = false){
  122. $this->ClearResults();
  123.  
  124. if(!$connection){
  125. $connection = $this->connection;
  126. }
  127.  
  128. $new_prepared_statement = true;
  129.  
  130. if($query instanceof mysqli_stmt){
  131. $stmt = $query;
  132. $new_prepared_statement = false;
  133. }else if(!($stmt = $connection->prepare($query))){
  134. $this->has_errors = true;
  135. $this->error = $connection->error;
  136. $error_handler = $this->on_error;
  137. $error_handler && $error_handler('statement_prepare_error', $this->error);
  138. return $this;
  139. }
  140.  
  141. if($types && $params){
  142. if(!is_array($params)){
  143. $params = array($params);
  144. }
  145.  
  146. $referencedParams = array();
  147. foreach($params as $k => $param){
  148. $referencesParams[$k] = &$params[$k];
  149. }
  150.  
  151. if(sizeof($params) > 0){
  152. call_user_func_array(array($stmt, "bind_param"), array_merge(array($types), $referencesParams));
  153. }
  154. }
  155.  
  156. $executed = $stmt->execute();
  157.  
  158. if(!$executed){
  159. $this->has_errors = true;
  160. $error_handler = $this->on_error;
  161. $error_handler && $error_handler('execute_error', $this->error);
  162. return false;
  163. }else if(!$results){
  164. $result = $stmt->result_metadata();
  165. if($result){
  166. $info_fields = $result->fetch_fields();
  167. $results = array();
  168. foreach ($info_fields as $field){
  169. $count = 0;
  170. $field_name = $field->name;
  171. while(in_array($field_name, $results)){
  172. $count++;
  173. $field_name = $field->name.$count;
  174. }
  175. $results[] = $field->name.($count > 0 ? $count : '');
  176. }
  177. }
  178. }
  179.  
  180. if($results){
  181. $formattedResults = array();
  182. $valuesContainer = array();
  183. foreach($results as $k => $value){
  184. $valuesContainer[$k] = null;
  185. $formattedResults[$value] = &$valuesContainer[$k];
  186. }
  187.  
  188. if(sizeof($results) > 0){
  189. call_user_func_array(array($stmt, "bind_result"), $formattedResults);
  190. }
  191. }
  192.  
  193. if(function_exists('mysqli_fetch_all')){
  194. do {
  195. $stmt->store_result();
  196. $rows = array();
  197. $row = array();
  198.  
  199. if(!$callback){
  200. if($stmt->num_rows == 1 && sizeof($results) == 1)
  201. $callback = $this->GetHandler('FETCH_FIELD_HANDLER');
  202. else if($stmt->num_rows == 1 && sizeof($results) > 1)
  203. $callback = $this->GetHandler('FETCH_ROW_HANDLER');
  204. else
  205. $callback = $this->GetHandler('FETCH_ROWS_HANDLER');
  206. }
  207.  
  208. while($stmt->fetch()){
  209. if($fetch){
  210. $row = array();
  211. foreach($formattedResults as $key => $value) $row[$key] = $value;
  212. $rows[] = $row;
  213. }
  214. if($callback) $callback($formattedResults);
  215. }
  216. } while ($stmt->more_results() && $stmt->next_result());
  217. }else{
  218. $stmt->store_result();
  219. $rows = array();
  220. $row = array();
  221.  
  222. if(!$callback){
  223. if($stmt->num_rows == 1 && sizeof($results) == 1)
  224. $callback = $this->GetHandler('FETCH_FIELD_HANDLER');
  225. else if($stmt->num_rows == 1 && sizeof($results) > 1)
  226. $callback = $this->GetHandler('FETCH_ROW_HANDLER');
  227. else
  228. $callback = $this->GetHandler('FETCH_ROWS_HANDLER');
  229. }
  230.  
  231. while($stmt->fetch()){
  232. if($fetch){
  233. $row = array();
  234. foreach($formattedResults as $key => $value) $row[$key] = $value;
  235. $rows[] = $row;
  236. }
  237. if($callback) $callback($formattedResults);
  238. }
  239. $stmt->free_result();
  240. if($new_prepared_statement){
  241. $stmt->close();
  242. }
  243. while ($connection->more_results()){
  244. $connection->next_result();
  245. $result = $connection->use_result();
  246. if ($result instanceof mysqli_result) {
  247. $result->free();
  248. }
  249. }
  250. }
  251.  
  252. return $this;
  253. }
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement