Class: SQLTree::Node::Expression::EscapedValue

Inherits:
Value
  • Object
show all
Defined in:
lib/active_record/turntable/sql_tree_patch.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, escape = nil) ⇒ EscapedValue

Returns a new instance of EscapedValue.



270
271
272
273
# File 'lib/active_record/turntable/sql_tree_patch.rb', line 270

def initialize(value, escape = nil)
  @value = value
  @escape = escape
end

Class Method Details

.parse(tokens) ⇒ Object



290
291
292
293
294
295
296
297
298
# File 'lib/active_record/turntable/sql_tree_patch.rb', line 290

def self.parse(tokens)
  escape = tokens.next
  case tokens.next
  when SQLTree::Token::String
    SQLTree::Node::Expression::EscapedValue.new(tokens.current.literal, escape.literal)
  else
    raise SQLTree::Parser::UnexpectedToken.new(tokens.current, :literal)
  end
end

Instance Method Details

#escape_stringObject



286
287
288
# File 'lib/active_record/turntable/sql_tree_patch.rb', line 286

def escape_string
  @escape.to_s
end

#to_sql(options = {}) ⇒ Object



275
276
277
278
279
280
281
282
283
284
# File 'lib/active_record/turntable/sql_tree_patch.rb', line 275

def to_sql(options = {})
  case value
  when nil then            "NULL"
  when String then         "#{escape_string}#{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'")
  else raise "Don't know how te represent this value in SQL!"
  end
end