Class: StructuredDataToSql::MysqlDumpXml::SqlEmitter

Inherits:
Object
  • Object
show all
Defined in:
lib/structured_data_to_sql/mysql_dump_xml/sql_emitter.rb

Instance Method Summary collapse

Instance Method Details

#column_formats_for(structure, sample_row) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/structured_data_to_sql/mysql_dump_xml/sql_emitter.rb', line 26

def column_formats_for(structure, sample_row)
  if structure
    structure.columns.map do |col|
      {
        name: col[:name],
        escaped_name: SqlText.escape_identifier(col[:name]),
        type: col[:type],
        allows_null: col[:null],
        unique: col[:key] == "UNI",
        numeric: numeric_type?(col[:type])
      }
    end
  else
    sample_row.keys.map do |name|
      {
        name: name,
        escaped_name: SqlText.escape_identifier(name),
        type: "text",
        allows_null: true,
        unique: false,
        numeric: false
      }
    end
  end
end

#format_value(value, format) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/structured_data_to_sql/mysql_dump_xml/sql_emitter.rb', line 75

def format_value(value, format)
  allows_null = format[:allows_null]
  if value.nil? || value == "null"
    return "NULL" if allows_null
    return "0" if format[:numeric]

    return "''"
  end

  if format[:numeric]
    return allows_null ? "NULL" : "0" if value == ""
    return value if value.to_s.match?(/\A-?\d+(?:\.\d+)?\z/)
  end

  # Repeated empty strings violate nullable unique indexes.
  return "NULL" if format[:unique] && allows_null && value == ""

  SqlText.escape_sql_string(value)
end


22
23
24
# File 'lib/structured_data_to_sql/mysql_dump_xml/sql_emitter.rb', line 22

def write_footer(out)
  out.write("\nSET FOREIGN_KEY_CHECKS = 1;\nSET UNIQUE_CHECKS = 1;\n")
end

#write_header(out, source_label:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/structured_data_to_sql/mysql_dump_xml/sql_emitter.rb', line 8

def write_header(out, source_label:)
  out.write("-- Generated by xml-to-sql converter\n")
  out.write("-- Source: #{source_label}\n")
  out.write(
    "-- Generated at: #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}\n\n"
  )
  out.write(
    "-- XML dumps are generated in autocommit mode to avoid huge transaction commits.\n"
  )
  out.write(
    "SET NAMES utf8mb4;\nSET sql_mode = '';\nSET FOREIGN_KEY_CHECKS = 0;\nSET UNIQUE_CHECKS = 0;\n\n"
  )
end

#write_rows_batch(out, table, rows, column_formats:) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/structured_data_to_sql/mysql_dump_xml/sql_emitter.rb', line 52

def write_rows_batch(out, table, rows, column_formats:)
  return if rows.empty?

  buffer = +"INSERT INTO #{SqlText.escape_identifier(table)} ("
  column_formats.each_with_index do |format, index|
    buffer << ", " if index.positive?
    buffer << format[:escaped_name]
  end
  buffer << ") VALUES\n"

  rows.each_with_index do |row, row_index|
    buffer << ",\n" if row_index.positive?
    buffer << "  ("
    column_formats.each_with_index do |format, column_index|
      buffer << ", " if column_index.positive?
      buffer << format_value(row[format[:name]], format)
    end
    buffer << ")"
  end
  buffer << ";\n"
  out.write(buffer)
end