Class: DbGui::Model::Db

Inherits:
Struct
  • Object
show all
Defined in:
app/db_gui/model/db.rb

Constant Summary collapse

DIR_DB_GUI =
File.expand_path(File.join('~', '.db_gui'))
FILE_DB_CONFIGS =
File.expand_path(File.join(DIR_DB_GUI, '.db_configs'))
FILE_DB_COMMANDS =
File.expand_path(File.join(DIR_DB_GUI, '.db_commands'))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDb

Returns a new instance of Db.



19
20
21
22
23
24
25
26
# File 'app/db_gui/model/db.rb', line 19

def initialize
  self.port = 5432 # PostgreSQL default port
  self.db_command_result = ''
  self.db_command_timeout = (ENV['DB_COMMAND_TIMEOUT_IN_MILLISECONDS'] || 300).to_i
  load_db_config
  load_db_command
  connect if to_h.except(:password).none? {|value| value.nil? || (value.respond_to?(:empty?) && value.empty?) }
end

Instance Attribute Details

#connectedObject Also known as: connected?

Returns the value of attribute connected.



13
14
15
# File 'app/db_gui/model/db.rb', line 13

def connected
  @connected
end

#db_commandObject

Returns the value of attribute db_command.



16
17
18
# File 'app/db_gui/model/db.rb', line 16

def db_command
  @db_command
end

#db_command_resultObject

Returns the value of attribute db_command_result.



15
16
17
# File 'app/db_gui/model/db.rb', line 15

def db_command_result
  @db_command_result
end

#db_command_timeoutObject

Returns the value of attribute db_command_timeout.



17
18
19
# File 'app/db_gui/model/db.rb', line 17

def db_command_timeout
  @db_command_timeout
end

#dbnameObject

Returns the value of attribute dbname

Returns:

  • (Object)

    the current value of dbname



6
7
8
# File 'app/db_gui/model/db.rb', line 6

def dbname
  @dbname
end

#hostObject

Returns the value of attribute host

Returns:

  • (Object)

    the current value of host



6
7
8
# File 'app/db_gui/model/db.rb', line 6

def host
  @host
end

#passwordObject

Returns the value of attribute password

Returns:

  • (Object)

    the current value of password



6
7
8
# File 'app/db_gui/model/db.rb', line 6

def password
  @password
end

#portObject

Returns the value of attribute port

Returns:

  • (Object)

    the current value of port



6
7
8
# File 'app/db_gui/model/db.rb', line 6

def port
  @port
end

#usernameObject

Returns the value of attribute username

Returns:

  • (Object)

    the current value of username



6
7
8
# File 'app/db_gui/model/db.rb', line 6

def username
  @username
end

Instance Method Details

#connectObject



36
37
38
39
40
# File 'app/db_gui/model/db.rb', line 36

def connect
  io
  self.connected = true
  save_db_config
end

#db_command_result_countObject



69
70
71
# File 'app/db_gui/model/db.rb', line 69

def db_command_result_count
  db_command_result_count_headers_rows[0]
end

#db_command_result_headersObject



73
74
75
# File 'app/db_gui/model/db.rb', line 73

def db_command_result_headers
  db_command_result_count_headers_rows[1]
end

#db_command_result_rowsObject



77
78
79
# File 'app/db_gui/model/db.rb', line 77

def db_command_result_rows
  db_command_result_count_headers_rows[2]
end

#disconnectObject



42
43
44
45
46
# File 'app/db_gui/model/db.rb', line 42

def disconnect
  io.close
  @io = nil
  self.connected = false
end

#ioObject



48
49
50
# File 'app/db_gui/model/db.rb', line 48

def io
  @io ||= IO.popen("PGPASSWORD=\"#{password}\" psql --host=#{host} --port=#{port} --username=#{username} --dbname=#{dbname}", 'r+')
end

#run_db_commandObject



64
65
66
67
# File 'app/db_gui/model/db.rb', line 64

def run_db_command
  run_io_command(db_command)
  save_db_command
end

#run_io_command(command) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'app/db_gui/model/db.rb', line 52

def run_io_command(command)
  command = command.strip
  command = "#{command};" unless command.end_with?(';')
  @io_command_try ||= 0
  @io_command_try += 1
  io.puts(command)
  read_io_into_db_command_result
rescue Errno::EPIPE => e
  @io = nil
  run_io_command(command) unless @io_command_try > 1
end

#toggle_connectionObject



28
29
30
31
32
33
34
# File 'app/db_gui/model/db.rb', line 28

def toggle_connection
  if connected?
    disconnect
  else
    connect
  end
end