Skip to content
This repository
Browse code

Merge pull request #1811 from elinw/datemath

Add a date addition/subtraction method to JDatabaseQuery
  • Loading branch information...
commit d0ec4626ffb1597d10b96da32165348fab01009f 2 parents 5fbac7b + 74dbaaf
Andrew Eddie eddieajau authored
23 libraries/joomla/database/query.php
@@ -1755,4 +1755,27 @@ public function format($format)
1755 1755 */
1756 1756 return preg_replace_callback('#%(((([\d]+)\$)?([aeEnqQryYmMdDhHiIsStzZ]))|(%))#', $func, $format);
1757 1757 }
  1758 +
  1759 + /**
  1760 + * Add to the current date and time.
  1761 + * Usage:
  1762 + * $query->select($query->dateAdd());
  1763 + * Prefixing the interval with a - (negative sign) will cause subtraction to be used.
  1764 + * Note: Not all drivers support all units.
  1765 + *
  1766 + * @param datetime $date The date to add to. May be date or datetime
  1767 + * @param string $interval The string representation of the appropriate number of units
  1768 + * @param string $datePart The part of the date to perform the addition on
  1769 + *
  1770 + * @return sring The string with the appropriate sql for addition of dates
  1771 + *
  1772 + * @since 13.1
  1773 + *
  1774 + * @see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
  1775 +
  1776 + */
  1777 + public function dateAdd($date, $interval, $datePart)
  1778 + {
  1779 + return trim("DATE_ADD('" . $date . "', INTERVAL " . $interval . ' ' . $datePart . ')');
  1780 + }
1758 1781 }
27 libraries/joomla/database/query/postgresql.php
@@ -599,4 +599,31 @@ public function processLimit($query, $limit, $offset = 0)
599 599
600 600 return $query;
601 601 }
  602 + /**
  603 + * Add to the current date and time in Postgresql.
  604 + * Usage:
  605 + * $query->select($query->dateAdd());
  606 + * Prefixing the interval with a - (negative sign) will cause subtraction to be used.
  607 + *
  608 + * @param datetime $date The date to add to
  609 + * @param string $interval The string representation of the appropriate number of units
  610 + * @param string $datePart The part of the date to perform the addition on
  611 + *
  612 + * @return sring The string with the appropriate sql for addition of dates
  613 + *
  614 + * @since 13.1
  615 + * @note Not all drivers support all units. Check appropriate references
  616 + * @link http://www.postgresql.org/docs/9.0/static/functions-datetime.html.
  617 + */
  618 + public function dateAdd($date, $interval, $datePart)
  619 + {
  620 + if (substr($interval, 0, 1) != '-')
  621 + {
  622 + return "timestamp '" . $date . "' + interval '" . $interval . " " . $datePart . "'";
  623 + }
  624 + else
  625 + {
  626 + return "timestamp '" . $date . "' - interval '" . ltrim($interval, '-') . " " . $datePart . "'";
  627 + }
  628 + }
602 629 }
32 libraries/joomla/database/query/sqlite.php
@@ -180,4 +180,36 @@ public function setLimit($limit = 0, $offset = 0)
180 180
181 181 return $this;
182 182 }
  183 + /**
  184 + * Add to the current date and time.
  185 + * Usage:
  186 + * $query->select($query->dateAdd());
  187 + * Prefixing the interval with a - (negative sign) will cause subtraction to be used.
  188 + *
  189 + * @param datetime $date The date or datetime to add to
  190 + * @param string $interval The string representation of the appropriate number of units
  191 + * @param string $datePart The part of the date to perform the addition on
  192 + *
  193 + * @return sring The string with the appropriate sql for addition of dates
  194 + *
  195 + * @since 13.1
  196 + * @link http://www.sqlite.org/lang_datefunc.html
  197 + */
  198 + public function dateAdd($date, $interval, $datePart)
  199 + {
  200 + // SQLite does not support microseconds as a separate unit. Convert the interval to seconds
  201 + if (strcasecmp($datePart, 'microseconds') == 0)
  202 + {
  203 + $interval = .001 * $interval;
  204 + $datePart = 'seconds';
  205 + }
  206 + if (substr($interval, 0, 1) != '-')
  207 + {
  208 + return "datetime('" . $date . "', '+" . $interval . " " . $datePart . "')";
  209 + }
  210 + else
  211 + {
  212 + return "datetime('" . $date . "', '" . $interval . " " . $datePart . "')";
  213 + }
  214 + }
