libasynql  3.2.0
Asynchronous MySQL access library for PocketMine plugins.
libasynql.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * libasynql
5  *
6  * Copyright (C) 2018 SOFe
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 declare(strict_types=1);
22 
23 namespace poggit\libasynql;
24 
33 use function extension_loaded;
34 use function is_array;
35 use function is_string;
36 use function strtolower;
37 use function usleep;
38 
42 final class libasynql{
44  private static $packaged;
45 
46  public static function isPackaged() : bool{
47  return self::$packaged;
48  }
49 
50  public static function detectPackaged() : void{
51  self::$packaged = __CLASS__ !== 'poggit\libasynql\libasynql';
52 
53  if(!self::$packaged){
54  echo Terminal::$COLOR_YELLOW . "Warning: Use of unshaded libasynql detected. Debug mode is enabled. This may lead to major performance drop. Please use a shaded package in production. See https://poggit.pmmp.io/virion for more information.\n";
55  }
56  }
57 
58  private function __construct(){
59  }
60 
71  public static function create(Plugin $plugin, $configData, array $sqlMap, bool $logQueries = null) : DataConnector{
72  if(!is_array($configData)){
73  throw new ConfigException("Database settings are missing or incorrect");
74  }
75 
76  $type = (string) $configData["type"];
77  if($type === ""){
78  throw new ConfigException("Database type is missing");
79  }
80 
81  $pdo = ($configData["prefer-pdo"] ?? false) && extension_loaded("pdo");
82 
83  $dialect = null;
84  $placeHolder = null;
85  switch(strtolower($type)){
86  case "sqlite":
87  case "sqlite3":
88  case "sq3":
89  if(!$pdo && !extension_loaded("sqlite3")){
90  throw new ExtensionMissingException("sqlite3");
91  }
92 
93  $fileName = self::resolvePath($plugin->getDataFolder(), $configData["sqlite"]["file"] ?? "data.sqlite");
94  if($pdo){
95  // TODO add PDO support
96  }else{
97  $factory = Sqlite3Thread::createFactory($fileName);
98  }
99  $dialect = "sqlite";
100  break;
101  case "mysql":
102  case "mysqli":
103  if(!$pdo && !extension_loaded("mysqli")){
104  throw new ExtensionMissingException("mysqli");
105  }
106 
107  if(!isset($configData["mysql"])){
108  throw new ConfigException("Missing MySQL settings");
109  }
110 
111  $cred = MysqlCredentials::fromArray($configData["mysql"], strtolower($plugin->getName()));
112 
113  if($pdo){
114  // TODO add PDO support
115  }else{
116  $factory = MysqliThread::createFactory($cred);
117  $placeHolder = "?";
118  }
119  $dialect = "mysql";
120 
121  break;
122  }
123 
124  if(!isset($dialect, $factory, $sqlMap[$dialect])){
125  throw new ConfigException("Unsupported database type \"$type\". Try \"sqlite\" or \"mysql\".");
126  }
127 
128  $pool = new SqlThreadPool($factory, $configData["worker-limit"] ?? 1);
129  while(!$pool->connCreated()){
130  usleep(1000);
131  }
132  if($pool->hasConnError()){
133  throw new SqlError(SqlError::STAGE_CONNECT, $pool->getConnError());
134  }
135 
136  $connector = new DataConnectorImpl($plugin, $pool, $placeHolder, $logQueries ?? !libasynql::isPackaged());
137  foreach(is_string($sqlMap[$dialect]) ? [$sqlMap[$dialect]] : $sqlMap[$dialect] as $file){
138  $connector->loadQueryFile($plugin->getResource($file));
139  }
140  return $connector;
141  }
142 
143  private static function resolvePath(string $folder, string $path) : string{
144  if($path{0} === "/"){
145  return $path;
146  }
147  if(Utils::getOS() === "win"){
148  if($path{0} === "\\" || $path{1} === ":"){
149  return $path;
150  }
151  }
152  return $folder . $path;
153  }
154 }
155 
159 function nop() : void{
160 
161 }
162 
163 libasynql::detectPackaged();
static fromArray(array $array, ?string $defaultSchema=null)
static createFactory(MysqlCredentials $credentials)
static create(Plugin $plugin, $configData, array $sqlMap, bool $logQueries=null)
Definition: libasynql.php:71