Skip to content

Commit ccece35

Browse files
committed
some others methods moved to PdoDriver
1 parent 38b88d5 commit ccece35

File tree

4 files changed

+108
-37
lines changed

4 files changed

+108
-37
lines changed

src/Database/Connection.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
use JetBrains\PhpStorm\Language;
1313
use Nette\Utils\Arrays;
14-
use PDO;
15-
use PDOException;
1614

1715

1816
/**
@@ -91,7 +89,7 @@ public function getDsn(): string
9189

9290

9391
/** deprecated use getDriver()->getPdo() */
94-
public function getPdo(): PDO
92+
public function getPdo(): \PDO
9593
{
9694
$this->connect();
9795
return $this->driver->getPdo();
@@ -129,22 +127,15 @@ public function setRowNormalizer(?callable $normalizer): static
129127

130128
public function getInsertId(?string $sequence = null): string
131129
{
132-
try {
133-
$res = $this->getPdo()->lastInsertId($sequence);
134-
return $res === false ? '0' : $res;
135-
} catch (PDOException $e) {
136-
throw $this->driver->convertException($e);
137-
}
130+
$this->connect();
131+
return $this->driver->getInsertId($sequence);
138132
}
139133

140134

141-
public function quote(string $string, int $type = PDO::PARAM_STR): string
135+
public function quote(string $string): string
142136
{
143-
try {
144-
return $this->getPdo()->quote($string, $type);
145-
} catch (PDOException $e) {
146-
throw DriverException::from($e);
147-
}
137+
$this->connect();
138+
return $this->driver->quote($string);
148139
}
149140

150141

@@ -214,7 +205,7 @@ public function query(#[Language('SQL')] string $sql, #[Language('GenericSQL')]
214205
[$this->sql, $params] = $this->preprocess($sql, ...$params);
215206
try {
216207
$result = new ResultSet($this, $this->sql, $params, $this->rowNormalizer);
217-
} catch (PDOException $e) {
208+
} catch (\PDOException $e) {
218209
Arrays::invoke($this->onQuery, $this, $e);
219210
throw $e;
220211
}

src/Database/Driver.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,26 @@ function connect(string $dsn, ?string $user = null, ?string $password = null, ?a
3434
*/
3535
function convertException(\PDOException $e): DriverException;
3636

37+
function query(string $queryString, array $params);
38+
39+
function beginTransaction(): void;
40+
41+
function commit(): void;
42+
43+
function rollBack(): void;
44+
45+
/**
46+
* Returns the ID of the last inserted row or sequence value.
47+
*/
48+
function getInsertId(?string $sequence = null): string;
49+
50+
/**
51+
* Delimits string for use in SQL statement.
52+
*/
53+
function quote(string $string): string;
54+
3755
/**
38-
* Delimites identifier for use in a SQL statement.
56+
* Delimits identifier for use in SQL statement.
3957
*/
4058
function delimite(string $name): string;
4159

src/Database/Drivers/PdoDriver.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Database\Drivers;
1111

1212
use Nette;
13+
use Nette\Database\DriverException;
1314
use PDO;
1415
use PDOException;
1516

@@ -43,4 +44,79 @@ public function getPdo(): ?PDO
4344
{
4445
return $this->pdo;
4546
}
47+
48+
49+
public function query(string $queryString, array $params)
50+
{
51+
try {
52+
$types = ['boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT, 'resource' => PDO::PARAM_LOB, 'NULL' => PDO::PARAM_NULL];
53+
54+
$statement = $this->pdo->prepare($queryString);
55+
foreach ($params as $key => $value) {
56+
$type = gettype($value);
57+
$statement->bindValue(is_int($key) ? $key + 1 : $key, $value, $types[$type] ?? PDO::PARAM_STR);
58+
}
59+
60+
$statement->setFetchMode(PDO::FETCH_ASSOC);
61+
@$statement->execute(); // @ PHP generates warning when ATTR_ERRMODE = ERRMODE_EXCEPTION bug #73878
62+
return $statement;
63+
64+
} catch (PDOException $e) {
65+
$e = $this->convertException($e);
66+
$e->queryString = $queryString;
67+
$e->params = $params;
68+
throw $e;
69+
}
70+
}
71+
72+
73+
public function beginTransaction(): void
74+
{
75+
try {
76+
$this->pdo->beginTransaction();
77+
} catch (PDOException $e) {
78+
throw $this->convertException($e);
79+
}
80+
}
81+
82+
83+
public function commit(): void
84+
{
85+
try {
86+
$this->pdo->commit();
87+
} catch (PDOException $e) {
88+
throw $this->convertException($e);
89+
}
90+
}
91+
92+
93+
public function rollBack(): void
94+
{
95+
try {
96+
$this->pdo->rollBack();
97+
} catch (PDOException $e) {
98+
throw $this->convertException($e);
99+
}
100+
}
101+
102+
103+
public function getInsertId(?string $sequence = null): string
104+
{
105+
try {
106+
$res = $this->pdo->lastInsertId($sequence);
107+
return $res === false ? '0' : $res;
108+
} catch (PDOException $e) {
109+
throw $this->convertException($e);
110+
}
111+
}
112+
113+
114+
public function quote(string $string, int $type = PDO::PARAM_STR): string
115+
{
116+
try {
117+
return $this->pdo->quote($string, $type);
118+
} catch (PDOException $e) {
119+
throw DriverException::from($e);
120+
}
121+
}
46122
}

src/Database/ResultSet.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,12 @@ public function __construct(
3939
) {
4040
$time = microtime(true);
4141
$this->normalizer = $normalizer;
42-
$types = ['boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT, 'resource' => PDO::PARAM_LOB, 'NULL' => PDO::PARAM_NULL];
43-
44-
try {
45-
if (str_starts_with($queryString, '::')) {
46-
$connection->getPdo()->{substr($queryString, 2)}();
47-
} else {
48-
$this->pdoStatement = $connection->getPdo()->prepare($queryString);
49-
foreach ($params as $key => $value) {
50-
$type = gettype($value);
51-
$this->pdoStatement->bindValue(is_int($key) ? $key + 1 : $key, $value, $types[$type] ?? PDO::PARAM_STR);
52-
}
53-
54-
$this->pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
55-
@$this->pdoStatement->execute(); // @ PHP generates warning when ATTR_ERRMODE = ERRMODE_EXCEPTION bug #73878
56-
}
57-
} catch (\PDOException $e) {
58-
$e = $connection->getDriver()->convertException($e);
59-
$e->queryString = $queryString;
60-
$e->params = $params;
61-
throw $e;
42+
43+
$driver = $connection->getDriver();
44+
if (str_starts_with($queryString, '::')) {
45+
$driver->{substr($queryString, 2)}();
46+
} else {
47+
$this->pdoStatement = $driver->query($queryString, $params);
6248
}
6349

6450
$this->time = microtime(true) - $time;

0 commit comments

Comments
 (0)