Skip to content

Commit

Permalink
Giving the tsvector and tsquery stuff a shot. Ref #353 (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoertink authored Apr 19, 2024
1 parent ce56ba6 commit db6ef66
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions spec/avram/string_criteria_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ describe String::Lucky::Criteria do
name.reverse.eq("revir").to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE REVERSE(users.name) = $1", "revir"]
end
end

describe "match" do
it "uses @@" do
name.to_tsvector.match("jeb").to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE TO_TSVECTOR(users.name) @@ TO_TSQUERY($1)", "jeb"]
end
end
end

private def name
Expand Down
6 changes: 6 additions & 0 deletions src/avram/charms/string_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ class String
add_clause(Avram::Where::Ilike.new(column, value))
end

def match(value : String) : T
add_clause(Avram::Where::TsMatch.new(column, value))
end

define_function_criteria(upper, V)
define_function_criteria(lower, V)
define_function_criteria(trim, String)
define_function_criteria(length, Int64)
define_function_criteria(reverse, String)
define_function_criteria(to_tsvector, String)
define_function_criteria(to_tsquery, String)
end
end
end
28 changes: 28 additions & 0 deletions src/avram/where.cr
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,34 @@ module Avram::Where
end
end

class TsMatch < ValueHoldingSqlClause
def operator : String
"@@"
end

def negated : TsNotMatch
TsNotMatch.new(column, value)
end

def prepare(placeholder_supplier : Proc(String)) : String
"#{column} #{operator} TO_TSQUERY(#{placeholder_supplier.call})"
end
end

class TsNotMatch < ValueHoldingSqlClause
def operator : String
"@@"
end

def negated : TsMatch
TsMatch.new(column, value)
end

def prepare(placeholder_supplier : Proc(String)) : String
"NOT(#{column} #{operator} TO_TSQUERY(#{placeholder_supplier.call}))"
end
end

class Raw < Condition
@clause : String

Expand Down

0 comments on commit db6ef66

Please sign in to comment.