summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Driver/SQLSrv
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
committerpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
commitbf6655a534a6775d30cafa67bd801276bda1d98d (patch)
treec6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/doctrine/dbal/src/Driver/SQLSrv
parent94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff)
downloadAppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/dbal/src/Driver/SQLSrv')
-rw-r--r--vendor/doctrine/dbal/src/Driver/SQLSrv/Connection.php108
-rw-r--r--vendor/doctrine/dbal/src/Driver/SQLSrv/Driver.php73
-rw-r--r--vendor/doctrine/dbal/src/Driver/SQLSrv/Exception/Error.php44
-rw-r--r--vendor/doctrine/dbal/src/Driver/SQLSrv/Result.php104
-rw-r--r--vendor/doctrine/dbal/src/Driver/SQLSrv/Statement.php140
5 files changed, 469 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Driver/SQLSrv/Connection.php b/vendor/doctrine/dbal/src/Driver/SQLSrv/Connection.php
new file mode 100644
index 0000000..71050f1
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/SQLSrv/Connection.php
@@ -0,0 +1,108 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\SQLSrv;
6
7use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
8use Doctrine\DBAL\Driver\Exception\NoIdentityValue;
9use Doctrine\DBAL\Driver\SQLSrv\Exception\Error;
10
11use function sqlsrv_begin_transaction;
12use function sqlsrv_commit;
13use function sqlsrv_query;
14use function sqlsrv_rollback;
15use function sqlsrv_rows_affected;
16use function sqlsrv_server_info;
17use function str_replace;
18
19final class Connection implements ConnectionInterface
20{
21 /**
22 * @internal The connection can be only instantiated by its driver.
23 *
24 * @param resource $connection
25 */
26 public function __construct(private readonly mixed $connection)
27 {
28 }
29
30 public function getServerVersion(): string
31 {
32 $serverInfo = sqlsrv_server_info($this->connection);
33
34 return $serverInfo['SQLServerVersion'];
35 }
36
37 public function prepare(string $sql): Statement
38 {
39 return new Statement($this->connection, $sql);
40 }
41
42 public function query(string $sql): Result
43 {
44 return $this->prepare($sql)->execute();
45 }
46
47 public function quote(string $value): string
48 {
49 return "'" . str_replace("'", "''", $value) . "'";
50 }
51
52 public function exec(string $sql): int
53 {
54 $stmt = sqlsrv_query($this->connection, $sql);
55
56 if ($stmt === false) {
57 throw Error::new();
58 }
59
60 $rowsAffected = sqlsrv_rows_affected($stmt);
61
62 if ($rowsAffected === false) {
63 throw Error::new();
64 }
65
66 return $rowsAffected;
67 }
68
69 public function lastInsertId(): int|string
70 {
71 $result = $this->query('SELECT @@IDENTITY');
72
73 $lastInsertId = $result->fetchOne();
74
75 if ($lastInsertId === null) {
76 throw NoIdentityValue::new();
77 }
78
79 return $lastInsertId;
80 }
81
82 public function beginTransaction(): void
83 {
84 if (! sqlsrv_begin_transaction($this->connection)) {
85 throw Error::new();
86 }
87 }
88
89 public function commit(): void
90 {
91 if (! sqlsrv_commit($this->connection)) {
92 throw Error::new();
93 }
94 }
95
96 public function rollBack(): void
97 {
98 if (! sqlsrv_rollback($this->connection)) {
99 throw Error::new();
100 }
101 }
102
103 /** @return resource */
104 public function getNativeConnection()
105 {
106 return $this->connection;
107 }
108}
diff --git a/vendor/doctrine/dbal/src/Driver/SQLSrv/Driver.php b/vendor/doctrine/dbal/src/Driver/SQLSrv/Driver.php
new file mode 100644
index 0000000..c9c2c34
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/SQLSrv/Driver.php
@@ -0,0 +1,73 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\SQLSrv;
6
7use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
8use Doctrine\DBAL\Driver\AbstractSQLServerDriver\Exception\PortWithoutHost;
9use Doctrine\DBAL\Driver\SQLSrv\Exception\Error;
10use SensitiveParameter;
11
12use function sqlsrv_configure;
13use function sqlsrv_connect;
14
15/**
16 * Driver for ext/sqlsrv.
17 */
18final class Driver extends AbstractSQLServerDriver
19{
20 /**
21 * {@inheritDoc}
22 */
23 public function connect(
24 #[SensitiveParameter]
25 array $params,
26 ): Connection {
27 $serverName = '';
28
29 if (isset($params['host'])) {
30 $serverName = $params['host'];
31
32 if (isset($params['port'])) {
33 $serverName .= ',' . $params['port'];
34 }
35 } elseif (isset($params['port'])) {
36 throw PortWithoutHost::new();
37 }
38
39 $driverOptions = $params['driverOptions'] ?? [];
40
41 if (isset($params['dbname'])) {
42 $driverOptions['Database'] = $params['dbname'];
43 }
44
45 if (isset($params['charset'])) {
46 $driverOptions['CharacterSet'] = $params['charset'];
47 }
48
49 if (isset($params['user'])) {
50 $driverOptions['UID'] = $params['user'];
51 }
52
53 if (isset($params['password'])) {
54 $driverOptions['PWD'] = $params['password'];
55 }
56
57 if (! isset($driverOptions['ReturnDatesAsStrings'])) {
58 $driverOptions['ReturnDatesAsStrings'] = 1;
59 }
60
61 if (! sqlsrv_configure('WarningsReturnAsErrors', 0)) {
62 throw Error::new();
63 }
64
65 $connection = sqlsrv_connect($serverName, $driverOptions);
66
67 if ($connection === false) {
68 throw Error::new();
69 }
70
71 return new Connection($connection);
72 }
73}
diff --git a/vendor/doctrine/dbal/src/Driver/SQLSrv/Exception/Error.php b/vendor/doctrine/dbal/src/Driver/SQLSrv/Exception/Error.php
new file mode 100644
index 0000000..f39d5fc
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/SQLSrv/Exception/Error.php
@@ -0,0 +1,44 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\SQLSrv\Exception;
6
7use Doctrine\DBAL\Driver\AbstractException;
8
9use function rtrim;
10use function sqlsrv_errors;
11
12use const SQLSRV_ERR_ERRORS;
13
14/**
15 * @internal
16 *
17 * @psalm-immutable
18 */
19final class Error extends AbstractException
20{
21 public static function new(): self
22 {
23 $message = '';
24 $sqlState = null;
25 $code = 0;
26
27 foreach ((array) sqlsrv_errors(SQLSRV_ERR_ERRORS) as $error) {
28 $message .= 'SQLSTATE [' . $error['SQLSTATE'] . ', ' . $error['code'] . ']: ' . $error['message'] . "\n";
29 $sqlState ??= $error['SQLSTATE'];
30
31 if ($code !== 0) {
32 continue;
33 }
34
35 $code = $error['code'];
36 }
37
38 if ($message === '') {
39 $message = 'SQL Server error occurred but no error message was retrieved from driver.';
40 }
41
42 return new self(rtrim($message), $sqlState, $code);
43 }
44}
diff --git a/vendor/doctrine/dbal/src/Driver/SQLSrv/Result.php b/vendor/doctrine/dbal/src/Driver/SQLSrv/Result.php
new file mode 100644
index 0000000..316368f
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/SQLSrv/Result.php
@@ -0,0 +1,104 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\SQLSrv;
6
7use Doctrine\DBAL\Driver\FetchUtils;
8use Doctrine\DBAL\Driver\Result as ResultInterface;
9
10use function sqlsrv_fetch;
11use function sqlsrv_fetch_array;
12use function sqlsrv_num_fields;
13use function sqlsrv_rows_affected;
14
15use const SQLSRV_FETCH_ASSOC;
16use const SQLSRV_FETCH_NUMERIC;
17
18final class Result implements ResultInterface
19{
20 /**
21 * @internal The result can be only instantiated by its driver connection or statement.
22 *
23 * @param resource $statement
24 */
25 public function __construct(private readonly mixed $statement)
26 {
27 }
28
29 public function fetchNumeric(): array|false
30 {
31 return $this->fetch(SQLSRV_FETCH_NUMERIC);
32 }
33
34 public function fetchAssociative(): array|false
35 {
36 return $this->fetch(SQLSRV_FETCH_ASSOC);
37 }
38
39 public function fetchOne(): mixed
40 {
41 return FetchUtils::fetchOne($this);
42 }
43
44 /**
45 * {@inheritDoc}
46 */
47 public function fetchAllNumeric(): array
48 {
49 return FetchUtils::fetchAllNumeric($this);
50 }
51
52 /**
53 * {@inheritDoc}
54 */
55 public function fetchAllAssociative(): array
56 {
57 return FetchUtils::fetchAllAssociative($this);
58 }
59
60 /**
61 * {@inheritDoc}
62 */
63 public function fetchFirstColumn(): array
64 {
65 return FetchUtils::fetchFirstColumn($this);
66 }
67
68 public function rowCount(): int
69 {
70 $count = sqlsrv_rows_affected($this->statement);
71
72 if ($count !== false) {
73 return $count;
74 }
75
76 return 0;
77 }
78
79 public function columnCount(): int
80 {
81 $count = sqlsrv_num_fields($this->statement);
82
83 if ($count !== false) {
84 return $count;
85 }
86
87 return 0;
88 }
89
90 public function free(): void
91 {
92 // emulate it by fetching and discarding rows, similarly to what PDO does in this case
93 // @link http://php.net/manual/en/pdostatement.closecursor.php
94 // @link https://github.com/php/php-src/blob/php-7.0.11/ext/pdo/pdo_stmt.c#L2075
95 // deliberately do not consider multiple result sets, since doctrine/dbal doesn't support them
96 while (sqlsrv_fetch($this->statement)) {
97 }
98 }
99
100 private function fetch(int $fetchType): mixed
101 {
102 return sqlsrv_fetch_array($this->statement, $fetchType) ?? false;
103 }
104}
diff --git a/vendor/doctrine/dbal/src/Driver/SQLSrv/Statement.php b/vendor/doctrine/dbal/src/Driver/SQLSrv/Statement.php
new file mode 100644
index 0000000..dc7827a
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/SQLSrv/Statement.php
@@ -0,0 +1,140 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\SQLSrv;
6
7use Doctrine\DBAL\Driver\Exception;
8use Doctrine\DBAL\Driver\SQLSrv\Exception\Error;
9use Doctrine\DBAL\Driver\Statement as StatementInterface;
10use Doctrine\DBAL\ParameterType;
11
12use function assert;
13use function is_int;
14use function sqlsrv_execute;
15use function SQLSRV_PHPTYPE_STREAM;
16use function SQLSRV_PHPTYPE_STRING;
17use function sqlsrv_prepare;
18use function SQLSRV_SQLTYPE_VARBINARY;
19use function stripos;
20
21use const SQLSRV_ENC_BINARY;
22use const SQLSRV_ENC_CHAR;
23use const SQLSRV_PARAM_IN;
24
25final class Statement implements StatementInterface
26{
27 /**
28 * The SQLSRV statement resource.
29 *
30 * @var resource|null
31 */
32 private $stmt;
33
34 /**
35 * References to the variables bound as statement parameters.
36 *
37 * @var array<int, mixed>
38 */
39 private array $variables = [];
40
41 /**
42 * Bound parameter types.
43 *
44 * @var array<int, ParameterType>
45 */
46 private array $types = [];
47
48 /**
49 * Append to any INSERT query to retrieve the last insert id.
50 */
51 private const LAST_INSERT_ID_SQL = ';SELECT SCOPE_IDENTITY() AS LastInsertId;';
52
53 /**
54 * @internal The statement can be only instantiated by its driver connection.
55 *
56 * @param resource $conn
57 */
58 public function __construct(
59 private readonly mixed $conn,
60 private string $sql,
61 ) {
62 if (stripos($sql, 'INSERT INTO ') !== 0) {
63 return;
64 }
65
66 $this->sql .= self::LAST_INSERT_ID_SQL;
67 }
68
69 public function bindValue(int|string $param, mixed $value, ParameterType $type): void
70 {
71 assert(is_int($param));
72
73 $this->variables[$param] = $value;
74 $this->types[$param] = $type;
75 }
76
77 public function execute(): Result
78 {
79 $this->stmt ??= $this->prepare();
80
81 if (! sqlsrv_execute($this->stmt)) {
82 throw Error::new();
83 }
84
85 return new Result($this->stmt);
86 }
87
88 /**
89 * Prepares SQL Server statement resource
90 *
91 * @return resource
92 *
93 * @throws Exception
94 */
95 private function prepare()
96 {
97 $params = [];
98
99 foreach ($this->variables as $column => &$variable) {
100 switch ($this->types[$column]) {
101 case ParameterType::LARGE_OBJECT:
102 $params[$column - 1] = [
103 &$variable,
104 SQLSRV_PARAM_IN,
105 SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY),
106 SQLSRV_SQLTYPE_VARBINARY('max'),
107 ];
108 break;
109
110 case ParameterType::BINARY:
111 $params[$column - 1] = [
112 &$variable,
113 SQLSRV_PARAM_IN,
114 SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY),
115 ];
116 break;
117
118 case ParameterType::ASCII:
119 $params[$column - 1] = [
120 &$variable,
121 SQLSRV_PARAM_IN,
122 SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR),
123 ];
124 break;
125
126 default:
127 $params[$column - 1] =& $variable;
128 break;
129 }
130 }
131
132 $stmt = sqlsrv_prepare($this->conn, $this->sql, $params);
133
134 if ($stmt === false) {
135 throw Error::new();
136 }
137
138 return $stmt;
139 }
140}