115 public function create( $type,
$params = [], $connect = Database::NEW_CONNECTED ) {
118 if ( class_exists( $class ) && is_subclass_of( $class, IDatabase::class ) ) {
129 'serverName' =>
null,
130 'topologyRole' =>
null,
134 'logger' =>
$params[
'logger'] ??
new NullLogger(),
135 'errorLogger' =>
$params[
'errorLogger'] ??
static function ( Throwable $e ) {
136 trigger_error( get_class( $e ) .
': ' . $e->getMessage(), E_USER_WARNING );
141 if ( $this->debugSql ) {
146 'flags' => $this->initConnectionFlags(
$params[
'flags'] ),
147 'cliMode' => $this->cliMode,
148 'agent' => $this->agent,
149 'profiler' => $this->profiler,
150 'deprecationLogger' => $this->deprecationLogger,
151 'criticalSectionProvider' => $this->csProvider,
155 $conn =
new $class( array_merge(
$params, $overrides ) );
156 if ( $connect === Database::NEW_CONNECTED ) {
157 $conn->initConnection();
174 Database::ATTR_DB_IS_FILE =>
false,
175 Database::ATTR_DB_LEVEL_LOCKING =>
false,
176 Database::ATTR_SCHEMAS_AS_TABLE_GROUPS => false
179 $class = $this->
getClass( $dbType, $driver );
180 if ( class_exists( $class ) ) {
181 return call_user_func( [ $class,
'getAttributes' ] ) + $defaults;
183 throw new DBUnexpectedError(
null,
"$dbType is not a supported database type." );
193 protected function getClass( $dbType, $driver =
null ) {
200 static $builtinTypes = [
201 'mysql' => [
'mysqli' => DatabaseMySQL::class ],
202 'sqlite' => DatabaseSqlite::class,
203 'postgres' => DatabasePostgres::class,
206 $dbType = strtolower( $dbType );
208 if ( !isset( $builtinTypes[$dbType] ) ) {
210 return 'Database' . ucfirst( $dbType );
214 $possibleDrivers = $builtinTypes[$dbType];
215 if ( is_string( $possibleDrivers ) ) {
216 $class = $possibleDrivers;
217 } elseif ( (
string)$driver !==
'' ) {
218 if ( !isset( $possibleDrivers[$driver] ) ) {
219 throw new InvalidArgumentException( __METHOD__ .
220 " type '$dbType' does not support driver '{$driver}'" );
223 $class = $possibleDrivers[$driver];
225 foreach ( $possibleDrivers as $posDriver => $possibleClass ) {
226 if ( extension_loaded( $posDriver ) ) {
227 $class = $possibleClass;
233 if ( $class ===
false ) {
234 throw new InvalidArgumentException( __METHOD__ .
235 " no viable database extension found for type '$dbType'" );