Class: Extract::Export::Table
- Includes:
- FromHash
- Defined in:
- lib/extract/export/table.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #cols ⇒ Object
- #create_table_sql ⇒ Object
- #inserts ⇒ Object
- #quoted_col(col) ⇒ Object
- #sql ⇒ Object
- #sql_statements ⇒ Object
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/extract/export/table.rb', line 5 def name @name end |
Instance Method Details
#cols ⇒ Object
8 9 10 |
# File 'lib/extract/export/table.rb', line 8 def cols rows.map { |x| x.keys }.flatten.uniq end |
#create_table_sql ⇒ Object
21 22 23 24 25 26 |
# File 'lib/extract/export/table.rb', line 21 def create_table_sql col_str = cols.map { |x| " #{quoted_col(x)} varchar(255)" }.join(",\n") "CREATE TABLE #{name} ( #{col_str} );" end |
#inserts ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/extract/export/table.rb', line 28 def inserts res = [] rows.each do |row| col_str = row.keys.map { |x| quoted_col(x) }.join(",") val_str = row.values.map { |x| "'#{x}'" }.join(',') str = "INSERT INTO #{name} (#{col_str}) VALUES (#{val_str});" res << str end res end |
#quoted_col(col) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/extract/export/table.rb', line 12 def quoted_col(col) return col if col.to_s[0..0] =~ /\d/ "\"#{col}\"" else col end end |
#sql ⇒ Object
43 44 45 |
# File 'lib/extract/export/table.rb', line 43 def sql sql_statements.join("\n") end |
#sql_statements ⇒ Object
39 40 41 |
# File 'lib/extract/export/table.rb', line 39 def sql_statements [create_table_sql,inserts].flatten end |