diff options
author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/doctrine/dbal/src/Driver/PDO/SQLSrv | |
parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/dbal/src/Driver/PDO/SQLSrv')
3 files changed, 183 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Connection.php b/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Connection.php new file mode 100644 index 0000000..78ba7f8 --- /dev/null +++ b/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Connection.php | |||
@@ -0,0 +1,29 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Driver\PDO\SQLSrv; | ||
6 | |||
7 | use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware; | ||
8 | use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection; | ||
9 | use PDO; | ||
10 | |||
11 | final class Connection extends AbstractConnectionMiddleware | ||
12 | { | ||
13 | public function __construct(private readonly PDOConnection $connection) | ||
14 | { | ||
15 | parent::__construct($connection); | ||
16 | } | ||
17 | |||
18 | public function prepare(string $sql): Statement | ||
19 | { | ||
20 | return new Statement( | ||
21 | $this->connection->prepare($sql), | ||
22 | ); | ||
23 | } | ||
24 | |||
25 | public function getNativeConnection(): PDO | ||
26 | { | ||
27 | return $this->connection->getNativeConnection(); | ||
28 | } | ||
29 | } | ||
diff --git a/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Driver.php b/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Driver.php new file mode 100644 index 0000000..a3cae97 --- /dev/null +++ b/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Driver.php | |||
@@ -0,0 +1,108 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Driver\PDO\SQLSrv; | ||
6 | |||
7 | use Doctrine\DBAL\Driver\AbstractSQLServerDriver; | ||
8 | use Doctrine\DBAL\Driver\AbstractSQLServerDriver\Exception\PortWithoutHost; | ||
9 | use Doctrine\DBAL\Driver\Exception; | ||
10 | use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection; | ||
11 | use Doctrine\DBAL\Driver\PDO\Exception as PDOException; | ||
12 | use PDO; | ||
13 | use SensitiveParameter; | ||
14 | |||
15 | use function is_int; | ||
16 | use function sprintf; | ||
17 | |||
18 | final class Driver extends AbstractSQLServerDriver | ||
19 | { | ||
20 | /** | ||
21 | * {@inheritDoc} | ||
22 | */ | ||
23 | public function connect( | ||
24 | #[SensitiveParameter] | ||
25 | array $params, | ||
26 | ): Connection { | ||
27 | $driverOptions = $dsnOptions = []; | ||
28 | |||
29 | if (isset($params['driverOptions'])) { | ||
30 | foreach ($params['driverOptions'] as $option => $value) { | ||
31 | if (is_int($option)) { | ||
32 | $driverOptions[$option] = $value; | ||
33 | } else { | ||
34 | $dsnOptions[$option] = $value; | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | |||
39 | if (! empty($params['persistent'])) { | ||
40 | $driverOptions[PDO::ATTR_PERSISTENT] = true; | ||
41 | } | ||
42 | |||
43 | $safeParams = $params; | ||
44 | unset($safeParams['password']); | ||
45 | |||
46 | try { | ||
47 | $pdo = new PDO( | ||
48 | $this->constructDsn($safeParams, $dsnOptions), | ||
49 | $params['user'] ?? '', | ||
50 | $params['password'] ?? '', | ||
51 | $driverOptions, | ||
52 | ); | ||
53 | } catch (\PDOException $exception) { | ||
54 | throw PDOException::new($exception); | ||
55 | } | ||
56 | |||
57 | return new Connection(new PDOConnection($pdo)); | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * Constructs the Sqlsrv PDO DSN. | ||
62 | * | ||
63 | * @param mixed[] $params | ||
64 | * @param string[] $connectionOptions | ||
65 | * | ||
66 | * @throws Exception | ||
67 | */ | ||
68 | private function constructDsn(array $params, array $connectionOptions): string | ||
69 | { | ||
70 | $dsn = 'sqlsrv:server='; | ||
71 | |||
72 | if (isset($params['host'])) { | ||
73 | $dsn .= $params['host']; | ||
74 | |||
75 | if (isset($params['port'])) { | ||
76 | $dsn .= ',' . $params['port']; | ||
77 | } | ||
78 | } elseif (isset($params['port'])) { | ||
79 | throw PortWithoutHost::new(); | ||
80 | } | ||
81 | |||
82 | if (isset($params['dbname'])) { | ||
83 | $connectionOptions['Database'] = $params['dbname']; | ||
84 | } | ||
85 | |||
86 | if (isset($params['MultipleActiveResultSets'])) { | ||
87 | $connectionOptions['MultipleActiveResultSets'] = $params['MultipleActiveResultSets'] ? 'true' : 'false'; | ||
88 | } | ||
89 | |||
90 | return $dsn . $this->getConnectionOptionsDsn($connectionOptions); | ||
91 | } | ||
92 | |||
93 | /** | ||
94 | * Converts a connection options array to the DSN | ||
95 | * | ||
96 | * @param string[] $connectionOptions | ||
97 | */ | ||
98 | private function getConnectionOptionsDsn(array $connectionOptions): string | ||
99 | { | ||
100 | $connectionOptionsDsn = ''; | ||
101 | |||
102 | foreach ($connectionOptions as $paramName => $paramValue) { | ||
103 | $connectionOptionsDsn .= sprintf(';%s=%s', $paramName, $paramValue); | ||
104 | } | ||
105 | |||
106 | return $connectionOptionsDsn; | ||
107 | } | ||
108 | } | ||
diff --git a/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Statement.php b/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Statement.php new file mode 100644 index 0000000..44cecc9 --- /dev/null +++ b/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Statement.php | |||
@@ -0,0 +1,46 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Driver\PDO\SQLSrv; | ||
6 | |||
7 | use Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware; | ||
8 | use Doctrine\DBAL\Driver\PDO\Statement as PDOStatement; | ||
9 | use Doctrine\DBAL\ParameterType; | ||
10 | use PDO; | ||
11 | |||
12 | final class Statement extends AbstractStatementMiddleware | ||
13 | { | ||
14 | /** @internal The statement can be only instantiated by its driver connection. */ | ||
15 | public function __construct(private readonly PDOStatement $statement) | ||
16 | { | ||
17 | parent::__construct($statement); | ||
18 | } | ||
19 | |||
20 | public function bindValue(int|string $param, mixed $value, ParameterType $type): void | ||
21 | { | ||
22 | switch ($type) { | ||
23 | case ParameterType::LARGE_OBJECT: | ||
24 | case ParameterType::BINARY: | ||
25 | $this->statement->bindParamWithDriverOptions( | ||
26 | $param, | ||
27 | $value, | ||
28 | $type, | ||
29 | PDO::SQLSRV_ENCODING_BINARY, | ||
30 | ); | ||
31 | break; | ||
32 | |||
33 | case ParameterType::ASCII: | ||
34 | $this->statement->bindParamWithDriverOptions( | ||
35 | $param, | ||
36 | $value, | ||
37 | ParameterType::STRING, | ||
38 | PDO::SQLSRV_ENCODING_SYSTEM, | ||
39 | ); | ||
40 | break; | ||
41 | |||
42 | default: | ||
43 | $this->statement->bindValue($param, $value, $type); | ||
44 | } | ||
45 | } | ||
46 | } | ||