class Sequel::LoadDataInfileExpression
@api private
Attributes
character_set[R]
columns[R]
ignore[R]
path[R]
table[R]
Public Class Methods
new(path, table, columns, opts={})
click to toggle source
# File lib/sequel/load_data_infile.rb, line 8 def initialize(path, table, columns, opts={}) @path = path @table = table @columns = columns @ignore = opts[:ignore] @update = opts[:update] @set = opts[:set] || {} @character_set = opts[:character_set] || "utf8" if opts[:format] == :csv @field_terminator = "," @enclosed_by = '"' @escaped_by = '"' end end
Public Instance Methods
ignore?()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 27 def ignore? @update == :ignore end
replace?()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 23 def replace? @update == :replace end
to_sql(db)
click to toggle source
# File lib/sequel/load_data_infile.rb, line 31 def to_sql(db) @db = db [load_fragment, replace_fragment, table_fragment, character_set_fragment, field_terminator_fragment, field_enclosure_fragment, escape_fragment, ignore_fragment, column_fragment, set_fragment].compact.join(" ") end
Private Instance Methods
binary_columns()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 102 def binary_columns @binary_columns ||= @db.schema(@table). select {|a| a[1][:db_type] =~ /^binary/ }.map {|a| a.first.to_s } end
character_set_fragment()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 60 def character_set_fragment "CHARACTER SET '#{character_set}'" end
column_fragment()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 80 def column_fragment "(" + columns.map {|c| format_column(c) }.join(",") + ")" end
escape_fragment()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 72 def escape_fragment "ESCAPED BY '#{@escaped_by}'" if @escaped_by end
field_enclosure_fragment()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 68 def field_enclosure_fragment "OPTIONALLY ENCLOSED BY '#{@enclosed_by}'" if @enclosed_by end
field_terminator_fragment()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 64 def field_terminator_fragment "FIELDS TERMINATED BY '#{@field_terminator}'" if @field_terminator end
format_column(column)
click to toggle source
# File lib/sequel/load_data_infile.rb, line 92 def format_column(column) if binary_columns.include?(column.to_s) "@#{column}" elsif column.to_s[0..0] == "@" column else "`#{column}`" end end
ignore_fragment()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 76 def ignore_fragment "IGNORE #{ignore} LINES" if ignore end
load_fragment()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 48 def load_fragment "LOAD DATA INFILE '#{path}'" end
replace_fragment()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 52 def replace_fragment @update.to_s.upcase if replace? || ignore? end
set_columns()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 107 def set_columns binary_columns.inject({}) do |hash, column| hash[column.to_sym] = Sequel.function(:unhex, Sequel.lit("@#{column}")) hash end.merge(@set) end
set_fragment()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 84 def set_fragment unless set_columns.empty? "SET " + set_columns.map do |k, v| "#{@db.literal(k)} = #{@db.literal(v)}" end.join(", ") end end
table_fragment()
click to toggle source
# File lib/sequel/load_data_infile.rb, line 56 def table_fragment "INTO TABLE `#{table}`" end