Class: DbGui::Model::Db
- Inherits:
-
Struct
- Object
- Struct
- DbGui::Model::Db
- Defined in:
- app/db_gui/model/db.rb
Constant Summary collapse
- DIR_DB_GUI =
File.(File.join('~', '.db_gui'))
- FILE_DB_CONFIGS =
File.(File.join(DIR_DB_GUI, '.db_configs'))
- FILE_DB_COMMANDS =
File.(File.join(DIR_DB_GUI, '.db_commands'))
Instance Attribute Summary collapse
-
#connected ⇒ Object
(also: #connected?)
Returns the value of attribute connected.
-
#db_command ⇒ Object
Returns the value of attribute db_command.
-
#db_command_result ⇒ Object
Returns the value of attribute db_command_result.
-
#db_command_timeout ⇒ Object
Returns the value of attribute db_command_timeout.
-
#dbname ⇒ Object
Returns the value of attribute dbname.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #connect ⇒ Object
- #db_command_result_count ⇒ Object
- #db_command_result_headers ⇒ Object
- #db_command_result_rows ⇒ Object
- #disconnect ⇒ Object
-
#initialize ⇒ Db
constructor
A new instance of Db.
- #io ⇒ Object
- #run_db_command ⇒ Object
- #run_io_command(command) ⇒ Object
- #toggle_connection ⇒ Object
Constructor Details
#initialize ⇒ Db
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
#connected ⇒ Object 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_command ⇒ Object
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_result ⇒ Object
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_timeout ⇒ Object
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 |
#dbname ⇒ Object
Returns the value of attribute dbname
6 7 8 |
# File 'app/db_gui/model/db.rb', line 6 def dbname @dbname end |
#host ⇒ Object
Returns the value of attribute host
6 7 8 |
# File 'app/db_gui/model/db.rb', line 6 def host @host end |
#password ⇒ Object
Returns the value of attribute password
6 7 8 |
# File 'app/db_gui/model/db.rb', line 6 def password @password end |
#port ⇒ Object
Returns the value of attribute port
6 7 8 |
# File 'app/db_gui/model/db.rb', line 6 def port @port end |
#username ⇒ Object
Returns the value of attribute username
6 7 8 |
# File 'app/db_gui/model/db.rb', line 6 def username @username end |
Instance Method Details
#connect ⇒ Object
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_count ⇒ Object
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_headers ⇒ Object
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_rows ⇒ Object
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 |
#disconnect ⇒ Object
42 43 44 45 46 |
# File 'app/db_gui/model/db.rb', line 42 def disconnect io.close @io = nil self.connected = false end |
#io ⇒ Object
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_command ⇒ Object
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_connection ⇒ Object
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 |