Class: Dbviewer::Datatable::QueryOperations
- Inherits:
-
Object
- Object
- Dbviewer::Datatable::QueryOperations
- Defined in:
- lib/dbviewer/datatable/query_operations.rb
Overview
QueryOperations handles CRUD operations and data querying for database tables It provides methods to fetch, filter and manipulate data in tables
Instance Attribute Summary collapse
-
#adapter_name ⇒ Object
readonly
Returns the value of attribute adapter_name.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
-
#analyze_query(table_name, query_params) ⇒ Hash
Analyze query patterns and return performance recommendations.
-
#column_count(table_name) ⇒ Integer
Get the number of columns in a table.
-
#filtered_record_count(table_name, column_filters = {}) ⇒ Integer
Get the number of records in a table with filters applied.
-
#initialize(connection, dynamic_model_factory, table_metadata_manager) ⇒ QueryOperations
constructor
Initialize with dependencies.
-
#record_count(table_name) ⇒ Integer
Get the number of records in a table (alias for table_count).
-
#table_count(table_name) ⇒ Integer
Get the total count of records in a table.
-
#table_records(table_name, params) ⇒ ActiveRecord::Result
Get records from a table with pagination and sorting.
Constructor Details
#initialize(connection, dynamic_model_factory, table_metadata_manager) ⇒ QueryOperations
Initialize with dependencies
12 13 14 15 16 17 18 |
# File 'lib/dbviewer/datatable/query_operations.rb', line 12 def initialize(connection, dynamic_model_factory, ) @connection = connection @adapter_name = connection.adapter_name.downcase @dynamic_model_factory = dynamic_model_factory @table_metadata_manager = @query_analyzer = ::Dbviewer::Query::Analyzer.new(connection) end |
Instance Attribute Details
#adapter_name ⇒ Object (readonly)
Returns the value of attribute adapter_name.
6 7 8 |
# File 'lib/dbviewer/datatable/query_operations.rb', line 6 def adapter_name @adapter_name end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
6 7 8 |
# File 'lib/dbviewer/datatable/query_operations.rb', line 6 def connection @connection end |
Instance Method Details
#analyze_query(table_name, query_params) ⇒ Hash
Analyze query patterns and return performance recommendations
97 98 99 |
# File 'lib/dbviewer/datatable/query_operations.rb', line 97 def analyze_query(table_name, query_params) @query_analyzer.analyze_query(table_name, query_params) end |
#column_count(table_name) ⇒ Integer
Get the number of columns in a table
23 24 25 |
# File 'lib/dbviewer/datatable/query_operations.rb', line 23 def column_count(table_name) table_columns(table_name).size end |
#filtered_record_count(table_name, column_filters = {}) ⇒ Integer
Get the number of records in a table with filters applied
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dbviewer/datatable/query_operations.rb', line 77 def filtered_record_count(table_name, column_filters = {}) return table_count(table_name) unless column_filters.present? model = get_model_for(table_name) query = model.all # Apply filters in the same way as table_records query = apply_column_filters(query, table_name, column_filters) query.count rescue => e Rails.logger.error("[DBViewer] Error counting filtered records in table #{table_name}: #{e.}") 0 end |
#record_count(table_name) ⇒ Integer
Get the number of records in a table (alias for table_count)
69 70 71 |
# File 'lib/dbviewer/datatable/query_operations.rb', line 69 def record_count(table_name) table_count(table_name) end |
#table_count(table_name) ⇒ Integer
Get the total count of records in a table
59 60 61 62 63 64 |
# File 'lib/dbviewer/datatable/query_operations.rb', line 59 def table_count(table_name) get_model_for(table_name).count rescue => e Rails.logger.error("[DBViewer] Error counting records in table #{table_name}: #{e.}") 0 end |
#table_records(table_name, params) ⇒ ActiveRecord::Result
Get records from a table with pagination and sorting
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dbviewer/datatable/query_operations.rb', line 31 def table_records(table_name, params) model = get_model_for(table_name) query = model.all # Apply column filters if provided query = apply_column_filters(query, table_name, params.column_filters) # Apply sorting if provided if params.order_by.present? && column_exists?(table_name, params.order_by) query = query.order("#{connection.quote_column_name(params.order_by)} #{params.direction}") end # Apply pagination records = query.limit(params.per_page).offset((params.page - 1) * params.per_page) # Get column names for consistent ordering column_names = table_columns(table_name).map { |c| c[:name] } # Format results to_result_set(records, column_names) rescue => e Rails.logger.error("[DBViewer] Error executing table query: #{e.}") raise e end |