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

#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

#indexObject



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

def index
  tables_count = fetch_tables_count
  render_success(total_tables: tables_count)
end

#recordsObject



9
10
11
12
# File 'app/controllers/dbviewer/api/tables_controller.rb', line 9

def records
  tables_stats = fetch_tables_stats
  render_success(tables_stats)
end

#relationship_countsObject



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/dbviewer/api/tables_controller.rb', line 19

def relationship_counts
  table_name = params[:id]
  record_id = params[:record_id]

  unless table_name.present? && record_id.present?
    render_error("Table name and record ID are required", 400)
    return
  end

  begin
    # Get table metadata to find relationships
     = (table_name)

    unless 
      render_error("Table not found", 404)
      return
    end

    # Get reverse foreign keys (has_many relationships)
    reverse_foreign_keys = .dig(:reverse_foreign_keys) || []

    relationship_counts = reverse_foreign_keys.map do |rel|
      begin
        # Count records in the related table that reference this record
        count_query = "SELECT COUNT(*) as count FROM #{rel[:from_table]} WHERE #{rel[:column]} = ?"
        result = database_manager.connection.exec_query(count_query, "Count Query", [ record_id ])
        count = result.rows.first&.first || 0

        {
          table: rel[:from_table],
          foreign_key: rel[:column],
          count: count.to_i
        }
      rescue => e
        Rails.logger.error "Error counting relationships for #{rel[:from_table]}: #{e.message}"
        {
          table: rel[:from_table],
          foreign_key: rel[:column],
          count: 0,
          error: e.message
        }
      end
    end

    render_success({
      table_name: table_name,
      record_id: record_id,
      relationships: relationship_counts
    })
  rescue => e
    Rails.logger.error "Error fetching relationship counts: #{e.message}"
    render_error("Error fetching relationship counts: #{e.message}", 500)
  end
end

#relationships_countObject



14
15
16
17
# File 'app/controllers/dbviewer/api/tables_controller.rb', line 14

def relationships_count
  total_relationships = calculate_total_relationships
  render_success(total_relationships: total_relationships)
end