Ubiquity  2.5.2
php rapid development framework
Database.php
Go to the documentation of this file.
1 <?php
2 
6 namespace Ubiquity\db;
7 
15 
26 
27  public static $wrappers = [ 'pdo' => \Ubiquity\db\providers\pdo\PDOWrapper::class,'tarantool' => '\Ubiquity\db\providers\tarantool\TarantoolWrapper','mysqli' => '\Ubiquity\db\providers\mysqli\MysqliWrapper','swoole' => '\Ubiquity\db\providers\swoole\SwooleWrapper' ];
28  private $dbType;
29  private $serverName;
30  private $port;
31  private $dbName;
32  private $user;
33  private $password;
34  private $cache;
35  private $options;
36  public $quote;
37 
42  protected $wrapperObject;
43 
57  public function __construct($dbWrapperClass, $dbType, $dbName, $serverName = "127.0.0.1", $port = "3306", $user = "root", $password = "", $options = [ ], $cache = false, $pool = null) {
58  $this->setDbWrapperClass ( $dbWrapperClass, $dbType );
59  $this->dbName = $dbName;
60  $this->serverName = $serverName;
61  $this->port = $port;
62  $this->user = $user;
63  $this->password = $password;
64  $this->options = $options;
65  if ($cache !== false) {
66  if ($cache instanceof \Closure) {
67  $this->cache = $cache ();
68  } else {
69  if (\class_exists ( $cache )) {
70  $this->cache = new $cache ();
71  } else {
72  throw new CacheException ( $cache . " is not a valid value for database cache" );
73  }
74  }
75  }
76  if ($pool && (\method_exists ( $this->wrapperObject, 'pool' ))) {
77  $this->wrapperObject->setPool ( $pool );
78  }
79  }
80 
81  private function setDbWrapperClass($dbWrapperClass, $dbType) {
82  $this->wrapperObject = new $dbWrapperClass ( $this->dbType = $dbType );
83  }
84 
91  public function connect() {
92  try {
93  $this->_connect ();
94  $this->quote = $this->wrapperObject->quote;
95  return true;
96  } catch ( \Exception $e ) {
97  throw new DBException ( $e->getMessage (), $e->getCode (), $e->getPrevious () );
98  }
99  }
100 
101  public function getDSN() {
102  return $this->wrapperObject->getDSN ( $this->serverName, $this->port, $this->dbName, $this->dbType );
103  }
104 
110  public function getServerName() {
111  return $this->serverName;
112  }
113 
114  public function setServerName($serverName) {
115  $this->serverName = $serverName;
116  }
117 
118  public function setDbType($dbType) {
119  $this->dbType = $dbType;
120  return $this;
121  }
122 
128  public function getPort() {
129  return $this->port;
130  }
131 
137  public function getDbName() {
138  return $this->dbName;
139  }
140 
146  public function getUser() {
147  return $this->user;
148  }
149 
150  public static function getAvailableDrivers($dbWrapperClass = \Ubiquity\db\providers\pdo\PDOWrapper::class) {
151  return \call_user_func ( $dbWrapperClass . '::getAvailableDrivers' );
152  }
153 
159  public function getDbType() {
160  return $this->dbType;
161  }
162 
168  public function getPassword() {
169  return $this->password;
170  }
171 
177  public function getOptions() {
178  return $this->options;
179  }
180 
185  public function setPort($port) {
186  $this->port = $port;
187  }
188 
193  public function setDbName($dbName) {
194  $this->dbName = $dbName;
195  }
196 
201  public function setUser($user) {
202  $this->user = $user;
203  }
204 
209  public function setPassword($password) {
210  $this->password = $password;
211  }
212 
217  public function setOptions($options) {
218  $this->options = $options;
219  }
220 
224  public function close() {
225  $this->wrapperObject->close ();
226  }
227 
235  public static function start(string $offset = null, ?array $config = null): ?self {
236  $config ??= Startup::$config;
237  $db = $offset ? ($config ['database'] [$offset] ?? ($config ['database'] ?? [ ])) : ($config ['database'] ?? [ ]);
238  if ($db ['dbName'] !== '') {
239  $database = new Database ( $db ['wrapper'] ?? \Ubiquity\db\providers\pdo\PDOWrapper::class, $db ['type'], $db ['dbName'], $db ['serverName'] ?? '127.0.0.1', $db ['port'] ?? 3306, $db ['user'] ?? 'root', $db ['password'] ?? '', $db ['options'] ?? [ ], $db ['cache'] ?? false);
240  $database->connect ();
241  return $database;
242  }
243  return null;
244  }
245 
246  public function quoteValue($value, $type = 2) {
247  return $this->wrapperObject->quoteValue ( ( string ) $value, $type );
248  }
249 
250  public function getUpdateFieldsKeyAndValues($keyAndValues, $fields) {
251  $ret = array ();
252  foreach ( $fields as $field ) {
253  $ret [] = $this->quote . $field . $this->quote . ' = ' . $this->quoteValue ( $keyAndValues [$field] );
254  }
255  return \implode ( ',', $ret );
256  }
257 
258  public function getInsertValues($keyAndValues) {
259  $ret = array ();
260  foreach ( $keyAndValues as $value ) {
261  $ret [] = $this->quoteValue ( $value );
262  }
263  return \implode ( ',', $ret );
264  }
265 
266  public function getCondition(array $keyValues, $separator = ' AND ') {
267  $retArray = array ();
268  foreach ( $keyValues as $key => $value ) {
269  $retArray [] = $this->quote . $key . $this->quote . " = " . $this->quoteValue ( $value );
270  }
271  return \implode ( $separator, $retArray );
272  }
273 
277  public function pool() {
278  return $this->wrapperObject->pool ();
279  }
280 
284  public function freePool($db) {
285  $this->wrapperObject->freePool ( $db );
286  }
287 
288  public function setPool($pool) {
289  $this->wrapperObject->setPool ( $pool );
290  }
291 
292  public static function getAvailableWrappers() {
293  $wrappers = [ ];
294  foreach ( self::$wrappers as $k => $wrapper ) {
295  if (\class_exists ( $wrapper, true )) {
296  $wrappers [$k] = $wrapper;
297  }
298  }
299  return $wrappers;
300  }
301 
302  public function getSpecificSQL($key, ?array $params = null) {
303  switch ($key) {
304  case 'groupconcat' :
305  return $this->wrapperObject->groupConcat ( $params [0], $params [1] ?? ',');
306  case 'tostring' :
307  return $this->wrapperObject->toStringOperator ();
308  }
309  }
310 
311  public function setCacheInstance(DbCache $cache) {
312  $this->cache = $cache;
313  }
314 
315  public function getCacheInstance() {
316  return $this->cache;
317  }
318 }
pool()
For databases with Connection pool (retrieve a new dbInstance from pool wrapper)
Definition: Database.php:277
connect()
Creates the Db instance and realize a safe connection.
Definition: Database.php:91
close()
Closes the active connection.
Definition: Database.php:224
__construct($dbWrapperClass, $dbType, $dbName, $serverName="127.0.0.1", $port="3306", $user="root", $password="", $options=[], $cache=false, $pool=null)
Constructor.
Definition: Database.php:57
setServerName($serverName)
Definition: Database.php:114
getInsertValues($keyAndValues)
Definition: Database.php:258
getSpecificSQL($key, ?array $params=null)
Definition: Database.php:302
freePool($db)
For databases with Connection pool (put a dbInstance in pool wrapper)
Definition: Database.php:284
Abstract class for database caching Ubiquity$DbCache This class is part of Ubiquity.
Definition: DbCache.php:20
Class Configuration .
static start(string $offset=null, ?array $config=null)
Starts and returns a database instance corresponding to an offset in config.
Definition: Database.php:235
getCondition(array $keyValues, $separator=' AND ')
Definition: Database.php:266
quoteValue($value, $type=2)
Definition: Database.php:246
setCacheInstance(DbCache $cache)
Definition: Database.php:311
Database implementation.
Definition: Database.php:6
setOptions($options)
Definition: Database.php:217
static getAvailableDrivers($dbWrapperClass=\Ubiquity\db\providers\pdo\PDOWrapper::class)
Definition: Database.php:150
setPassword($password)
Definition: Database.php:209
Ubiquity Generic database class.
Definition: Database.php:25
static getAvailableWrappers()
Definition: Database.php:292
getUpdateFieldsKeyAndValues($keyAndValues, $fields)
Definition: Database.php:250
setDbWrapperClass($dbWrapperClass, $dbType)
Definition: Database.php:81