Set database

@param string $dbname
@return Adapter
@throws UnknownDatabaseException
166907
public function setDatabase($dbname)
    {
        $this->config->setDatabase($dbname);
        if ($this->connection) {
            try {
                $this->query('USE ' . $this->quote()->identifier($dbname));
            } catch (RuntimeException $exception) {
                /** @var \PDOException $prevException */
                $prevException = $exception->getPrevious();
                if (UnknownDatabaseException::isUnknownDatabase($prevException)) {
                    throw UnknownDatabaseException::createFromUnknownDatabase($dbname, $prevException);
                }

                throw $exception;
            }
        }

        return $this;
    }
914531
public function createDatabaseObject()
    {
        $dbname = trim($this->getSubmitValue('dbname'));
        if (empty($dbname)) // disallow database object creation w/ no selected database
        {
            throw new Exception("No database name");
        }

        $adapter = $this->getSubmitValue('adapter');
        $port = Adapter::getDefaultPortForAdapter($adapter);

        $host = $this->getSubmitValue('host');
        $tables_prefix = $this->getSubmitValue('tables_prefix');
        
        $dbInfos = array(
            'host'          => (is_null($host)) ? $host : trim($host),
            'username'      => $this->getSubmitValue('username'),
            'password'      => $this->getSubmitValue('password'),
            'dbname'        => $dbname,
            'tables_prefix' => (is_null($tables_prefix)) ? $tables_prefix : trim($tables_prefix),
            'adapter'       => $adapter,
            'port'          => $port,
            'schema'        => Config::getInstance()->database['schema'],
            'type'          => $this->getSubmitValue('type')
        );

        if (($portIndex = strpos($dbInfos['host'], '/')) !== false) {
            // unix_socket=/path/sock.n
            $dbInfos['port'] = substr($dbInfos['host'], $portIndex);
            $dbInfos['host'] = '';
        } else if (($portIndex = strpos($dbInfos['host'], ':')) !== false) {
            // host:port
            $dbInfos['port'] = substr($dbInfos['host'], $portIndex + 1);
            $dbInfos['host'] = substr($dbInfos['host'], 0, $portIndex);
        }

        try {
            @Db::createDatabaseObject($dbInfos);
        } catch (Zend_Db_Adapter_Exception $e) {
            $db = Adapter::factory($adapter, $dbInfos, $connect = false);

            // database not found, we try to create  it
            if ($db->isErrNo($e, '1049')) {
                $dbInfosConnectOnly = $dbInfos;
                $dbInfosConnectOnly['dbname'] = null;
                @Db::createDatabaseObject($dbInfosConnectOnly);
                @DbHelper::createDatabase($dbInfos['dbname']);

                // select the newly created database
                @Db::createDatabaseObject($dbInfos);
            } else {
                throw $e;
            }
        }

        return $dbInfos;
    }
571374
public function connectDb($sDbName)
    {
        try {
            $this->_oConn->exec("USE `$sDbName`");
        } catch (Exception $e) {
            throw new Exception($this->getInstance("Language")->getText('ERROR_COULD_NOT_CREATE_DB') . " - " . $e->getMessage(), Database::ERROR_COULD_NOT_CREATE_DB, $e);
        }
    }
930894
public function selectDatabase(string $databaseName = ''): void
    {
        $this->selectedDB = $databaseName;
        $this->currentDB = $this->currentLink->select_db($databaseName);

        if (!$this->currentDB) {
            $this->setError(AbstractMySQL::E_NO_DB_SELECTED, __CLASS__);
        }
    }
720613
public function setDbConnection( $dbc ){
		if( !is_object( $dbc ) )
			throw new \InvalidArgumentException( 'Database connection resource must be an object' );
		if( !is_a( $dbc, 'PDO' ) )
			throw new \InvalidArgumentException( 'Database connection resource must be a direct or inherited PDO object' );
		$this->dbc = $dbc;
	}
74504
public function getDatabase($name = null)
    {
        if (empty($name)) {
            $name = $this->getCurrentDatabaseName();
        }

        if (!isset($this->databasePool[$name])) {
            // init db
            $database = new Database($this, $name);
            if (isset($this->mapping[$name])) {
                $database->map($this->mapping[$name]);
            }

            // configure db
            $this->databasePool[$name] = $database;
        }
        
        return $this->databasePool[$name];
    }
