require 'open3' require 'shellwords' require 'flydata/command/sync' # Command class must be in module Flydata::Command module Flydata module Command class Psql < Sync def run(*args) de = data_entry cmd = generate_command(de['postgresql_data_entry_preference'], args) return if cmd.to_s.empty? $stderr.puts "command:#{cmd}" if FLYDATA_DEBUG if $stdin.tty? # interactive shell system cmd else # execute queries given to $stdin Open3.popen2e(cmd) do |i, o, wt| $stdin.each_line do |line| i.print line end i.close while line = o.gets print line end end end end def generate_command(dbconf, args) password = Shellwords.escape(dbconf['password']) database = Shellwords.escape(dbconf['database']) host = Shellwords.escape(dbconf['host']) port = Shellwords.escape(dbconf['port']) username = Shellwords.escape(dbconf['username']) "PGCONNECT_TIMEOUT=5 PGPASSWORD=#{password} psql -d #{database} -h #{host} -p #{port} -U #{username}" end def flush; end def reset; end def skip; end def generate_table_ddl; end end end end