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, #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_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

Instance Method Details

#export_csvObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/dbviewer/tables_controller.rb', line 64

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

  csv_data = export_table_to_csv(table_name, limit, include_headers)

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

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

#indexObject



5
6
7
# File 'app/controllers/dbviewer/tables_controller.rb', line 5

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

#queryObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/dbviewer/tables_controller.rb', line 49

def query
  @table_name = params[:id]
  @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



9
10
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
# File 'app/controllers/dbviewer/tables_controller.rb', line 9

def show
  @table_name = params[:id]
  @columns = fetch_table_columns(@table_name)
  @metadata = (@table_name)
  @tables = fetch_tables_with_stats  # Fetch tables for sidebar

  set_pagination_params
  set_sorting_params

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

  if @column_filters.present? && @column_filters.values.any?(&:present?)
    @total_count = fetch_filtered_record_count(@table_name, @column_filters)
  else
    @total_count = fetch_table_record_count(@table_name)
  end

  @total_pages = calculate_total_pages(@total_count, @per_page)
  @records = fetch_table_records(@table_name)

  # 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