Skip to content
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

Accept DBAL driver connection in Connection::connect #939

Merged
merged 11 commits into from
Nov 29, 2021
2 changes: 1 addition & 1 deletion .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ jobs:

- name: "Run tests: MSSQL"
env:
DB_DSN: "sqlsrv:Server=mssql;Database=master"
DB_DSN: "sqlsrv:host=mssql;dbname=master"
DB_USER: sa
DB_PASSWORD: atk4_pass
run: |
Expand Down
4 changes: 2 additions & 2 deletions docs/persistence/sql/connection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ connection in a global variable or global class::
Determine which Connection class should be used for specified $dsn,
establish connection to DB by creating new object of this connection class and return.

:param string $dsn: DSN, see http://php.net/manual/en/ref.pdo-mysql.connection.php
:param string $dsn: DSN, see https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html
:param string $user: username
:param string $password: password
:param array $args: Other default properties for connection class.
Expand Down Expand Up @@ -88,7 +88,7 @@ if you connect to vendor that does not use PDO.

Developers can register custom classes to handle driver types using the `Connecion::registerConnectionClass` method::

Connection::registerConnectionClass(Custom\MySQL\Connection::class, 'mysql');
Connection::registerConnectionClass(Custom\MySQL\Connection::class, 'pdo_mysql');

.. php:method:: connectDbalConnection(array $dsn)

Expand Down
4 changes: 2 additions & 2 deletions docs/persistence/sql/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ automatically do some of the hard work to adopt query building for your
database vendor.
There are more ways to create connection, see `Advanced Connections`_ section.

The format of the ``$dsn`` is the same as with
`PDO class <http://php.net/manual/en/ref.pdo-mysql.connection.php>`_.
The format of the ``$dsn`` is the same as with for
`DBAL connection <https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html>`_.
If you need to execute query that is not supported by DSQL, you should always
use expressions::

Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ parameters:
message: '~^(Call to an undefined method Doctrine\\DBAL\\Driver\\Connection::getWrappedConnection\(\)\.|Call to an undefined method Doctrine\\DBAL\\Connection::createSchemaManager\(\)\.|Call to an undefined static method Doctrine\\DBAL\\Exception::invalidPdoInstance\(\)\.|Call to method getCreateTableSQL\(\) of deprecated class Doctrine\\DBAL\\Platforms\\AbstractPlatform:\n.+|Anonymous class extends deprecated class Doctrine\\DBAL\\Platforms\\PostgreSQL94Platform:\n.+|Call to deprecated method fetch(|All)\(\) of class Doctrine\\DBAL\\Result:\n.+|Call to deprecated method getSchemaManager\(\) of class Doctrine\\DBAL\\Connection:\n.+|Access to an undefined property Doctrine\\DBAL\\Driver\\PDO\\Connection::\$connection\.|Parameter #1 \$dsn of class Doctrine\\DBAL\\Driver\\PDO\\SQLSrv\\Connection constructor expects string, Doctrine\\DBAL\\Driver\\PDO\\Connection given\.|Method Atk4\\Data\\Persistence\\Sql\\Expression::execute\(\) should return Doctrine\\DBAL\\Result\|PDOStatement but returns bool\.|PHPDoc tag @return contains generic type Doctrine\\DBAL\\Schema\\AbstractSchemaManager<Doctrine\\DBAL\\Platforms\\AbstractPlatform> but class Doctrine\\DBAL\\Schema\\AbstractSchemaManager is not generic\.|Class Doctrine\\DBAL\\Platforms\\(MySqlPlatform|PostgreSqlPlatform) referenced with incorrect case: Doctrine\\DBAL\\Platforms\\(MySQLPlatform|PostgreSQLPlatform)\.)$~'
path: '*'
# count for DBAL 3.x matched in "src/Persistence/GenericPlatform.php" file
count: 42
count: 39

# TODO these rules are generated, this ignores should be fixed in the code
# for src/Schema/TestCase.php
Expand Down
20 changes: 10 additions & 10 deletions src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ abstract class Persistence
public static function connect($dsn, string $user = null, string $password = null, array $args = []): self
{
// parse DSN string
$dsn = \Atk4\Data\Persistence\Sql\Connection::normalizeDsn($dsn, $user, $password);
$dsn = Persistence\Sql\Connection::normalizeDsn($dsn, $user, $password);

switch ($dsn['driverSchema']) {
case 'sqlite':
case 'mysql':
case 'pgsql':
case 'sqlsrv':
case 'oci':
$persistence = new Persistence\Sql($dsn['dsn'], $dsn['user'], $dsn['pass'], $args);
switch ($dsn['driver']) {
case 'pdo_sqlite':
case 'pdo_mysql':
case 'pdo_pgsql':
case 'pdo_sqlsrv':
case 'pdo_oci':
$persistence = new Persistence\Sql($dsn, $dsn['user'], $dsn['password'], $args);

return $persistence;
default:
throw (new Exception('Unable to determine persistence driver type from DSN'))
->addMoreInfo('dsn', $dsn['dsn']);
throw (new Exception('Unable to determine persistence driver type'))
->addMoreInfo('dsn', $dsn);
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/Persistence/GenericPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ private function createNotSupportedException(): \Exception
$connection->getSchemaManager();
$connection->getSchemaManager();
$connection->getSchemaManager();
$connection->getSchemaManager();
$connection->getSchemaManager();
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Atk4\Data\Persistence\Sql\Expression;
use Atk4\Data\Persistence\Sql\Expressionable;
use Atk4\Data\Persistence\Sql\Query;
use Doctrine\DBAL\Connection as DbalConnection;
use Doctrine\DBAL\Driver\Connection as DbalDriverConnection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
Expand Down Expand Up @@ -81,20 +83,20 @@ class Sql extends Persistence
/**
* Constructor.
*
* @param Connection|string $connection
* @param string $user
* @param string $password
* @param array $args
* @param Connection|string|array|DbalConnection|DbalDriverConnection $connection
* @param string $user
* @param string $password
* @param array $args
*/
public function __construct($connection, $user = null, $password = null, $args = [])
{
if (is_object($connection)) {
$this->connection = Connection::assertInstanceOf($connection);
if ($connection instanceof Connection) {
$this->connection = $connection;

return;
}

// attempt to connect.
// attempt to connect
$this->connection = Connection::connect(
$connection,
$user,
Expand Down
Loading