Class: Insertica::Table
- Inherits:
-
Object
- Object
- Insertica::Table
- Defined in:
- lib/insertica/table.rb
Instance Attribute Summary collapse
-
#schema_name ⇒ Object
Returns the value of attribute schema_name.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
- #columns ⇒ Object
- #definition ⇒ Object
- #filler_definition ⇒ Object
- #full_name ⇒ Object
-
#initialize(schema_name, filename) ⇒ Table
constructor
A new instance of Table.
- #insert(options = {}) ⇒ Object
- #rows ⇒ Object
- #to_tsv ⇒ Object
Constructor Details
#initialize(schema_name, filename) ⇒ Table
11 12 13 14 15 16 17 18 |
# File 'lib/insertica/table.rb', line 11 def initialize(schema_name, filename) @filename = filename @schema_name = schema_name @table_name = File.basename(filename, '.*') @file = File.read(@filename) columns and rows end |
Instance Attribute Details
#schema_name ⇒ Object
Returns the value of attribute schema_name.
8 9 10 |
# File 'lib/insertica/table.rb', line 8 def schema_name @schema_name end |
#table_name ⇒ Object
Returns the value of attribute table_name.
9 10 11 |
# File 'lib/insertica/table.rb', line 9 def table_name @table_name end |
Instance Method Details
#columns ⇒ Object
59 60 61 |
# File 'lib/insertica/table.rb', line 59 def columns @columns ||= generate_columns end |
#definition ⇒ Object
24 25 26 |
# File 'lib/insertica/table.rb', line 24 def definition @definition ||= @columns.values.map { |column| "#{column.definition}" }.join(",\n") end |
#filler_definition ⇒ Object
28 29 30 31 32 33 |
# File 'lib/insertica/table.rb', line 28 def filler_definition @filler_definition ||= [ @columns.values.map { |column| "#{column.filler_definition}" }.join(",\n"), @columns.values.map { |column| "#{column.fix_nulls_definition}" }.join(",\n") ].join(",\n") end |
#full_name ⇒ Object
20 21 22 |
# File 'lib/insertica/table.rb', line 20 def full_name "#{schema_name}.#{table_name}" end |
#insert(options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/insertica/table.rb', line 35 def insert( = {}) vertica_connection = Vertica.connect({ host: [:host], user: [:user], password: [:password], port: [:port] }) drop_statement = "DROP TABLE IF EXISTS #{full_name} CASCADE" create_statement = "CREATE TABLE #{full_name} (#{definition})" copy_statement = "COPY #{full_name} (#{filler_definition}) FROM STDIN DELIMITER '\t' ENCLOSED BY '\"' NULL AS 'NULL' ABORT ON ERROR" vertica_connection.query(drop_statement) vertica_connection.query(create_statement) vertica_connection.copy(copy_statement) do |stdin| stdin << to_tsv end vertica_connection.query("COMMIT") end |
#rows ⇒ Object
55 56 57 |
# File 'lib/insertica/table.rb', line 55 def rows @rows ||= generate_rows end |
#to_tsv ⇒ Object
63 64 65 66 67 |
# File 'lib/insertica/table.rb', line 63 def to_tsv @rows.map do |row| "\"#{row.join("\"\t\"")}\"\n" end.join("") end |