Skip to content

Commit a89fdfe

Browse files
committed
Add static connect method to PDO subclass polyfills
1 parent 15a7a4b commit a89fdfe

File tree

7 files changed

+137
-6
lines changed

7 files changed

+137
-6
lines changed

src/Php84/Resources/stubs/Pdo/Dblib.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,16 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
3333
throw new PDOException("Pdo\Dblib::__construct() cannot be used for connecting to the \"$driver\" driver");
3434
}
3535
}
36+
37+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): self
38+
{
39+
try {
40+
return new self($dsn, $username, $password, $options);
41+
} catch (PDOException $e) {
42+
throw $e->getMessage() === 'Pdo\Dblib::__construct() cannot be used for connecting to the "sqlite" driver'
43+
? new PDOException("Pdo\Dblib::connect() cannot be used for connecting to the \"sqlite\" driver")
44+
: $e;
45+
}
46+
}
3647
}
3748
}

src/Php84/Resources/stubs/Pdo/Firebird.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,16 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
2929
throw new PDOException("Pdo\Firebird::__construct() cannot be used for connecting to the \"$driver\" driver");
3030
}
3131
}
32+
33+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): self
34+
{
35+
try {
36+
return new self($dsn, $username, $password, $options);
37+
} catch (PDOException $e) {
38+
throw $e->getMessage() === 'Pdo\Firebird::__construct() cannot be used for connecting to the "sqlite" driver'
39+
? new PDOException("Pdo\Firebird::connect() cannot be used for connecting to the \"sqlite\" driver")
40+
: $e;
41+
}
42+
}
3243
}
3344
}

src/Php84/Resources/stubs/Pdo/Mysql.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
4848
throw new PDOException("Pdo\Mysql::__construct() cannot be used for connecting to the \"$driver\" driver");
4949
}
5050
}
51+
52+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): self
53+
{
54+
try {
55+
return new self($dsn, $username, $password, $options);
56+
} catch (PDOException $e) {
57+
throw $e->getMessage() === 'Pdo\Mysql::__construct() cannot be used for connecting to the "sqlite" driver'
58+
? new PDOException("Pdo\Mysql::connect() cannot be used for connecting to the \"sqlite\" driver")
59+
: $e;
60+
}
61+
}
5162
}
5263
} else {
5364
class Mysql extends PDO
@@ -81,6 +92,17 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
8192
throw new PDOException("Pdo\Mysql::__construct() cannot be used for connecting to the \"$driver\" driver");
8293
}
8394
}
95+
96+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): self
97+
{
98+
try {
99+
return new self($dsn, $username, $password, $options);
100+
} catch (PDOException $e) {
101+
throw $e->getMessage() === 'Pdo\Mysql::__construct() cannot be used for connecting to the "sqlite" driver'
102+
? new PDOException("Pdo\Mysql::connect() cannot be used for connecting to the \"sqlite\" driver")
103+
: $e;
104+
}
105+
}
84106
}
85107
}
86108
}

src/Php84/Resources/stubs/Pdo/Odbc.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,16 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
3131
throw new PDOException("Pdo\Odbc::__construct() cannot be used for connecting to the \"$driver\" driver");
3232
}
3333
}
34+
35+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): self
36+
{
37+
try {
38+
return new self($dsn, $username, $password, $options);
39+
} catch (PDOException $e) {
40+
throw $e->getMessage() === 'Pdo\Odbc::__construct() cannot be used for connecting to the "sqlite" driver'
41+
? new PDOException("Pdo\Odbc::connect() cannot be used for connecting to the \"sqlite\" driver")
42+
: $e;
43+
}
44+
}
3445
}
3546
}

src/Php84/Resources/stubs/Pdo/Pgsql.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
2828
}
2929
}
3030

