Module: StructuredDataToSql::SqlText

Defined in:
lib/structured_data_to_sql/sql_text.rb

Overview

MySQL/MariaDB literal and identifier escaping shared by the XML and JSON dump converters.

Class Method Summary collapse

Class Method Details

.escape_identifier(name) ⇒ Object



25
26
27
# File 'lib/structured_data_to_sql/sql_text.rb', line 25

def escape_identifier(name)
  "`#{name.to_s.gsub("`", "``")}`"
end

.escape_sql_string(value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/structured_data_to_sql/sql_text.rb', line 9

def escape_sql_string(value)
  return "NULL" if value.nil?

  escaped =
    value
      .to_s
      .gsub("\\", "\\\\\\")
      .gsub("'", "\\\\'")
      .gsub("\n", "\\n")
      .gsub("\r", "\\r")
      .gsub("\t", "\\t")
      .gsub("\x00", "\\0")
      .gsub("\x1a", "\\Z")
  "'#{escaped}'"
end