lib/veritas/sql/generator/literal.rb in veritas-sql-generator-0.0.3 vs lib/veritas/sql/generator/literal.rb in veritas-sql-generator-0.0.4

- old
+ new

@@ -5,19 +5,17 @@ module Generator # Generates an SQL statement for a literal module Literal - TRUE = 'TRUE'.freeze - FALSE = 'FALSE'.freeze - NULL = 'NULL'.freeze - QUOTE = "'".freeze - ESCAPED_QUOTE = "''".freeze - SEPARATOR = ', '.freeze - DATE_FORMAT = '%F'.freeze - DATETIME_FORMAT = "#{DATE_FORMAT}T%T.%N%Z".freeze - TIME_SCALE = 9 + TRUE = 'TRUE'.freeze + FALSE = 'FALSE'.freeze + NULL = 'NULL'.freeze + QUOTE = "'".freeze + ESCAPED_QUOTE = "''".freeze + SEPARATOR = ', '.freeze + TIME_SCALE = 9 # Returns an unfrozen object # # Some objects, like Date, DateTime and Time memoize values # when serialized to a String, so when they are frozen this will @@ -39,24 +37,27 @@ # # @return [#to_s] # # @api private def visit_enumerable(enumerable) - "(#{enumerable.map { |entry| dispatch entry }.join(SEPARATOR)})" + Generator.parenthesize!( + enumerable.map { |entry| dispatch entry }.join(SEPARATOR) + ) end # Visit a String # - # @note The string must be utf-8 encoded + # @note The string must be UTF-8 encoded # # @param [String] string # # @return [#to_s] # # @api private def visit_string(string) - "#{QUOTE}#{string.gsub(QUOTE, ESCAPED_QUOTE)}#{QUOTE}" + escaped = string.gsub(QUOTE, ESCAPED_QUOTE) + escaped.insert(0, QUOTE) << QUOTE end # Visit a Numeric # # @param [Numeric] numeric @@ -84,29 +85,25 @@ # # @param [Date] date # # @return [#to_s] # - # @todo use Date#iso8601 when added to 1.8.7 by backports - # # @api private def visit_date(date) - dispatch Literal.dup_frozen(date).strftime(DATE_FORMAT) + dispatch date.iso8601 end # Visit a DateTime and return in ISO 8601 date-time format # # Converts the DateTime to UTC format. # # @param [DateTime] date_time # # @return [#to_s] # - # @todo use DateTime#iso8601(TIME_SCALE) when added to 1.8.7 by backports - # # @api private def visit_date_time(date_time) - dispatch date_time.new_offset.strftime(DATETIME_FORMAT) + dispatch date_time.new_offset.iso8601(TIME_SCALE) end # Visit a Time # # Converts the Time to UTC format.