Class: Dbviewer::TablesController

Inherits:
ApplicationController show all
Includes:
PaginationConcern
Defined in:
app/controllers/dbviewer/tables_controller.rb

Instance Method Summary collapse

Methods included from PaginationConcern

#calculate_total_pages, #fetch_total_count

Methods included from DatabaseOperations

#database_manager, #table_query_operations

Methods included from DatatableSupport

#fetch_datatable_data

Methods included from DataExport

#export_table_to_csv

Methods included from QueryOperations

#default_query, #execute_query, #prepare_query

Methods included from RelationshipManagement

#fetch_mini_erd_for_table, #fetch_table_relationships

Methods included from TableOperations

#fetch_filtered_record_count, #fetch_table_columns, #fetch_table_metadata, #fetch_table_record_count, #fetch_table_records, #fetch_tables, #safe_quote_table_name

Methods included from DatabaseInformation

#fetch_database_analytics, #get_adapter_name, #get_database_name

Methods included from ConnectionManagement

#available_connections, #current_connection_key, #switch_connection

Instance Method Details

#export_csvObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/dbviewer/tables_controller.rb', line 70

def export_csv
  unless Dbviewer.configuration.enable_data_export
    flash[:alert] = "Data export is disabled in the configuration"
    redirect_to table_path(params[:id])
    return
  end

  include_headers = params[:include_headers] != "0"
  query_params = Dbviewer::Datatable::QueryParams.new(
    page: @current_page,
    per_page: (params[:limit] || 10000).to_i,
    order_by: @order_by,
    direction: @order_direction,
    column_filters: @column_filters.reject { |_, v| v.blank? }
  )
  csv_data = export_table_to_csv(@table_name, query_params, include_headers)

  timestamp = Time.now.strftime("%Y%m%d%H%M%S")
  filename = "#{@table_name}_#{timestamp}.csv"

  send_data csv_data,
            type: "text/csv; charset=utf-8; header=present",
            disposition: "attachment; filename=#{filename}"
end

#indexObject



9
10
11
# File 'app/controllers/dbviewer/tables_controller.rb', line 9

def index
  @tables = fetch_tables(include_record_counts: true)
end

#mini_erdObject



45
46
47
48
49
50
51
52
# File 'app/controllers/dbviewer/tables_controller.rb', line 45

def mini_erd
  @erd_data = fetch_mini_erd_for_table(@table_name)

  respond_to do |format|
    format.json { render json: @erd_data }
    format.html { render layout: false }
  end
end

#queryObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/dbviewer/tables_controller.rb', line 54

def query
  @read_only_mode = true # Flag to indicate we're in read-only mode
  @columns = fetch_table_columns(@table_name)
  @tables = fetch_tables  # Fetch tables for sidebar

  @query = prepare_query(@table_name, params[:query])
  @records = begin
    execute_query(@query)
  rescue => e
    @error = "Error executing query: #{e.message}"
    nil
  end

  render :query
end

#showObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/dbviewer/tables_controller.rb', line 13

def show
  query_params = Dbviewer::Datatable::QueryParams.new(
    page: @current_page,
    per_page: @per_page,
    order_by: @order_by,
    direction: @order_direction,
    column_filters: @column_filters.reject { |_, v| v.blank? }
  )

  # Get all datatable data in one method call
  datatable_data = fetch_datatable_data(@table_name, query_params)

  # Assign to instance variables for view access
  @total_count = datatable_data[:total_count]
  @records = datatable_data[:records]
  @total_pages = datatable_data[:total_pages]
  @columns = datatable_data[:columns]
  @metadata = datatable_data[:metadata]

  respond_to do |format|
    format.html # Default HTML response
    format.json do
      render json: {
        table_name: @table_name,
        columns: @columns,
        metadata: @metadata,
        record_count: @total_count
      }
    end
  end
end