/** * 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; }
/** * @param array $schema */ protected static function prepareConfigSchemaField(array &$schema) { parent::prepareConfigSchemaField($schema); switch ($schema['name']) { case 'driver': $values = ConnectionFactory::getAllDrivers(); $schema['type'] = 'picklist'; $schema['values'] = $values; $schema['affects'] = 'dsn'; $schema['description'] = 'Select the driver that matches the database type for which you want to connect.' . ' For further information, see http://php.net/manual/en/pdo.drivers.php.'; break; case 'dsn': $schema['label'] = 'Connection String (DSN)'; $schema['description'] = 'The Data Source Name, or DSN, contains the information required to connect to the database.' . ' For further information, see http://php.net/manual/en/pdo.construct.php.'; break; case 'username': $schema['type'] = 'string'; $schema['description'] = 'The name of the database user. This can be a lookup key.'; break; case 'password': $schema['type'] = 'password'; $schema['description'] = 'The password for the database user. This can be a lookup key.'; break; case 'options': $schema['type'] = 'object'; $schema['object'] = ['key' => ['label' => 'Name', 'type' => 'string'], 'value' => ['label' => 'Value', 'type' => 'string']]; $schema['description'] = 'A key=>value array of connection options.'; break; case 'attributes': $schema['type'] = 'object'; $schema['object'] = ['key' => ['label' => 'Name', 'type' => 'string'], 'value' => ['label' => 'Value', 'type' => 'string']]; $schema['description'] = 'A key=>value array of attributes to be set after connection.' . ' For further information, see http://php.net/manual/en/pdo.setattribute.php'; break; case 'default_schema_only': $schema['description'] = 'Do not include other schemas/databases on this server ' . 'regardless of permissions given to the supplied credentials.'; break; } }