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 ErrorHandling

#handle_database_error, #log_error

Methods included from DatabaseOperations

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

Instance Method Details

#export_csvObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/dbviewer/tables_controller.rb', line 65

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

  prepare_query
  execute_query

  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? }
  )
  @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 { |column| column[:name] }
    @records = ActiveRecord::Result.new(column_names, [])
  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