Skip to content

Commit

Permalink
BUGFIX Fixing date/datetime handling in MSSQLDatabase
Browse files Browse the repository at this point in the history
Fixing the date type to actually be "date", and not "datetime" so
that values are returned as "Y-m-d" for dates, and not "Y-m-d H:i:s".

Additionally, fixing a case where using the mssql functions would
return strange datetime formats. We need the datetime field values
in Y-m-d H:i:s. To get this working, we need to inspect the field type
when retrieving rows from the database and re-format the datetime
value at this point. This is done in MSSQLQuery::nextRecord()
  • Loading branch information
Sean Harvey committed Jun 12, 2012
1 parent 4962524 commit a4c269b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
18 changes: 15 additions & 3 deletions code/MSSQLDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ public function boolean($values) {
* @return string
*/
public function date($values) {
return 'datetime null';
return 'date null';
}

/**
Expand Down Expand Up @@ -1221,7 +1221,6 @@ public function sqlQueryToString(SQLQuery $query) {
$text = 'DELETE ';
} else {
$distinct = $query->getDistinct() ? 'DISTINCT ' : '';

// If there's a limit but no offset, just use 'TOP X'
// rather than the more complex sub-select method
if ($limit != 0 && $offset == 0) {
Expand Down Expand Up @@ -1775,7 +1774,20 @@ public function nextRecord() {
if(!is_resource($this->handle)) return false;

if($this->mssql) {
if($data = mssql_fetch_assoc($this->handle)) {
if($row = mssql_fetch_row($this->handle)) {
foreach($row as $i => $value) {
$field = mssql_fetch_field($this->handle, $i);

// fix datetime formatting from format "Jan 1 2012 12:00:00:000AM" to "2012-01-01 12:00:00"
// strtotime doesn't understand this format, so we need to do some modification of the value first
if($field->type == 'datetime' && $value) {
$value = date('Y-m-d H:i:s', strtotime(preg_replace('/:[0-9][0-9][0-9]([ap]m)$/i', ' \\1', $value)));
}

if(isset($value) || !isset($data[$field->name])) {
$data[$field->name] = $value;
}
}
return $data;
}
} else {
Expand Down
28 changes: 28 additions & 0 deletions tests/MSSQLDatabaseQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
class MSSQLDatabaseQueryTest extends SapphireTest {

public static $fixture_file = 'MSSQLDatabaseQueryTest.yml';

protected $extraDataObjects = array(
'MSSQLDatabaseQueryTestDataObject'
);

public function testDateValueFormatting() {
$obj = $this->objFromFixture('MSSQLDatabaseQueryTestDataObject', 'test-data-1');
$this->assertEquals('2012-01-01', $obj->TestDate, 'Date field value is formatted correctly (Y-m-d)');
}

public function testDatetimeValueFormatting() {
$obj = $this->objFromFixture('MSSQLDatabaseQueryTestDataObject', 'test-data-1');
$this->assertEquals('2012-01-01 10:30:00', $obj->TestDatetime, 'Datetime field value is formatted correctly (Y-m-d H:i:s)');
}

}
class MSSQLDatabaseQueryTestDataObject extends DataObject implements TestOnly {

public static $db = array(
'TestDate' => 'Date',
'TestDatetime' => 'SS_Datetime'
);

}
5 changes: 5 additions & 0 deletions tests/MSSQLDatabaseQueryTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MSSQLDatabaseQueryTestDataObject:
test-data-1:
TestDate: 2012-01-01
TestDatetime: 2012-01-01 10:30:00

0 comments on commit a4c269b

Please sign in to comment.