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