Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Sequel.extract, missed when the other core extension replacements…

… were added
  • Loading branch information...
commit 32b0fbc99ad6f1bd3683405b8ff3b27e968f7098 1 parent 96b9d72
@jeremyevans authored
Showing with 12 additions and 4 deletions.
  1. +8 −4 lib/sequel/sql.rb
  2. +4 −0 spec/core/expression_filters_spec.rb
View
12 lib/sequel/sql.rb
@@ -412,6 +412,14 @@ def expr(arg=(no_arg=true), &block)
end
end
+ # Extract a datetime_part (e.g. year, month) from the given
+ # expression:
+ #
+ # Sequel.extract(:year, :date) # extract(year FROM "date")
+ def extract(datetime_part, exp)
+ SQL::NumericExpression.new(:extract, datetime_part, exp)
+ end
+
# Returns a <tt>Sequel::SQL::Function</tt> with the function name
# and the given arguments.
#
@@ -612,10 +620,6 @@ module ComplexExpressionMethods
#
# Also has the benefit of returning the result as a
# NumericExpression instead of a generic ComplexExpression.
- #
- # The extract function is in the SQL standard, but it doesn't
- # doesn't use the standard function calling convention, and it
- # doesn't work on all databases.
def extract(datetime_part)
NumericExpression.new(:extract, datetime_part, self)
end
View
4 spec/core/expression_filters_spec.rb
@@ -980,6 +980,10 @@ def l(arg, should)
l(Sequel.function(:a, 1), 'a(1)')
l(Sequel.function(:a, :b, 2), 'a(b, 2)')
end
+
+ it "Sequel.extract should use a date/time extraction" do
+ l(Sequel.extract(:year, :a), 'extract(year FROM a)')
+ end
end
describe "Sequel::SQL::Function#==" do
Please sign in to comment.
Something went wrong with that request. Please try again.