Class: DWH::Table
- Inherits:
-
Object
- Object
- DWH::Table
- Defined in:
- lib/dwh/table.rb
Overview
Container to map to a data warehouse table. If you initialize with a fuly qualified table name , it will automatically create catalog and schema components.
This is the object returned from +metadata+ method call of an adapter
==== Examples Table.new("dwh.public.hello_world_table")
table_stats_instance = adapter.stats("my_table", schema: "dwh") Table.new("my_table", schema: "dwh", stats: table_stats_instance)
Instance Attribute Summary collapse
-
#catalog ⇒ Object
readonly
Returns the value of attribute catalog.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#physical_name ⇒ Object
readonly
Returns the value of attribute physical_name.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#table_stats ⇒ Object
readonly
Returns the value of attribute table_stats.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(column) ⇒ Object
- #catalog? ⇒ Boolean
- #catalog_and_schema? ⇒ Boolean
- #catalog_or_schema? ⇒ Boolean
- #find_column(name) ⇒ Object
- #fully_qualified_schema_name ⇒ Object
- #fully_qualified_table_name ⇒ Object
-
#initialize(physical_name, schema: nil, catalog: nil, table_stats: nil) ⇒ Table
constructor
A new instance of Table.
- #schema? ⇒ Boolean
- #size ⇒ Object
- #stats ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(physical_name, schema: nil, catalog: nil, table_stats: nil) ⇒ Table
Returns a new instance of Table.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dwh/table.rb', line 20 def initialize(physical_name, schema: nil, catalog: nil, table_stats: nil) parts = physical_name.split('.') @physical_name = parts.last @table_stats = table_stats @catalog = catalog&.strip @schema = schema&.strip @catalog = parts.first.strip if @catalog.nil? && parts.length > 2 if @schema.nil? if parts.length == 2 @schema = parts.first.strip elsif parts.length > 2 @schema = parts[1].strip end end @columns = [] end |
Instance Attribute Details
#catalog ⇒ Object (readonly)
Returns the value of attribute catalog.
18 19 20 |
# File 'lib/dwh/table.rb', line 18 def catalog @catalog end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
18 19 20 |
# File 'lib/dwh/table.rb', line 18 def columns @columns end |
#physical_name ⇒ Object (readonly)
Returns the value of attribute physical_name.
18 19 20 |
# File 'lib/dwh/table.rb', line 18 def physical_name @physical_name end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
18 19 20 |
# File 'lib/dwh/table.rb', line 18 def schema @schema end |
#table_stats ⇒ Object (readonly)
Returns the value of attribute table_stats.
18 19 20 |
# File 'lib/dwh/table.rb', line 18 def table_stats @table_stats end |
Class Method Details
.from_hash_or_json(physical_name, metadata) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/dwh/table.rb', line 91 def self.from_hash_or_json(physical_name, ) = JSON.parse() if .is_a?(String) .transform_keys!(&:to_sym) stats = TableStats.new(**[:stats]&.transform_keys!(&:to_sym)) if .key?(:stats) table = new(physical_name, table_stats: stats) [:columns]&.each do |col| col.transform_keys!(&:to_sym) table << Column.new( name: col[:name], data_type: col[:data_type], precision: col[:precision], scale: col[:scale], max_char_length: col[:max_char_length], schema_type: col[:schema_type] ) end table end |
Instance Method Details
#<<(column) ⇒ Object
41 42 43 |
# File 'lib/dwh/table.rb', line 41 def <<(column) @columns << column end |
#catalog? ⇒ Boolean
53 54 55 |
# File 'lib/dwh/table.rb', line 53 def catalog? catalog && !catalog.empty? end |
#catalog_and_schema? ⇒ Boolean
61 62 63 |
# File 'lib/dwh/table.rb', line 61 def catalog_and_schema? catalog? && schema? end |
#catalog_or_schema? ⇒ Boolean
65 66 67 |
# File 'lib/dwh/table.rb', line 65 def catalog_or_schema? catalog? || schema? end |
#find_column(name) ⇒ Object
87 88 89 |
# File 'lib/dwh/table.rb', line 87 def find_column(name) columns.find { |c| c.name.downcase == name.downcase } end |
#fully_qualified_schema_name ⇒ Object
49 50 51 |
# File 'lib/dwh/table.rb', line 49 def fully_qualified_schema_name [catalog, schema].compact.join('.') end |
#fully_qualified_table_name ⇒ Object
45 46 47 |
# File 'lib/dwh/table.rb', line 45 def fully_qualified_table_name [catalog, schema, physical_name].compact.join('.') end |
#schema? ⇒ Boolean
57 58 59 |
# File 'lib/dwh/table.rb', line 57 def schema? schema && !schema.empty? end |
#size ⇒ Object
83 84 85 |
# File 'lib/dwh/table.rb', line 83 def size @table_stats&.row_count || 0 end |
#stats ⇒ Object
69 70 71 |
# File 'lib/dwh/table.rb', line 69 def stats @table_stats end |
#to_h ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/dwh/table.rb', line 73 def to_h { physical_name: physical_name, schema: schema, catalog: catalog, columns: columns.map(&:to_h), stats: table_stats&.to_h } end |