Navigation Menu

Skip to content

Commit

Permalink
Support call expression
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Oct 6, 2015
1 parent d97b934 commit 3cec5bc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
28 changes: 27 additions & 1 deletion lib/groonga/expression-builder.rb
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Masafumi Yokoyama <yokoyama@clear-code.com>
# Copyright (C) 2009-2012 Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2009-2015 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
Expand Down Expand Up @@ -448,6 +448,28 @@ def initialize(column_value_builder, value)
super(Groonga::Operation::TERM_EXTRACT, column_value_builder, value)
end
end

# @private
class CallExpressionBuilder < ExpressionBuilder
def initialize(function, *arguments)
super()
@function = function
@arguments = arguments
end

def build(expression, variable)
expression.append_object(@function)
@arguments.each do |argument|
case argument
when String
expression.append_constant(argument)
else
argument.build(expression, variable)
end
end
expression.append_operation(Operation::CALL, @arguments.size)
end
end
end

# @private
Expand Down Expand Up @@ -516,6 +538,10 @@ def index(name)
column_expression_builder(object, name)
end

def call(function, *arguments)
CallExpressionBuilder.new(@table.context[function], *arguments)
end

private
def build_match_target(&block)
sub_builder = MatchTargetRecordExpressionBuilder.new(@table, nil)
Expand Down
39 changes: 38 additions & 1 deletion test/test-expression-builder.rb
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014-2015 Masafumi Yokoyama <yokoyama@clear-code.com>
# Copyright (C) 2009-2012 Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2009-2015 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
Expand Down Expand Up @@ -523,6 +523,43 @@ def test_not_key_column
end
end

class CallTest < self
def setup_tables
Groonga::Schema.define do |schema|
schema.create_table("Shops",
:type => :hash,
:key_type => "ShortText") do |table|
table.wgs84_geo_point("location")
end

schema.create_table("Locations",
:type => :patricia_trie,
:key_type => :wgs84_geo_point) do |table|
table.index("Shops.location")
end
end

@shops = Groonga["Shops"]
end

def setup_data
@shops.add("Nezu no taiyaki", :location => "35.720253,139.762573")
@shops.add("Taiyaki Kataoka", :location => "35.712521,139.715591")
@shops.add("Taiyaki Sharaku", :location => "35.716969,139.794846")
end

def test_search
result = @shops.select do |record|
record.call("geo_in_rectangle",
record.location,
"35.7185,139.7912",
"35.7065,139.8069")
end
assert_equal(["Taiyaki Sharaku"],
result.collect(&:_key))
end
end

class RecordTest < self
def setup_tables
Groonga::Schema.define do |schema|
Expand Down

0 comments on commit 3cec5bc

Please sign in to comment.