设置bug
User: ZeMing Shao
Email: szm19920426@gmail.com
@param $database
@return XunsearchService
@throws \XSException
556875
public function setDatabase($database){

        $file = XS_APP_ROOT.'/xunsearch.php';
        if(!file_exists($file)){
            throw new \Exception('配置文件'.$file.'不存在');
        }
        $config = include $file;
        if(!isset($config['databases'][$database])){
            throw new \Exception('配置文件'.$file.'缺少这个'.$database.'配置');
        }

        $this->database = $database;

        $config = $config['databases'][$database];

        return $this->setConfig($config);

    }
1052917
public function select($database)
	{
		$this->connect();

		if (!$database)
		{
			return false;
		}

		if (!sqlsrv_query($this->connection, 'USE ' . $database, null, array('scrollable' => SQLSRV_CURSOR_STATIC)))
		{
			throw new \RuntimeException('Could not connect to database');
		}

		return true;
	}
823242
public function connectDatabase($database = array())
    {
        $model = new BaseModel();
        if (isset($this->options['debugStatus']) && $this->options['debugStatus'] === TRUE) {
            $model->debugStatus = $this->options['debugStatus'];
            if (isset($this->options['debugLevel']) && !empty($this->options['debugLevel'])) {
                $model->debugLevel = $this->options['debugLevel'];
            }
            if (isset($this->options['loggerPath']) && !empty($this->options['loggerPath'])) {
                $model->debugLoggerPath = $this->options['loggerPath'];
            }
            $model->debugLoggerFilename = 'Log-' . date('Y-m-d') . '.log';
            $model->__construct();
        }
        $model->setDatabase($database);

        return $model;
    }
843174
public function getConnection($database = null)
    {
        $buildConnection = $this->getBuildConnection($database);

        //Still useful ?
        //$dsn = str_replace("@DB@", $database, $buildConnection['dsn']);
        $dsn = $buildConnection['dsn'];

        // Set user + password to null if they are empty strings or missing
        $username = isset($buildConnection['user']) && $buildConnection['user'] ? $buildConnection['user'] : null;
        $password = isset($buildConnection['password']) && $buildConnection['password'] ? $buildConnection['password'] : null;

        // Get options from and config and default to null
        $options = isset($buildConnection['options']) && is_array($buildConnection['options']) ? $buildConnection['options'] : null;

        $con = ConnectionFactory::create(['dsn' => $dsn, 'user' => $username, 'password' => $password, 'options' => $options], AdapterFactory::create($buildConnection['adapter']));

        return $con;
    }
452659
public function query(string $sql)
    {
        try {
            $this->stmt = $this->dbh->query($sql);            
            return $this;
        } catch (\PDOException $e) {
            die("Error: " . $e->getMessage());
        }
    }
1185
public static function getInstance($database = null)
    {

        if (self::$instance instanceof Pool) {
            self::$instance->add($database);

            return self::$instance;
        }

        self::$instance = new Pool($database);

        return self::$instance;
    }
303969
public static function drop_db($database = null)
	{
		if (empty($database))
		{
			throw new \Mongo_DbException('Failed to drop MongoDB database because name is empty');
		}

		else
		{
			try
			{
				static::instance()->connection->{$database}->drop();
				return true;
			}
			catch (\Exception $e)
			{
				throw new \Mongo_DbException("Unable to drop Mongo database `{$database}`: {$e->getMessage()}", $e->getCode());
			}

		}
	}
263062
public static function make(
        $database,
        $propertyContainer
    ) {
        if (!array_key_exists(
            'repository',
            $database
        )
        ) {
            throw new SchemaException(
                __CLASS__,
                __METHOD__,
                "'repository' field has not been found for defining database schema entry ",
                400
            );
        }

        if (!array_key_exists(
            'keys',
            $database
        )
        ) {
            throw new SchemaException(
                __CLASS__,
                __METHOD__,
                "'keys' field has not been found for defining database schema entry ",
                400
            );
        }

        $metaKeys = $propertyContainer->getMeta($database['keys']);
        if (empty($metaKeys)) {
            throw new SchemaException(
                __CLASS__,
                __METHOD__,
                'properties used as primary keys hasn\'t been found at properties entry array',
                400
            );
        }

        $instance = new self(
            $database['repository'],
            $metaKeys
        );

        $metaHasOneDependencies = $propertyContainer->getMetaForRelationshipType('hasOne');
        $metaHasManyDependencies = $propertyContainer->getMetaForRelationshipType('hasMany');
        if (!empty($metaHasOneDependencies) || !empty($metaHasManyDependencies)) {
            $dependency = new Dependencies();
            if (!empty($metaHasManyDependencies)) {
                $dependency->setHasMany(array_values($metaHasManyDependencies));
            }
            if (!empty($metaHasOneDependencies)) {
                $dependency->setHasOne(array_values($metaHasOneDependencies));
            }
            $instance->setDependencies($dependency);
        }

        if (array_key_exists(
            'actions',
            $database
        )) {
            $instance->setActions(Action::make($database['actions']));
        }

        return $instance;
    }
