Module: Veritas::SQL::Generator::Literal

Included in:
Function, Relation::Unary
Defined in:
lib/veritas/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.

Parameters:

  • object (Object)

Returns:

  • (Object)

    non-frozen object



30
31
32
# File 'lib/veritas/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

Parameters:

  • klass (Class)

Returns:

  • (#to_s)


79
80
81
82
# File 'lib/veritas/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

Parameters:

Returns:

  • (#to_s)


91
92
93
# File 'lib/veritas/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.

Parameters:

Returns:

  • (#to_s)


104
105
106
# File 'lib/veritas/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

Parameters:

  • enumerable (Enumerable)

Returns:

  • (#to_s)


41
42
43
44
45
# File 'lib/veritas/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

Parameters:

  • (false)

Returns:

  • (#to_s)


139
140
141
# File 'lib/veritas/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

Parameters:

  • (nil)

Returns:

  • (#to_s)


150
151
152
# File 'lib/veritas/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

Parameters:

  • numeric (Numeric)

Returns:

  • (#to_s)


68
69
70
# File 'lib/veritas/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

Parameters:

  • string (String)

Returns:

  • (#to_s)


56
57
58
59
# File 'lib/veritas/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.

Parameters:

  • time (Time)

Returns:

  • (#to_s)


117
118
119
# File 'lib/veritas/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

Parameters:

  • (true)

Returns:

  • (#to_s)


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

def visit_true_class(_true)
  TRUE
end