ConnectionFactory.php
3 weeks ago
Connector.php
2 years ago
ConnectorInterface.php
2 years ago
MySqlConnector.php
2 years ago
PostgresConnector.php
3 weeks ago
SQLiteConnector.php
3 weeks ago
SqlServerConnector.php
3 weeks ago
SqlServerConnector.php
188 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Connectors; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Support\Arr; |
| 6 | use PDO; |
| 7 | /** @internal */ |
| 8 | class SqlServerConnector extends Connector implements ConnectorInterface |
| 9 | { |
| 10 | /** |
| 11 | * The PDO connection options. |
| 12 | * |
| 13 | * @var array |
| 14 | */ |
| 15 | 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]; |
| 16 | /** |
| 17 | * Establish a database connection. |
| 18 | * |
| 19 | * @param array $config |
| 20 | * @return \PDO |
| 21 | */ |
| 22 | public function connect(array $config) |
| 23 | { |
| 24 | $options = $this->getOptions($config); |
| 25 | $connection = $this->createConnection($this->getDsn($config), $config, $options); |
| 26 | $this->configureIsolationLevel($connection, $config); |
| 27 | return $connection; |
| 28 | } |
| 29 | /** |
| 30 | * Set the connection transaction isolation level. |
| 31 | * |
| 32 | * https://learn.microsoft.com/en-us/sql/t-sql/statements/set-transaction-isolation-level-transact-sql |
| 33 | * |
| 34 | * @param \PDO $connection |
| 35 | * @param array $config |
| 36 | * @return void |
| 37 | */ |
| 38 | protected function configureIsolationLevel($connection, array $config) |
| 39 | { |
| 40 | if (!isset($config['isolation_level'])) { |
| 41 | return; |
| 42 | } |
| 43 | $connection->prepare("SET TRANSACTION ISOLATION LEVEL {$config['isolation_level']}")->execute(); |
| 44 | } |
| 45 | /** |
| 46 | * Create a DSN string from a configuration. |
| 47 | * |
| 48 | * @param array $config |
| 49 | * @return string |
| 50 | */ |
| 51 | protected function getDsn(array $config) |
| 52 | { |
| 53 | // First we will create the basic DSN setup as well as the port if it is in |
| 54 | // in the configuration options. This will give us the basic DSN we will |
| 55 | // need to establish the PDO connections and return them back for use. |
| 56 | if ($this->prefersOdbc($config)) { |
| 57 | return $this->getOdbcDsn($config); |
| 58 | } |
| 59 | if (\in_array('sqlsrv', $this->getAvailableDrivers())) { |
| 60 | return $this->getSqlSrvDsn($config); |
| 61 | } else { |
| 62 | return $this->getDblibDsn($config); |
| 63 | } |
| 64 | } |
| 65 | /** |
| 66 | * Determine if the database configuration prefers ODBC. |
| 67 | * |
| 68 | * @param array $config |
| 69 | * @return bool |
| 70 | */ |
| 71 | protected function prefersOdbc(array $config) |
| 72 | { |
| 73 | return \in_array('odbc', $this->getAvailableDrivers()) && ($config['odbc'] ?? null) === \true; |
| 74 | } |
| 75 | /** |
| 76 | * Get the DSN string for a DbLib connection. |
| 77 | * |
| 78 | * @param array $config |
| 79 | * @return string |
| 80 | */ |
| 81 | protected function getDblibDsn(array $config) |
| 82 | { |
| 83 | return $this->buildConnectString('dblib', \array_merge(['host' => $this->buildHostString($config, ':'), 'dbname' => $config['database']], Arr::only($config, ['appname', 'charset', 'version']))); |
| 84 | } |
| 85 | /** |
| 86 | * Get the DSN string for an ODBC connection. |
| 87 | * |
| 88 | * @param array $config |
| 89 | * @return string |
| 90 | */ |
| 91 | protected function getOdbcDsn(array $config) |
| 92 | { |
| 93 | return isset($config['odbc_datasource_name']) ? 'odbc:' . $config['odbc_datasource_name'] : ''; |
| 94 | } |
| 95 | /** |
| 96 | * Get the DSN string for a SqlSrv connection. |
| 97 | * |
| 98 | * @param array $config |
| 99 | * @return string |
| 100 | */ |
| 101 | protected function getSqlSrvDsn(array $config) |
| 102 | { |
| 103 | $arguments = ['Server' => $this->buildHostString($config, ',')]; |
| 104 | if (isset($config['database'])) { |
| 105 | $arguments['Database'] = $config['database']; |
| 106 | } |
| 107 | if (isset($config['readonly'])) { |
| 108 | $arguments['ApplicationIntent'] = 'ReadOnly'; |
| 109 | } |
| 110 | if (isset($config['pooling']) && $config['pooling'] === \false) { |
| 111 | $arguments['ConnectionPooling'] = '0'; |
| 112 | } |
| 113 | if (isset($config['appname'])) { |
| 114 | $arguments['APP'] = $config['appname']; |
| 115 | } |
| 116 | if (isset($config['encrypt'])) { |
| 117 | $arguments['Encrypt'] = $config['encrypt']; |
| 118 | } |
| 119 | if (isset($config['trust_server_certificate'])) { |
| 120 | $arguments['TrustServerCertificate'] = $config['trust_server_certificate']; |
| 121 | } |
| 122 | if (isset($config['multiple_active_result_sets']) && $config['multiple_active_result_sets'] === \false) { |
| 123 | $arguments['MultipleActiveResultSets'] = 'false'; |
| 124 | } |
| 125 | if (isset($config['transaction_isolation'])) { |
| 126 | $arguments['TransactionIsolation'] = $config['transaction_isolation']; |
| 127 | } |
| 128 | if (isset($config['multi_subnet_failover'])) { |
| 129 | $arguments['MultiSubnetFailover'] = $config['multi_subnet_failover']; |
| 130 | } |
| 131 | if (isset($config['column_encryption'])) { |
| 132 | $arguments['ColumnEncryption'] = $config['column_encryption']; |
| 133 | } |
| 134 | if (isset($config['key_store_authentication'])) { |
| 135 | $arguments['KeyStoreAuthentication'] = $config['key_store_authentication']; |
| 136 | } |
| 137 | if (isset($config['key_store_principal_id'])) { |
| 138 | $arguments['KeyStorePrincipalId'] = $config['key_store_principal_id']; |
| 139 | } |
| 140 | if (isset($config['key_store_secret'])) { |
| 141 | $arguments['KeyStoreSecret'] = $config['key_store_secret']; |
| 142 | } |
| 143 | if (isset($config['login_timeout'])) { |
| 144 | $arguments['LoginTimeout'] = $config['login_timeout']; |
| 145 | } |
| 146 | if (isset($config['authentication'])) { |
| 147 | $arguments['Authentication'] = $config['authentication']; |
| 148 | } |
| 149 | return $this->buildConnectString('sqlsrv', $arguments); |
| 150 | } |
| 151 | /** |
| 152 | * Build a connection string from the given arguments. |
| 153 | * |
| 154 | * @param string $driver |
| 155 | * @param array $arguments |
| 156 | * @return string |
| 157 | */ |
| 158 | protected function buildConnectString($driver, array $arguments) |
| 159 | { |
| 160 | return $driver . ':' . \implode(';', \array_map(function ($key) use($arguments) { |
| 161 | return \sprintf('%s=%s', $key, $arguments[$key]); |
| 162 | }, \array_keys($arguments))); |
| 163 | } |
| 164 | /** |
| 165 | * Build a host string from the given configuration. |
| 166 | * |
| 167 | * @param array $config |
| 168 | * @param string $separator |
| 169 | * @return string |
| 170 | */ |
| 171 | protected function buildHostString(array $config, $separator) |
| 172 | { |
| 173 | if (empty($config['port'])) { |
| 174 | return $config['host']; |
| 175 | } |
| 176 | return $config['host'] . $separator . $config['port']; |
| 177 | } |
| 178 | /** |
| 179 | * Get the available PDO drivers. |
| 180 | * |
| 181 | * @return array |
| 182 | */ |
| 183 | protected function getAvailableDrivers() |
| 184 | { |
| 185 | return PDO::getAvailableDrivers(); |
| 186 | } |
| 187 | } |
| 188 |