Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
367cc4e
Selection: support for generics
dg May 10, 2024
0608c21
PascalCase constants
dg May 10, 2024
90bb859
Reflection::getTable() can access unlisted table
dg May 13, 2024
a96116c
renamed IStructure::FIELD_* to Type::*
dg May 10, 2024
ebbb7cd
Driver::getColumns() added NDB type
dg May 13, 2024
da1b2c8
Column::$nativeType changed to NDB $type
dg May 10, 2024
d43272b
Helpers::detectType() supports 'INT UNSIGNED'
dg May 10, 2024
9e87eda
Reflection: removed $schema
dg May 13, 2024
e11f612
opened 4.0-dev
dg Mar 1, 2021
c4e5746
removed old Driver::SUPPORT constants (BC break)
dg May 10, 2024
57e5fa0
deprecated methods trigger notices
dg Jan 19, 2022
ccd13ce
returns always date-time as immutable Nette\Database\DateTime (BC break)
dg May 10, 2024
b23a3b3
Helpers::normalizeRow() & detection moved to new class RowNormalizer
dg May 13, 2024
3e7168b
RowNormalizer: refactoring
dg Dec 14, 2023
229fec5
RowNormalizer: added configuring methods [Closes #257]
dg Jan 19, 2022
98d167f
introduced PdoDriver, descendant of all PDO-based drivers
dg Sep 20, 2021
1297897
database connection moved to PdoDriver::connect()
dg Dec 3, 2023
38b88d5
drivers uses PDO instead of Connection
dg Nov 30, 2023
ccece35
some others methods moved to PdoDriver
dg Dec 13, 2023
c7c81ee
added ResultDriver
dg May 10, 2024
2cf6efa
exceptions are converted in PdoDriver
dg Sep 20, 2021
071d442
added Nette\Database\QueryException
dg Sep 20, 2021
860a033
drivers: getForeignKeys() works with multi-column foreign keys
dg Jan 19, 2022
c328e2c
readme: added jumbo
dg May 16, 2024
9bf1d51
added CredentialProvider
Jun 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
some others methods moved to PdoDriver
  • Loading branch information
dg committed May 13, 2024
commit ccece35e50995cbf55fe50f476e2d343bedd70e0
23 changes: 7 additions & 16 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

use JetBrains\PhpStorm\Language;
use Nette\Utils\Arrays;
use PDO;
use PDOException;


/**
Expand Down Expand Up @@ -91,7 +89,7 @@ public function getDsn(): string


/** deprecated use getDriver()->getPdo() */
public function getPdo(): PDO
public function getPdo(): \PDO
{
$this->connect();
return $this->driver->getPdo();
Expand Down Expand Up @@ -129,22 +127,15 @@ public function setRowNormalizer(?callable $normalizer): static

public function getInsertId(?string $sequence = null): string
{
try {
$res = $this->getPdo()->lastInsertId($sequence);
return $res === false ? '0' : $res;
} catch (PDOException $e) {
throw $this->driver->convertException($e);
}
$this->connect();
return $this->driver->getInsertId($sequence);
}


public function quote(string $string, int $type = PDO::PARAM_STR): string
public function quote(string $string): string
{
try {
return $this->getPdo()->quote($string, $type);
} catch (PDOException $e) {
throw DriverException::from($e);
}
$this->connect();
return $this->driver->quote($string);
}


Expand Down Expand Up @@ -214,7 +205,7 @@ public function query(#[Language('SQL')] string $sql, #[Language('GenericSQL')]
[$this->sql, $params] = $this->preprocess($sql, ...$params);
try {
$result = new ResultSet($this, $this->sql, $params, $this->rowNormalizer);
} catch (PDOException $e) {
} catch (\PDOException $e) {
Arrays::invoke($this->onQuery, $this, $e);
throw $e;
}
Expand Down
20 changes: 19 additions & 1 deletion src/Database/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,26 @@ function connect(string $dsn, ?string $user = null, ?string $password = null, ?a
*/
function convertException(\PDOException $e): DriverException;

function query(string $queryString, array $params);

function beginTransaction(): void;

function commit(): void;

function rollBack(): void;

/**
* Returns the ID of the last inserted row or sequence value.
*/
function getInsertId(?string $sequence = null): string;

/**
* Delimits string for use in SQL statement.
*/
function quote(string $string): string;

/**
* Delimites identifier for use in a SQL statement.
* Delimits identifier for use in SQL statement.
*/
function delimite(string $name): string;

Expand Down
76 changes: 76 additions & 0 deletions src/Database/Drivers/PdoDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Database\Drivers;

use Nette;
use Nette\Database\DriverException;
use PDO;
use PDOException;

Expand Down Expand Up @@ -43,4 +44,79 @@ public function getPdo(): ?PDO
{
return $this->pdo;
}


public function query(string $queryString, array $params)
{
try {
$types = ['boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT, 'resource' => PDO::PARAM_LOB, 'NULL' => PDO::PARAM_NULL];

$statement = $this->pdo->prepare($queryString);
foreach ($params as $key => $value) {
$type = gettype($value);
$statement->bindValue(is_int($key) ? $key + 1 : $key, $value, $types[$type] ?? PDO::PARAM_STR);
}

$statement->setFetchMode(PDO::FETCH_ASSOC);
@$statement->execute(); // @ PHP generates warning when ATTR_ERRMODE = ERRMODE_EXCEPTION bug #73878
return $statement;

} catch (PDOException $e) {
$e = $this->convertException($e);
$e->queryString = $queryString;
$e->params = $params;
throw $e;
}
}


public function beginTransaction(): void
{
try {
$this->pdo->beginTransaction();
} catch (PDOException $e) {
throw $this->convertException($e);
}
}


public function commit(): void
{
try {
$this->pdo->commit();
} catch (PDOException $e) {
throw $this->convertException($e);
}
}


public function rollBack(): void
{
try {
$this->pdo->rollBack();
} catch (PDOException $e) {
throw $this->convertException($e);
}
}


public function getInsertId(?string $sequence = null): string
{
try {
$res = $this->pdo->lastInsertId($sequence);
return $res === false ? '0' : $res;
} catch (PDOException $e) {
throw $this->convertException($e);
}
}


public function quote(string $string, int $type = PDO::PARAM_STR): string
{
try {
return $this->pdo->quote($string, $type);
} catch (PDOException $e) {
throw DriverException::from($e);
}
}
}
26 changes: 6 additions & 20 deletions src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,12 @@ public function __construct(
) {
$time = microtime(true);
$this->normalizer = $normalizer;
$types = ['boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT, 'resource' => PDO::PARAM_LOB, 'NULL' => PDO::PARAM_NULL];

try {
if (str_starts_with($queryString, '::')) {
$connection->getPdo()->{substr($queryString, 2)}();
} else {
$this->pdoStatement = $connection->getPdo()->prepare($queryString);
foreach ($params as $key => $value) {
$type = gettype($value);
$this->pdoStatement->bindValue(is_int($key) ? $key + 1 : $key, $value, $types[$type] ?? PDO::PARAM_STR);
}

$this->pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
@$this->pdoStatement->execute(); // @ PHP generates warning when ATTR_ERRMODE = ERRMODE_EXCEPTION bug #73878
}
} catch (\PDOException $e) {
$e = $connection->getDriver()->convertException($e);
$e->queryString = $queryString;
$e->params = $params;
throw $e;

$driver = $connection->getDriver();
if (str_starts_with($queryString, '::')) {
$driver->{substr($queryString, 2)}();
} else {
$this->pdoStatement = $driver->query($queryString, $params);
}

$this->time = microtime(true) - $time;
Expand Down