Skip to content

Commit 2aa30e7

Browse files
committed
others methods moved to PDODriver
1 parent 4900753 commit 2aa30e7

4 files changed

Lines changed: 108 additions & 39 deletions

File tree

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 Nette;
1313
use Nette\Utils\Arrays;
14-
use PDO;
15-
use PDOException;
1614

1715

1816
/**
@@ -95,7 +93,7 @@ public function getDsn(): string
9593

9694

9795
/** deprecated use getDriver()->getPDO() */
98-
public function getPdo(): PDO
96+
public function getPdo(): \PDO
9997
{
10098
$this->connect();
10199
return $this->driver->getPDO();
@@ -126,22 +124,15 @@ public function setRowNormalizer(?callable $normalizer): self
126124

127125
public function getInsertId(string $sequence = null): string
128126
{
129-
try {
130-
$res = $this->getPdo()->lastInsertId($sequence);
131-
return $res === false ? '0' : $res;
132-
} catch (PDOException $e) {
133-
throw $this->driver->convertException($e);
134-
}
127+
$this->connect();
128+
return $this->driver->getInsertId($sequence);
135129
}
136130

137131

138-
public function quote(string $string, int $type = PDO::PARAM_STR): string
132+
public function quote(string $string): string
139133
{
140-
try {
141-
return $this->getPdo()->quote($string, $type);
142-
} catch (PDOException $e) {
143-
throw DriverException::from($e);
144-
}
134+
$this->connect();
135+
return $this->driver->quote($string);
145136
}
146137

147138

@@ -209,7 +200,7 @@ public function query(string $sql, ...$params): ResultSet
209200
[$this->sql, $params] = $this->preprocess($sql, ...$params);
210201
try {
211202
$result = new ResultSet($this, $this->sql, $params, $this->rowNormalizer);
212-
} catch (PDOException $e) {
203+
} catch (\PDOException $e) {
213204
Arrays::invoke($this->onQuery, $this, $e);
214205
throw $e;
215206
}

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, arra
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
@@ -11,6 +11,7 @@
1111

1212
use Nette;
1313
use Nette\Database\Connection;
14+
use Nette\Database\DriverException;
1415
use PDO;
1516
use PDOException;
1617

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

src/Database/ResultSet.php

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,17 @@ public function __construct(Connection $connection, string $queryString, array $
5151
$this->params = $params;
5252
$this->normalizer = $normalizer;
5353

54-
try {
55-
if (str_starts_with($queryString, '::')) {
56-
$connection->getPdo()->{substr($queryString, 2)}();
57-
} elseif ($queryString !== null) {
58-
static $types = ['boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT,
59-
'resource' => PDO::PARAM_LOB, 'NULL' => PDO::PARAM_NULL, ];
60-
$this->pdoStatement = $connection->getPdo()->prepare($queryString);
61-
foreach ($params as $key => $value) {
62-
$type = gettype($value);
63-
$this->pdoStatement->bindValue(is_int($key) ? $key + 1 : $key, $value, $types[$type] ?? PDO::PARAM_STR);
64-
}
65-
$this->pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
66-
@$this->pdoStatement->execute(); // @ PHP generates warning when ATTR_ERRMODE = ERRMODE_EXCEPTION bug #73878
67-
}
68-
} catch (\PDOException $e) {
69-
$e = $connection->getDriver()->convertException($e);
70-
$e->queryString = $queryString;
71-
$e->params = $params;
72-
throw $e;
54+
$driver = $connection->getDriver();
55+
if (str_starts_with($queryString, '::')) {
56+
$driver->{substr($queryString, 2)}();
57+
} elseif ($queryString !== null) {
58+
$this->pdoStatement = $driver->query($queryString, $params);
7359
}
7460
$this->time = microtime(true) - $time;
7561
}
7662

7763

78-
/**
79-
* @internal
80-
*/
64+
/** @deprecated */
8165
public function getPdoStatement(): ?\PDOStatement
8266
{
8367
return $this->pdoStatement;

0 commit comments

Comments
 (0)