From a6765201ce9101b6745dff28d56372e76fcf137f Mon Sep 17 00:00:00 2001 From: Matthew Bucci Date: Thu, 17 Oct 2024 02:54:44 -0700 Subject: [PATCH] rewrite utc timestamp requests --- pg4wp/rewriters/InsertSQLRewriter.php | 2 ++ pg4wp/rewriters/SelectSQLRewriter.php | 2 ++ tests/rewriteTest.php | 28 ++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pg4wp/rewriters/InsertSQLRewriter.php b/pg4wp/rewriters/InsertSQLRewriter.php index 89d28a3..e323c1e 100644 --- a/pg4wp/rewriters/InsertSQLRewriter.php +++ b/pg4wp/rewriters/InsertSQLRewriter.php @@ -11,6 +11,8 @@ class InsertSQLRewriter extends AbstractSQLRewriter // Those are used when we need to set the date to now() in gmt time $sql = str_replace("'0000-00-00 00:00:00'", 'now() AT TIME ZONE \'gmt\'', $sql); + $sql = str_replace("utc_timestamp()", "CURRENT_TIMESTAMP AT TIME ZONE 'UTC'", $sql); + // Multiple values group when calling INSERT INTO don't always work if(false !== strpos($sql, $wpdb->options) && false !== strpos($sql, '), (')) { $pattern = '/INSERT INTO.+VALUES/'; diff --git a/pg4wp/rewriters/SelectSQLRewriter.php b/pg4wp/rewriters/SelectSQLRewriter.php index bbe13d6..7f8899d 100644 --- a/pg4wp/rewriters/SelectSQLRewriter.php +++ b/pg4wp/rewriters/SelectSQLRewriter.php @@ -68,6 +68,8 @@ class SelectSQLRewriter extends AbstractSQLRewriter // HANDLE REGEXP $sql = preg_replace('/REGEXP/', '~', $sql); + $sql = str_replace("utc_timestamp()", "CURRENT_TIMESTAMP AT TIME ZONE 'UTC'", $sql); + // In order for users counting to work... $matches = array(); if(preg_match_all('/COUNT[^C]+\),/', $sql, $matches)) { diff --git a/tests/rewriteTest.php b/tests/rewriteTest.php index 9dfa187..7555682 100644 --- a/tests/rewriteTest.php +++ b/tests/rewriteTest.php @@ -757,7 +757,33 @@ final class rewriteTest extends TestCase $this->assertSame(trim($expected), trim($postgresql)); } - + public function test_it_rewrites_utc_timestamp_inserts() + { + $sql = <<assertSame(trim($expected), trim($postgresql)); + } + + public function test_it_rewrites_utc_timestamp_selects() + { + $sql = <<assertSame(trim($expected), trim($postgresql)); + } protected function setUp(): void {