lib/sql_tree/node/expression.rb in sql_tree-0.1.1 vs lib/sql_tree/node/expression.rb in sql_tree-0.2.0
- old
+ new
@@ -47,10 +47,15 @@
elsif SQLTree::Token::DOT === tokens.peek(2)
Field.parse(tokens)
else
Variable.parse(tokens)
end
+ elsif SQLTree::Token::STRING_ESCAPE == tokens.peek
+ tokens.consume(SQLTree::Token::STRING_ESCAPE)
+ Value.parse(tokens)
+ elsif SQLTree::Token::INTERVAL === tokens.peek
+ IntervalValue.parse(tokens)
else
Value.parse(tokens)
end
end
@@ -331,11 +336,39 @@
function_call.arguments = self.parse_list(tokens) unless SQLTree::Token::RPAREN === tokens.peek
tokens.consume(SQLTree::Token::RPAREN)
return function_call
end
end
-
+
+
+ # Represents a postgresql INTERVAL value. Example: interval '2 days'.
+ #
+ # The value is the literal text of the interval (e.g. "2 days").
+ class IntervalValue < SQLTree::Node::Expression
+ # The actual value this node represents.
+ leaf :value
+
+ def initialize(value) # :nodoc:
+ @value = value
+ end
+
+ # Generates an SQL representation for this value.
+ def to_sql(options = {})
+ "interval " + quote_str(@value)
+ end
+
+ def self.parse(tokens)
+ tokens.consume(SQLTree::Token::INTERVAL)
+ if SQLTree::Token::String === tokens.peek
+ self.new(tokens.next.literal)
+ else
+ raise SQLTree::Parser::UnexpectedToken.new(tokens.current, :literal)
+ end
+ end
+ end
+
+
# Represents alitreal value in an SQL expression. This node is a leaf node
# and thus has no child nodes.
#
# A value can either be:
# * the SQL <tt>NULL</tt> keyword, which is represented by <tt>nil</tt>.
@@ -343,31 +376,31 @@
# * an SQL date or time value, which can be represented as a <tt>Date</tt>,
# <tt>Time</tt> or <tt>DateTime</tt> instance.
# * an integer or decimal value, which is represented by an appropriate
# <tt>Numeric</tt> instance.
class Value < SQLTree::Node::Expression
-
+
# The actual value this node represents.
leaf :value
def initialize(value) # :nodoc:
@value = value
end
-
+
# Generates an SQL representation for this value.
#
# This method supports nil, string, numeric, date and time values.
#
# @return [String] A correctly quoted value that can be used safely
# within an SQL query
def to_sql(options = {})
case value
- when nil then 'NULL'
- when String then quote_str(@value)
- when Numeric then @value.to_s
- when Date then @value.strftime("'%Y-%m-%d'")
- when DateTime, Time then @value.strftime("'%Y-%m-%d %H:%M:%S'")
+ when nil; 'NULL'
+ when String; quote_str(@value)
+ when Numeric; @value.to_s
+ when Date; @value.strftime("'%Y-%m-%d'")
+ when DateTime, Time; @value.strftime("'%Y-%m-%d %H:%M:%S'")
else raise "Don't know how te represent this value in SQL!"
end
end
# Parses a literal value.
@@ -387,10 +420,10 @@
else
raise SQLTree::Parser::UnexpectedToken.new(tokens.current, :literal)
end
end
end
-
+
# Represents a variable within an SQL expression. This is a leaf node, so it
# does not have any child nodes. A variale can point to a field of a table or
# to another expression that was declared elsewhere.
class Variable < SQLTree::Node::Expression