Class: DbdocEngine::DbDesignDynamicTableQueries

Inherits:
Object
  • Object
show all
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

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

Returns:

  • (ActiveRecord::Relation) —

    Collection of deleted tables



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

Returns:

  • (ActiveRecord::Relation) —

    Collection of tables with associations



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

Parameters:

  • table_name (String) —

    Source table name

  • column_name (String) —

    Column name to check

  • only_active (Boolean) (defaults to: false) —

    Only check active tables

Returns:

  • (Boolean) —

    Reference existence status



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

Returns:

  • (Integer) —

    Total deleted table count



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

Returns:

  • (Integer) —

    Total active table count



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

Parameters:

Returns:

  • (Integer) —

    Number of tables in the 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

Parameters:

  • relation (ActiveRecord::Relation) —

    Base query to filter

  • group_id (Integer) —

    Group ID to filter by

Returns:

  • (ActiveRecord::Relation) —

    Filtered query



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

Parameters:

  • id (Integer) —

    Table ID to exclude

Returns:

  • (ActiveRecord::Relation) —

    Tables excluding 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

Parameters:

  • table_name (String) —

    Name to search for

Returns:



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

Parameters:

  • group_id (Integer) —

    Group ID to lookup

Returns:

  • (String, nil) —

    Group name or nil



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

Parameters:

  • table_id (Integer) —

    Table ID to lookup

Returns:

  • (String, nil) —

    Table name or nil



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

Parameters:

  • id (Integer) —

    Table ID to find

Returns:



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

Parameters:

  • id (Integer) —

    Table ID to find

Returns:



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

Parameters:

  • id (Integer) —

    Table ID to find

Returns:



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

Parameters:

  • group_id (Integer) —

    Group ID to check

  • include_deleted (Boolean) (defaults to: false) —

    Include deleted tables in check

Returns:

  • (Boolean) —

    Existence status



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

Returns:

  • (Class) —

    DbDesignDynamicTable 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

Parameters:

  • relation (ActiveRecord::Relation) —

    Base query to search

  • search_term (String) —

    Term to search for

Returns:

  • (ActiveRecord::Relation) —

    Modified query with search filters



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

Returns:

  • (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