/** * Create a new SqlDbSvc * * @param array $settings * * @throws \InvalidArgumentException * @throws \Exception */ public function __construct($settings = []) { parent::__construct($settings); $config = ArrayUtils::clean(ArrayUtils::get($settings, 'config')); Session::replaceLookups($config, true); $driver = isset($config['driver']) ? $config['driver'] : null; $this->dbConn = ConnectionFactory::createConnection($driver, $config); $this->dbConn->setCache($this); $this->dbConn->setExtraStore($this); $defaultSchemaOnly = ArrayUtils::getBool($config, 'default_schema_only'); $this->dbConn->setDefaultSchemaOnly($defaultSchemaOnly); switch ($this->dbConn->getDBName()) { case SqlDbDriverTypes::MYSQL: case SqlDbDriverTypes::MYSQLI: $this->dbConn->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true); break; case SqlDbDriverTypes::DBLIB: $this->dbConn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); break; } $attributes = ArrayUtils::clean(ArrayUtils::get($settings, 'attributes')); if (!empty($attributes)) { $this->dbConn->setAttributes($attributes); } }
/** * @param \Illuminate\Database\Connection $eloquentConnection * * @return \DreamFactory\Core\Database\Connection * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException */ public static function getLegacyConnection($eloquentConnection) { if (empty(static::$connection)) { $driver = $eloquentConnection->getDriverName(); if (empty($driver)) { throw new InternalServerErrorException('No database driver supplied'); } $connections = config('database.connections'); if (empty($connections)) { throw new InternalServerErrorException('No connections found in database.connections config'); } $configKeys = []; foreach ($connections as $name => $connectionConfig) { if ($driver === $name || $driver === $connectionConfig['driver'] || $driver === 'dblib' && $name === 'sqlsrv') { $configKeys = array_keys($connectionConfig); } } if (empty($configKeys)) { throw new InternalServerErrorException('Unsupported driver - ' . $driver); } $config = []; foreach ($configKeys as $key) { $config[$key] = $eloquentConnection->getConfig($key); } switch ($driver) { case 'sqlite': $dsn = $driver . ":" . $config['database']; break; case 'mysql': $dsn = static::getMySqlDsn($config); break; case 'pgsql': $dsn = static::getPgSqlDsn($config); break; case 'sqlsrv': case 'dblib': $dsn = static::getSqlSrvDsn($config); break; default: throw new InternalServerErrorException('Unsupported driver - ' . $driver); break; } $config['dsn'] = $dsn; static::$connection = ConnectionFactory::createConnection($driver, $config); } return static::$connection; }