lib/groonga/expression-builder.rb in rroonga-1.0.8 vs lib/groonga/expression-builder.rb in rroonga-1.0.9
- old
+ new
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
#
-# Copyright (C) 2009-2010 Kouhei Sutou <kou@clear-code.com>
+# Copyright (C) 2009-2011 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
@@ -37,11 +37,11 @@
@allow_update = nil
@default_column = nil
end
def build(&block)
- expression = Expression.new(:name => @name)
+ expression = Expression.new(:name => @name, :context => @table.context)
variable = expression.define_variable(:domain => @table)
build_expression(expression, variable, &block)
end
def &(other)
@@ -210,10 +210,18 @@
options[:syntax] ||= :query
options[:default_column] = @column_name
SubExpressionBuilder.new(query, options)
end
+ def prefix_search(other)
+ PrefixSearchExpressionBuilder.new(self, normalize(other))
+ end
+
+ def suffix_search(other)
+ SuffixSearchExpressionBuilder.new(self, normalize(other))
+ end
+
private
def normalize(other)
if @range.is_a?(Groonga::Table)
if other.respond_to?(:record_id)
id = other.record_id
@@ -361,10 +369,22 @@
def build(expression, variable)
expression.parse(@query, @options)
end
end
+
+ class PrefixSearchExpressionBuilder < BinaryExpressionBuilder # :nodoc:
+ def initialize(column_value_builder, value)
+ super(Groonga::Operation::PREFIX, column_value_builder, value)
+ end
+ end
+
+ class SuffixSearchExpressionBuilder < BinaryExpressionBuilder # :nodoc:
+ def initialize(column_value_builder, value)
+ super(Groonga::Operation::SUFFIX, column_value_builder, value)
+ end
+ end
end
class RecordExpressionBuilder # :nodoc:
include ExpressionBuildable
@@ -414,9 +434,21 @@
SubExpressionBuilder.new(query, options)
end
def match_target(&block)
MatchTargetExpressionBuilder.new(build_match_target(&block))
+ end
+
+ def index(name)
+ object = @table.context[name]
+ if object.nil?
+ raise ArgumentError, "unknown index column: <#{name}>"
+ end
+ if object.range != @table
+ raise ArgumentError,
+ "differenct index column: <#{name}>: #{object.inspect}"
+ end
+ column_expression_builder(object, name)
end
private
def build_match_target(&block)
sub_builder = MatchTargetRecordExpressionBuilder.new(@table, nil)