31+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): self
32+
{
33+
try {
34+
return new self($dsn, $username, $password, $options);
35+
} catch (PDOException $e) {
36+
throw $e->getMessage() === 'Pdo\Pgsql::__construct() cannot be used for connecting to the "sqlite" driver'
37+
? new PDOException("Pdo\Pgsql::connect() cannot be used for connecting to the \"sqlite\" driver")
38+
: $e;
39+
}
40+
}
41+
3142
public function copyFromArray(string $tableName, array $rows, string $separator = "\t", string $nullAs = "\\\\N", ?string $fields = null): bool
3243
{
3344
return $this->pgsqlCopyFromArray($tableName, $rows, $separator, $nullAs, $fields);

src/Php84/Resources/stubs/Pdo/Sqlite.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ class Sqlite extends PDO
2424
public const OPEN_READWRITE = \PHP_VERSION_ID >= 70300 ? \PDO::SQLITE_OPEN_READWRITE : 2;
2525
public const OPEN_CREATE = \PHP_VERSION_ID >= 70300 ? \PDO::SQLITE_OPEN_CREATE : 4;
2626

27-
public function __construct(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null)
27+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): self
2828
{
29-
parent::__construct($dsn, $username, $password, $options);
30-
31-
if (($driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME)) !== 'sqlite')
32-
{
33-
throw new PDOException("Pdo\Sqlite::__construct() cannot be used for connecting to the \"$driver\" driver");
29+
try {
30+
return new self($dsn, $username, $password, $options);
31+
} catch (PDOException $e) {
32+
throw $e->getMessage() === 'Pdo\Sqlite::__construct() cannot be used for connecting to the "sqlite" driver'
33+
? new PDOException("Pdo\Sqlite::connect() cannot be used for connecting to the \"sqlite\" driver")
34+
: $e;
3435
}
3536
}
3637

tests/Php84/PdoTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,70 @@ public function testSqliteConstructor()
8080
$this->assertInstanceOf(\Pdo\Sqlite::class, $sqlite);
8181
}
8282

83+
/**
84+
* @requires extension pdo_dblib
85+
* @requires extension pdo_sqlite
86+
*/
87+
public function testDblibConnect()
88+
{
89+
$this->expectException(PDOException::class);
90+
$this->expectExceptionMessage("Pdo\Dblib::connect() cannot be used for connecting to the \"sqlite\" driver");
91+
\Pdo\Dblib::connect("sqlite:");
92+
}
93+
94+
/**
95+
* @requires extension pdo_firebird
96+
* @requires extension pdo_sqlite
97+
*/
98+
public function testFirebirdConnect()
99+
{
100+
$this->expectException(PDOException::class);
101+
$this->expectExceptionMessage("Pdo\Firebird::connect() cannot be used for connecting to the \"sqlite\" driver");
102+
\Pdo\Firebird::connect("sqlite:");
103+
}
104+
105+
/**
106+
* @requires extension pdo_mysql
107+
* @requires extension pdo_sqlite
108+
*/
109+
public function testMysqlConnect()
110+
{
111+
$this->expectException(PDOException::class);
112+
$this->expectExceptionMessage("Pdo\Mysql::connect() cannot be used for connecting to the \"sqlite\" driver");
113+
\Pdo\Mysql::connect("sqlite:");
114+
}
115+
116+
/**
117+
* @requires extension pdo_odbc
118+
* @requires extension pdo_sqlite
119+
*/
120+
public function testOdbcConnect()
121+
{
122+
$this->expectException(PDOException::class);
123+
$this->expectExceptionMessage("Pdo\Odbc::connect() cannot be used for connecting to the \"sqlite\" driver");
124+
\Pdo\Odbc::connect("sqlite:");
125+
}
126+
127+
/**
128+
* @requires extension pdo_pgsql
129+
* @requires extension pdo_sqlite
130+
*/
131+
public function testPgsqlConnect()
132+
{
133+
$this->expectException(PDOException::class);
134+
$this->expectExceptionMessage("Pdo\Pgsql::connect() cannot be used for connecting to the \"sqlite\" driver");
135+
\Pdo\Pgsql::connect("sqlite:");
136+
}
137+
138+
/**
139+
* @requires extension pdo_sqlite
140+
*/
141+
public function testSqliteConnect()
142+
{
143+
$sqlite = \Pdo\Sqlite::connect("sqlite:");
144+
$this->assertInstanceOf(\Pdo\Sqlite::class, $sqlite);
145+
}
146+
83147
/**
84148
* @requires extension pdo_dblib
85149
*/

0 commit comments

Comments
 (0)