Class: Jetel::Loaders::Pg

Inherits:
Loader
  • Object
show all
Defined in:
lib/jetel/loaders/pg/pg.rb

Instance Attribute Summary

Attributes inherited from Loader

#uri

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Pg

Returns a new instance of Pg.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jetel/loaders/pg/pg.rb', line 12

def initialize(uri)
  super

  tmp = uri.split('://')
  tmp = tmp[1].split('@')

  parts = tmp[0].split(':')
  user = parts[0]
  password = parts[1]

  parts = tmp[1].split('/')
  host, port = parts[0].split(':')
  dbname = parts[1]

  opts = {
    :host => host,
    :port => port.to_i,
    # :options => '',
    # :tty => '',
    :dbname => dbname,
    :user => user,
    :password => password
  }

  @client = PG.connect(opts)
end

Instance Method Details

#load(modul, source, file, opts) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jetel/loaders/pg/pg.rb', line 39

def load(modul, source, file, opts)
  super

  convert_opts = {
    :l => opts['analyze_num_rows'] && opts['analyze_num_rows'].to_i,
    :skip => 0,
    :header => true,
    :delimiter => opts[:delimiter]
  }

  schema_list = Csv2Psql::Convert.generate_schema([file], convert_opts)
  _file_name, schema = schema_list.first

  return nil if schema.nil?

  analyzer = Csv2Psql::Analyzer.new
  column_types = (opts['column_type'] && opts['column_type'].split(/[;,]/)) || []
  column_types.each do |ct|
    name, type = ct.split('=')

    columns = schema[:columns] || []
    column = columns.find do |k, v|
      k.downcase == name
    end

    analyzer_type = analyzer.analyzers.find do |spec|
      spec[:class].name.split('::').last.downcase == type.downcase
    end

    type_val = analyzer_type ? analyzer_type[:class].const_get(:TYPE) : type

    if column
      columns[column[0]] = {
        type: type_val.to_sym,
        null: true
      }
    end
  end

  ctx = {
    :ctx => {
      :table => Helper.sanitize(source[:name]).downcase,
      :columns => schema[:columns],
      :source => source,
      :module => modul,
      :file => File.absolute_path(modul.transformed_file(source, opts)),
      :delimiter => opts[:delimiter]
    }
  }

  sql = Helper.erb_template(File.expand_path('../sql/schema.sql.erb', __FILE__), ctx)
  sql.gsub!("\n\n", "\n")
  puts sql
  @client.exec(sql)

  sql = Helper.erb_template(File.expand_path('../sql/copy.sql.erb', __FILE__), ctx)
  sql.gsub!("\n\n", "\n")
  puts sql
  @client.exec(sql)

  file = File.open(ctx[:ctx][:file], 'r')
  while !file.eof?
    # Add row to copy data
    @client.put_copy_data(file.readline)
  end

  # We are done adding copy data
  @client.put_copy_end

  # Display any error messages
  while res = @client.get_result
    e_message = res.error_message
    if e_message && !e_message.empty?
      puts e_message
    end
    puts "#{res.cmdtuples} row(s) affected"
  end

  sql
end