Skip to content

Commit

Permalink
Add query_decorator to builder (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
xronos-i-am committed Jun 30, 2020
1 parent b96bca3 commit 877b35c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
10 changes: 10 additions & 0 deletions lib/mini_sql/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ def #{m}(hash_args = nil)
RUBY
end

def query_decorator(decorator, hash_args = nil)
hash_args = @args.merge(hash_args) if hash_args && @args
hash_args ||= @args
if hash_args
@connection.query_decorator(decorator, to_sql, hash_args)
else
@connection.query_decorator(decorator, to_sql)
end
end

end
19 changes: 13 additions & 6 deletions test/mini_sql/builder_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ def test_set
assert_equal([7, 8], @connection.query_single("select * from ta"))
end

def test_accepts_params_at_end
builder = @connection.build("select :bob as a")
r = builder.query_hash(bob: 1)
assert_equal([{ "a" => 1 }], r)
end

def test_accepts_params_at_end
builder = @connection.build("select :bob as a /*where*/")
builder.where('1 = :one', one: 1)
Expand All @@ -93,4 +87,17 @@ def test_offset_limit
builder.offset(1)
assert_equal([2], builder.query_single)
end

module ProductDecorator
def amount_price
price * quantity
end
end

def test_query_decorator
builder = @connection.build("select 20 price, 3 quantity")

r = builder.query_decorator(ProductDecorator).first
assert_equal(60, r.amount_price)
end
end

0 comments on commit 877b35c

Please sign in to comment.