Class: Dbviewer::ConnectionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/dbviewer/connections_controller.rb

Instance Method Summary collapse

Methods included from DatabaseOperations

#available_connections, #current_connection_key, #current_table?, #database_manager, #execute_query, #export_table_to_csv, #fetch_database_analytics, #fetch_filtered_record_count, #fetch_mini_erd_for_table, #fetch_table_columns, #fetch_table_metadata, #fetch_table_record_count, #fetch_table_records, #fetch_table_relationships, #fetch_tables, #get_adapter_name, #get_database_name, #prepare_query, #safe_quote_table_name, #switch_connection, #table_query_operations

Instance Method Details

#indexObject

GET /dbviewer/connections



4
5
6
7
8
9
10
11
# File 'app/controllers/dbviewer/connections_controller.rb', line 4

def index
  @connections = available_connections

  respond_to do |format|
    format.html
    format.json { render json: @connections }
  end
end

#updateObject

POST /dbviewer/connections/:id



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/dbviewer/connections_controller.rb', line 14

def update
  connection_key = params[:id]

  if switch_connection(connection_key)
    # Safely get the connection name
    connection_config = Dbviewer.configuration.database_connections[connection_key.to_sym]
    connection_name = connection_config && connection_config[:name] ? connection_config[:name] : "selected"

    respond_to do |format|
      format.html { redirect_to root_path, notice: "Switched to #{connection_name} database." }
      format.json { render json: { success: true, current: connection_key } }
    end
  else
    # Handle the case where switching failed
    respond_to do |format|
      format.html { redirect_to connections_path, alert: "Failed to switch connection. The connection may be invalid." }
      format.json { render json: { success: false, error: "Invalid connection" }, status: :unprocessable_entity }
    end
  end
end