创建一个连接单例（相同的库只创建一个连接）

@param string $sName 数据库alias
@param boolean $bReset 是否重置连接
@static
@access public
@return DB
@throws TangoException
14097
public static function getInstance($sName, $bReset = FALSE) {

		if ($bReset) {
			static::$_lInstance[$sName] = NULL;
		}

		if (!$oDB =& static::$_lInstance[$sName]) {

			$aConfig = Config::get('db');
			$aServer =& $aConfig['server'][$sName];
			if (!is_array($aServer)) {
				throw new TangoException('unknown server "' . $sName . '"');
			}

			$aServer += $aConfig['default'];
			$aServer += [
				'dbname' => $sName, // if dbname is empty, set as name
				'debug' => FALSE,
			];

			$oDB = new self($aServer, $sName);
		}
		return $oDB;
	}
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];
	}
622853
public function getLibrary($sName)
    {
        try
        {
            $library = $this->di[$sName];
        }
        catch(\Exception $e)
        {
            $library = null;
        }
        return $library;
    }
1106555
final public function sqlTable(string $tableName, string $asTable = ''): string
    {
        return $this->sqlTableEscape($this->settings->get('prefix', '') . $tableName, $asTable);
    }
973759
public function create( $serviceName ) {

		if ( is_string( $serviceName ) && isset( $this->aliases[$serviceName] ) ) {
			$serviceName = $this->aliases[$serviceName];
		}

		return $this->getReturnValueFromCallbackHandlerFor( $serviceName, func_get_args() );
	}
588381
public static function GetInstance($bReset = false, $bReloadFromSession = false)
    {
        /** @var $shopService ShopServiceInterface */
        $shopService = \ChameleonSystem\CoreBundle\ServiceLocator::get('chameleon_system_shop.shop_service');
        if ($bReset) {
            $shopService->resetBasket();
        }

        return $shopService->getActiveBasket();
    }
817812
private static function db(string $name = null) : TransactionDatabase
    {
        list($container, $config, $return) = DI::detect(__METHOD__, 'db', $name);

        if ($return) {
            return $return;
        }

        if (is_string($config)) {
            $key = 'instance_' . md5($config);
        } else {
            $key = 'instance_' . md5(serialize($config));
        }

        if (DI::get(__METHOD__, $key)) {
            return DI::get(__METHOD__, $key);
        }

        $db = AbstractDatabase::create($config);
        DI::set(__METHOD__, $key, $db);

        return DI::set(__METHOD__, $container, $db);
    }
303501
public function copy(string $newTableName = null): Table {
		if (is_null($newTableName)) {
			$newTableName = $this->getName();
		}
		$newTable = new PgsqlTable($newTableName);
		
		$newTable->applyColumnsFrom($this);
		$newTable->applyIndexesFrom($this);
		return $newTable;
	}
455251
public static function getInstance(
        CuDBiResult $cuDBiResult,
        $serverName,
        $username,
        $password,
        $dbName,
        $port = null,
        $socket = null
    )
    {

        self::$cuDBiResult = $cuDBiResult;

        if ($port === null) {
            $port = ini_get('mysqli.default_port');
        }

        if ($socket === null) {
            $socket = ini_get('mysqli.default_socket');
        }

        if (self::$instance === null) {

            try {
                /** @noinspection PhpUsageOfSilenceOperatorInspection */
                self::$instance = @new static($serverName, $username, $password, $dbName, $port, $socket);

                if (!self::$instance ||
                    ((self::$instance instanceof self) === false) ||
                    self::$instance->connect_errno > 0) {

                    $errorMessage = 'Error while connecting to Database';

                    /** @noinspection NotOptimalIfConditionsInspection */
                    if (self::$instance instanceof self) {
                        $errorMessage = self::$instance->connect_error;
                    }

                    throw new RuntimeException($errorMessage);

                }

            } catch (Exception $e) {

                echo file_get_contents(__DIR__ . '/../db_error.html');
                exit;

            }
        }

        return self::$instance;
    }
