Class: Dbviewer::Api::TablesController

Inherits:
BaseController show all
Defined in:
app/controllers/dbviewer/api/tables_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#render_error, #render_success

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

#indexObject



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

def index
  tables = fetch_tables_with_stats(include_record_counts: false)
  render_success(total_tables: tables.size)
end

#recordsObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/dbviewer/api/tables_controller.rb', line 9

def records
  tables = fetch_tables_with_stats(include_record_counts: true)

  records_data = {
    total_records: tables.sum { |t| t[:record_count] },
    largest_tables: tables.sort_by { |t| -t[:record_count] }.first(10),
    empty_tables: tables.select { |t| t[:record_count] == 0 },
    avg_records_per_table: tables.any? ? (tables.sum { |t| t[:record_count] }.to_f / tables.size).round(1) : 0
  }

  render_success(records_data)
end

#relationships_countObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/dbviewer/api/tables_controller.rb', line 22

def relationships_count
  begin
    tables = fetch_tables_with_stats(include_record_counts: false)
    total_relationships = 0

    tables.each do |table|
       = (table[:name])
      total_relationships += [:foreign_keys].size if  && [:foreign_keys]
    end

    render_success(total_relationships: total_relationships)
  rescue => e
    render_error("Error calculating relationship count: #{e.message}")
  end
end