183 215 }
21 libraries/joomla/database/query/sqlsrv.php
@@ -175,4 +175,25 @@ public function length($value)
175 175 {
176 176 return 'LEN(' . $value . ')';
177 177 }
  178 +
  179 + /**
  180 + * Add to the current date and time.
  181 + * Usage:
  182 + * $query->select($query->dateAdd());
  183 + * Prefixing the interval with a - (negative sign) will cause subtraction to be used.
  184 + *
  185 + * @param datetime $date The date to add to; type may be time or datetime.
  186 + * @param string $interval The string representation of the appropriate number of units
  187 + * @param string $datePart The part of the date to perform the addition on
  188 + *
  189 + * @return sring The string with the appropriate sql for addition of dates
  190 + *
  191 + * @since 13.1
  192 + * @note Not all drivers support all units.
  193 + * @link http://msdn.microsoft.com/en-us/library/ms186819.aspx for more information
  194 + */
  195 + public function dateAdd($date, $interval, $datePart)
  196 + {
  197 + return "DATEADD('" . $datePart . "', '" . $interval . "', '" . $date . "'" . ')';
  198 + }
178 199 }
38 tests/suites/unit/joomla/database/JDatabaseQueryTest.php
@@ -1834,4 +1834,42 @@ protected function setUp()
1834 1834
1835 1835 $this->_instance = new JDatabaseQueryInspector($this->dbo);
1836 1836 }
  1837 +
  1838 + /**
  1839 + * Data for the testDateAdd test.
  1840 + *
  1841 + * @return array
  1842 + *
  1843 + * @since 13.1
  1844 + */
  1845 + public function seedDateAdd()
  1846 + {
  1847 + return array(
  1848 + // date, interval, datepart, expected
  1849 + 'Add date' => array('2008-12-31', '1', 'DAY', "DATE_ADD('2008-12-31', INTERVAL 1 DAY)"),
  1850 + 'Subtract date' => array('2008-12-31', '-1', 'DAY', "DATE_ADD('2008-12-31', INTERVAL -1 DAY)"),
  1851 + 'Add datetime' => array('2008-12-31 23:59:59', '1', 'DAY', "DATE_ADD('2008-12-31 23:59:59', INTERVAL 1 DAY)"),
  1852 + );
  1853 + }
  1854 +
  1855 + /**
  1856 + * Tests the JDatabaseQuery::DateAdd method
  1857 + *
  1858 + * @param datetime $date The date or datetime to add to.
  1859 + * @param string $interval The maximum length of the text.
  1860 + * @param string $datePart The part of the date to be added to (such as day or micosecond)
  1861 + * @param string $expected The expected result.
  1862 + *
  1863 + * @return void
  1864 + *
  1865 + * @dataProvider seedDateAdd
  1866 + * @since 13.1
  1867 + */
  1868 + public function testDateAdd($date, $interval, $datePart, $expected)
  1869 + {
  1870 + $this->assertThat(
  1871 + $this->_instance->dateAdd($date, $interval, $datePart),
  1872 + $this->equalTo($expected)
  1873 + );
  1874 + }
1837 1875 }
49 tests/suites/unit/joomla/database/database/JDatabasePostgresqlQueryTest.php
@@ -6,6 +6,8 @@
6 6 * @copyright Copyright (C) 2005 - 2013 Open Source Matters. All rights reserved.
7 7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 8 */
  9 +require_once __DIR__ . '/JDatabaseQueryPostgresqlInspector.php';
  10 +require_once JPATH_PLATFORM . '/joomla/database/query/postgresql.php';
