1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-08 07:06:52 +02:00

dibi internally uses DateTime object in PHP 5.2

This commit is contained in:
David Grudl
2009-08-20 23:42:50 +02:00
parent 3777bacc02
commit fa6d771813
14 changed files with 76 additions and 44 deletions

View File

@@ -582,11 +582,8 @@ class dibi
} elseif (is_numeric($time)) { } elseif (is_numeric($time)) {
$time = (int) $time; // timestamp $time = (int) $time; // timestamp
} elseif ($time instanceof DateTime) { } elseif (is_string($time)) {
$time = $time->format('U'); $time = class_exists('DateTime', FALSE) ? new DateTime($time) : strtotime($time); // DateTime is since PHP 5.2
} else {
$time = strtotime($time); // try convert to timestamp
} }
return new DibiVariable($time, dibi::DATETIME); return new DibiVariable($time, dibi::DATETIME);
} }

View File

@@ -244,7 +244,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...) * @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException * @throws InvalidArgumentException
@@ -263,10 +263,10 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver
return $value ? 1 : 0; return $value ? 1 : 0;
case dibi::DATE: 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: 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: default:
throw new InvalidArgumentException('Unsupported type.'); throw new InvalidArgumentException('Unsupported type.');

View File

@@ -195,7 +195,7 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...) * @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException * @throws InvalidArgumentException
@@ -216,10 +216,10 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver
return $value ? 1 : 0; return $value ? 1 : 0;
case dibi::DATE: 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: 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: default:
throw new InvalidArgumentException('Unsupported type.'); throw new InvalidArgumentException('Unsupported type.');

View File

@@ -197,7 +197,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...) * @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException * @throws InvalidArgumentException
@@ -218,10 +218,10 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver
return $value ? 1 : 0; return $value ? 1 : 0;
case dibi::DATE: 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: 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: default:
throw new InvalidArgumentException('Unsupported type.'); throw new InvalidArgumentException('Unsupported type.');

View File

@@ -272,7 +272,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...) * @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException * @throws InvalidArgumentException
@@ -295,10 +295,10 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver
return $value ? 1 : 0; return $value ? 1 : 0;
case dibi::DATE: 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: 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: default:
throw new InvalidArgumentException('Unsupported type.'); throw new InvalidArgumentException('Unsupported type.');

View File

@@ -256,7 +256,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...) * @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException * @throws InvalidArgumentException
@@ -278,10 +278,10 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver
return $value ? 1 : 0; return $value ? 1 : 0;
case dibi::DATE: 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: 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: default:
throw new InvalidArgumentException('Unsupported type.'); throw new InvalidArgumentException('Unsupported type.');

View File

@@ -203,7 +203,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...) * @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException * @throws InvalidArgumentException
@@ -223,10 +223,10 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver
return $value ? 1 : 0; return $value ? 1 : 0;
case dibi::DATE: 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: 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: default:
throw new InvalidArgumentException('Unsupported type.'); throw new InvalidArgumentException('Unsupported type.');

View File

@@ -231,10 +231,10 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver
return $value ? 1 : 0; return $value ? 1 : 0;
case dibi::DATE: case dibi::DATE:
return date($this->fmtDate, $value); return $value instanceof DateTime ? $value->format($this->fmtDate) : date($this->fmtDate, $value);
case dibi::DATETIME: case dibi::DATETIME:
return date($this->fmtDateTime, $value); return $value instanceof DateTime ? $value->format($this->fmtDateTime) : date($this->fmtDateTime, $value);
default: default:
throw new InvalidArgumentException('Unsupported type.'); throw new InvalidArgumentException('Unsupported type.');

View File

@@ -222,7 +222,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...) * @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException * @throws InvalidArgumentException
@@ -267,10 +267,10 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver
return $this->connection->quote($value, PDO::PARAM_BOOL); return $this->connection->quote($value, PDO::PARAM_BOOL);
case dibi::DATE: 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: 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: default:
throw new InvalidArgumentException('Unsupported type.'); throw new InvalidArgumentException('Unsupported type.');

View File

@@ -231,7 +231,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...) * @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException * @throws InvalidArgumentException
@@ -267,10 +267,10 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver
return $value ? 'TRUE' : 'FALSE'; return $value ? 'TRUE' : 'FALSE';
case dibi::DATE: 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: 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: default:
throw new InvalidArgumentException('Unsupported type.'); throw new InvalidArgumentException('Unsupported type.');

View File

@@ -218,7 +218,7 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...) * @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException * @throws InvalidArgumentException
@@ -240,10 +240,10 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver
return $value ? 1 : 0; return $value ? 1 : 0;
case dibi::DATE: case dibi::DATE:
return date($this->fmtDate, $value); return $value instanceof DateTime ? $value->format($this->fmtDate) : date($this->fmtDate, $value);
case dibi::DATETIME: case dibi::DATETIME:
return date($this->fmtDateTime, $value); return $value instanceof DateTime ? $value->format($this->fmtDateTime) : date($this->fmtDateTime, $value);
default: default:
throw new InvalidArgumentException('Unsupported type.'); throw new InvalidArgumentException('Unsupported type.');

View File

@@ -500,7 +500,7 @@ class DibiResult extends DibiObject implements IDataSource
final public function convert($value, $type, $format = NULL) final public function convert($value, $type, $format = NULL)
{ {
if ($value === NULL || $value === FALSE) { if ($value === NULL || $value === FALSE) {
return $value; return NULL;
} }
switch ($type) { switch ($type) {
@@ -518,8 +518,25 @@ class DibiResult extends DibiObject implements IDataSource
case dibi::DATE: case dibi::DATE:
case dibi::DATETIME: case dibi::DATETIME:
$value = is_numeric($value) ? (int) $value : strtotime($value); if ($value == NULL) { // intentionally ==
return $format === NULL ? $value : date($format, $value); 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: case dibi::BOOL:
return ((bool) $value) && $value !== 'f' && $value !== 'F'; return ((bool) $value) && $value !== 'f' && $value !== 'F';

View File

@@ -42,18 +42,30 @@ class DibiRow extends ArrayObject
/** /**
* Converts value to date-time format. * Converts value to date-time format.
* @param string key * @param string key
* @param string format * @param string format (TRUE means DateTime object)
* @return mixed * @return mixed
*/ */
public function asDate($key, $format = NULL) public function asDate($key, $format = NULL)
{ {
$value = $this[$key]; $time = $this[$key];
if ($value === NULL || $value === FALSE) { if ($time == NULL) { // intentionally ==
return $value; 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 { } else {
$value = is_numeric($value) ? (int) $value : strtotime($value); return date($format, strtotime($time));
return $format === NULL ? $value : date($format, $value);
} }
} }

View File

@@ -372,7 +372,13 @@ final class DibiTranslator extends DibiObject
if ($value === NULL) { if ($value === NULL) {
return 'NULL'; return 'NULL';
} else { } 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': case 'by':