Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Added JDatabaseQuery function to extract part of timestamp. #758

Merged
merged 3 commits into from
Jan 24, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 102 additions & 0 deletions libraries/joomla/database/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,108 @@ public function from($tables)
return $this;
}

/**
* Used to get a string to extract year from date column.
*
* Usage:
* $query->select($query->year($query->quoteName('dateColumn')));
*
* @param string $date Date column containing year to be extracted.
*
* @return string Returns string to extract year from a date.
*
* @since 12.1
*/
public function year($date)
{
return 'YEAR(' . $date . ')';
}

/**
* Used to get a string to extract month from date column.
*
* Usage:
* $query->select($query->month($query->quoteName('dateColumn')));
*
* @param string $date Date column containing month to be extracted.
*
* @return string Returns string to extract month from a date.
*
* @since 12.1
*/
public function month($date)
{
return 'MONTH(' . $date . ')';
}

/**
* Used to get a string to extract day from date column.
*
* Usage:
* $query->select($query->day($query->quoteName('dateColumn')));
*
* @param string $date Date column containing day to be extracted.
*
* @return string Returns string to extract day from a date.
*
* @since 12.1
*/
public function day($date)
{
return 'DAY(' . $date . ')';
}

/**
* Used to get a string to extract hour from date column.
*
* Usage:
* $query->select($query->hour($query->quoteName('dateColumn')));
*
* @param string $date Date column containing hour to be extracted.
*
* @return string Returns string to extract hour from a date.
*
* @since 12.1
*/
public function hour($date)
{
return 'HOUR(' . $date . ')';
}

/**
* Used to get a string to extract minute from date column.
*
* Usage:
* $query->select($query->minute($query->quoteName('dateColumn')));
*
* @param string $date Date column containing minute to be extracted.
*
* @return string Returns string to extract minute from a date.
*
* @since 12.1
*/
public function minute($date)
{
return 'MINUTE(' . $date . ')';
}

/**
* Used to get a string to extract seconds from date column.
*
* Usage:
* $query->select($query->second($query->quoteName('dateColumn')));
*
* @param string $date Date column containing second to be extracted.
*
* @return string Returns string to extract second from a date.
*
* @since 12.1
*/
public function second($date)
{
return 'SECOND(' . $date . ')';
}

/**
* Add a grouping column to the GROUP clause of the query.
*
Expand Down
114 changes: 114 additions & 0 deletions tests/suite/joomla/database/JDatabaseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,120 @@ public function test__call()
);
}

/**
* Test for year extraction from date.
*
* @return void
*
* @since 12.1
*/
public function test__toStringYear()
{
$q = new JDatabaseQueryInspector($this->dbo);

$q->select($q->year($q->quoteName('col')))->from('table');

$this->assertThat(
(string) $q,
$this->equalTo("\nSELECT YEAR(`col`)\nFROM table")
);
}

/**
* Test for month extraction from date.
*
* @return void
*
* @since 12.1
*/
public function test__toStringMonth()
{
$q = new JDatabaseQueryInspector($this->dbo);

$q->select($q->month($q->quoteName('col')))->from('table');

$this->assertThat(
(string) $q,
$this->equalTo("\nSELECT MONTH(`col`)\nFROM table")
);
}

/**
* Test for day extraction from date.
*
* @return void
*
* @since 12.1
*/
public function test__toStringDay()
{
$q = new JDatabaseQueryInspector($this->dbo);

$q->select($q->day($q->quoteName('col')))->from('table');

$this->assertThat(
(string) $q,
$this->equalTo("\nSELECT DAY(`col`)\nFROM table")
);
}

/**
* Test for hour extraction from date.
*
* @return void
*
* @since 12.1
*/
public function test__toStringHour()
{
$q = new JDatabaseQueryInspector($this->dbo);

$q->select($q->hour($q->quoteName('col')))->from('table');

$this->assertThat(
(string) $q,
$this->equalTo("\nSELECT HOUR(`col`)\nFROM table")
);
}

/**
* Test for minute extraction from date.
*
* @return void
*
* @since 12.1
*/
public function test__toStringMinute()
{
$q = new JDatabaseQueryInspector($this->dbo);

$q->select($q->minute($q->quoteName('col')))->from('table');

$this->assertThat(
(string) $q,
$this->equalTo("\nSELECT MINUTE(`col`)\nFROM table")
);
}

/**
* Test for seconds extraction from date.
*
* @return void
*
* @since 12.1
*/
public function test__toStringSecond()
{
$q = new JDatabaseQueryInspector($this->dbo);

$q->select($q->second($q->quoteName('col')))->from('table');

$this->assertThat(
(string) $q,
$this->equalTo("\nSELECT SECOND(`col`)\nFROM table")
);
}

/**
* Test for the JDatabaseQuery::__string method for a 'select' case.
*
Expand Down