Skip to content

Commit

Permalink
Merge pull request #41 from boan2010/feature/added-date-type
Browse files Browse the repository at this point in the history
Add support for SQL `DATE` via `DateType`
  • Loading branch information
mindplay-dk authored Dec 22, 2017
2 parents 7c0e7cd + 2e6383d commit af3a58a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/model/types/DateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace mindplay\sql\model\types;

class DateType extends TimestampType
{
const FORMAT = 'Y-m-d';
}
2 changes: 1 addition & 1 deletion src/model/types/TimestampType.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function convertToPHP($value)
return $value; // return NULL value as-is
}

$datetime = DateTime::createFromFormat(static::FORMAT, $value, self::$utc_timezone);
$datetime = DateTime::createFromFormat('!' . static::FORMAT, $value, self::$utc_timezone);

if ($datetime === false) {
throw new UnexpectedValueException("unable to convert value from int: " . $value);
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use mindplay\sql\model\schema\Schema;
use mindplay\sql\model\schema\Table;
use mindplay\sql\model\types\BoolType;
use mindplay\sql\model\types\DateType;
use mindplay\sql\model\types\IntType;
use mindplay\sql\model\types\StringType;
use mindplay\sql\model\types\TimestampType;
Expand Down Expand Up @@ -150,6 +151,11 @@ public function dob($alias)
return $this->requiredColumn(__FUNCTION__, TimestampType::class, $alias);
}

public function birthday($alias)
{
return $this->requiredColumn(__FUNCTION__, DateType::class, $alias);
}

public function home_address_id($alias)
{
return $this->requiredColumn(__FUNCTION__, IntType::class, $alias);
Expand Down
17 changes: 16 additions & 1 deletion test/test-unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use mindplay\sql\model\query\SelectQuery;
use mindplay\sql\model\schema\Column;
use mindplay\sql\model\types\BoolType;
use mindplay\sql\model\types\DateType;
use mindplay\sql\model\types\IntType;
use mindplay\sql\model\types\JSONType;
use mindplay\sql\model\types\StringType;
Expand Down Expand Up @@ -754,6 +755,19 @@ function () {
}
);

test(
'can map DATE to Unix timestamps',
function () {
$type = new DateType();

$valid_date = '2015-11-04';
$valid_timestamp = 1446595200;

eq($type->convertToPHP($valid_date), $valid_timestamp, "can convert to PHP value");
eq($type->convertToSQL($valid_timestamp), $valid_date, "can convert to SQL DATE value");
}
);

test(
'can map PHP values to JSON',
function () {
Expand Down Expand Up @@ -840,7 +854,7 @@ function () {

$columns = $user->listColumns();

eq(count($columns), 6);
eq(count($columns), 7);

foreach ($columns as $column) {
ok($column instanceof Column);
Expand Down Expand Up @@ -1084,6 +1098,7 @@ function () {
'first_name' => StringType::class,
'last_name' => StringType::class,
'dob' => TimestampType::class,
'birthday' => DateType::class,
'home_address_id' => IntType::class,
'deleted' => BoolType::class,
];
Expand Down

0 comments on commit af3a58a

Please sign in to comment.