Module: Axiom::SQL::Generator::Literal

Included in:
Function, Relation::Materialized, Relation::Unary
Defined in:
lib/axiom/sql/generator/literal.rb

Overview

Generates an SQL statement for a literal

Constant Summary collapse

TRUE =
'TRUE'.freeze
FALSE =
'FALSE'.freeze
NULL =
'NULL'.freeze
QUOTE =
"'".freeze
ESCAPED_QUOTE =
"''".freeze
SEPARATOR =
', '.freeze
TIME_SCALE =
9

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dup_frozen(object) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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 dup them and then return the unfrozen copy.



30
31
32
# File 'lib/axiom/sql/generator/literal.rb', line 30

def self.dup_frozen(object)
  object.frozen? ? object.dup : object
end

Instance Method Details

#visit_class(klass) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Visit a Class



79
80
81
82
# File 'lib/axiom/sql/generator/literal.rb', line 79

def visit_class(klass)
  name = klass.name.to_s
  name.empty? ? NULL : visit_string(name)
end

#visit_date(date) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Visit a Date and return in ISO 8601 date format



91
92
93
# File 'lib/axiom/sql/generator/literal.rb', line 91

def visit_date(date)
  dispatch date.iso8601
end

#visit_date_time(date_time) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Visit a DateTime and return in ISO 8601 date-time format

Converts the DateTime to UTC format.



104
105
106
# File 'lib/axiom/sql/generator/literal.rb', line 104

def visit_date_time(date_time)
  dispatch date_time.new_offset.iso8601(TIME_SCALE)
end

#visit_enumerable(enumerable) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Visit an Enumerable



41
42
43
44
45
# File 'lib/axiom/sql/generator/literal.rb', line 41

def visit_enumerable(enumerable)
  Generator.parenthesize!(
    enumerable.map { |entry| dispatch entry }.join(SEPARATOR)
  )
end

#visit_false_class(_false) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Visit a false value



139
140
141
# File 'lib/axiom/sql/generator/literal.rb', line 139

def visit_false_class(_false)
  FALSE
end

#visit_nil_class(_nil) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Visit a nil value



150
151
152
# File 'lib/axiom/sql/generator/literal.rb', line 150

def visit_nil_class(_nil)
  NULL
end

#visit_numeric(numeric) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Visit a Numeric



68
69
70
# File 'lib/axiom/sql/generator/literal.rb', line 68

def visit_numeric(numeric)
  numeric.to_s
end

#visit_string(string) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

The string must be UTF-8 encoded

Visit a String



56
57
58
59
# File 'lib/axiom/sql/generator/literal.rb', line 56

def visit_string(string)
  escaped = string.gsub(QUOTE, ESCAPED_QUOTE)
  escaped.insert(0, QUOTE) << QUOTE
end

#visit_time(time) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Visit a Time

Converts the Time to UTC format.



117
118
119
# File 'lib/axiom/sql/generator/literal.rb', line 117

def visit_time(time)
  dispatch Literal.dup_frozen(time).utc.iso8601(TIME_SCALE)
end

#visit_true_class(_true) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Visit a true value



128
129
130
# File 'lib/axiom/sql/generator/literal.rb', line 128

def visit_true_class(_true)
  TRUE
end