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