Hi,
2009/3/5 Jeremy Evans <[email protected]>:
>> I think that it's convenience to handle it as `no entry is rejected.'
(snip)
> I'm open to this idea. Implementation might be difficult without
> setting up a special case, though, which I try to avoid. If you want
> to look at the code for Dataset#filter and Dataset#filter_expr, and
> see if you can think of an elegant way to do what you want, that would
> be nice. Otherwise, a simple patch that just returns an unmodified
> clone in case an empty hash or array is used should be fine.
Thank you for your consideration. How about this?
irb(main):001:0> DB[:foo].where({}).sql
=> "SELECT * FROM `foo` WHERE 1"
irb(main):002:0> DB[:foo].where(~{}).sql
=> "SELECT * FROM `foo` WHERE 0"
Because I'm not familiar with SQL, I don't know if such translation
can be used with any DB library. But it worked with sqlite3-ruby.
diff --git a/lib/sequel_core/dataset/sql.rb b/lib/sequel_core/dataset/sql.rb
index 7aeb0f7..eab3a14 100644
--- a/lib/sequel_core/dataset/sql.rb
+++ b/lib/sequel_core/dataset/sql.rb
@@ -7,7 +7,8 @@ module Sequel
COLUMN_REF_RE2 = /\A([\w ]+)___([\w ]+)\z/.freeze
COLUMN_REF_RE3 = /\A([\w ]+)__([\w ]+)\z/.freeze
COUNT_FROM_SELF_OPTS = [:distinct, :group, :sql, :limit, :compounds]
- N_ARITY_OPERATORS = ::Sequel::SQL::ComplexExpression::N_ARITY_OPERATORS
+ ZERO_OR_MORE_ARITY_OPERATORS =
::Sequel::SQL::ComplexExpression::ZERO_OR_MORE_ARITY_OPERATORS
+ ONE_OR_MORE_ARITY_OPERATORS =
::Sequel::SQL::ComplexExpression::ONE_OR_MORE_ARITY_OPERATORS
NULL = "NULL".freeze
QUESTION_MARK = '?'.freeze
STOCK_COUNT_OPTS = {:select =>
[LiteralString.new("COUNT(*)").freeze], :order => nil}.freeze
@@ -58,7 +59,13 @@ module Sequel
case op
when *TWO_ARITY_OPERATORS
"(#{literal(args.at(0))} #{op} #{literal(args.at(1))})"
- when *N_ARITY_OPERATORS
+ when *ZERO_OR_MORE_ARITY_OPERATORS
+ if args.empty?
+ op == :AND ? "1" : "0"
+ else
+ "(#{args.collect{|a| literal(a)}.join(" #{op} ")})"
+ end
+ when *ONE_OR_MORE_ARITY_OPERATORS
"(#{args.collect{|a| literal(a)}.join(" #{op} ")})"
when :NOT
"NOT #{literal(args.at(0))}"
diff --git a/lib/sequel_core/sql.rb b/lib/sequel_core/sql.rb
index 48ab0cf..68c335f 100644
--- a/lib/sequel_core/sql.rb
+++ b/lib/sequel_core/sql.rb
@@ -52,8 +52,11 @@ module Sequel
:~, :'!~', :'~*', :'!~*', :IN, :'NOT IN', :ILIKE, :'NOT ILIKE'] + \
INEQUALITY_OPERATORS + BITWISE_OPERATORS
+ # Operator symbols that take zero or more arguments
+ ZERO_OR_MORE_ARITY_OPERATORS = [:AND, :OR, :'||']
+
# Operator symbols that take one or more arguments
- N_ARITY_OPERATORS = [:AND, :OR, :'||'] + MATHEMATICAL_OPERATORS
+ ONE_OR_MORE_ARITY_OPERATORS = MATHEMATICAL_OPERATORS
# Operator symbols that take one argument
ONE_ARITY_OPERATORS = [:NOT, :NOOP, :'B~']
@@ -80,7 +83,8 @@ module Sequel
end
end
case op
- when *N_ARITY_OPERATORS
+ when *ZERO_OR_MORE_ARITY_OPERATORS
+ when *ONE_OR_MORE_ARITY_OPERATORS
raise(Error, "The #{op} operator requires at least 1
argument") unless args.length >= 1
when *TWO_ARITY_OPERATORS
raise(Error, "The #{op} operator requires precisely 2
arguments") unless args.length == 2
diff --git a/spec/sequel_core/dataset_spec.rb b/spec/sequel_core/dataset_spec.rb
index ac40702..f46a1ae 100644
--- a/spec/sequel_core/dataset_spec.rb
+++ b/spec/sequel_core/dataset_spec.rb
@@ -735,8 +735,8 @@ context "Dataset#literal" do
@dataset.literal(:items__name).should == "items.name"
end
- specify "should raise an error for unsupported types" do
- proc {[email protected]({})}.should raise_error
+ specify "should literalize a hash properly" do
+ @dataset.literal({}).should == "1"
end
specify "should literalize datasets as subqueries" do
--
Yusuke ENDOH <[email protected]>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---