Skip to content

Commit c7fd4d8

Browse files
committed
some others methods moved to PdoDriver
1 parent a29ba35 commit c7fd4d8

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
/**
@@ -90,7 +88,7 @@ public function getDsn(): string
9088

9189

9290
/** deprecated use getDriver()->getPdo() */
93-
public function getPdo(): PDO
91+
public function getPdo(): \PDO
9492
{
9593
$this->connect();
9694
return $this->driver->getPdo();
@@ -122,22 +120,15 @@ public function setRowNormalizer(?callable $normalizer): self
122120

123121
public function getInsertId(string $sequence = null): string
124122
{
125-
try {
126-
$res = $this->getPdo()->lastInsertId($sequence);
127-
return $res === false ? '0' : $res;
128-
} catch (PDOException $e) {
129-
throw $this->driver->convertException($e);
130-
}
123+
$this->connect();
124+
return $this->driver->getInsertId($sequence);
131125
}
132126

133127

134-
public function quote(string $string, int $type = PDO::PARAM_STR): string
128+
public function quote(string $string): string
135129
{
136-
try {
137-
return $this->getPdo()->quote($string, $type);
138-
} catch (PDOException $e) {
139-
throw DriverException::from($e);
140-
}
130+
$this->connect();
131+
return $this->driver->quote($string);
141132
}
142133

143134

@@ -206,7 +197,7 @@ public function query(string $sql, ...$params): ResultSet
206197
[$this->sql, $params] = $this->preprocess($sql, ...$params);
207198
try {
208199
$result = new ResultSet($this, $this->sql, $params, $this->rowNormalizer);
209-
} catch (PDOException $e) {
200+
} catch (\PDOException $e) {
210201
Arrays::invoke($this->onQuery, $this, $e);
211202
throw $e;
212203
}

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
@@ -46,25 +46,11 @@ public function __construct(Connection $connection, string $queryString, array $
4646
$this->params = $params;
4747
$this->normalizer = $normalizer;
4848

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

0 commit comments

Comments
 (0)