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/Tools | |
| parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
| download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.tar.gz AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.tar.bz2 AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip | |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/dbal/src/Tools')
5 files changed, 396 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Tools/Console/Command/RunSqlCommand.php b/vendor/doctrine/dbal/src/Tools/Console/Command/RunSqlCommand.php new file mode 100644 index 0000000..8ec6f23 --- /dev/null +++ b/vendor/doctrine/dbal/src/Tools/Console/Command/RunSqlCommand.php | |||
| @@ -0,0 +1,119 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\DBAL\Tools\Console\Command; | ||
| 6 | |||
| 7 | use Doctrine\DBAL\Connection; | ||
| 8 | use Doctrine\DBAL\Exception; | ||
| 9 | use Doctrine\DBAL\Tools\Console\ConnectionProvider; | ||
| 10 | use RuntimeException; | ||
| 11 | use Symfony\Component\Console\Command\Command; | ||
| 12 | use Symfony\Component\Console\Input\InputArgument; | ||
| 13 | use Symfony\Component\Console\Input\InputInterface; | ||
| 14 | use Symfony\Component\Console\Input\InputOption; | ||
| 15 | use Symfony\Component\Console\Output\OutputInterface; | ||
| 16 | use Symfony\Component\Console\Style\SymfonyStyle; | ||
| 17 | |||
| 18 | use function array_keys; | ||
| 19 | use function assert; | ||
| 20 | use function is_bool; | ||
| 21 | use function is_string; | ||
| 22 | use function sprintf; | ||
| 23 | use function stripos; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * Task for executing arbitrary SQL that can come from a file or directly from | ||
| 27 | * the command line. | ||
| 28 | */ | ||
| 29 | class RunSqlCommand extends Command | ||
| 30 | { | ||
| 31 | public function __construct(private readonly ConnectionProvider $connectionProvider) | ||
| 32 | { | ||
| 33 | parent::__construct(); | ||
| 34 | } | ||
| 35 | |||
| 36 | protected function configure(): void | ||
| 37 | { | ||
| 38 | $this | ||
| 39 | ->setName('dbal:run-sql') | ||
| 40 | ->setDescription('Executes arbitrary SQL directly from the command line.') | ||
| 41 | ->setDefinition([ | ||
| 42 | new InputOption('connection', null, InputOption::VALUE_REQUIRED, 'The named database connection'), | ||
| 43 | new InputArgument('sql', InputArgument::REQUIRED, 'The SQL statement to execute.'), | ||
| 44 | new InputOption('depth', null, InputOption::VALUE_REQUIRED, 'Dumping depth of result set (deprecated).'), | ||
| 45 | new InputOption('force-fetch', null, InputOption::VALUE_NONE, 'Forces fetching the result.'), | ||
| 46 | ]) | ||
| 47 | ->setHelp(<<<'EOT' | ||
| 48 | The <info>%command.name%</info> command executes the given SQL query and | ||
| 49 | outputs the results: | ||
| 50 | |||
| 51 | <info>php %command.full_name% "SELECT * FROM users"</info> | ||
| 52 | EOT); | ||
| 53 | } | ||
| 54 | |||
| 55 | /** | ||
| 56 | * {@inheritDoc} | ||
| 57 | * | ||
| 58 | * @throws Exception | ||
| 59 | */ | ||
| 60 | protected function execute(InputInterface $input, OutputInterface $output): int | ||
| 61 | { | ||
| 62 | $conn = $this->getConnection($input); | ||
| 63 | $io = new SymfonyStyle($input, $output); | ||
| 64 | |||
| 65 | $sql = $input->getArgument('sql'); | ||
| 66 | |||
| 67 | if ($sql === null) { | ||
| 68 | throw new RuntimeException('Argument "sql" is required in order to execute this command correctly.'); | ||
| 69 | } | ||
| 70 | |||
| 71 | assert(is_string($sql)); | ||
| 72 | |||
| 73 | if ($input->getOption('depth') !== null) { | ||
| 74 | $io->warning('Parameter "depth" is deprecated and has no effect anymore.'); | ||
| 75 | } | ||
| 76 | |||
| 77 | $forceFetch = $input->getOption('force-fetch'); | ||
| 78 | assert(is_bool($forceFetch)); | ||
| 79 | |||
| 80 | if (stripos($sql, 'select') === 0 || $forceFetch) { | ||
| 81 | $this->runQuery($io, $conn, $sql); | ||
| 82 | } else { | ||
| 83 | $this->runStatement($io, $conn, $sql); | ||
| 84 | } | ||
| 85 | |||
| 86 | return 0; | ||
| 87 | } | ||
| 88 | |||
| 89 | private function getConnection(InputInterface $input): Connection | ||
| 90 | { | ||
| 91 | $connectionName = $input->getOption('connection'); | ||
| 92 | assert(is_string($connectionName) || $connectionName === null); | ||
| 93 | |||
| 94 | if ($connectionName !== null) { | ||
| 95 | return $this->connectionProvider->getConnection($connectionName); | ||
| 96 | } | ||
| 97 | |||
| 98 | return $this->connectionProvider->getDefaultConnection(); | ||
| 99 | } | ||
| 100 | |||
| 101 | /** @throws Exception */ | ||
| 102 | private function runQuery(SymfonyStyle $io, Connection $conn, string $sql): void | ||
| 103 | { | ||
| 104 | $resultSet = $conn->fetchAllAssociative($sql); | ||
| 105 | if ($resultSet === []) { | ||
| 106 | $io->success('The query yielded an empty result set.'); | ||
| 107 | |||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | $io->table(array_keys($resultSet[0]), $resultSet); | ||
| 112 | } | ||
| 113 | |||
| 114 | /** @throws Exception */ | ||
| 115 | private function runStatement(SymfonyStyle $io, Connection $conn, string $sql): void | ||
| 116 | { | ||
| 117 | $io->success(sprintf('%d rows affected.', $conn->executeStatement($sql))); | ||
| 118 | } | ||
| 119 | } | ||
diff --git a/vendor/doctrine/dbal/src/Tools/Console/ConnectionNotFound.php b/vendor/doctrine/dbal/src/Tools/Console/ConnectionNotFound.php new file mode 100644 index 0000000..049d658 --- /dev/null +++ b/vendor/doctrine/dbal/src/Tools/Console/ConnectionNotFound.php | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\DBAL\Tools\Console; | ||
| 6 | |||
| 7 | use OutOfBoundsException; | ||
| 8 | |||
| 9 | final class ConnectionNotFound extends OutOfBoundsException | ||
| 10 | { | ||
| 11 | } | ||
diff --git a/vendor/doctrine/dbal/src/Tools/Console/ConnectionProvider.php b/vendor/doctrine/dbal/src/Tools/Console/ConnectionProvider.php new file mode 100644 index 0000000..0ae28c5 --- /dev/null +++ b/vendor/doctrine/dbal/src/Tools/Console/ConnectionProvider.php | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\DBAL\Tools\Console; | ||
| 6 | |||
| 7 | use Doctrine\DBAL\Connection; | ||
| 8 | |||
| 9 | interface ConnectionProvider | ||
| 10 | { | ||
| 11 | public function getDefaultConnection(): Connection; | ||
| 12 | |||
| 13 | /** @throws ConnectionNotFound in case a connection with the given name does not exist. */ | ||
| 14 | public function getConnection(string $name): Connection; | ||
| 15 | } | ||
diff --git a/vendor/doctrine/dbal/src/Tools/Console/ConnectionProvider/SingleConnectionProvider.php b/vendor/doctrine/dbal/src/Tools/Console/ConnectionProvider/SingleConnectionProvider.php new file mode 100644 index 0000000..12b40c0 --- /dev/null +++ b/vendor/doctrine/dbal/src/Tools/Console/ConnectionProvider/SingleConnectionProvider.php | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\DBAL\Tools\Console\ConnectionProvider; | ||
| 6 | |||
| 7 | use Doctrine\DBAL\Connection; | ||
| 8 | use Doctrine\DBAL\Tools\Console\ConnectionNotFound; | ||
| 9 | use Doctrine\DBAL\Tools\Console\ConnectionProvider; | ||
| 10 | |||
| 11 | use function sprintf; | ||
| 12 | |||
| 13 | class SingleConnectionProvider implements ConnectionProvider | ||
| 14 | { | ||
| 15 | public function __construct( | ||
| 16 | private readonly Connection $connection, | ||
| 17 | private readonly string $defaultConnectionName = 'default', | ||
| 18 | ) { | ||
| 19 | } | ||
| 20 | |||
| 21 | public function getDefaultConnection(): Connection | ||
| 22 | { | ||
| 23 | return $this->connection; | ||
| 24 | } | ||
| 25 | |||
| 26 | public function getConnection(string $name): Connection | ||
| 27 | { | ||
| 28 | if ($name !== $this->defaultConnectionName) { | ||
| 29 | throw new ConnectionNotFound(sprintf('Connection with name "%s" does not exist.', $name)); | ||
| 30 | } | ||
| 31 | |||
| 32 | return $this->connection; | ||
| 33 | } | ||
| 34 | } | ||
diff --git a/vendor/doctrine/dbal/src/Tools/DsnParser.php b/vendor/doctrine/dbal/src/Tools/DsnParser.php new file mode 100644 index 0000000..61edc6a --- /dev/null +++ b/vendor/doctrine/dbal/src/Tools/DsnParser.php | |||
| @@ -0,0 +1,217 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\DBAL\Tools; | ||
| 6 | |||
| 7 | use Doctrine\DBAL\Driver; | ||
| 8 | use Doctrine\DBAL\DriverManager; | ||
| 9 | use Doctrine\DBAL\Exception\MalformedDsnException; | ||
| 10 | use SensitiveParameter; | ||
| 11 | |||
| 12 | use function array_merge; | ||
| 13 | use function assert; | ||
| 14 | use function is_a; | ||
| 15 | use function is_string; | ||
| 16 | use function parse_str; | ||
| 17 | use function parse_url; | ||
| 18 | use function preg_replace; | ||
| 19 | use function rawurldecode; | ||
| 20 | use function str_replace; | ||
| 21 | use function strpos; | ||
| 22 | use function substr; | ||
| 23 | |||
| 24 | /** @psalm-import-type Params from DriverManager */ | ||
| 25 | final class DsnParser | ||
| 26 | { | ||
| 27 | /** @param array<string, string|class-string<Driver>> $schemeMapping An array used to map DSN schemes to DBAL drivers */ | ||
| 28 | public function __construct( | ||
| 29 | private readonly array $schemeMapping = [], | ||
| 30 | ) { | ||
| 31 | } | ||
| 32 | |||
| 33 | /** | ||
| 34 | * @psalm-return Params | ||
| 35 | * | ||
| 36 | * @throws MalformedDsnException | ||
| 37 | */ | ||
| 38 | public function parse( | ||
| 39 | #[SensitiveParameter] | ||
| 40 | string $dsn, | ||
| 41 | ): array { | ||
| 42 | // (pdo-)?sqlite3?:///... => (pdo-)?sqlite3?://localhost/... or else the URL will be invalid | ||
| 43 | $url = preg_replace('#^((?:pdo-)?sqlite3?):///#', '$1://localhost/', $dsn); | ||
| 44 | assert($url !== null); | ||
| 45 | |||
| 46 | $url = parse_url($url); | ||
| 47 | |||
| 48 | if ($url === false) { | ||
| 49 | throw MalformedDsnException::new(); | ||
| 50 | } | ||
| 51 | |||
| 52 | foreach ($url as $param => $value) { | ||
| 53 | if (! is_string($value)) { | ||
| 54 | continue; | ||
| 55 | } | ||
| 56 | |||
| 57 | $url[$param] = rawurldecode($value); | ||
| 58 | } | ||
| 59 | |||
| 60 | $params = []; | ||
| 61 | |||
| 62 | if (isset($url['scheme'])) { | ||
| 63 | $params['driver'] = $this->parseDatabaseUrlScheme($url['scheme']); | ||
| 64 | } | ||
| 65 | |||
| 66 | if (isset($url['host'])) { | ||
| 67 | $params['host'] = $url['host']; | ||
| 68 | } | ||
| 69 | |||
| 70 | if (isset($url['port'])) { | ||
| 71 | $params['port'] = $url['port']; | ||
| 72 | } | ||
| 73 | |||
| 74 | if (isset($url['user'])) { | ||
| 75 | $params['user'] = $url['user']; | ||
| 76 | } | ||
| 77 | |||
| 78 | if (isset($url['pass'])) { | ||
| 79 | $params['password'] = $url['pass']; | ||
| 80 | } | ||
| 81 | |||
| 82 | if (isset($params['driver']) && is_a($params['driver'], Driver::class, true)) { | ||
| 83 | $params['driverClass'] = $params['driver']; | ||
| 84 | unset($params['driver']); | ||
| 85 | } | ||
| 86 | |||
| 87 | $params = $this->parseDatabaseUrlPath($url, $params); | ||
| 88 | $params = $this->parseDatabaseUrlQuery($url, $params); | ||
| 89 | |||
| 90 | return $params; | ||
| 91 | } | ||
| 92 | |||
| 93 | /** | ||
| 94 | * Parses the given connection URL and resolves the given connection parameters. | ||
| 95 | * | ||
| 96 | * Assumes that the connection URL scheme is already parsed and resolved into the given connection parameters | ||
| 97 | * via {@see parseDatabaseUrlScheme}. | ||
| 98 | * | ||
| 99 | * @see parseDatabaseUrlScheme | ||
| 100 | * | ||
| 101 | * @param mixed[] $url The URL parts to evaluate. | ||
| 102 | * @param mixed[] $params The connection parameters to resolve. | ||
| 103 | * | ||
| 104 | * @return mixed[] The resolved connection parameters. | ||
| 105 | */ | ||
| 106 | private function parseDatabaseUrlPath(array $url, array $params): array | ||
| 107 | { | ||
| 108 | if (! isset($url['path'])) { | ||
| 109 | return $params; | ||
| 110 | } | ||
| 111 | |||
| 112 | $url['path'] = $this->normalizeDatabaseUrlPath($url['path']); | ||
| 113 | |||
| 114 | // If we do not have a known DBAL driver, we do not know any connection URL path semantics to evaluate | ||
| 115 | // and therefore treat the path as a regular DBAL connection URL path. | ||
| 116 | if (! isset($params['driver'])) { | ||
| 117 | return $this->parseRegularDatabaseUrlPath($url, $params); | ||
| 118 | } | ||
| 119 | |||
| 120 | if (strpos($params['driver'], 'sqlite') !== false) { | ||
| 121 | return $this->parseSqliteDatabaseUrlPath($url, $params); | ||
| 122 | } | ||
| 123 | |||
| 124 | return $this->parseRegularDatabaseUrlPath($url, $params); | ||
| 125 | } | ||
| 126 | |||
| 127 | /** | ||
| 128 | * Normalizes the given connection URL path. | ||
| 129 | * | ||
| 130 | * @return string The normalized connection URL path | ||
| 131 | */ | ||
| 132 | private function normalizeDatabaseUrlPath(string $urlPath): string | ||
| 133 | { | ||
| 134 | // Trim leading slash from URL path. | ||
| 135 | return substr($urlPath, 1); | ||
| 136 | } | ||
| 137 | |||
| 138 | /** | ||
| 139 | * Parses the query part of the given connection URL and resolves the given connection parameters. | ||
| 140 | * | ||
| 141 | * @param mixed[] $url The connection URL parts to evaluate. | ||
| 142 | * @param mixed[] $params The connection parameters to resolve. | ||
| 143 | * | ||
| 144 | * @return mixed[] The resolved connection parameters. | ||
| 145 | */ | ||
| 146 | private function parseDatabaseUrlQuery(array $url, array $params): array | ||
| 147 | { | ||
| 148 | if (! isset($url['query'])) { | ||
| 149 | return $params; | ||
| 150 | } | ||
| 151 | |||
| 152 | $query = []; | ||
| 153 | |||
| 154 | parse_str($url['query'], $query); // simply ingest query as extra params, e.g. charset or sslmode | ||
| 155 | |||
| 156 | return array_merge($params, $query); // parse_str wipes existing array elements | ||
| 157 | } | ||
| 158 | |||
| 159 | /** | ||
| 160 | * Parses the given regular connection URL and resolves the given connection parameters. | ||
| 161 | * | ||
| 162 | * Assumes that the "path" URL part is already normalized via {@see normalizeDatabaseUrlPath}. | ||
| 163 | * | ||
| 164 | * @see normalizeDatabaseUrlPath | ||
| 165 | * | ||
| 166 | * @param mixed[] $url The regular connection URL parts to evaluate. | ||
| 167 | * @param mixed[] $params The connection parameters to resolve. | ||
| 168 | * | ||
| 169 | * @return mixed[] The resolved connection parameters. | ||
| 170 | */ | ||
| 171 | private function parseRegularDatabaseUrlPath(array $url, array $params): array | ||
| 172 | { | ||
| 173 | $params['dbname'] = $url['path']; | ||
| 174 | |||
| 175 | return $params; | ||
| 176 | } | ||
| 177 | |||
| 178 | /** | ||
| 179 | * Parses the given SQLite connection URL and resolves the given connection parameters. | ||
| 180 | * | ||
| 181 | * Assumes that the "path" URL part is already normalized via {@see normalizeDatabaseUrlPath}. | ||
| 182 | * | ||
| 183 | * @see normalizeDatabaseUrlPath | ||
| 184 | * | ||
| 185 | * @param mixed[] $url The SQLite connection URL parts to evaluate. | ||
| 186 | * @param mixed[] $params The connection parameters to resolve. | ||
| 187 | * | ||
| 188 | * @return mixed[] The resolved connection parameters. | ||
| 189 | */ | ||
| 190 | private function parseSqliteDatabaseUrlPath(array $url, array $params): array | ||
| 191 | { | ||
| 192 | if ($url['path'] === ':memory:') { | ||
| 193 | $params['memory'] = true; | ||
| 194 | |||
| 195 | return $params; | ||
| 196 | } | ||
| 197 | |||
| 198 | $params['path'] = $url['path']; // pdo_sqlite driver uses 'path' instead of 'dbname' key | ||
| 199 | |||
| 200 | return $params; | ||
| 201 | } | ||
| 202 | |||
| 203 | /** | ||
| 204 | * Parses the scheme part from given connection URL and resolves the given connection parameters. | ||
| 205 | * | ||
| 206 | * @return string The resolved driver. | ||
| 207 | */ | ||
| 208 | private function parseDatabaseUrlScheme(string $scheme): string | ||
| 209 | { | ||
| 210 | // URL schemes must not contain underscores, but dashes are ok | ||
| 211 | $driver = str_replace('-', '_', $scheme); | ||
| 212 | |||
| 213 | // If the driver is an alias (e.g. "postgres"), map it to the actual name ("pdo-pgsql"). | ||
| 214 | // Otherwise, let checkParams decide later if the driver exists. | ||
| 215 | return $this->schemeMapping[$driver] ?? $driver; | ||
| 216 | } | ||
| 217 | } | ||
