Class: TableCopy::PG::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/table_copy/pg/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Source

Returns a new instance of Source.



9
10
11
12
13
# File 'lib/table_copy/pg/source.rb', line 9

def initialize(args)
  @table_name    = args[:table_name]
  @conn_method   = args[:conn_method]
  @infer_pk_proc = args[:infer_pk_proc]
end

Instance Attribute Details

#conn_methodObject (readonly)

Returns the value of attribute conn_method.



7
8
9
# File 'lib/table_copy/pg/source.rb', line 7

def conn_method
  @conn_method
end

#infer_pk_procObject (readonly)

Returns the value of attribute infer_pk_proc.



7
8
9
# File 'lib/table_copy/pg/source.rb', line 7

def infer_pk_proc
  @infer_pk_proc
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



7
8
9
# File 'lib/table_copy/pg/source.rb', line 7

def table_name
  @table_name
end

Instance Method Details

#copy_from(fields_list_arg, where = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/table_copy/pg/source.rb', line 31

def copy_from(fields_list_arg, where=nil)
  with_conn do |conn|
    conn.copy_data("copy (select #{fields_list_arg} from #{table_name} #{where}) to stdout csv")  do
      yield conn
    end
  end
end

#fieldsObject



39
40
41
# File 'lib/table_copy/pg/source.rb', line 39

def fields
  fields_objects.map(&:name)
end

#fields_ddlObject



23
24
25
# File 'lib/table_copy/pg/source.rb', line 23

def fields_ddl
  fields_objects.map(&:ddl).join(",\n  ")
end

#indexesObject



27
28
29
# File 'lib/table_copy/pg/source.rb', line 27

def indexes
  viable_index_columns.map { |name, columns| TableCopy::PG::Index.new(table_name, name, columns) }
end

#primary_keyObject



19
20
21
# File 'lib/table_copy/pg/source.rb', line 19

def primary_key
  @primary_key ||= get_primary_key
end

#to_sObject



15
16
17
# File 'lib/table_copy/pg/source.rb', line 15

def to_s
  table_name
end