Skip to content

Commit 4d7e4e8

Browse files
authored
Merge pull request #44 from PostgreSQL-For-Wordpress/mb-fix-insert-caching
correct regex for insert matching
2 parents 974ccf1 + ae2cbff commit 4d7e4e8

4 files changed

Lines changed: 72 additions & 8 deletions

File tree

pg4wp/driver_pgsql_rewrite.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,26 @@ function pg4wp_rewrite($sql)
7171
// Put back the end of the query if it was separated
7272
$sql .= $end;
7373

74-
// For insert ID catching
74+
// For insert ID caching
7575
if($logto == 'INSERT') {
76-
$pattern = '/INSERT INTO (\w+)\s+\([ a-zA-Z_"]+/';
76+
$pattern = '/INSERT INTO "?([\w_]+)"? \(([^)]+)\)/i';
7777
preg_match($pattern, $sql, $matches);
78-
$GLOBALS['pg4wp_ins_table'] = $matches[1];
79-
$match_list = explode(' ', $matches[0]);
80-
if($GLOBALS['pg4wp_ins_table']) {
81-
$GLOBALS['pg4wp_ins_field'] = trim($match_list[3], ' () ');
82-
if(!$GLOBALS['pg4wp_ins_field']) {
83-
$GLOBALS['pg4wp_ins_field'] = trim($match_list[4], ' () ');
78+
79+
if (isset($matches[1])) {
80+
$GLOBALS['pg4wp_ins_table'] = $matches[1];
81+
}
82+
83+
if (isset($matches[2])) {
84+
$columns_str = $matches[2];
85+
$columns = explode(',', $columns_str);
86+
$columns = array_map(function ($column) {
87+
return trim(trim($column), '"');
88+
}, $columns);
89+
if (isset($columns[0])) {
90+
$GLOBALS['pg4wp_ins_field'] = $columns[0];
8491
}
8592
}
93+
8694
$GLOBALS['pg4wp_last_insert'] = $sql;
8795
} elseif(isset($GLOBALS['pg4wp_queued_query'])) {
8896
pg_query($GLOBALS['pg4wp_queued_query']);

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ PG4WP 2.0 will have a mechanism to add plugin support.
4242
| Theme | Version | Working |
4343
| ----------- | ----------- | --------- |
4444
| Twenty Twenty-Three | 1.2 | Confirmed |
45+
| Twenty Twenty-Two | 1.5 | Confirmed |
46+
| Twenty Twenty-One | 1.9 | Confirmed |
4547

4648
### Installation
4749

tests/parseTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types=1);
2+
use PHPUnit\Framework\TestCase;
3+
4+
if (!defined('ABSPATH')) {
5+
define('ABSPATH', __DIR__ . "/../");
6+
}
7+
8+
if (!defined('WPINC')) {
9+
define('WPINC', 'wp-includes');
10+
}
11+
12+
require_once __DIR__ . "/../pg4wp/db.php";
13+
14+
final class parseTest extends TestCase
15+
{
16+
public function test_it_can_parse_a_theme_change_correctly()
17+
{
18+
$sql = 'INSERT INTO "wp_options" ("option_name", "option_value", "autoload") VALUES (\'theme_switch_menu_locations\', \'a:0:{}\', \'yes\') ON CONFLICT ("option_name") DO UPDATE SET "option_name" = EXCLUDED."option_name", "option_value" = EXCLUDED."option_value", "autoload" = EXCLUDED."autoload"';
19+
$postgresql = pg4wp_rewrite($sql);
20+
$this->assertSame($GLOBALS['pg4wp_ins_table'], "wp_options");
21+
$this->assertSame($GLOBALS['pg4wp_ins_field'], "option_name");
22+
}
23+
24+
25+
public function test_it_can_parse_a_page_creation_correctly()
26+
{
27+
28+
$sql = 'INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type, guid) VALUES (\'1\', \'2023-10-31 03:54:02\', now() AT TIME ZONE \'gmt\', \'\', \'\', \'Auto Draft\', \'\', \'auto-draft\', \'page\', \'closed\', \'closed\', \'\', \'\', \'\', \'\', \'2023-10-31 03:54:02\', now() AT TIME ZONE \'gmt\', 0, 0, \'\', \'\')';
29+
$postgresql = pg4wp_rewrite($sql);
30+
$this->assertSame($GLOBALS['pg4wp_ins_table'], "wp_posts");
31+
$this->assertSame($GLOBALS['pg4wp_ins_field'], "post_author");
32+
}
33+
34+
35+
protected function setUp(): void
36+
{
37+
global $wpdb;
38+
$wpdb = new class() {
39+
public $options = "wp_options";
40+
public $categories = "wp_categories";
41+
};
42+
}
43+
44+
}

tests/verifyAgainstStubsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public function test_verify_against_stubs()
2222
$data = json_decode(file_get_contents(self::STUBS_DIRECTORY . "/" . $file), true);
2323
$this->assertSame(pg4wp_rewrite($data['mysql']), $data['postgresql']);
2424
}
25+
}
2526

27+
protected function setUp(): void
28+
{
29+
global $wpdb;
30+
$wpdb = new class() {
31+
public $categories = "wp_categories";
32+
public $comments = "wp_comments";
33+
public $prefix = "wp_";
34+
public $options = "wp_options";
35+
};
2636
}
2737
}

0 commit comments

Comments
 (0)