2807
public static function setDefaultDbo(AbstractDatabaseDriver $db = null)
    {
        self::$db = $db;

        if ($db) {
            $driver = $db->getName();

            self::$instances[$driver] = $db;
        }
    }
338447
public function setDbAdapter($config)
    {
        if(is_array($config)) {
            $this->odbAdapter = new DbAdapter(array_merge(array(
                'driver' => 'Pdo_Mysql',
                'driver_options' => array(
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
                )
            ), $config));

            $config = new \Zend\Config\Config($config, true);
            $writer = new \Zend\Config\Writer\PhpArray();
            $conf = $writer->toString($config);
        }
    }
500195
public static function setConfig(array $config)
    {
        if ( ! isset($config['connection'])) {
            throw new DbException('can`t find `connection` parameter in database config');
        }
        $connection = &$config['connection'];
        if ( ! isset($connection['default'])) {
            throw new DbException('can`t find `default` parameter in database config');
        }
        $default = &$connection['default'];
        if ( ! is_string($default) || empty($connection[$default])) {
            throw new DbException('parameter `default` must be string and contain contains name of default connection. So given section `' .
                $default .
                '` must present in database config');
        }

        return self::$config = $config;
    }
130827
public function setDatabase($database)
    {
        if(!$this->_connection->select_db($database)) {
            throw new RuntimeException('Could not connect with database : '.$database);
        }

        $this->_database = $database;
        return $this;
    }
259958
public function set(DB $db, $name = null)
    {
        if ($name === null) {
            $name = $this->mainname;
        }
        if (isset($this->dbs[$name])) {
            throw new StorageEngaged($name);
        }
        $this->dbs[$name] = $db;
    }
824211
public function setDbName( ?string $dbName ) : PgSQL
   {

      if ( ! $this->_supportedAttributes->get( 'dbname' )->validateValue( $dbName ) )
      {
         throw new ArgumentException( 'dbname', $dbName, 'Invalid database name!' );
      }

      $this->_attributes[ 'dbname' ] = $dbName;

      return $this;

   }
730076
public function setName($name)
    {
        if (empty($name))
            throw new \Exception('Invalid characters in database name:'.$name);
        $this->name = $name;
        return $this;
    }
536905
final public static function getDbInstance(string $databaseName = '#none'):\PDO
    {
        if (self::$co_instance == null) {
            new self($databaseName);
        }
        return (self::$co_instance);
    }
796598
public function getDatabaseMetadata(string $databaseName): DatabaseMetadata
    {
        if (!array_key_exists($databaseName, $this->databaseMetadataList)) {
            throw new \RuntimeException('Server "' . $this->host . '" doesn\'t contain database "' . $databaseName . '"');
        }

        return $this->databaseMetadataList[$databaseName];
    }
1181529
public function setName($name)
    {
        if ($this->name && $this->name !== $name) {
            user_error("DBField::setName() shouldn't be called once a DBField already has a name."
                . "It's partially immutable - it shouldn't be altered after it's given a value.", E_USER_WARNING);
        }

        $this->name = $name;

        return $this;
    }
861995
public static function &getDatabase($jname)
	{
		static $instances;
		if (!isset($instances)) {
			$instances = array();
		}
		//only create a new database instance if it has not been created before
		if (!isset($instances[$jname])) {
			/**
			 * TODO: MUST BE CHANGED! as do not rely on joomla_int
			 */
			if ($jname == 'joomla_int') {
				$db = self::getDBO();
			} else {
				//get config values
				$params = static::getParams($jname);
				//prepare the data for creating a database connection
				$host = $params->get('database_host');
				$user = $params->get('database_user');
				$password = $params->get('database_password');
				$database = $params->get('database_name');
				$prefix = $params->get('database_prefix', '');
				$driver = $params->get('database_type');
				$charset = $params->get('database_charset', 'utf8');
				//added extra code to prevent error when $driver is incorrect

				$options = array('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix);

				$db = DatabaseFactory::getInstance()->getDriver($driver, $options);

				if ($driver != 'sqlite') {
					//add support for UTF8
					$db->setQuery('SET names ' . $db->quote($charset));
					$db->execute();
				}

				//get the debug configuration setting
				$db->setDebug(Config::get()->get('debug'));
			}
			$instances[$jname] = $db;
		}
		return $instances[$jname];
	}