Skip to content

Commit b8d04ea

Browse files
committed
wip explorations
1 parent fe241bd commit b8d04ea

7 files changed

+821
-537
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
require_once __DIR__ . '/WP_SQLite_Translator_Tests.php';
4+
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
5+
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-expression.php';
6+
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-token-factory.php';
7+
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-token.php';
8+
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-query-builder.php';
9+
10+
use PHPUnit\Framework\TestCase;
11+
use WIP\WP_SQLite_Query_Builder;
12+
13+
class WP_SQLite_Driver_Translator_Tests extends TestCase {
14+
const GRAMMAR_PATH = __DIR__ . '/../wp-includes/mysql/mysql-grammar.php';
15+
16+
/**
17+
* @var WP_Parser_Grammar
18+
*/
19+
private static $grammar;
20+
21+
public static function setUpBeforeClass(): void {
22+
self::$grammar = new WP_Parser_Grammar( include self::GRAMMAR_PATH );
23+
}
24+
25+
public function testSelect(): void {
26+
$this->assertTranslation( 'SELECT 1', 'SELECT 1' );
27+
$this->assertTranslation( 'SELECT * FROM "t"', 'SELECT * FROM t' );
28+
$this->assertTranslation( 'SELECT "c" FROM "t"', 'SELECT c FROM t' );
29+
$this->assertTranslation( 'SELECT "t"."c" FROM "t"', 'SELECT t.c FROM t' );
30+
$this->assertTranslation(
31+
'SELECT * FROM "t1" LEFT JOIN "t2" ON "t1"."id" = "t2"."t1_id" WHERE "t1"."name" = \'abc\'',
32+
"SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.t1_id WHERE t1.name = 'abc'"
33+
);
34+
}
35+
36+
public function testInsert(): void {
37+
$this->assertTranslation( 'INSERT INTO "t" ("c") VALUES (1)', 'INSERT INTO t (c) VALUES (1)' );
38+
}
39+
40+
private function assertTranslation( string $expected, string $query ) {
41+
$lexer = new WP_MySQL_Lexer( $query );
42+
$tokens = $lexer->remaining_tokens();
43+
44+
$parser = new WP_MySQL_Parser( self::$grammar, $tokens );
45+
$ast = $parser->parse();
46+
47+
$translator = new WIP\WP_SQLite_Translator();
48+
$this->assertSame( $expected, WP_SQLite_Query_Builder::stringify( $translator->translate( $ast ) ) );
49+
}
50+
}

tests/tools/dump-sqlite-query.php

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
$driver = new WP_SQLite_Driver( new PDO( 'sqlite::memory:' ) );
1818

1919
$query = "SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.t1_id WHERE t1.name = 'abc'";
20+
//$query = 'CREATE TABLE t1 LIKE t2';
2021

2122
echo $driver->query( $query );

wp-includes/parser/class-wp-parser-node.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ public function get_token( $token_id = null ) {
135135
return null;
136136
}
137137

138+
public function get_descendant_token( $token_id = null ) {
139+
$parse_trees = array( $this );
140+
while ( count( $parse_trees ) ) {
141+
$parse_tree = array_pop( $parse_trees );
142+
$token = $parse_tree->get_token( $token_id );
143+
if ( $token ) {
144+
return $token;
145+
}
146+
array_push( $parse_trees, ...$parse_tree->get_children() );
147+
}
148+
return null;
149+
}
150+
138151
public function get_child( $rule_name = null ) {
139152
foreach ( $this->children as $child ) {
140153
if ( $child instanceof WP_Parser_Node && (
@@ -158,7 +171,7 @@ public function get_descendant( $rule_name ) {
158171
return null;
159172
}
160173

161-
public function get_descendants( $rule_name ) {
174+
public function get_descendants( $rule_name = null ) {
162175
$parse_trees = array( $this );
163176
$all_descendants = array();
164177
while ( count( $parse_trees ) ) {

0 commit comments

Comments
 (0)