From 78d6603bb0032960557e5d43db6053eec9b1a83a Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 30 Aug 2019 18:05:38 +0200 Subject: [PATCH] Driver::escapeDate() & escapeDateTime() accepts only DateTimeInterface (BC break) --- src/Dibi/Drivers/FirebirdDriver.php | 16 ++-------------- src/Dibi/Drivers/MySqliDriver.php | 16 ++-------------- src/Dibi/Drivers/OdbcDriver.php | 16 ++-------------- src/Dibi/Drivers/OracleDriver.php | 16 ++-------------- src/Dibi/Drivers/PdoDriver.php | 16 ++-------------- src/Dibi/Drivers/PostgreDriver.php | 16 ++-------------- src/Dibi/Drivers/SqliteDriver.php | 16 ++-------------- src/Dibi/Drivers/SqlsrvDriver.php | 16 ++-------------- src/Dibi/Translator.php | 7 ++++--- src/Dibi/interfaces.php | 10 ++-------- 10 files changed, 22 insertions(+), 123 deletions(-) diff --git a/src/Dibi/Drivers/FirebirdDriver.php b/src/Dibi/Drivers/FirebirdDriver.php index cdec57e7..50551a52 100644 --- a/src/Dibi/Drivers/FirebirdDriver.php +++ b/src/Dibi/Drivers/FirebirdDriver.php @@ -247,26 +247,14 @@ class FirebirdDriver implements Dibi\Driver } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDate($value): string + public function escapeDate(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $value->format("'Y-m-d'"); } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDateTime($value): string + public function escapeDateTime(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return "'" . substr($value->format('Y-m-d H:i:s.u'), 0, -2) . "'"; } diff --git a/src/Dibi/Drivers/MySqliDriver.php b/src/Dibi/Drivers/MySqliDriver.php index 6ba8e6ae..f38630aa 100644 --- a/src/Dibi/Drivers/MySqliDriver.php +++ b/src/Dibi/Drivers/MySqliDriver.php @@ -290,26 +290,14 @@ class MySqliDriver implements Dibi\Driver } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDate($value): string + public function escapeDate(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $value->format("'Y-m-d'"); } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDateTime($value): string + public function escapeDateTime(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $value->format("'Y-m-d H:i:s.u'"); } diff --git a/src/Dibi/Drivers/OdbcDriver.php b/src/Dibi/Drivers/OdbcDriver.php index 8d4c9367..17b6ac34 100644 --- a/src/Dibi/Drivers/OdbcDriver.php +++ b/src/Dibi/Drivers/OdbcDriver.php @@ -226,26 +226,14 @@ class OdbcDriver implements Dibi\Driver } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDate($value): string + public function escapeDate(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $value->format('#m/d/Y#'); } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDateTime($value): string + public function escapeDateTime(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $value->format($this->microseconds ? '#m/d/Y H:i:s.u#' : '#m/d/Y H:i:s#'); } diff --git a/src/Dibi/Drivers/OracleDriver.php b/src/Dibi/Drivers/OracleDriver.php index aa269b8e..369a24e5 100644 --- a/src/Dibi/Drivers/OracleDriver.php +++ b/src/Dibi/Drivers/OracleDriver.php @@ -241,28 +241,16 @@ class OracleDriver implements Dibi\Driver } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDate($value): string + public function escapeDate(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $this->nativeDate ? "to_date('" . $value->format('Y-m-d') . "', 'YYYY-mm-dd')" : $value->format('U'); } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDateTime($value): string + public function escapeDateTime(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $this->nativeDate ? "to_date('" . $value->format('Y-m-d G:i:s') . "', 'YYYY-mm-dd hh24:mi:ss')" : $value->format('U'); diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index 82991dd8..1e07af3f 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -289,26 +289,14 @@ class PdoDriver implements Dibi\Driver } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDate($value): string + public function escapeDate(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $value->format($this->driverName === 'odbc' ? '#m/d/Y#' : "'Y-m-d'"); } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDateTime($value): string + public function escapeDateTime(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } switch ($this->driverName) { case 'odbc': return $value->format('#m/d/Y H:i:s.u#'); diff --git a/src/Dibi/Drivers/PostgreDriver.php b/src/Dibi/Drivers/PostgreDriver.php index 87d37958..d3fbdaf5 100644 --- a/src/Dibi/Drivers/PostgreDriver.php +++ b/src/Dibi/Drivers/PostgreDriver.php @@ -292,26 +292,14 @@ class PostgreDriver implements Dibi\Driver } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDate($value): string + public function escapeDate(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $value->format("'Y-m-d'"); } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDateTime($value): string + public function escapeDateTime(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $value->format("'Y-m-d H:i:s.u'"); } diff --git a/src/Dibi/Drivers/SqliteDriver.php b/src/Dibi/Drivers/SqliteDriver.php index 488017fe..d5d1c9b7 100644 --- a/src/Dibi/Drivers/SqliteDriver.php +++ b/src/Dibi/Drivers/SqliteDriver.php @@ -230,26 +230,14 @@ class SqliteDriver implements Dibi\Driver } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDate($value): string + public function escapeDate(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $value->format($this->fmtDate); } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDateTime($value): string + public function escapeDateTime(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $value->format($this->fmtDateTime); } diff --git a/src/Dibi/Drivers/SqlsrvDriver.php b/src/Dibi/Drivers/SqlsrvDriver.php index 3e7c7136..81871622 100644 --- a/src/Dibi/Drivers/SqlsrvDriver.php +++ b/src/Dibi/Drivers/SqlsrvDriver.php @@ -219,26 +219,14 @@ class SqlsrvDriver implements Dibi\Driver } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDate($value): string + public function escapeDate(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return $value->format("'Y-m-d'"); } - /** - * @param \DateTimeInterface|string|int $value - */ - public function escapeDateTime($value): string + public function escapeDateTime(\DateTimeInterface $value): string { - if (!$value instanceof \DateTimeInterface) { - $value = new Dibi\DateTime($value); - } return 'CONVERT(DATETIME2(7), ' . $value->format("'Y-m-d H:i:s.u'") . ')'; } diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index 6820b2fb..8b95238b 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -381,10 +381,11 @@ final class Translator case 'dt': // datetime if ($value === null) { return 'NULL'; - } else { - return $modifier === 'd' ? $this->driver->escapeDate($value) : $this->driver->escapeDateTime($value); + } elseif (!$value instanceof \DateTimeInterface) { + $value = new DateTime($value); } - // break omitted + return $modifier === 'd' ? $this->driver->escapeDate($value) : $this->driver->escapeDateTime($value); + case 'by': case 'n': // composed identifier name return $this->identifiers->$value; diff --git a/src/Dibi/interfaces.php b/src/Dibi/interfaces.php index 64c2fcc3..429b15d2 100644 --- a/src/Dibi/interfaces.php +++ b/src/Dibi/interfaces.php @@ -87,15 +87,9 @@ interface Driver function escapeBool(bool $value): string; - /** - * @param \DateTimeInterface|string|int $value - */ - function escapeDate($value): string; + function escapeDate(\DateTimeInterface $value): string; - /** - * @param \DateTimeInterface|string|int $value - */ - function escapeDateTime($value): string; + function escapeDateTime(\DateTimeInterface $value): string; /** * Encodes string for use in a LIKE statement.