Class: Sqldump::InsertFormatter
- Inherits:
-
Object
- Object
- Sqldump::InsertFormatter
- Defined in:
- lib/sqldump/insert_formatter.rb
Instance Method Summary collapse
- #cols_and_values(row) ⇒ Object
- #column_type(column_name) ⇒ Object
- #indent ⇒ Object
-
#initialize(sth, io, options) ⇒ InsertFormatter
constructor
A new instance of InsertFormatter.
- #output ⇒ Object
- #output_list(list) ⇒ Object
- #output_values(row) ⇒ Object
- #pretty ⇒ Object
- #quote(value, column_name) ⇒ Object
- #setup_column_type_mapping ⇒ Object
Constructor Details
#initialize(sth, io, options) ⇒ InsertFormatter
Returns a new instance of InsertFormatter.
5 6 7 8 9 10 11 |
# File 'lib/sqldump/insert_formatter.rb', line 5 def initialize(sth, io, ) @sth = sth @io = io @options = setup_column_type_mapping() end |
Instance Method Details
#cols_and_values(row) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sqldump/insert_formatter.rb', line 37 def cols_and_values(row) cols = [] values = [] row.each_with_name do |value, column_name| unless @options.suppress_nulls && value.nil? cols.push column_name values.push quote(value, column_name) end end [cols, values] end |
#column_type(column_name) ⇒ Object
17 18 19 |
# File 'lib/sqldump/insert_formatter.rb', line 17 def column_type(column_name) @column_type_by_name[column_name] end |
#indent ⇒ Object
76 77 78 |
# File 'lib/sqldump/insert_formatter.rb', line 76 def indent " " * 4 end |
#output ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sqldump/insert_formatter.rb', line 49 def output @sth.fetch do |row| (cols, values) = cols_and_values(row) @io.print("INSERT INTO #{@options.table} (") output_list(cols) @io.print ")" @io.print pretty ? "\n" : " " @io.print "VALUES (" output_list(values) @io.print ");\n" end end |
#output_list(list) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/sqldump/insert_formatter.rb', line 21 def output_list(list) @io.print "\n#{indent}" if pretty join_string = pretty ? ",\n#{indent}" : ", " @io.print list.join(join_string) @io.print "\n" if pretty end |
#output_values(row) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/sqldump/insert_formatter.rb', line 28 def output_values(row) quoted_list = [] row.each_with_name do |value, column_name| quoted_list.push quote(value, column_name) end output_list(quoted_list) #@io.print quoted_list.join(", ") end |
#pretty ⇒ Object
80 81 82 |
# File 'lib/sqldump/insert_formatter.rb', line 80 def pretty @options.pretty end |
#quote(value, column_name) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/sqldump/insert_formatter.rb', line 62 def quote(value, column_name) if value.nil? "NULL" else type = column_type(column_name) if type == DBI::Type::Integer or type == DBI::Type::Float or type == DBI::Type::Decimal value else value = value.to_s.gsub(/'/, "''") "'#{value}'" end end end |
#setup_column_type_mapping ⇒ Object
13 14 15 |
# File 'lib/sqldump/insert_formatter.rb', line 13 def setup_column_type_mapping @column_type_by_name = Hash[@sth.column_names.zip(@sth.column_types)] end |