-
Notifications
You must be signed in to change notification settings - Fork 10
Support for Doctrine DBAL 3 #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4a03d6f
cf2ca79
5bfc306
9cf0a19
832226c
dbc307d
8d8e25f
40283de
1038d06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| .project | ||
| .vagrant | ||
| .phpunit.result.cache | ||
| *.cache | ||
| *-console.log | ||
| .crate-docs | ||
| /composer.lock | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
| // PHP Coding Standards Fixer | ||
| // https://cs.symfony.com/ | ||
| // https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/doc/config.rst | ||
|
|
||
| $finder = (new PhpCsFixer\Finder()) | ||
| ->in(__DIR__) | ||
| ; | ||
|
|
||
| return (new PhpCsFixer\Config()) | ||
| ->setUnsupportedPhpVersionAllowed(true) | ||
| ->setRules([ | ||
| // '@auto' => true, | ||
| // '@PHP7x3Migration' => true, | ||
| // '@PSR1' => true, | ||
| // '@PSR2' => true, | ||
| // '@PSR12' => true, | ||
| // '@Symfony' => true, | ||
| // 'array_syntax' => ['syntax' => 'short'], | ||
| 'ordered_imports' => true, | ||
| // 'strict_param' => true, | ||
| ]) | ||
| ->setFinder($finder) | ||
| ; | ||
|
|
||
| ?> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * Basic example program about handling CrateDB OBJECTs with Doctrine DBAL. | ||
| * https://github.com/crate/crate-dbal | ||
| */ | ||
| require __DIR__ . '/../vendor/autoload.php'; | ||
|
|
||
| use Crate\DBAL\Platforms\CratePlatform4; | ||
| use Crate\DBAL\Types\MapType; | ||
| use Doctrine\DBAL\DriverManager; | ||
| use Doctrine\DBAL\Exception\TableNotFoundException; | ||
| use Doctrine\DBAL\Schema\Column; | ||
| use Doctrine\DBAL\Schema\Table; | ||
|
|
||
| use Doctrine\DBAL\Tools\DsnParser; | ||
| use Doctrine\DBAL\Types\Type; | ||
|
|
||
| // Initialize machinery. | ||
| // This ensures that the 'map' type is registered in the type system from the beginning. | ||
| $platform = new CratePlatform4(); | ||
|
|
||
| // Define table schema. | ||
| $table = new Table('example'); | ||
| $objDefinition = array( | ||
| 'type' => MapType::STRICT, | ||
| 'fields' => array( | ||
| new Column('id', Type::getType('integer'), array()), | ||
| new Column('name', Type::getType('string'), array()), | ||
| ), | ||
| ); | ||
| $table->addColumn( | ||
| 'data', | ||
| MapType::NAME, | ||
| array('platformOptions' => $objDefinition), | ||
| ); | ||
|
|
||
| // Register driver. | ||
| $dsnParser = new DsnParser(array('crate' => 'Crate\DBAL\Driver\PDOCrate\Driver')); | ||
|
|
||
| // Connect to database. | ||
| $connectionParams = $dsnParser->parse('crate://crate:crate@localhost:4200/'); | ||
| $connection = DriverManager::getConnection($connectionParams); | ||
| $schemaManager = $connection->createSchemaManager(); | ||
|
|
||
| // Provision database table. | ||
| try { | ||
| $schemaManager->dropTable($table->getName()); | ||
| } catch (TableNotFoundException) { | ||
| } | ||
| $schemaManager->createTable($table); | ||
|
|
||
| // Insert data. | ||
| $connection->insert('example', array('data' => array('id' => 42, 'name' => 'foo')), array('data' => 'map')); | ||
| $connection->insert('example', array('data' => array('id' => 43, 'name' => 'bar')), array('data' => 'map')); | ||
| $connection->executeStatement('REFRESH TABLE example'); | ||
|
|
||
| // Query data. | ||
| $result = $connection->executeQuery('SELECT * FROM example'); | ||
| print_r($result->fetchAllAssociative()); | ||
|
|
||
| ?> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Licensed to CRATE Technology GmbH("Crate") under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with this work for | ||
|
|
@@ -22,14 +23,127 @@ | |
|
|
||
| namespace Crate\DBAL\Driver\PDOCrate; | ||
|
|
||
| use Crate\PDO\PDOInterface; | ||
| use Crate\PDO\PDOStatement; | ||
| use Doctrine\DBAL\Driver\PDO\Exception; | ||
| use Doctrine\DBAL\Driver\PDOStatementImplementations; | ||
| use Doctrine\DBAL\Driver\Result as ResultInterface; | ||
| use Doctrine\DBAL\Driver\Statement as StatementInterface; | ||
| use Crate\PDO\PDOStatement; | ||
| use Doctrine\DBAL\ParameterType; | ||
| use Doctrine\Deprecations\Deprecation; | ||
| use PDO; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| class CrateStatement extends PDOStatement implements StatementInterface | ||
| final class CrateStatement implements StatementInterface | ||
| { | ||
| use PDOStatementImplementations; | ||
| private PDOInterface $pdo; | ||
| private PDOStatement $stmt; | ||
|
|
||
| /** | ||
| * @param string $sql | ||
| * @param array<string,mixed> $options | ||
| */ | ||
| public function __construct(PDOInterface $pdo, $sql, $options = []) | ||
| { | ||
| $this->pdo = $pdo; | ||
| $this->stmt = $pdo->prepare($sql, $options); | ||
| } | ||
|
Comment on lines
+41
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainConstructor now correctly forwards options to the underlying PDO connection Using composition with If you want an extra sanity check, you can grep for all 🏁 Script executed: #!/bin/bash
rg -n "new\s+CrateStatement\(" -C2Length of output: 443 Length of output: 0 🏁 Script executed: #!/bin/bash
# Also check with rg to see the actual execute() implementation
rg -n "public\s+function\s+execute" src/Crate/DBAL/Driver/PDOCrate/CrateStatement.php -A10Length of output: 461 Length of output: 859 Constructor correctly forwards options, but execute() must catch and wrap PDOException Constructor alignment with public function execute($params = null): ResultInterface
{
if ($params !== null) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5556',
'Passing $params to Statement::execute() is deprecated. Bind parameters using'
. ' Statement::bindParam() or Statement::bindValue() instead.',
);
}
try {
$this->stmt->execute($params);
} catch (PDOException $exception) {
throw Exception::new($exception);
}
return new Result($this);
}This ensures PDOException is converted to the driver-level 🤖 Prompt for AI Agents |
||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function execute($params = null): ResultInterface | ||
| { | ||
|
|
||
| if ($params !== null) { | ||
| Deprecation::trigger( | ||
| 'doctrine/dbal', | ||
| 'https://github.com/doctrine/dbal/pull/5556', | ||
| 'Passing $params to Statement::execute() is deprecated. Bind parameters using' | ||
| . ' Statement::bindParam() or Statement::bindValue() instead.', | ||
| ); | ||
| } | ||
| $this->stmt->execute($params); | ||
| return new Result($this); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function columnCount(): int | ||
| { | ||
| return $this->stmt->columnCount(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function rowCount(): int | ||
| { | ||
| return $this->stmt->rowCount(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function bindValue($param, $value, $type = ParameterType::STRING) | ||
| { | ||
| return $this->stmt->bindValue($param, $value, $type); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null) | ||
| { | ||
| return $this->stmt->bindParam($param, $variable, $type, $length); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function fetch( | ||
| $fetch_style = PDO::FETCH_ASSOC, | ||
| $cursor_orientation = PDO::FETCH_ORI_NEXT, | ||
| $cursor_offset = 0, | ||
| ) { | ||
| return $this->stmt->fetch($fetch_style, $cursor_orientation, $cursor_offset); | ||
| } | ||
|
|
||
| /** | ||
| * @phpstan-param PDO::FETCH_* $mode | ||
| * | ||
| * @return list<mixed> | ||
| * | ||
| * @throws Exception | ||
| */ | ||
| public function fetchAll(int $mode): array | ||
| { | ||
| return $this->stmt->fetchAll($mode); | ||
| } | ||
|
|
||
| public function fetchColumn($column_number = 0) | ||
| { | ||
| return $this->stmt->fetchColumn($column_number); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function closeCursor(): bool | ||
| { | ||
| return $this->stmt->closeCursor(); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the wrapped CrateDB PDOStatement. | ||
| * | ||
| * @return PDOStatement | ||
| */ | ||
| public function getWrappedStatement(): PDOStatement | ||
| { | ||
| return $this->stmt; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The versioning of Doctrine ORM is different, so let's be closer to the truth here, by discriminating between Doctrine ORM and Doctrine DBAL.