87 public function create(
$type, $params = [], $connect = Database::NEW_CONNECTED ) {
90 if ( class_exists( $class ) && is_subclass_of( $class, IDatabase::class ) ) {
102 'cliMode' => ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ),
104 'serverName' =>
null,
105 'topologyRole' =>
null,
107 'topologicalPrimaryConnRef' => $params[
'topologicalPrimaryConnRef'] ??
null,
109 'profiler' => $params[
'profiler'] ??
null,
111 'connLogger' => $params[
'connLogger'] ??
new NullLogger(),
112 'queryLogger' => $params[
'queryLogger'] ??
new NullLogger(),
113 'replLogger' => $params[
'replLogger'] ??
new NullLogger(),
114 'errorLogger' => $params[
'errorLogger'] ??
static function ( Throwable $e ) {
115 trigger_error( get_class( $e ) .
': ' . $e->getMessage(), E_USER_WARNING );
117 'deprecationLogger' => $params[
'deprecationLogger'] ??
static function ( $msg ) {
118 trigger_error( $msg, E_USER_DEPRECATED );
123 $conn =
new $class( $params );
124 if ( $connect === Database::NEW_CONNECTED ) {
125 $conn->initConnection();
142 Database::ATTR_DB_IS_FILE =>
false,
143 Database::ATTR_DB_LEVEL_LOCKING =>
false,
144 Database::ATTR_SCHEMAS_AS_TABLE_GROUPS => false
147 $class = $this->
getClass( $dbType, $driver );
148 if ( class_exists( $class ) ) {
149 return call_user_func( [ $class,
'getAttributes' ] ) + $defaults;
151 throw new DBUnexpectedError(
null,
"$dbType is not a supported database type." );
161 protected function getClass( $dbType, $driver =
null ) {
168 static $builtinTypes = [
169 'mysql' => [
'mysqli' => DatabaseMysqli::class ],
170 'sqlite' => DatabaseSqlite::class,
171 'postgres' => DatabasePostgres::class,
174 $dbType = strtolower( $dbType );
176 if ( !isset( $builtinTypes[$dbType] ) ) {
178 return 'Database' . ucfirst( $dbType );
182 $possibleDrivers = $builtinTypes[$dbType];
183 if ( is_string( $possibleDrivers ) ) {
184 $class = $possibleDrivers;
185 } elseif ( (
string)$driver !==
'' ) {
186 if ( !isset( $possibleDrivers[$driver] ) ) {
187 throw new InvalidArgumentException( __METHOD__ .
188 " type '$dbType' does not support driver '{$driver}'" );
191 $class = $possibleDrivers[$driver];
193 foreach ( $possibleDrivers as $posDriver => $possibleClass ) {
194 if ( extension_loaded( $posDriver ) ) {
195 $class = $possibleClass;
201 if ( $class ===
false ) {
202 throw new InvalidArgumentException( __METHOD__ .
203 " no viable database extension found for type '$dbType'" );