summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Driver/IBMDB2
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/IBMDB2
parent94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff)
downloadAppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/dbal/src/Driver/IBMDB2')
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/Connection.php131
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/DataSourceName.php80
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/Driver.php41
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/CannotCopyStreamToStream.php27
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/CannotCreateTemporaryFile.php27
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/ConnectionError.php29
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/ConnectionFailed.php28
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/Factory.php35
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/PrepareFailed.php25
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/StatementError.php34
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/Result.php106
-rw-r--r--vendor/doctrine/dbal/src/Driver/IBMDB2/Statement.php156
12 files changed, 719 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/Connection.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/Connection.php
new file mode 100644
index 0000000..2c8783b
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/Connection.php
@@ -0,0 +1,131 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2;
6
7use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
8use Doctrine\DBAL\Driver\Exception\NoIdentityValue;
9use Doctrine\DBAL\Driver\IBMDB2\Exception\ConnectionError;
10use Doctrine\DBAL\Driver\IBMDB2\Exception\PrepareFailed;
11use Doctrine\DBAL\Driver\IBMDB2\Exception\StatementError;
12use stdClass;
13
14use function assert;
15use function db2_autocommit;
16use function db2_commit;
17use function db2_escape_string;
18use function db2_exec;
19use function db2_last_insert_id;
20use function db2_num_rows;
21use function db2_prepare;
22use function db2_rollback;
23use function db2_server_info;
24use function error_get_last;
25
26use const DB2_AUTOCOMMIT_OFF;
27use const DB2_AUTOCOMMIT_ON;
28
29final class Connection implements ConnectionInterface
30{
31 /**
32 * @internal The connection can be only instantiated by its driver.
33 *
34 * @param resource $connection
35 */
36 public function __construct(private readonly mixed $connection)
37 {
38 }
39
40 public function getServerVersion(): string
41 {
42 $serverInfo = db2_server_info($this->connection);
43 assert($serverInfo instanceof stdClass);
44
45 return $serverInfo->DBMS_VER;
46 }
47
48 public function prepare(string $sql): Statement
49 {
50 $stmt = @db2_prepare($this->connection, $sql);
51
52 if ($stmt === false) {
53 throw PrepareFailed::new(error_get_last());
54 }
55
56 return new Statement($stmt);
57 }
58
59 public function query(string $sql): Result
60 {
61 return $this->prepare($sql)->execute();
62 }
63
64 public function quote(string $value): string
65 {
66 return "'" . db2_escape_string($value) . "'";
67 }
68
69 public function exec(string $sql): int|string
70 {
71 $stmt = @db2_exec($this->connection, $sql);
72
73 if ($stmt === false) {
74 throw StatementError::new();
75 }
76
77 $numRows = db2_num_rows($stmt);
78
79 if ($numRows === false) {
80 throw StatementError::new();
81 }
82
83 return $numRows;
84 }
85
86 public function lastInsertId(): string
87 {
88 $lastInsertId = db2_last_insert_id($this->connection);
89
90 if ($lastInsertId === null) {
91 throw NoIdentityValue::new();
92 }
93
94 return $lastInsertId;
95 }
96
97 public function beginTransaction(): void
98 {
99 if (db2_autocommit($this->connection, DB2_AUTOCOMMIT_OFF) !== true) {
100 throw ConnectionError::new($this->connection);
101 }
102 }
103
104 public function commit(): void
105 {
106 if (! db2_commit($this->connection)) {
107 throw ConnectionError::new($this->connection);
108 }
109
110 if (db2_autocommit($this->connection, DB2_AUTOCOMMIT_ON) !== true) {
111 throw ConnectionError::new($this->connection);
112 }
113 }
114
115 public function rollBack(): void
116 {
117 if (! db2_rollback($this->connection)) {
118 throw ConnectionError::new($this->connection);
119 }
120
121 if (db2_autocommit($this->connection, DB2_AUTOCOMMIT_ON) !== true) {
122 throw ConnectionError::new($this->connection);
123 }
124 }
125
126 /** @return resource */
127 public function getNativeConnection()
128 {
129 return $this->connection;
130 }
131}
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/DataSourceName.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/DataSourceName.php
new file mode 100644
index 0000000..a1e5948
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/DataSourceName.php
@@ -0,0 +1,80 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2;
6
7use SensitiveParameter;
8
9use function implode;
10use function sprintf;
11use function str_contains;
12
13/**
14 * IBM DB2 DSN
15 */
16final class DataSourceName
17{
18 private function __construct(
19 #[SensitiveParameter]
20 private readonly string $string,
21 ) {
22 }
23
24 public function toString(): string
25 {
26 return $this->string;
27 }
28
29 /**
30 * Creates the object from an array representation
31 *
32 * @param array<string,mixed> $params
33 */
34 public static function fromArray(
35 #[SensitiveParameter]
36 array $params,
37 ): self {
38 $chunks = [];
39
40 foreach ($params as $key => $value) {
41 $chunks[] = sprintf('%s=%s', $key, $value);
42 }
43
44 return new self(implode(';', $chunks));
45 }
46
47 /**
48 * Creates the object from the given DBAL connection parameters.
49 *
50 * @param array<string,mixed> $params
51 */
52 public static function fromConnectionParameters(#[SensitiveParameter]
53 array $params,): self
54 {
55 if (isset($params['dbname']) && str_contains($params['dbname'], '=')) {
56 return new self($params['dbname']);
57 }
58
59 $dsnParams = [];
60
61 foreach (
62 [
63 'host' => 'HOSTNAME',
64 'port' => 'PORT',
65 'protocol' => 'PROTOCOL',
66 'dbname' => 'DATABASE',
67 'user' => 'UID',
68 'password' => 'PWD',
69 ] as $dbalParam => $dsnParam
70 ) {
71 if (! isset($params[$dbalParam])) {
72 continue;
73 }
74
75 $dsnParams[$dsnParam] = $params[$dbalParam];
76 }
77
78 return self::fromArray($dsnParams);
79 }
80}
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/Driver.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/Driver.php
new file mode 100644
index 0000000..f2f4ed7
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/Driver.php
@@ -0,0 +1,41 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2;
6
7use Doctrine\DBAL\Driver\AbstractDB2Driver;
8use Doctrine\DBAL\Driver\IBMDB2\Exception\ConnectionFailed;
9use SensitiveParameter;
10
11use function db2_connect;
12use function db2_pconnect;
13
14final class Driver extends AbstractDB2Driver
15{
16 /**
17 * {@inheritDoc}
18 */
19 public function connect(
20 #[SensitiveParameter]
21 array $params,
22 ): Connection {
23 $dataSourceName = DataSourceName::fromConnectionParameters($params)->toString();
24
25 $username = $params['user'] ?? '';
26 $password = $params['password'] ?? '';
27 $driverOptions = $params['driverOptions'] ?? [];
28
29 if (! empty($params['persistent'])) {
30 $connection = db2_pconnect($dataSourceName, $username, $password, $driverOptions);
31 } else {
32 $connection = db2_connect($dataSourceName, $username, $password, $driverOptions);
33 }
34
35 if ($connection === false) {
36 throw ConnectionFailed::new();
37 }
38
39 return new Connection($connection);
40 }
41}
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/CannotCopyStreamToStream.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/CannotCopyStreamToStream.php
new file mode 100644
index 0000000..c584fb8
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/CannotCopyStreamToStream.php
@@ -0,0 +1,27 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2\Exception;
6
7use Doctrine\DBAL\Driver\AbstractException;
8
9/**
10 * @internal
11 *
12 * @psalm-immutable
13 */
14final class CannotCopyStreamToStream extends AbstractException
15{
16 /** @psalm-param array{message: string, ...}|null $error */
17 public static function new(?array $error): self
18 {
19 $message = 'Could not copy source stream to temporary file';
20
21 if ($error !== null) {
22 $message .= ': ' . $error['message'];
23 }
24
25 return new self($message);
26 }
27}
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/CannotCreateTemporaryFile.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/CannotCreateTemporaryFile.php
new file mode 100644
index 0000000..d7646a0
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/CannotCreateTemporaryFile.php
@@ -0,0 +1,27 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2\Exception;
6
7use Doctrine\DBAL\Driver\AbstractException;
8
9/**
10 * @internal
11 *
12 * @psalm-immutable
13 */
14final class CannotCreateTemporaryFile extends AbstractException
15{
16 /** @psalm-param array{message: string, ...}|null $error */
17 public static function new(?array $error): self
18 {
19 $message = 'Could not create temporary file';
20
21 if ($error !== null) {
22 $message .= ': ' . $error['message'];
23 }
24
25 return new self($message);
26 }
27}
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/ConnectionError.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/ConnectionError.php
new file mode 100644
index 0000000..b7bd8be
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/ConnectionError.php
@@ -0,0 +1,29 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2\Exception;
6
7use Doctrine\DBAL\Driver\AbstractException;
8
9use function db2_conn_error;
10use function db2_conn_errormsg;
11
12/**
13 * @internal
14 *
15 * @psalm-immutable
16 */
17final class ConnectionError extends AbstractException
18{
19 /** @param resource $connection */
20 public static function new($connection): self
21 {
22 $message = db2_conn_errormsg($connection);
23 $sqlState = db2_conn_error($connection);
24
25 return Factory::create($message, static function (int $code) use ($message, $sqlState): self {
26 return new self($message, $sqlState, $code);
27 });
28 }
29}
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/ConnectionFailed.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/ConnectionFailed.php
new file mode 100644
index 0000000..9dd0443
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/ConnectionFailed.php
@@ -0,0 +1,28 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2\Exception;
6
7use Doctrine\DBAL\Driver\AbstractException;
8
9use function db2_conn_error;
10use function db2_conn_errormsg;
11
12/**
13 * @internal
14 *
15 * @psalm-immutable
16 */
17final class ConnectionFailed extends AbstractException
18{
19 public static function new(): self
20 {
21 $message = db2_conn_errormsg();
22 $sqlState = db2_conn_error();
23
24 return Factory::create($message, static function (int $code) use ($message, $sqlState): self {
25 return new self($message, $sqlState, $code);
26 });
27 }
28}
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/Factory.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/Factory.php
new file mode 100644
index 0000000..91b9b43
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/Factory.php
@@ -0,0 +1,35 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2\Exception;
6
7use Doctrine\DBAL\Driver\AbstractException;
8
9use function preg_match;
10
11/**
12 * @internal
13 *
14 * @psalm-immutable
15 */
16final class Factory
17{
18 /**
19 * @param callable(int): T $constructor
20 *
21 * @return T
22 *
23 * @template T of AbstractException
24 */
25 public static function create(string $message, callable $constructor): AbstractException
26 {
27 $code = 0;
28
29 if (preg_match('/ SQL(\d+)N /', $message, $matches) === 1) {
30 $code = -(int) $matches[1];
31 }
32
33 return $constructor($code);
34 }
35}
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/PrepareFailed.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/PrepareFailed.php
new file mode 100644
index 0000000..5344b65
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/PrepareFailed.php
@@ -0,0 +1,25 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2\Exception;
6
7use Doctrine\DBAL\Driver\AbstractException;
8
9/**
10 * @internal
11 *
12 * @psalm-immutable
13 */
14final class PrepareFailed extends AbstractException
15{
16 /** @psalm-param array{message: string, ...}|null $error */
17 public static function new(?array $error): self
18 {
19 if ($error === null) {
20 return new self('Unknown error');
21 }
22
23 return new self($error['message']);
24 }
25}
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/StatementError.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/StatementError.php
new file mode 100644
index 0000000..6bf8511
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/Exception/StatementError.php
@@ -0,0 +1,34 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2\Exception;
6
7use Doctrine\DBAL\Driver\AbstractException;
8
9use function db2_stmt_error;
10use function db2_stmt_errormsg;
11
12/**
13 * @internal
14 *
15 * @psalm-immutable
16 */
17final class StatementError extends AbstractException
18{
19 /** @param resource|null $statement */
20 public static function new($statement = null): self
21 {
22 if ($statement !== null) {
23 $message = db2_stmt_errormsg($statement);
24 $sqlState = db2_stmt_error($statement);
25 } else {
26 $message = db2_stmt_errormsg();
27 $sqlState = db2_stmt_error();
28 }
29
30 return Factory::create($message, static function (int $code) use ($message, $sqlState): self {
31 return new self($message, $sqlState, $code);
32 });
33 }
34}
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/Result.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/Result.php
new file mode 100644
index 0000000..461f44a
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/Result.php
@@ -0,0 +1,106 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2;
6
7use Doctrine\DBAL\Driver\FetchUtils;
8use Doctrine\DBAL\Driver\IBMDB2\Exception\StatementError;
9use Doctrine\DBAL\Driver\Result as ResultInterface;
10
11use function db2_fetch_array;
12use function db2_fetch_assoc;
13use function db2_free_result;
14use function db2_num_fields;
15use function db2_num_rows;
16use function db2_stmt_error;
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 $row = @db2_fetch_array($this->statement);
32
33 if ($row === false && db2_stmt_error($this->statement) !== '02000') {
34 throw StatementError::new($this->statement);
35 }
36
37 return $row;
38 }
39
40 public function fetchAssociative(): array|false
41 {
42 $row = @db2_fetch_assoc($this->statement);
43
44 if ($row === false && db2_stmt_error($this->statement) !== '02000') {
45 throw StatementError::new($this->statement);
46 }
47
48 return $row;
49 }
50
51 public function fetchOne(): mixed
52 {
53 return FetchUtils::fetchOne($this);
54 }
55
56 /**
57 * {@inheritDoc}
58 */
59 public function fetchAllNumeric(): array
60 {
61 return FetchUtils::fetchAllNumeric($this);
62 }
63
64 /**
65 * {@inheritDoc}
66 */
67 public function fetchAllAssociative(): array
68 {
69 return FetchUtils::fetchAllAssociative($this);
70 }
71
72 /**
73 * {@inheritDoc}
74 */
75 public function fetchFirstColumn(): array
76 {
77 return FetchUtils::fetchFirstColumn($this);
78 }
79
80 public function rowCount(): int
81 {
82 $numRows = @db2_num_rows($this->statement);
83
84 if ($numRows === false) {
85 throw StatementError::new($this->statement);
86 }
87
88 return $numRows;
89 }
90
91 public function columnCount(): int
92 {
93 $count = db2_num_fields($this->statement);
94
95 if ($count !== false) {
96 return $count;
97 }
98
99 return 0;
100 }
101
102 public function free(): void
103 {
104 db2_free_result($this->statement);
105 }
106}
diff --git a/vendor/doctrine/dbal/src/Driver/IBMDB2/Statement.php b/vendor/doctrine/dbal/src/Driver/IBMDB2/Statement.php
new file mode 100644
index 0000000..96852da
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/IBMDB2/Statement.php
@@ -0,0 +1,156 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\IBMDB2;
6
7use Doctrine\DBAL\Driver\Exception;
8use Doctrine\DBAL\Driver\IBMDB2\Exception\CannotCopyStreamToStream;
9use Doctrine\DBAL\Driver\IBMDB2\Exception\CannotCreateTemporaryFile;
10use Doctrine\DBAL\Driver\IBMDB2\Exception\StatementError;
11use Doctrine\DBAL\Driver\Statement as StatementInterface;
12use Doctrine\DBAL\ParameterType;
13
14use function assert;
15use function db2_bind_param;
16use function db2_execute;
17use function error_get_last;
18use function fclose;
19use function is_int;
20use function is_resource;
21use function stream_copy_to_stream;
22use function stream_get_meta_data;
23use function tmpfile;
24
25use const DB2_BINARY;
26use const DB2_CHAR;
27use const DB2_LONG;
28use const DB2_PARAM_FILE;
29use const DB2_PARAM_IN;
30
31final class Statement implements StatementInterface
32{
33 /** @var mixed[] */
34 private array $parameters = [];
35
36 /**
37 * Map of LOB parameter positions to the tuples containing reference to the variable bound to the driver statement
38 * and the temporary file handle bound to the underlying statement
39 *
40 * @var array<int,string|resource|null>
41 */
42 private array $lobs = [];
43
44 /**
45 * @internal The statement can be only instantiated by its driver connection.
46 *
47 * @param resource $stmt
48 */
49 public function __construct(private readonly mixed $stmt)
50 {
51 }
52
53 public function bindValue(int|string $param, mixed $value, ParameterType $type): void
54 {
55 assert(is_int($param));
56
57 switch ($type) {
58 case ParameterType::INTEGER:
59 $this->bind($param, $value, DB2_PARAM_IN, DB2_LONG);
60 break;
61
62 case ParameterType::LARGE_OBJECT:
63 $this->lobs[$param] = &$value;
64 break;
65
66 default:
67 $this->bind($param, $value, DB2_PARAM_IN, DB2_CHAR);
68 break;
69 }
70 }
71
72 /** @throws Exception */
73 private function bind(int $position, mixed &$variable, int $parameterType, int $dataType): void
74 {
75 $this->parameters[$position] =& $variable;
76
77 if (! db2_bind_param($this->stmt, $position, '', $parameterType, $dataType)) {
78 throw StatementError::new($this->stmt);
79 }
80 }
81
82 public function execute(): Result
83 {
84 $handles = $this->bindLobs();
85
86 $result = @db2_execute($this->stmt, $this->parameters);
87
88 foreach ($handles as $handle) {
89 fclose($handle);
90 }
91
92 $this->lobs = [];
93
94 if ($result === false) {
95 throw StatementError::new($this->stmt);
96 }
97
98 return new Result($this->stmt);
99 }
100
101 /**
102 * @return list<resource>
103 *
104 * @throws Exception
105 */
106 private function bindLobs(): array
107 {
108 $handles = [];
109
110 foreach ($this->lobs as $param => $value) {
111 if (is_resource($value)) {
112 $handle = $handles[] = $this->createTemporaryFile();
113 $path = stream_get_meta_data($handle)['uri'];
114
115 $this->copyStreamToStream($value, $handle);
116
117 $this->bind($param, $path, DB2_PARAM_FILE, DB2_BINARY);
118 } else {
119 $this->bind($param, $value, DB2_PARAM_IN, DB2_CHAR);
120 }
121
122 unset($value);
123 }
124
125 return $handles;
126 }
127
128 /**
129 * @return resource
130 *
131 * @throws Exception
132 */
133 private function createTemporaryFile()
134 {
135 $handle = @tmpfile();
136
137 if ($handle === false) {
138 throw CannotCreateTemporaryFile::new(error_get_last());
139 }
140
141 return $handle;
142 }
143
144 /**
145 * @param resource $source
146 * @param resource $target
147 *
148 * @throws Exception
149 */
150 private function copyStreamToStream($source, $target): void
151 {
152 if (@stream_copy_to_stream($source, $target) === false) {
153 throw CannotCopyStreamToStream::new(error_get_last());
154 }
155 }
156}