From fa6d7718130040cffc95a25cbaf71e92685fcab3 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 20 Aug 2009 23:42:50 +0200 Subject: [PATCH] dibi internally uses DateTime object in PHP 5.2 --- dibi/dibi.php | 7 ++----- dibi/drivers/firebird.php | 6 +++--- dibi/drivers/mssql.php | 6 +++--- dibi/drivers/mssql2005.php | 6 +++--- dibi/drivers/mysql.php | 6 +++--- dibi/drivers/mysqli.php | 6 +++--- dibi/drivers/odbc.php | 6 +++--- dibi/drivers/oracle.php | 4 ++-- dibi/drivers/pdo.php | 6 +++--- dibi/drivers/postgre.php | 6 +++--- dibi/drivers/sqlite.php | 6 +++--- dibi/libs/DibiResult.php | 23 ++++++++++++++++++++--- dibi/libs/DibiRow.php | 24 ++++++++++++++++++------ dibi/libs/DibiTranslator.php | 8 +++++++- 14 files changed, 76 insertions(+), 44 deletions(-) diff --git a/dibi/dibi.php b/dibi/dibi.php index 04b0320d..9e240bf1 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -582,11 +582,8 @@ class dibi } elseif (is_numeric($time)) { $time = (int) $time; // timestamp - } elseif ($time instanceof DateTime) { - $time = $time->format('U'); - - } else { - $time = strtotime($time); // try convert to timestamp + } elseif (is_string($time)) { + $time = class_exists('DateTime', FALSE) ? new DateTime($time) : strtotime($time); // DateTime is since PHP 5.2 } return new DibiVariable($time, dibi::DATETIME); } diff --git a/dibi/drivers/firebird.php b/dibi/drivers/firebird.php index 609fd02d..b67fa009 100644 --- a/dibi/drivers/firebird.php +++ b/dibi/drivers/firebird.php @@ -244,7 +244,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver /** * Encodes data for use in a SQL statement. - * @param string value + * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException @@ -263,10 +263,10 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver return $value ? 1 : 0; case dibi::DATE: - return date("'Y-m-d'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); case dibi::DATETIME: - return date("'Y-m-d H:i:s'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index a4f0a63a..23c0546a 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -195,7 +195,7 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver /** * Encodes data for use in a SQL statement. - * @param string value + * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException @@ -216,10 +216,10 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver return $value ? 1 : 0; case dibi::DATE: - return date("'Y-m-d'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); case dibi::DATETIME: - return date("'Y-m-d H:i:s'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/mssql2005.php b/dibi/drivers/mssql2005.php index 26413d4a..b19163e3 100644 --- a/dibi/drivers/mssql2005.php +++ b/dibi/drivers/mssql2005.php @@ -197,7 +197,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver /** * Encodes data for use in a SQL statement. - * @param string value + * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException @@ -218,10 +218,10 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver return $value ? 1 : 0; case dibi::DATE: - return date("'Y-m-d'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); case dibi::DATETIME: - return date("'Y-m-d H:i:s'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index ec2c3843..f5990ea5 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -272,7 +272,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver /** * Encodes data for use in a SQL statement. - * @param string value + * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException @@ -295,10 +295,10 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver return $value ? 1 : 0; case dibi::DATE: - return date("'Y-m-d'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); case dibi::DATETIME: - return date("'Y-m-d H:i:s'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index bceb1d93..275f2f82 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -256,7 +256,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver /** * Encodes data for use in a SQL statement. - * @param string value + * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException @@ -278,10 +278,10 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver return $value ? 1 : 0; case dibi::DATE: - return date("'Y-m-d'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); case dibi::DATETIME: - return date("'Y-m-d H:i:s'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index 1a2c3de0..9b737dcd 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -203,7 +203,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver /** * Encodes data for use in a SQL statement. - * @param string value + * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException @@ -223,10 +223,10 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver return $value ? 1 : 0; case dibi::DATE: - return date("#m/d/Y#", $value); + return $value instanceof DateTime ? $value->format("#m/d/Y#") : date("#m/d/Y#", $value); case dibi::DATETIME: - return date("#m/d/Y H:i:s#", $value); + return $value instanceof DateTime ? $value->format("#m/d/Y H:i:s#") : date("#m/d/Y H:i:s#", $value); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index 7ee18a0a..dd4a4d88 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -231,10 +231,10 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver return $value ? 1 : 0; case dibi::DATE: - return date($this->fmtDate, $value); + return $value instanceof DateTime ? $value->format($this->fmtDate) : date($this->fmtDate, $value); case dibi::DATETIME: - return date($this->fmtDateTime, $value); + return $value instanceof DateTime ? $value->format($this->fmtDateTime) : date($this->fmtDateTime, $value); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index 4b48d771..323d1bb3 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -222,7 +222,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver /** * Encodes data for use in a SQL statement. - * @param string value + * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException @@ -267,10 +267,10 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver return $this->connection->quote($value, PDO::PARAM_BOOL); case dibi::DATE: - return date("'Y-m-d'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); case dibi::DATETIME: - return date("'Y-m-d H:i:s'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index 25865d33..c0345b8c 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -231,7 +231,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver /** * Encodes data for use in a SQL statement. - * @param string value + * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException @@ -267,10 +267,10 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver return $value ? 'TRUE' : 'FALSE'; case dibi::DATE: - return date("'Y-m-d'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value); case dibi::DATETIME: - return date("'Y-m-d H:i:s'", $value); + return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 6d9bbc94..0431e47e 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -218,7 +218,7 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver /** * Encodes data for use in a SQL statement. - * @param string value + * @param mixed value * @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string encoded value * @throws InvalidArgumentException @@ -240,10 +240,10 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver return $value ? 1 : 0; case dibi::DATE: - return date($this->fmtDate, $value); + return $value instanceof DateTime ? $value->format($this->fmtDate) : date($this->fmtDate, $value); case dibi::DATETIME: - return date($this->fmtDateTime, $value); + return $value instanceof DateTime ? $value->format($this->fmtDateTime) : date($this->fmtDateTime, $value); default: throw new InvalidArgumentException('Unsupported type.'); diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 62b09d9c..d1223140 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -500,7 +500,7 @@ class DibiResult extends DibiObject implements IDataSource final public function convert($value, $type, $format = NULL) { if ($value === NULL || $value === FALSE) { - return $value; + return NULL; } switch ($type) { @@ -518,8 +518,25 @@ class DibiResult extends DibiObject implements IDataSource case dibi::DATE: case dibi::DATETIME: - $value = is_numeric($value) ? (int) $value : strtotime($value); - return $format === NULL ? $value : date($format, $value); + if ($value == NULL) { // intentionally == + return NULL; + + } elseif ($format === NULL) { // return timestamp (default) + return is_numeric($value) ? (int) $value : strtotime($value); + + } elseif ($format === TRUE) { // return DateTime object + return new DateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value); + + } elseif (is_numeric($value)) { // single timestamp + return date($format, $value); + + } elseif (class_exists('DateTime', FALSE)) { // since PHP 5.2 + $value = new DateTime($value); + return $value ? $value->format($format) : NULL; + + } else { + return date($format, strtotime($value)); + } case dibi::BOOL: return ((bool) $value) && $value !== 'f' && $value !== 'F'; diff --git a/dibi/libs/DibiRow.php b/dibi/libs/DibiRow.php index 8103dd45..20cf183b 100644 --- a/dibi/libs/DibiRow.php +++ b/dibi/libs/DibiRow.php @@ -42,18 +42,30 @@ class DibiRow extends ArrayObject /** * Converts value to date-time format. * @param string key - * @param string format + * @param string format (TRUE means DateTime object) * @return mixed */ public function asDate($key, $format = NULL) { - $value = $this[$key]; - if ($value === NULL || $value === FALSE) { - return $value; + $time = $this[$key]; + if ($time == NULL) { // intentionally == + return NULL; + + } elseif ($format === NULL) { // return timestamp (default) + return is_numeric($time) ? (int) $time : strtotime($time); + + } elseif ($format === TRUE) { // return DateTime object + return new DateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time); + + } elseif (is_numeric($time)) { // single timestamp + return date($format, $time); + + } elseif (class_exists('DateTime', FALSE)) { // since PHP 5.2 + $time = new DateTime($time); + return $time ? $time->format($format) : NULL; } else { - $value = is_numeric($value) ? (int) $value : strtotime($value); - return $format === NULL ? $value : date($format, $value); + return date($format, strtotime($time)); } } diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index 79527657..cf2f8390 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -372,7 +372,13 @@ final class DibiTranslator extends DibiObject if ($value === NULL) { return 'NULL'; } else { - return $this->driver->escape(is_numeric($value) ? (int) $value : strtotime($value), $modifier); + if (is_numeric($value)) { + $value = (int) $value; // timestamp + + } elseif (is_string($value)) { + $value = class_exists('DateTime', FALSE) ? new DateTime($value) : strtotime($value); + } + return $this->driver->escape($value, $modifier); } case 'by':