Skip to content

Commit

Permalink
Add support for DATE_ADD and DATE_SUB functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JanJakes committed Jan 10, 2025
1 parent f29464c commit 8cfe7f4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
88 changes: 88 additions & 0 deletions tests/WP_SQLite_Driver_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,94 @@ public function testSelectIndexHintUseGroup() {
$this->assertEquals( 1, $result[0]->output );
}

public function testDateAddFunction() {
// second
$result = $this->assertQuery(
'SELECT DATE_ADD("2008-01-02 13:29:17", INTERVAL 1 SECOND) as output'
);
$this->assertEquals( '2008-01-02 13:29:18', $result[0]->output );

// minute
$result = $this->assertQuery(
'SELECT DATE_ADD("2008-01-02 13:29:17", INTERVAL 1 MINUTE) as output'
);
$this->assertEquals( '2008-01-02 13:30:17', $result[0]->output );

// hour
$result = $this->assertQuery(
'SELECT DATE_ADD("2008-01-02 13:29:17", INTERVAL 1 HOUR) as output'
);
$this->assertEquals( '2008-01-02 14:29:17', $result[0]->output );

// day
$result = $this->assertQuery(
'SELECT DATE_ADD("2008-01-02 13:29:17", INTERVAL 1 DAY) as output'
);
$this->assertEquals( '2008-01-03 13:29:17', $result[0]->output );

// week
$result = $this->assertQuery(
'SELECT DATE_ADD("2008-01-02 13:29:17", INTERVAL 1 WEEK) as output'
);
$this->assertEquals( '2008-01-09 13:29:17', $result[0]->output );

// month
$result = $this->assertQuery(
'SELECT DATE_ADD("2008-01-02 13:29:17", INTERVAL 1 MONTH) as output'
);
$this->assertEquals( '2008-02-02 13:29:17', $result[0]->output );

// year
$result = $this->assertQuery(
'SELECT DATE_ADD("2008-01-02 13:29:17", INTERVAL 1 YEAR) as output'
);
$this->assertEquals( '2009-01-02 13:29:17', $result[0]->output );
}

public function testDateSubFunction() {
// second
$result = $this->assertQuery(
'SELECT DATE_SUB("2008-01-02 13:29:17", INTERVAL 1 SECOND) as output'
);
$this->assertEquals( '2008-01-02 13:29:16', $result[0]->output );

// minute
$result = $this->assertQuery(
'SELECT DATE_SUB("2008-01-02 13:29:17", INTERVAL 1 MINUTE) as output'
);
$this->assertEquals( '2008-01-02 13:28:17', $result[0]->output );

// hour
$result = $this->assertQuery(
'SELECT DATE_SUB("2008-01-02 13:29:17", INTERVAL 1 HOUR) as output'
);
$this->assertEquals( '2008-01-02 12:29:17', $result[0]->output );

// day
$result = $this->assertQuery(
'SELECT DATE_SUB("2008-01-02 13:29:17", INTERVAL 1 DAY) as output'
);
$this->assertEquals( '2008-01-01 13:29:17', $result[0]->output );

// week
$result = $this->assertQuery(
'SELECT DATE_SUB("2008-01-02 13:29:17", INTERVAL 1 WEEK) as output'
);
$this->assertEquals( '2007-12-26 13:29:17', $result[0]->output );

// month
$result = $this->assertQuery(
'SELECT DATE_SUB("2008-01-02 13:29:17", INTERVAL 1 MONTH) as output'
);
$this->assertEquals( '2007-12-02 13:29:17', $result[0]->output );

// year
$result = $this->assertQuery(
'SELECT DATE_SUB("2008-01-02 13:29:17", INTERVAL 1 YEAR) as output'
);
$this->assertEquals( '2007-01-02 13:29:17', $result[0]->output );
}

public function testLeftFunction1Char() {
$result = $this->assertQuery(
'SELECT LEFT("abc", 1) as output'
Expand Down
16 changes: 16 additions & 0 deletions wp-includes/sqlite-ast/class-wp-sqlite-driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,22 @@ private function translate_runtime_function_call( WP_Parser_Node $node ): string
}

Check failure on line 1450 in wp-includes/sqlite-ast/class-wp-sqlite-driver.php

View workflow job for this annotation

GitHub Actions / Check code style

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 1450 in wp-includes/sqlite-ast/class-wp-sqlite-driver.php

View workflow job for this annotation

GitHub Actions / Check code style

Tabs must be used to indent lines; spaces are not allowed

switch ( $child->id ) {

Check failure on line 1452 in wp-includes/sqlite-ast/class-wp-sqlite-driver.php

View workflow job for this annotation

GitHub Actions / Check code style

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 1452 in wp-includes/sqlite-ast/class-wp-sqlite-driver.php

View workflow job for this annotation

GitHub Actions / Check code style

Tabs must be used to indent lines; spaces are not allowed
case WP_MySQL_Lexer::DATE_ADD_SYMBOL:

Check failure on line 1453 in wp-includes/sqlite-ast/class-wp-sqlite-driver.php

View workflow job for this annotation

GitHub Actions / Check code style

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 1453 in wp-includes/sqlite-ast/class-wp-sqlite-driver.php

View workflow job for this annotation

GitHub Actions / Check code style

Tabs must be used to indent lines; spaces are not allowed
case WP_MySQL_Lexer::DATE_SUB_SYMBOL:

Check failure on line 1454 in wp-includes/sqlite-ast/class-wp-sqlite-driver.php

View workflow job for this annotation

GitHub Actions / Check code style

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 1454 in wp-includes/sqlite-ast/class-wp-sqlite-driver.php

View workflow job for this annotation

GitHub Actions / Check code style

Tabs must be used to indent lines; spaces are not allowed
$nodes = $node->get_child_nodes();
$value = $this->translate( $nodes[1] );
$unit = $this->translate( $nodes[2] );
if ( 'WEEK' === $unit ) {
$unit = 'DAY';

Check warning on line 1459 in wp-includes/sqlite-ast/class-wp-sqlite-driver.php

View workflow job for this annotation

GitHub Actions / Check code style

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

Check warning on line 1459 in wp-includes/sqlite-ast/class-wp-sqlite-driver.php

View workflow job for this annotation

GitHub Actions / Check code style

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
$value = 7 * $value;
}
return sprintf(
"DATETIME(%s, '%s%s %s')",
$this->translate( $nodes[0] ),
WP_MySQL_Lexer::DATE_SUB_SYMBOL === $child->id ? '-' : '+',
$value,
$unit
);
case WP_MySQL_Lexer::LEFT_SYMBOL:
$nodes = $node->get_child_nodes();
return sprintf(
Expand Down

0 comments on commit 8cfe7f4

Please sign in to comment.