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



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

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
end

#queryObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/dbviewer/tables_controller.rb', line 41

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

  @total_count = fetch_table_record_count(@table_name)
  @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