Class: DbdocEngine::DbDesignDynamicTableQueries
- Inherits:
-
Object
- Object
- DbdocEngine::DbDesignDynamicTableQueries
- Defined in:
- app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb
Constant Summary collapse
- INCLUDES_ASSOCIATIONS =
Associations to eager load with table queries
%i[db_design_dynamic_columns db_design_table_group].freeze
Class Method Summary collapse
- .active_tables_with_columns ⇒ Object
-
.all_deleted_tables_with_columns_and_groups ⇒ ActiveRecord::Relation
Get soft-deleted tables with their associations.
-
.all_tables_ordered_by_group ⇒ Object
Ordered table groups with associated tables.
-
.all_tables_with_columns_and_groups ⇒ ActiveRecord::Relation
Get all active tables with their columns and groups.
-
.check_column_is_referenced(table_name, column_name, only_active: false) ⇒ Boolean
Check if a column is referenced as foreign key.
-
.count_all_deleted_tables ⇒ Integer
Count all soft-deleted tables.
-
.count_all_tables ⇒ Integer
Count all active tables.
-
.count_tables_in_group(group, include_deleted = false) ⇒ Integer
Count tables belonging to a specific group.
-
.filter_by_group(relation, group_id) ⇒ ActiveRecord::Relation
Filter relation by group ID.
-
.find_all_except(id) ⇒ ActiveRecord::Relation
Get all tables except specified ID.
-
.find_by_table_name(table_name) ⇒ DbDesignDynamicTable?
Find table by its name.
-
.find_table_group_name(group_id) ⇒ String?
Get group name for a table group ID.
-
.find_table_name(table_id) ⇒ String?
Get table name by ID.
-
.find_table_with_columns_and_group(id) ⇒ DbDesignDynamicTable?
Find a table by ID with eager-loaded columns and group.
-
.find_table_with_deleted(id) ⇒ DbDesignDynamicTable
Find table including soft-deleted records.
-
.find_table_with_group(id) ⇒ DbDesignDynamicTable
Find table with eager-loaded group information.
-
.group_has_active_tables?(group_id, include_deleted = false) ⇒ Boolean
Check if group contains active/non-deleted tables.
-
.model ⇒ Class
Centralized access to the DynamicTable model class.
-
.search_tables(relation, search_term) ⇒ ActiveRecord::Relation
Search tables by name or group name.
- .table_exists?(table_name) ⇒ Boolean
Class Method Details
.active_tables_with_columns ⇒ Object
154 155 156 157 158 159 160 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 154 def active_tables_with_columns model.where(deleted_at: nil) .where( "EXISTS (SELECT 1 FROM db_design_dynamic_columns " \ "WHERE db_design_dynamic_columns.db_design_dynamic_table_id = db_design_dynamic_tables.id)" ) end |
.all_deleted_tables_with_columns_and_groups ⇒ ActiveRecord::Relation
Get soft-deleted tables with their associations
50 51 52 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 50 def all_deleted_tables_with_columns_and_groups model.with_deleted.only_deleted.includes(INCLUDES_ASSOCIATIONS) end |
.all_tables_ordered_by_group ⇒ Object
Ordered table groups with associated tables
41 42 43 44 45 46 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 41 def all_tables_ordered_by_group model.includes(INCLUDES_ASSOCIATIONS) .joins(:db_design_table_group) .order(Arel.sql('LOWER(db_design_table_groups.group_name) ASC, LOWER(db_design_dynamic_tables.table_name) ASC')) end |
.all_tables_with_columns_and_groups ⇒ ActiveRecord::Relation
Get all active tables with their columns and groups
36 37 38 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 36 def all_tables_with_columns_and_groups model.includes(INCLUDES_ASSOCIATIONS) end |
.check_column_is_referenced(table_name, column_name, only_active: false) ⇒ Boolean
Check if a column is referenced as foreign key
92 93 94 95 96 97 98 99 100 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 92 def check_column_is_referenced(table_name, column_name, only_active: false) query = DbDesignDynamicColumn.where( foreign_table_name: table_name, foreign_column_name: column_name ) query = active_tables_scope(query) if only_active query.exists? end |
.count_all_deleted_tables ⇒ Integer
Count all soft-deleted tables
62 63 64 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 62 def count_all_deleted_tables model.with_deleted.only_deleted.count end |
.count_all_tables ⇒ Integer
Count all active tables
56 57 58 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 56 def count_all_tables model.count end |
.count_tables_in_group(group, include_deleted = false) ⇒ Integer
Count tables belonging to a specific group
19 20 21 22 23 24 25 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 19 def count_tables_in_group(group, include_deleted = false) if include_deleted group.db_design_dynamic_tables.with_deleted.size else group.db_design_dynamic_tables.size end end |
.filter_by_group(relation, group_id) ⇒ ActiveRecord::Relation
Filter relation by group ID
137 138 139 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 137 def filter_by_group(relation, group_id) relation.where(db_design_table_group_id: group_id) end |
.find_all_except(id) ⇒ ActiveRecord::Relation
Get all tables except specified ID
76 77 78 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 76 def find_all_except(id) model.where.not(id: id) end |
.find_by_table_name(table_name) ⇒ DbDesignDynamicTable?
Find table by its name
83 84 85 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 83 def find_by_table_name(table_name) model.find_by(table_name: table_name) end |
.find_table_group_name(group_id) ⇒ String?
Get group name for a table group ID
105 106 107 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 105 def find_table_group_name(group_id) DbDesignTableGroupQueries.find_group_name(group_id) end |
.find_table_name(table_id) ⇒ String?
Get table name by ID
112 113 114 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 112 def find_table_name(table_id) model.find_by(id: table_id)&.table_name end |
.find_table_with_columns_and_group(id) ⇒ DbDesignDynamicTable?
Find a table by ID with eager-loaded columns and group
30 31 32 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 30 def find_table_with_columns_and_group(id) model.includes(INCLUDES_ASSOCIATIONS).find_by(id: id) end |
.find_table_with_deleted(id) ⇒ DbDesignDynamicTable
Find table including soft-deleted records
69 70 71 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 69 def find_table_with_deleted(id) model.with_deleted.find(id) end |
.find_table_with_group(id) ⇒ DbDesignDynamicTable
Find table with eager-loaded group information
129 130 131 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 129 def find_table_with_group(id) model.includes(:db_design_table_group).find(id) end |
.group_has_active_tables?(group_id, include_deleted = false) ⇒ Boolean
Check if group contains active/non-deleted tables
120 121 122 123 124 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 120 def group_has_active_tables?(group_id, include_deleted = false) query = model.where(db_design_table_group_id: group_id) query = include_deleted ? query.with_deleted : query.without_deleted query.exists? end |
.model ⇒ Class
Centralized access to the DynamicTable model class
12 13 14 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 12 def model DbDesignDynamicTable end |
.search_tables(relation, search_term) ⇒ ActiveRecord::Relation
Search tables by name or group name
145 146 147 148 149 150 151 152 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 145 def search_tables(relation, search_term) return relation if search_term.blank? sanitized_term = "%#{search_term.downcase}%" relation.left_joins(:db_design_table_group) .where("LOWER(db_design_dynamic_tables.table_name) LIKE :term OR LOWER(db_design_table_groups.group_name) LIKE :term", term: sanitized_term) end |
.table_exists?(table_name) ⇒ Boolean
162 163 164 |
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_queries.rb', line 162 def table_exists?(table_name) model.exists?(table_name: table_name, deleted_at: nil) end |