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' ];
66 if (
$cache instanceof \Closure) {
69 if (\class_exists (
$cache )) {
70 $this->cache =
new $cache ();
76 if ($pool && (\method_exists ( $this->wrapperObject,
'pool' ))) {
77 $this->wrapperObject->setPool ( $pool );
82 $this->wrapperObject =
new $dbWrapperClass ( $this->dbType =
$dbType );
94 $this->quote = $this->wrapperObject->quote;
96 }
catch ( \Exception $e ) {
97 throw new DBException ( $e->getMessage (), $e->getCode (), $e->getPrevious () );
102 return $this->wrapperObject->getDSN ( $this->serverName, $this->port, $this->dbName, $this->dbType );
151 return \call_user_func ( $dbWrapperClass .
'::getAvailableDrivers' );
225 $this->wrapperObject->close ();
235 public static function start(
string $offset =
null, ?array $config =
null): ?self {
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 ();
247 return $this->wrapperObject->quoteValue ( (
string ) $value, $type );
252 foreach ( $fields as $field ) {
253 $ret [] = $this->quote . $field . $this->quote .
' = ' . $this->quoteValue ( $keyAndValues [$field] );
255 return \implode (
',', $ret );
260 foreach ( $keyAndValues as $value ) {
261 $ret [] = $this->quoteValue ( $value );
263 return \implode (
',', $ret );
267 $retArray = array ();
268 foreach ( $keyValues as $key => $value ) {
269 $retArray [] = $this->quote . $key . $this->quote .
" = " . $this->quoteValue ( $value );
271 return \implode ( $separator, $retArray );
278 return $this->wrapperObject->pool ();
285 $this->wrapperObject->freePool ( $db );
289 $this->wrapperObject->setPool ( $pool );
294 foreach ( self::$wrappers as $k => $wrapper ) {
295 if (\class_exists ( $wrapper,
true )) {
296 $wrappers [$k] = $wrapper;
305 return $this->wrapperObject->groupConcat ( $params [0], $params [1] ??
',');
307 return $this->wrapperObject->toStringOperator ();
312 $this->cache = $cache;
Abstract class for database caching Ubiquity\cache\database$DbCache This class is part of Ubiquity.
Ubiquity Generic database class.
setServerName($serverName)
pool()
For databases with Connection pool (retrieve a new dbInstance from pool wrapper)
getInsertValues($keyAndValues)
static start(string $offset=null, ?array $config=null)
Starts and returns a database instance corresponding to an offset in config.
connect()
Creates the Db instance and realize a safe connection.
setCacheInstance(DbCache $cache)
quoteValue($value, $type=2)
setDbWrapperClass($dbWrapperClass, $dbType)
close()
Closes the active connection.
__construct($dbWrapperClass, $dbType, $dbName, $serverName="127.0.0.1", $port="3306", $user="root", $password="", $options=[], $cache=false, $pool=null)
Constructor.
getCondition(array $keyValues, $separator=' AND ')
getUpdateFieldsKeyAndValues($keyAndValues, $fields)
static getAvailableDrivers($dbWrapperClass=\Ubiquity\db\providers\pdo\PDOWrapper::class)
static getAvailableWrappers()
getSpecificSQL($key, ?array $params=null)
freePool($db)
For databases with Connection pool (put a dbInstance in pool wrapper)
Class Configuration \config.