Class: Jetel::Loaders::Pg
Instance Attribute Summary
Attributes inherited from Loader
Instance Method Summary collapse
-
#initialize(uri) ⇒ Pg
constructor
A new instance of Pg.
- #load(modul, source, file, opts) ⇒ Object
Constructor Details
#initialize(uri) ⇒ Pg
Returns a new instance of Pg.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jetel/loaders/pg/pg.rb', line 34 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
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/jetel/loaders/pg/pg.rb', line 61 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.('../sql/schema.sql.erb', __FILE__), ctx) sql.gsub!("\n\n", "\n") puts sql @client.exec(sql) sql = Helper.erb_template(File.('../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 = res. if && !.empty? puts end puts "#{res.cmdtuples} row(s) affected" end sql end |