9 11
10 12 /**
11 13 * Test class for JDatabasePostgresqlQuery.
@@ -23,6 +25,14 @@ class JDatabasePostgresqlQueryTest extends TestCase
23 25 protected $dbo;
24 26
25 27 /**
  28 + * The instance of the object to test.
  29 + *
  30 + * @var JDatabasePostgresqlQuery
  31 + * @since 12.3
  32 + */
  33 + private $_instance;
  34 +
  35 + /**
26 36 * Data for the testNullDate test.
27 37 *
28 38 * @return array
@@ -118,6 +128,8 @@ protected function setUp()
118 128
119 129 $this->dbo = TestMockDatabaseDriver::create($this, '1970-01-01 00:00:00', 'Y-m-d H:i:s');
120 130
  131 + $this->_instance = new JDatabaseQueryPostgresqlInspector($this->dbo);
  132 +
121 133 // Mock the escape method to ensure the API is calling the DBO's escape method.
122 134 $this->assignMockCallbacks(
123 135 $this->dbo,
@@ -1246,4 +1258,41 @@ public function testReturning()
1246 1258 'Tests rendered value.'
1247 1259 );
1248 1260 }
  1261 + /**
  1262 + * Data for the testDateAdd test.
  1263 + *
  1264 + * @return array
  1265 + *
  1266 + * @since 13.1
  1267 + */
  1268 + public function seedDateAdd()
  1269 + {
  1270 + return array(
  1271 + // date, interval, datepart, expected
  1272 + 'Add date' => array('2008-12-31', '1', 'day', "timestamp '2008-12-31' + interval '1 day'"),
  1273 + 'Subtract date' => array('2008-12-31', '-1', 'day', "timestamp '2008-12-31' - interval '1 day'"),
  1274 + 'Add datetime' => array('2008-12-31 23:59:59', '1', 'day', "timestamp '2008-12-31 23:59:59' + interval '1 day'"),
  1275 + );
  1276 + }
  1277 +
  1278 + /**
  1279 + * Tests the JDatabasePostgresqlQuery::DateAdd method
  1280 + *
  1281 + * @param datetime $date The date or datetime to add to.
  1282 + * @param string $interval The maximum length of the text.
  1283 + * @param string $datePart The part of the date to be added to (such as day or micosecond).
  1284 + * @param string $expected The expected result.
  1285 + *
  1286 + * @return void
  1287 + *
  1288 + * @dataProvider seedDateAdd
  1289 + * @since 13.1
  1290 + */
  1291 + public function testDateAdd($date, $interval, $datePart, $expected)
  1292 + {
  1293 + $this->assertThat(
  1294 + $this->_instance->dateAdd($date, $interval, $datePart),
  1295 + $this->equalTo($expected)
  1296 + );
  1297 + }
1249 1298 }
50 tests/suites/unit/joomla/database/database/JDatabaseQueryPostgresqlInspector.php
... ... @@ -0,0 +1,50 @@
  1 +<?php
  2 +/**
  3 + * @package Joomla.UnitTest
  4 + * @subpackage Database
  5 + *
  6 + * @copyright Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.
  7 + * @license GNU General Public License version 2 or later; see LICENSE.txt
  8 + */
  9 +
  10 +require_once JPATH_PLATFORM . '/joomla/database/query/postgresql.php';
  11 +
  12 +/**
  13 + * Class to expose protected properties and methods in JDatabaseQuery for testing purposes.
  14 + *
  15 + * @package Joomla.UnitTest
  16 + * @subpackage Database
  17 + *
  18 + * @since 11.1
  19 + */
  20 +class JDatabaseQueryPostgresqlInspector extends JDatabaseQueryPostgresql
  21 +{
  22 + /**
  23 + * Sets any property from the class.
  24 + *
  25 + * @param string $property The name of the class property.
  26 + * @param string $value The value of the class property.
  27 + *
  28 + * @return void
  29 + *
  30 + * @since 11.1
  31 + */
  32 + public function __set($property, $value)
  33 + {
  34 + return $this->$property = $value;
  35 + }
  36 +
  37 + /**
  38 + * Gets any property from the class.
  39 + *
  40 + * @param string $property The name of the class property.
  41 + *
  42 + * @return mixed The value of the class property.
  43 + *
  44 + * @since 11.1
  45 + */
  46 + public function get($property)
  47 + {
  48 + return $this->$property;
  49 + }
  50 +}
50 tests/suites/unit/joomla/database/database/JDatabaseQuerySqliteInspector.php
... ... @@ -0,0 +1,50 @@
  1 +<?php
  2 +/**
  3 + * @package Joomla.UnitTest
  4 + * @subpackage Database
  5 + *
  6 + * @copyright Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.
  7 + * @license GNU General Public License version 2 or later; see LICENSE.txt
  8 + */
  9 +
  10 +require_once JPATH_PLATFORM . '/joomla/database/query/sqlite.php';
  11 +
  12 +/**
  13 + * Class to expose protected properties and methods in JDatabaseQuery for testing purposes.
  14 + *
  15 + * @package Joomla.UnitTest
  16 + * @subpackage Database
  17 + *
  18 + * @since 13.1
  19 + */
  20 +class JDatabaseQuerySqliteInspector extends JDatabaseQuerySqlite
  21 +{
  22 + /**
  23 + * Sets any property from the class.
  24 + *
  25 + * @param string $property The name of the class property.
  26 + * @param string $value The value of the class property.
  27 + *
  28 + * @return void
  29 + *
  30 + * @since 13.1
  31 + */
  32 + public function __set($property, $value)
  33 + {
  34 + return $this->$property = $value;
  35 + }
  36 +
  37 + /**
  38 + * Gets any property from the class.
  39 + *
  40 + * @param string $property The name of the class property.
  41 + *
  42 + * @return mixed The value of the class property.
  43 + *
  44 + * @since 13.1
  45 + */
  46 + public function get($property)
  47 + {
  48 + return $this->$property;
  49 + }
  50 +}