289653
public static function get($sName) {
		$aReturn =& static::$_lStore[$sName];

		if (!$aReturn) {

			$sFileDefault =& static::$_lFileDefault[$sName];
			$sFile =& static::$_lFile[$sName];

			if (!$sFile && static::$_sDir) {
				$sGuess = static::$_sDir . $sName . '.php';
				if (file_exists($sGuess)) {
					$sFile = $sGuess;
				}
			}

			$aReturn = array_replace_recursive(
				$sFileDefault ? require $sFileDefault : [],
				$sFile ? require $sFile : []
			);
		}
		return $aReturn;
	}
22124
public function createDb($sDbName)
    {
        try {
            $this->execSql("CREATE DATABASE `$sDbName` CHARACTER SET utf8 COLLATE utf8_general_ci;");
            $this->connectDb($sDbName);
        } catch (Exception $e) {
            $oSetup = $this->getInstance("Setup");
            $oSetup->setNextStep($oSetup->getStep('STEP_DB_INFO'));
            throw new Exception(sprintf($this->getInstance("Language")->getText('ERROR_COULD_NOT_CREATE_DB'), $sDbName) . " - " . $e->getMessage(), $e->getCode(), $e);
        }
    }
712727
public static function createFromDSN($dsn)
    {
        if (is_string($dsn)) {
            $parsed = self::parseDSN($dsn);
        } else {
            $parsed = $dsn;
        }

        $connection = DriverManager::getConnection($parsed);

        if ($parsed['driver'] === 'pdo_sqlite') {
            return new ConnectionHandler\SqliteConnectionHandler($connection);
        }

        if ($parsed['driver'] === 'pdo_pgsql') {
            return new ConnectionHandler\PostgresConnectionHandler($connection);
        }

        return new self($connection);
    }
265675
public function createTable($tableName, $attributes)
    {
        try {
            $this->db->createTable($tableName, $attributes);
        } catch (\Exception $e) {
            throw new DbException($e->getMessage());
        }
    }
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;

   }
366509
protected function _createAttributeListFromSql($sSelect)
    {
        $aAttributes = [];
        $rs = \OxidEsales\Eshop\Core\DatabaseProvider::getDb()->select($sSelect);
        if ($rs != false && $rs->count() > 0) {
            while (!$rs->EOF) {
                if (!isset($aAttributes[$rs->fields[0]])) {
                    $aAttributes[$rs->fields[0]] = new stdClass();
                }

                $aAttributes[$rs->fields[0]]->title = $rs->fields[1];
                if (!isset($aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]])) {
                    $aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]] = new stdClass();
                }
                $aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]]->value = $rs->fields[2];
                $rs->fetchRow();
            }
        }

        return $aAttributes;
    }
1008841
public function hasSingleResult($name) {
        $parts = explode(".", $name);
        $nameSoFar = "";
        foreach ($parts as $part) {
            $lastNameSoFar = $nameSoFar;
            $nameSoFar .= ($nameSoFar ? '.' : '') . $part;
            if (!isset($this->relations[$nameSoFar])) {
                $this->relations[$nameSoFar] = $this->initSubRelation($nameSoFar, $this->relations[$lastNameSoFar]);
            }
            if (!$this->relations[$nameSoFar]->hasSingleResult()) {
                if (!isset($this->toBeSelectedSeparately[$nameSoFar])) {
                    $this->toBeSelectedSeparately[$nameSoFar] = [
                        'relation' => $this->relations[$nameSoFar],
                        'subrelations' => []
                    ];
                }
                if ($nameSoFar != $name) {
                    $this->toBeSelectedSeparately[$nameSoFar]['subrelations'][] = substr($name, strlen($nameSoFar) + 1); // also keep name for separate select
                }
                return false;
            }
        }
        return true;
    }
1010426
public function db($dbName) {
		if (!$this->isOpen) return;
		$this->db=$dbName;
		$this->conn1->select_db($dbName);
	}