SqlServerConnector.php
| 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 | return $this->createConnection($this->getDsn($config), $config, $options); |
| 26 | } |
| 27 | /** |
| 28 | * Create a DSN string from a configuration. |
| 29 | * |
| 30 | * @param array $config |
| 31 | * @return string |
| 32 | */ |
| 33 | protected function getDsn(array $config) |
| 34 | { |
| 35 | // First we will create the basic DSN setup as well as the port if it is in |
| 36 | // in the configuration options. This will give us the basic DSN we will |
| 37 | // need to establish the PDO connections and return them back for use. |
| 38 | if ($this->prefersOdbc($config)) { |
| 39 | return $this->getOdbcDsn($config); |
| 40 | } |
| 41 | if (\in_array('sqlsrv', $this->getAvailableDrivers())) { |
| 42 | return $this->getSqlSrvDsn($config); |
| 43 | } else { |
| 44 | return $this->getDblibDsn($config); |
| 45 | } |
| 46 | } |
| 47 | /** |
| 48 | * Determine if the database configuration prefers ODBC. |
| 49 | * |
| 50 | * @param array $config |
| 51 | * @return bool |
| 52 | */ |
| 53 | protected function prefersOdbc(array $config) |
| 54 | { |
| 55 | return \in_array('odbc', $this->getAvailableDrivers()) && ($config['odbc'] ?? null) === \true; |
| 56 | } |
| 57 | /** |
| 58 | * Get the DSN string for a DbLib connection. |
| 59 | * |
| 60 | * @param array $config |
| 61 | * @return string |
| 62 | */ |
| 63 | protected function getDblibDsn(array $config) |
| 64 | { |
| 65 | return $this->buildConnectString('dblib', \array_merge(['host' => $this->buildHostString($config, ':'), 'dbname' => $config['database']], Arr::only($config, ['appname', 'charset', 'version']))); |
| 66 | } |
| 67 | /** |
| 68 | * Get the DSN string for an ODBC connection. |
| 69 | * |
| 70 | * @param array $config |
| 71 | * @return string |
| 72 | */ |
| 73 | protected function getOdbcDsn(array $config) |
| 74 | { |
| 75 | return isset($config['odbc_datasource_name']) ? 'odbc:' . $config['odbc_datasource_name'] : ''; |
| 76 | } |
| 77 | /** |
| 78 | * Get the DSN string for a SqlSrv connection. |
| 79 | * |
| 80 | * @param array $config |
| 81 | * @return string |
| 82 | */ |
| 83 | protected function getSqlSrvDsn(array $config) |
| 84 | { |
| 85 | $arguments = ['Server' => $this->buildHostString($config, ',')]; |
| 86 | if (isset($config['database'])) { |
| 87 | $arguments['Database'] = $config['database']; |
| 88 | } |
| 89 | if (isset($config['readonly'])) { |
| 90 | $arguments['ApplicationIntent'] = 'ReadOnly'; |
| 91 | } |
| 92 | if (isset($config['pooling']) && $config['pooling'] === \false) { |
| 93 | $arguments['ConnectionPooling'] = '0'; |
| 94 | } |
| 95 | if (isset($config['appname'])) { |
| 96 | $arguments['APP'] = $config['appname']; |
| 97 | } |
| 98 | if (isset($config['encrypt'])) { |
| 99 | $arguments['Encrypt'] = $config['encrypt']; |
| 100 | } |
| 101 | if (isset($config['trust_server_certificate'])) { |
| 102 | $arguments['TrustServerCertificate'] = $config['trust_server_certificate']; |
| 103 | } |
| 104 | if (isset($config['multiple_active_result_sets']) && $config['multiple_active_result_sets'] === \false) { |
| 105 | $arguments['MultipleActiveResultSets'] = 'false'; |
| 106 | } |
| 107 | if (isset($config['transaction_isolation'])) { |
| 108 | $arguments['TransactionIsolation'] = $config['transaction_isolation']; |
| 109 | } |
| 110 | if (isset($config['multi_subnet_failover'])) { |
| 111 | $arguments['MultiSubnetFailover'] = $config['multi_subnet_failover']; |
| 112 | } |
| 113 | if (isset($config['column_encryption'])) { |
| 114 | $arguments['ColumnEncryption'] = $config['column_encryption']; |
| 115 | } |
| 116 | if (isset($config['key_store_authentication'])) { |
| 117 | $arguments['KeyStoreAuthentication'] = $config['key_store_authentication']; |
| 118 | } |
| 119 | if (isset($config['key_store_principal_id'])) { |
| 120 | $arguments['KeyStorePrincipalId'] = $config['key_store_principal_id']; |
| 121 | } |
| 122 | if (isset($config['key_store_secret'])) { |
| 123 | $arguments['KeyStoreSecret'] = $config['key_store_secret']; |
| 124 | } |
| 125 | if (isset($config['login_timeout'])) { |
| 126 | $arguments['LoginTimeout'] = $config['login_timeout']; |
| 127 | } |
| 128 | return $this->buildConnectString('sqlsrv', $arguments); |
| 129 | } |
| 130 | /** |
| 131 | * Build a connection string from the given arguments. |
| 132 | * |
| 133 | * @param string $driver |
| 134 | * @param array $arguments |
| 135 | * @return string |
| 136 | */ |
| 137 | protected function buildConnectString($driver, array $arguments) |
| 138 | { |
| 139 | return $driver . ':' . \implode(';', \array_map(function ($key) use($arguments) { |
| 140 | return \sprintf('%s=%s', $key, $arguments[$key]); |
| 141 | }, \array_keys($arguments))); |
| 142 | } |
| 143 | /** |
| 144 | * Build a host string from the given configuration. |
| 145 | * |
| 146 | * @param array $config |
| 147 | * @param string $separator |
| 148 | * @return string |
| 149 | */ |
| 150 | protected function buildHostString(array $config, $separator) |
| 151 | { |
| 152 | if (empty($config['port'])) { |
| 153 | return $config['host']; |
| 154 | } |
| 155 | return $config['host'] . $separator . $config['port']; |
| 156 | } |
| 157 | /** |
| 158 | * Get the available PDO drivers. |
| 159 | * |
| 160 | * @return array |
| 161 | */ |
| 162 | protected function getAvailableDrivers() |
| 163 | { |
| 164 | return PDO::getAvailableDrivers(); |
| 165 | } |
| 166 | } |
| 167 |