123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
-
- namespace Illuminate\Database\Connectors;
-
- use PDO;
- use Illuminate\Support\Arr;
-
- class SqlServerConnector extends Connector implements ConnectorInterface
- {
- /**
- * The PDO connection options.
- *
- * @var array
- */
- protected $options = [
- PDO::ATTR_CASE => PDO::CASE_NATURAL,
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
- PDO::ATTR_STRINGIFY_FETCHES => false,
- ];
-
- /**
- * Establish a database connection.
- *
- * @param array $config
- * @return \PDO
- */
- public function connect(array $config)
- {
- $options = $this->getOptions($config);
-
- return $this->createConnection($this->getDsn($config), $config, $options);
- }
-
- /**
- * Create a DSN string from a configuration.
- *
- * @param array $config
- * @return string
- */
- protected function getDsn(array $config)
- {
- // First we will create the basic DSN setup as well as the port if it is in
- // in the configuration options. This will give us the basic DSN we will
- // need to establish the PDO connections and return them back for use.
- if (in_array('dblib', $this->getAvailableDrivers())) {
- return $this->getDblibDsn($config);
- } elseif ($this->prefersOdbc($config)) {
- return $this->getOdbcDsn($config);
- }
-
- return $this->getSqlSrvDsn($config);
- }
-
- /**
- * Determine if the database configuration prefers ODBC.
- *
- * @param array $config
- * @return bool
- */
- protected function prefersOdbc(array $config)
- {
- return in_array('odbc', $this->getAvailableDrivers()) &&
- ($config['odbc'] ?? null) === true;
- }
-
- /**
- * Get the DSN string for a DbLib connection.
- *
- * @param array $config
- * @return string
- */
- protected function getDblibDsn(array $config)
- {
- return $this->buildConnectString('dblib', array_merge([
- 'host' => $this->buildHostString($config, ':'),
- 'dbname' => $config['database'],
- ], Arr::only($config, ['appname', 'charset', 'version'])));
- }
-
- /**
- * Get the DSN string for an ODBC connection.
- *
- * @param array $config
- * @return string
- */
- protected function getOdbcDsn(array $config)
- {
- return isset($config['odbc_datasource_name'])
- ? 'odbc:'.$config['odbc_datasource_name'] : '';
- }
-
- /**
- * Get the DSN string for a SqlSrv connection.
- *
- * @param array $config
- * @return string
- */
- protected function getSqlSrvDsn(array $config)
- {
- $arguments = [
- 'Server' => $this->buildHostString($config, ','),
- ];
-
- if (isset($config['database'])) {
- $arguments['Database'] = $config['database'];
- }
-
- if (isset($config['readonly'])) {
- $arguments['ApplicationIntent'] = 'ReadOnly';
- }
-
- if (isset($config['pooling']) && $config['pooling'] === false) {
- $arguments['ConnectionPooling'] = '0';
- }
-
- if (isset($config['appname'])) {
- $arguments['APP'] = $config['appname'];
- }
-
- if (isset($config['encrypt'])) {
- $arguments['Encrypt'] = $config['encrypt'];
- }
-
- if (isset($config['trust_server_certificate'])) {
- $arguments['TrustServerCertificate'] = $config['trust_server_certificate'];
- }
-
- if (isset($config['multiple_active_result_sets']) && $config['multiple_active_result_sets'] === false) {
- $arguments['MultipleActiveResultSets'] = 'false';
- }
-
- if (isset($config['transaction_isolation'])) {
- $arguments['TransactionIsolation'] = $config['transaction_isolation'];
- }
-
- if (isset($config['multi_subnet_failover'])) {
- $arguments['MultiSubnetFailover'] = $config['multi_subnet_failover'];
- }
-
- return $this->buildConnectString('sqlsrv', $arguments);
- }
-
- /**
- * Build a connection string from the given arguments.
- *
- * @param string $driver
- * @param array $arguments
- * @return string
- */
- protected function buildConnectString($driver, array $arguments)
- {
- return $driver.':'.implode(';', array_map(function ($key) use ($arguments) {
- return sprintf('%s=%s', $key, $arguments[$key]);
- }, array_keys($arguments)));
- }
-
- /**
- * Build a host string from the given configuration.
- *
- * @param array $config
- * @param string $separator
- * @return string
- */
- protected function buildHostString(array $config, $separator)
- {
- if (isset($config['port']) && ! empty($config['port'])) {
- return $config['host'].$separator.$config['port'];
- } else {
- return $config['host'];
- }
- }
-
- /**
- * Get the available PDO drivers.
- *
- * @return array
- */
- protected function getAvailableDrivers()
- {
- return PDO::getAvailableDrivers();
- }
- }
|