51 tests/suites/unit/joomla/database/database/JDatabaseQuerySqlsrvInspector.php
... ... @@ -0,0 +1,51 @@
  1 +<?php
  2 +/**
  3 + * @package Joomla.UnitTest
  4 + * @subpackage Database
  5 + *
  6 + * @copyright Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.
  7 + * @license GNU General Public License version 2 or later; see LICENSE.txt
  8 + */
  9 +
  10 +require_once JPATH_PLATFORM . '/joomla/database/query/sqlsrv.php';
  11 +
  12 +/**
  13 + * Class to expose protected properties and methods in JDatabaseQuery for testing purposes.
  14 + *
  15 + * @package Joomla.UnitTest
  16 + * @subpackage Database
  17 + *
  18 + * @since 13.1
  19 + */
  20 +class JDatabaseQuerySqlsrvInspector extends JDatabaseQuerySqlsrv
  21 +{
  22 + /**
  23 + * Sets any property from the class.
  24 + *
  25 + * @param string $property The name of the class property.
  26 + * @param string $value The value of the class property.
  27 + *
  28 + * @return void
  29 + *
  30 + * @since 13.1
  31 + */
  32 + public function __set($property, $value)
  33 + {
  34 + return $this->$property = $value;
  35 + }
  36 +
  37 + /**
  38 + * Gets any property from the class.
  39 + *
  40 + * @param string $property The name of the class property.
  41 + *
  42 + * @return mixed The value of the class property.
  43 + *
  44 + * @since 13.1
  45 + */
  46 + public function get($property)
  47 + {
  48 + return $this->$property;
  49 + }
  50 +}
  51 +
93 tests/suites/unit/joomla/database/database/JDatabaseSqliteQueryTest.php
... ... @@ -0,0 +1,93 @@
  1 +<?php
  2 +/**
  3 + * @package Joomla.UnitTest
  4 + * @subpackage Database
  5 + *
  6 + * @copyright Copyright (C) 2005 - 2013 Open Source Matters. All rights reserved.
  7 + * @license GNU General Public License version 2 or later; see LICENSE.txt
  8 + */
  9 +
  10 +require_once __DIR__ . '/JDatabaseQuerySqliteInspector.php';
  11 +require_once JPATH_PLATFORM . '/joomla/database/query/sqlite.php';
  12 +
  13 +/**
  14 + * Test class for JDatabaseSqliteQuery.
  15 + *
  16 + * @package Joomla.UnitTest
  17 + * @subpackage Database
  18 + *
  19 + * @since 11.3
  20 + */
  21 +class JDatabaseSqliteQueryTest extends TestCase
  22 +{
  23 + /**
  24 + * @var JDatabase A mock of the JDatabase object for testing purposes.
  25 + */
  26 + protected $dbo;
  27 +
  28 + /**
  29 + * The instance of the object to test.
  30 + *
  31 + * @var JDatabaseSqliteQuery
  32 + * @since 12.3
  33 + */
  34 + private $_instance;
  35 +
  36 +
  37 + /**
  38 + * Sets up the fixture.
  39 + *
  40 + * This method is called before a test is executed.
  41 + *
  42 + * @return void
  43 + *
  44 + * @since 13.1
  45 + */
  46 + protected function setUp()
  47 + {
  48 + parent::setUp();
  49 +
  50 + $this->dbo = $this->getMockDatabase();
  51 +
  52 + $this->_instance = new JDatabaseQuerySqliteInspector($this->dbo);
  53 + }
  54 +
  55 +/**
  56 + * Data for the testDateAdd test.
  57 + *
  58 + * @return array
  59 + *
  60 + * @since 13.1
  61 + */
  62 + public function seedDateAdd()
  63 + {
  64 + return array(
  65 + // date, interval, datepart, expected
  66 + 'Add date' => array('2008-12-31', '1', 'DAY', "datetime('2008-12-31', '+1 DAY')"),
  67 + 'Subtract date' => array('2008-12-31', '-1', 'DAY', "datetime('2008-12-31', '-1 DAY')"),
  68 + 'Add datetime' => array('2008-12-31 23:59:59', '1', 'DAY', "datetime('2008-12-31 23:59:59', '+1 DAY')"),
  69 + 'Add microseconds' => array('2008-12-31 23:59:59', '53', 'microseconds', "datetime('2008-12-31 23:59:59', '+0.053 seconds')"),
  70 + );
  71 + }
  72 +
  73 + /**
  74 + * Tests the JDatabaseSqliteQuery::DateAdd method
  75 + *
  76 + * @param datetime $date The date or datetime to add to.
  77 + * @param string $interval The maximum length of the text.
  78 + * @param string $datePart The part of the date to be added to (such as day or micosecond)
  79 + * @param string $expected The expected result.
  80 + *
  81 + * @return void
  82 + *
  83 + * @dataProvider seedDateAdd
  84 + * @since 13.1
  85 + */
  86 + public function testDateAdd($date, $interval, $datePart, $expected)
  87 + {
  88 + $this->assertThat(
  89 + $this->_instance->dateAdd($date, $interval, $datePart),
  90 + $this->equalTo($expected)
  91 + );
  92 + }
  93 +}