309667
public function listTableForeignKeys($table, $database = null)
    {
        if ($database === null) {
            $database = $this->_conn->getDatabase();
        }
        $sql              = $this->_platform->getListTableForeignKeysSQL($table, $database);
        $tableForeignKeys = $this->_conn->fetchAll($sql);

        return $this->_getPortableTableForeignKeysList($tableForeignKeys);
    }
390545
public function createDatabase($database=null)
    {
        if (!$database) $database = $this->getDatabase();
        $sql = $this->getSchema()->getCreateDatabaseSql($database);
        return $this->query($sql);
    }
213722
public function setDatabase($database)
    {
        $database = empty($database)
            ? $this->database
            : $database;

        if (is_file($database)) {
            $this->handle->open($database);
            $this->database = $database;
        }

        return $this;
    }
532880
public function query($sql)
    {
        #鉴定是执行查询还是commit提交操作。如果是select、show，可以有后续操作。
        $_key = strtolower(substr($sql, 0, 4));
        if ($_key == 'sele' || $_key == 'show') {
            $this->query_sql = $sql;
            return $this;
        } else {
            return $this->conn('master')->query($sql);
        }
    }
1241642
public function getBuildConnection($database)
    {
        if (!isset($this->buildConnections[$database])) {
            // fallback to default connection settings from build.properties
            return array(
                'dsn'      => $this->url,
                'user'     => $this->userId,
                'password' => $this->password,
            );
        }

        return $this->buildConnections[$database];
    }
1112690
public function query($sql = null)
    {
        if (!$sql) {
            $sqlWhere = '';
            if (!empty($this->getWhere())) {
                $sqlWhere = $this->getWhere();
            }

            $sqlLimit = '';
            if (!empty($this->getLimit())) {
                $sqlLimit = $this->getLimit();
            }

            $sqlOrder = '';
            $orderBy = $this->getOrderBy();
            if (!empty($orderBy)) {
                $sqlOrder = $orderBy;
            }

            $sqlSelect = "SELECT " . $this->getSelect();
            $sqlFrom = " FROM " . $this->getTable();

            $sql = $sqlSelect . $sqlFrom . $sqlWhere . $sqlOrder . $sqlLimit;
        }
        $this->setLastQuery($sql);

        try {
            $stmt = $this->db->prepare($sql);
            $this->setLastQuery($stmt->queryString, $this->getParameters());
            $stmt->execute($this->getParameters());
        } catch (\PDOException $e) {
            throw new DatabaseException($e->getMessage() . '<br>' . $this->getLastQuery()[0]);
        }

        $this->setResult($stmt);
        $this->clearQuery();

        return $this;
    }
1319550
public function exec($statement)
    {
        $connection = $this->getWrappedConnection();

        $logger = $this->_config->getSQLLogger();
        if ($logger) {
            $logger->startQuery($statement);
        }

        try {
            $result = $connection->exec($statement);
        } catch (Throwable $ex) {
            throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
        }

        if ($logger) {
            $logger->stopQuery();
        }

        return $result;
    }
869991
public function prepare($statement)
    {
        try {
            $stmt = new Statement($statement, $this);
        } catch (Throwable $ex) {
            throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
        }

        $stmt->setFetchMode($this->defaultFetchMode);

        return $stmt;
    }
900076
public function checkDbExists($database)
    {
        try {
            $databaseExists = true;
            $database->execSql("select * from oxconfig");
        } catch (Exception $exception) {
            $databaseExists = false;
        }

        return $databaseExists;
    }