Skip to content

Commit

Permalink
Switch to using to_tsquery
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk authored Mar 3, 2024
1 parent 6063920 commit f7748a9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 36 deletions.
34 changes: 1 addition & 33 deletions src/Functions/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,6 @@
/**
* "query" "(" Column, Query, Lang ")"
*/
final class Query extends FunctionNode
final class Query extends Search
{
public Node $column;
public Node $query;
public Node $lang;


public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER); // (2)
$parser->match(Lexer::T_OPEN_PARENTHESIS); // (3)

$this->column = $parser->StringPrimary(); // (4)

$parser->match(Lexer::T_COMMA); // (5)

$this->query = $parser->InstanceOfParameter(); // (6)

$parser->match(Lexer::T_COMMA); // (7)

$this->lang = $parser->StringPrimary(); // (8)

$parser->match(Lexer::T_CLOSE_PARENTHESIS); // (9)
}


public function getSql(SqlWalker $sqlWalker): string
{
$column = $sqlWalker->walkSimpleArithmeticExpression($this->column);
$query = $sqlWalker->walkSimpleArithmeticExpression($this->query);
$lang = $sqlWalker->walkSimpleArithmeticExpression($this->lang);

return "{$column} @@ to_tsquery({$lang}, {$query})";
}
}
2 changes: 1 addition & 1 deletion src/Functions/Rank.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public function getSql(SqlWalker $sqlWalker): string
$query = $sqlWalker->walkSimpleArithmeticExpression($this->query);
$lang = $sqlWalker->walkSimpleArithmeticExpression($this->lang);

return "ts_rank_cd({$column}, websearch_to_tsquery({$lang}, {$query}))";
return "ts_rank_cd({$column}, to_tsquery({$lang}, {$query}))";
}
}
4 changes: 2 additions & 2 deletions src/Functions/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* "search" "(" Column, Query, Lang ")"
*/
final class Search extends FunctionNode
class Search extends FunctionNode
{
public Node $column;
public Node $query;
Expand Down Expand Up @@ -48,6 +48,6 @@ public function getSql(SqlWalker $sqlWalker): string
$query = $sqlWalker->walkSimpleArithmeticExpression($this->query);
$lang = $sqlWalker->walkSimpleArithmeticExpression($this->lang);

return "{$column} @@ websearch_to_tsquery({$lang}, {$query})";
return "{$column} @@ to_tsquery({$lang}, {$query})";
}
}
87 changes: 87 additions & 0 deletions src/Utils/SearchQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php declare(strict_types=1);

/**
* @copyright Martin Procházka (c) 2024
* @license MIT License
*/

namespace JuniWalk\ORM\Utils;

use Stringable;

class SearchQuery implements Stringable
{
protected const CharFollow = '"';
protected const CharPartial = '*';

protected const ModifierPartial = ':*';

protected const MethodFollow = '<->';
protected const MethodAnd = '&';
protected const MethodOr = '|';

protected const TupleAnd = ['and', 'a', self::MethodAnd];
protected const TupleOr = ['or', 'nebo', self::MethodOr];

public function __construct(
protected string $query,
protected string $methodDefault = self::MethodAnd,
) {
}


public function __toString(): string
{
return $this->format();
}


public function format(): string
{
$tokens = preg_split('/\s+/', $this->query);
$lastKey = array_key_last($tokens);
$method = $this->methodDefault;
$output = '';

foreach ($tokens as $key => $token) {
$token = strtolower($token);

if (in_array($token, self::TupleAnd)) {
$output = substr($output, 0, -3).' '.self::MethodAnd.' ';
$method = $methodNext = $this->methodDefault;
continue;
}

if (in_array($token, self::TupleOr)) {
$output = substr($output, 0, -3).' '.self::MethodOr.' ';
$method = $methodNext = $this->methodDefault;
continue;
}

if (str_starts_with($token, self::CharFollow)) {
$token = substr($token, 1);
$methodNext = $method = self::MethodFollow;
}

if (str_ends_with($token, self::CharFollow)) {
$token = substr($token, 0, -1);
$method = $this->methodDefault;
}

if (str_ends_with($token, self::CharPartial)) {
$token = str_replace(self::CharPartial, self::ModifierPartial, $token);
}

$output .= $token;

if ($key <> $lastKey) {
$output .= ' '.$method.' ';
}

$methodNext ??= $method;
$method = $methodNext;
}

return $output;
}
}

0 comments on commit f7748a9

Please sign in to comment.