86 public function create(
$type, $params = [], $connect = Database::NEW_CONNECTED ) {
89 if ( class_exists( $class ) && is_subclass_of( $class, IDatabase::class ) ) {
101 'cliMode' => ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ),
103 'serverName' =>
null,
104 'topologyRole' =>
null,
107 'profiler' => $params[
'profiler'] ??
null,
109 'logger' => $params[
'logger'] ??
new NullLogger(),
110 'errorLogger' => $params[
'errorLogger'] ??
static function ( Throwable $e ) {
111 trigger_error( get_class( $e ) .
': ' . $e->getMessage(), E_USER_WARNING );
113 'deprecationLogger' => $params[
'deprecationLogger'] ??
static function ( $msg ) {
114 trigger_error( $msg, E_USER_DEPRECATED );
119 $conn =
new $class( $params );
120 if ( $connect === Database::NEW_CONNECTED ) {
121 $conn->initConnection();
138 Database::ATTR_DB_IS_FILE =>
false,
139 Database::ATTR_DB_LEVEL_LOCKING =>
false,
140 Database::ATTR_SCHEMAS_AS_TABLE_GROUPS => false
143 $class = $this->
getClass( $dbType, $driver );
144 if ( class_exists( $class ) ) {
145 return call_user_func( [ $class,
'getAttributes' ] ) + $defaults;
147 throw new DBUnexpectedError(
null,
"$dbType is not a supported database type." );
157 protected function getClass( $dbType, $driver =
null ) {
164 static $builtinTypes = [
165 'mysql' => [
'mysqli' => DatabaseMysqli::class ],
166 'sqlite' => DatabaseSqlite::class,
167 'postgres' => DatabasePostgres::class,
170 $dbType = strtolower( $dbType );
172 if ( !isset( $builtinTypes[$dbType] ) ) {
174 return 'Database' . ucfirst( $dbType );
178 $possibleDrivers = $builtinTypes[$dbType];
179 if ( is_string( $possibleDrivers ) ) {
180 $class = $possibleDrivers;
181 } elseif ( (
string)$driver !==
'' ) {
182 if ( !isset( $possibleDrivers[$driver] ) ) {
183 throw new InvalidArgumentException( __METHOD__ .
184 " type '$dbType' does not support driver '{$driver}'" );
187 $class = $possibleDrivers[$driver];
189 foreach ( $possibleDrivers as $posDriver => $possibleClass ) {
190 if ( extension_loaded( $posDriver ) ) {
191 $class = $possibleClass;
197 if ( $class ===
false ) {
198 throw new InvalidArgumentException( __METHOD__ .
199 " no viable database extension found for type '$dbType'" );