Skip to content

Commit

Permalink
added full_text_select method - the ability to add a full_text query …
Browse files Browse the repository at this point in the history
…to the select statement.
  • Loading branch information
Dusty Doris committed Apr 10, 2009
1 parent ac1976a commit 6879e5e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
30 changes: 28 additions & 2 deletions lib/sequel/adapters/shared/mysql.rb
Expand Up @@ -188,8 +188,34 @@ def distinct(*columns)
super
end

# MySQL specific full text search syntax.
# Adds full text search in WHERE clause
def full_text_search(cols, terms, opts = {})
filter(full_text_sql(cols, terms, opts))
end

# Adds full text search in SELECT clause
def full_text_select(cols, terms, opts = {})
# You must provide the :as argument to opts
as = opts[:as] || raise(Error, "Must provide :as option")

# Sequel defines * to be default select options if none have
# been previously defined. We will be defining a select
# for full_text, so we need to add back the * if it
# hasn't already been removed by a custom select
orig_select = (@opts[:select].nil? || @opts[:select].empty?) ?
Sequel::Dataset::WILDCARD.lit : nil

# Select more will add the select given to the already created
# selects. If none have been given it will be *, MATCH..AS.
# Otherwise, select what has previously been declared and add
# the full_text MATCH to it
select_more(
*[orig_select,full_text_sql(cols,terms,opts).lit.as(as)].compact
)
end

# MySQL specific full text search syntax.
def full_text_sql(cols, terms, opts = {})
mode = opts[:boolean] ? " IN BOOLEAN MODE" : ""
s = if Array === terms
if mode.empty?
Expand All @@ -200,7 +226,7 @@ def full_text_search(cols, terms, opts = {})
else
"MATCH #{literal(Array(cols))} AGAINST (#{literal(terms)}#{mode})"
end
filter(s)
s
end

# MySQL allows HAVING clause on ungrouped datasets.
Expand Down
17 changes: 16 additions & 1 deletion spec/adapters/mysql_spec.rb
Expand Up @@ -612,7 +612,7 @@ def MYSQL_DB.ret_commit
]
end

specify "should support full_text_search" do
specify "should support full_text_search in WHERE clause" do
MYSQL_DB[:posts].full_text_search(:title, 'ruby').sql.should ==
"SELECT * FROM posts WHERE (MATCH (title) AGAINST ('ruby'))"

Expand All @@ -622,6 +622,21 @@ def MYSQL_DB.ret_commit
MYSQL_DB[:posts].full_text_search(:title, '+ruby -rails', :boolean => true).sql.should ==
"SELECT * FROM posts WHERE (MATCH (title) AGAINST ('+ruby -rails' IN BOOLEAN MODE))"
end

specify "should support full_text_search in SELECT CLAUSE" do
# Should add full_text_search to * when select is not defined
MYSQL_DB[:posts].full_text_select(:title, 'ruby', :as => :score).sql.should ==
"SELECT *, MATCH (title) AGAINST ('ruby') AS score FROM posts"

# Should add full_text_search to custom select
MYSQL_DB[:posts].select(:title).full_text_select(:title, 'ruby', :as => :score).sql.should ==
"SELECT title, MATCH (title) AGAINST ('ruby') AS score FROM posts"

# Should raise Error when :as option is not provided
lambda {
MYSQL_DB[:posts].full_text_select(:title, 'ruby').sql
}.should raise_error(Sequel::Error)
end

specify "should support spatial indexes" do
g = Sequel::Schema::Generator.new(MYSQL_DB) do
Expand Down

0 comments on commit 6879e5e

Please sign in to comment.