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, #set_pagination_params, #set_sorting_params

Methods included from ErrorHandling

#handle_database_error, #log_error

Methods included from DatabaseOperations

#calculate_schema_size, #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_with_stats, #get_database_name, #prepare_query, #safe_quote_table_name, #table_query_operations

Instance Method Details

#export_csvObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/dbviewer/tables_controller.rb', line 99

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

  limit = (params[:limit] || 10000).to_i
  include_headers = params[:include_headers] != "0"

  # Apply global creation filters
  set_global_filters

  # Create query parameters for export
  query_params = Dbviewer::TableQueryParams.new(
    page: 1,
    per_page: limit,
    order_by: nil,
    direction: "asc",
    column_filters: @column_filters.reject { |_, v| v.blank? }
  )

  # Get filtered data for export
  csv_data = export_table_to_csv(@table_name, query_params, include_headers)

  # Set filename with timestamp for uniqueness
  timestamp = Time.now.strftime("%Y%m%d%H%M%S")

  # Add filter info to filename if filters are applied
  filename_suffix = ""
  if @creation_filter_start.present? || @creation_filter_end.present?
    filename_suffix = "_filtered"
  end

  filename = "#{@table_name}#{filename_suffix}_#{timestamp}.csv"

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

#indexObject



7
8
9
# File 'app/controllers/dbviewer/tables_controller.rb', line 7

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

#mini_erdObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/dbviewer/tables_controller.rb', line 59

def mini_erd
  begin
    @erd_data = fetch_mini_erd_for_table(@table_name)

    if @erd_data[:error].present?
      Rails.logger.error("Mini ERD error: #{@erd_data[:error]}")
    end

    respond_to do |format|
      format.json { render json: @erd_data }
      format.html { render layout: false }
    end
  rescue => e
    Rails.logger.error("Error generating Mini ERD: #{e.message}")
    Rails.logger.error(e.backtrace.join("\n"))

    @error_message = e.message
    @erd_data = { tables: [], relationships: [], error: @error_message }

    respond_to do |format|
      format.json { render json: { error: @error_message }, status: :internal_server_error }
      format.html { render layout: false }
    end
  end
end

#queryObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/dbviewer/tables_controller.rb', line 85

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

  # Set active table for sidebar highlighting
  @active_table = @table_name

  prepare_query
  execute_query

  render :query
end

#showObject



11
12
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/dbviewer/tables_controller.rb', line 11

def show
  set_pagination_params
  set_sorting_params

  # Get column filters from params first
  @column_filters = params[:column_filters].presence ? params[:column_filters].to_enum.to_h : {}

  # Then apply global creation filters (this will modify @column_filters)
  set_global_filters

  # Now create the query params with the combined filters
  query_params = Dbviewer::TableQueryParams.new(
    page: @current_page,
    per_page: @per_page,
    order_by: @order_by,
    direction: @order_direction,
    column_filters: @column_filters.reject { |_, v| v.blank? }
  )
  @total_count = fetch_total_count(@table_name, query_params)
  @records = fetch_table_records(@table_name, query_params)
  @total_pages = calculate_total_pages(@total_count, @per_page)
  @columns = fetch_table_columns(@table_name)
  @metadata = (@table_name)

  if @records.nil?
    column_names = @columns.map { |c| c[:name] }
    @records = ActiveRecord::Result.new(column_names, [])
  end

  # Fetch timestamp visualization data if the table has a created_at column
  if has_timestamp_column?(@table_name)
    @time_grouping = params[:time_group] || "daily"
    @timestamp_data = fetch_timestamp_data(@table_name, @time_grouping)
  end

  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