Skip to content

Commit e4138a6

Browse files
committed
some others methods moved to PdoDriver
1 parent be20a7c commit e4138a6

4 files changed

Lines changed: 107 additions & 36 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();
@@ -127,22 +125,15 @@ public function setRowNormalizer(?callable $normalizer): self
127125

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

138132

139-
public function quote(string $string, int $type = PDO::PARAM_STR): string
133+
public function quote(string $string): string
140134
{
141-
try {
142-
return $this->getPdo()->quote($string, $type);
143-
} catch (PDOException $e) {
144-
throw DriverException::from($e);
145-
}
135+
$this->connect();
136+
return $this->driver->quote($string);
146137
}
147138

148139

@@ -211,7 +202,7 @@ public function query(string $sql, ...$params): ResultSet
211202
[$this->sql, $params] = $this->preprocess($sql, ...$params);
212203
try {
213204
$result = new ResultSet($this, $this->sql, $params, $this->rowNormalizer);
214-
} catch (PDOException $e) {
205+
} catch (\PDOException $e) {
215206
Arrays::invoke($this->onQuery, $this, $e);
216207
throw $e;
217208
}

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
@@ -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

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

src/Database/ResultSet.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,11 @@ 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
}

0 commit comments

Comments
 (0)