Skip to content

Commit

Permalink
Merge pull request #44 from PostgreSQL-For-Wordpress/mb-fix-insert-ca…
Browse files Browse the repository at this point in the history
…ching

correct regex for insert matching
  • Loading branch information
mattbucci authored Oct 31, 2023
2 parents 974ccf1 + ae2cbff commit 4d7e4e8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
24 changes: 16 additions & 8 deletions pg4wp/driver_pgsql_rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,26 @@ function pg4wp_rewrite($sql)
// Put back the end of the query if it was separated
$sql .= $end;

// For insert ID catching
// For insert ID caching
if($logto == 'INSERT') {
$pattern = '/INSERT INTO (\w+)\s+\([ a-zA-Z_"]+/';
$pattern = '/INSERT INTO "?([\w_]+)"? \(([^)]+)\)/i';
preg_match($pattern, $sql, $matches);
$GLOBALS['pg4wp_ins_table'] = $matches[1];
$match_list = explode(' ', $matches[0]);
if($GLOBALS['pg4wp_ins_table']) {
$GLOBALS['pg4wp_ins_field'] = trim($match_list[3], ' () ');
if(!$GLOBALS['pg4wp_ins_field']) {
$GLOBALS['pg4wp_ins_field'] = trim($match_list[4], ' () ');

if (isset($matches[1])) {
$GLOBALS['pg4wp_ins_table'] = $matches[1];
}

if (isset($matches[2])) {
$columns_str = $matches[2];
$columns = explode(',', $columns_str);
$columns = array_map(function ($column) {
return trim(trim($column), '"');
}, $columns);
if (isset($columns[0])) {
$GLOBALS['pg4wp_ins_field'] = $columns[0];
}
}

$GLOBALS['pg4wp_last_insert'] = $sql;
} elseif(isset($GLOBALS['pg4wp_queued_query'])) {
pg_query($GLOBALS['pg4wp_queued_query']);
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ PG4WP 2.0 will have a mechanism to add plugin support.
| Theme | Version | Working |
| ----------- | ----------- | --------- |
| Twenty Twenty-Three | 1.2 | Confirmed |
| Twenty Twenty-Two | 1.5 | Confirmed |
| Twenty Twenty-One | 1.9 | Confirmed |

### Installation

Expand Down
44 changes: 44 additions & 0 deletions tests/parseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

if (!defined('ABSPATH')) {
define('ABSPATH', __DIR__ . "/../");
}

if (!defined('WPINC')) {
define('WPINC', 'wp-includes');
}

require_once __DIR__ . "/../pg4wp/db.php";

final class parseTest extends TestCase
{
public function test_it_can_parse_a_theme_change_correctly()
{
$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"';
$postgresql = pg4wp_rewrite($sql);
$this->assertSame($GLOBALS['pg4wp_ins_table'], "wp_options");
$this->assertSame($GLOBALS['pg4wp_ins_field'], "option_name");
}


public function test_it_can_parse_a_page_creation_correctly()
{

$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, \'\', \'\')';
$postgresql = pg4wp_rewrite($sql);
$this->assertSame($GLOBALS['pg4wp_ins_table'], "wp_posts");
$this->assertSame($GLOBALS['pg4wp_ins_field'], "post_author");
}


protected function setUp(): void
{
global $wpdb;
$wpdb = new class() {
public $options = "wp_options";
public $categories = "wp_categories";
};
}

}
10 changes: 10 additions & 0 deletions tests/verifyAgainstStubsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public function test_verify_against_stubs()
$data = json_decode(file_get_contents(self::STUBS_DIRECTORY . "/" . $file), true);
$this->assertSame(pg4wp_rewrite($data['mysql']), $data['postgresql']);
}
}

protected function setUp(): void
{
global $wpdb;
$wpdb = new class() {
public $categories = "wp_categories";
public $comments = "wp_comments";
public $prefix = "wp_";
public $options = "wp_options";
};
}
}

0 comments on commit 4d7e4e8

Please sign in to comment.