91 tests/suites/unit/joomla/database/database/JDatabaseSqlsrvQueryTest.php
... ... @@ -0,0 +1,91 @@
  1 +<?php
  2 +/**
  3 + * @package Joomla.UnitTest
  4 + * @subpackage Database
  5 + *
  6 + * @copyright Copyright (C) 2005 - 2013 Open Source Matters. All rights reserved.
  7 + * @license GNU General Public License version 2 or later; see LICENSE.txt
  8 + */
  9 +
  10 +require_once __DIR__ . '/JDatabaseQuerySqlsrvInspector.php';
  11 +require_once JPATH_PLATFORM . '/joomla/database/query/sqlsrv.php';
  12 +
  13 +/**
  14 + * Test class for JDatabaseSqlsrvQuery.
  15 +*
  16 +* @package Joomla.UnitTest
  17 +* @subpackage Database
  18 +*
  19 +* @since 11.3
  20 +*/
  21 +class JDatabaseSqlsrvQueryTest extends TestCase
  22 +{
  23 + /**
  24 + * @var JDatabase A mock of the JDatabase object for testing purposes.
  25 + */
  26 + protected $dbo;
  27 +
  28 + /**
  29 + * The instance of the object to test.
  30 + *
  31 + * @var JDatabaseSqlsrvQuery
  32 + * @since 12.3
  33 + */
  34 + private $_instance;
  35 +
  36 + /**
  37 + * Sets up the fixture.
  38 + *
  39 + * This method is called before a test is executed.
  40 + *
  41 + * @return void
  42 + *
  43 + * @since 13.1
  44 + */
  45 + protected function setUp()
  46 + {
  47 + parent::setUp();
  48 +
  49 + $this->dbo = $this->getMockDatabase();
  50 +
  51 + $this->_instance = new JDatabaseQuerySqlsrvInspector($this->dbo);
  52 + }
  53 +
  54 + /**
  55 + * Data for the testDateAdd test.
  56 + *
  57 + * @return array
  58 + *
  59 + * @since 13.1
  60 + */
  61 + public function seedDateAdd()
  62 + {
  63 + return array(
  64 + // date, interval, datepart, expected
  65 + 'Add date' => array('2008-12-31', '1', 'day', "DATEADD('day', '1', '2008-12-31')"),
  66 + 'Subtract date' => array('2008-12-31', '-1', 'day', "DATEADD('day', '-1', '2008-12-31')"),
  67 + 'Add datetime' => array('2008-12-31 23:59:59', '1', 'day', "DATEADD('day', '1', '2008-12-31 23:59:59')"),
  68 + );
  69 + }
  70 +
  71 + /**
  72 + * Tests the JDatabaseSqlsrvQuery::DateAdd method
  73 + *
  74 + * @param datetime $date The date or datetime to add to.
  75 + * @param string $interval The maximum length of the text.
  76 + * @param string $datePart The part of the date to be added to (such as day or micosecond)
  77 + * @param string $expected The expected result.
  78 + *
  79 + * @return void
  80 + *
  81 + * @dataProvider seedDateAdd
  82 + * @since 13.1
  83 + */
  84 + public function testDateAdd($date, $interval, $datePart, $expected)
  85 + {
  86 + $this->assertThat(
  87 + $this->_instance->dateAdd($date, $interval, $datePart),
  88 + $this->equalTo($expected)
  89 + );
  90 + }
  91 +}

0 comments on commit d0ec462

Please sign in to comment.
Something went wrong with that request. Please try again.