Class: RailsLens::Schema::Adapters::Sqlite3

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_lens/schema/adapters/sqlite3.rb

Instance Attribute Summary

Attributes inherited from Base

#connection, #table_name

Instance Method Summary collapse

Methods inherited from Base

#generate_view_annotation, #initialize, #unqualified_table_name

Constructor Details

This class inherits a constructor from RailsLens::Schema::Adapters::Base

Instance Method Details

#adapter_nameObject



7
8
9
# File 'lib/rails_lens/schema/adapters/sqlite3.rb', line 7

def adapter_name
  'SQLite'
end

#fetch_view_metadataObject

Fetch all view metadata in a single consolidated query



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rails_lens/schema/adapters/sqlite3.rb', line 106

def 
  result = connection.exec_query("    SELECT sql FROM sqlite_master\n    WHERE type = 'view' AND name = '\#{connection.quote_string(table_name)}'\n    LIMIT 1\n  SQL\n\n  return nil if result.rows.empty?\n\n  definition = result.rows.first&.first&.strip\n  return nil unless definition\n\n  # Parse dependencies from the SQL definition\n  tables = []\n  definition.scan(/(?:FROM|JOIN)\\s+(\\w+)/i) do |match|\n    table_name_match = match[0]\n    # Exclude the view itself and common SQL keywords\n    if !table_name_match.downcase.in?(%w[select where order group having limit offset]) &&\n       tables.exclude?(table_name_match) &&\n       table_name_match != table_name\n      tables << table_name_match\n    end\n  end\n\n  {\n    type: 'regular',  # SQLite only supports regular views\n    updatable: false, # SQLite views are generally read-only\n    dependencies: tables.sort\n  }\nrescue ActiveRecord::StatementInvalid, SQLite3::Exception => e\n  RailsLens.logger.debug { \"Failed to fetch view metadata for \#{table_name}: \#{e.message}\" }\n  nil\nend\n".squish, 'SQLite View Metadata')

#generate_annotation(model_class) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/rails_lens/schema/adapters/sqlite3.rb', line 11

def generate_annotation(model_class)
  if model_class && ModelDetector.view_exists?(model_class)
    generate_view_annotation(model_class)
  else
    generate_table_annotation(model_class)
  end
end

#generate_table_annotation(_model_class) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rails_lens/schema/adapters/sqlite3.rb', line 19

def generate_table_annotation(_model_class)
  lines = []
  lines << "table = \"#{table_name}\""
  lines << "database_dialect = \"#{database_dialect}\""
  lines << ''

  add_columns_toml(lines)
  add_indexes_toml(lines) if show_indexes?
  add_foreign_keys_toml(lines) if show_foreign_keys?
  add_sqlite_pragmas_toml(lines)

  lines.join("\n")
end

#view_definitionObject



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rails_lens/schema/adapters/sqlite3.rb', line 156

def view_definition
  result = connection.exec_query("    SELECT sql FROM sqlite_master\n    WHERE type = 'view' AND name = '\#{connection.quote_string(table_name)}'\n    LIMIT 1\n  SQL\n\n  result.rows.first&.first&.strip\nrescue ActiveRecord::StatementInvalid, SQLite3::Exception\n  nil\nend\n".squish, 'SQLite View Definition')

#view_dependenciesObject



151
152
153
154
# File 'lib/rails_lens/schema/adapters/sqlite3.rb', line 151

def view_dependencies
   ||= 
  &.dig(:dependencies) || []
end

#view_last_refreshedObject



172
173
174
# File 'lib/rails_lens/schema/adapters/sqlite3.rb', line 172

def view_last_refreshed
  nil # SQLite doesn't have materialized views
end

#view_refresh_strategyObject



168
169
170
# File 'lib/rails_lens/schema/adapters/sqlite3.rb', line 168

def view_refresh_strategy
  nil # SQLite doesn't have materialized views
end

#view_typeObject

Legacy methods - kept for backward compatibility but now use consolidated query



141
142
143
144
# File 'lib/rails_lens/schema/adapters/sqlite3.rb', line 141

def view_type
   ||= 
  &.dig(:type)
end

#view_updatable?Boolean

Returns:



146
147
148
149
# File 'lib/rails_lens/schema/adapters/sqlite3.rb', line 146

def view_updatable?
   ||= 
  &.dig(:updatable) || false
end