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(<<~SQL.squish, 'SQLite View Metadata')
    SELECT sql FROM sqlite_master
    WHERE type = 'view' AND name = '#{connection.quote_string(table_name)}'
    LIMIT 1
  SQL

  return nil if result.rows.empty?

  definition = result.rows.first&.first&.strip
  return nil unless definition

  # Parse dependencies from the SQL definition
  tables = []
  definition.scan(/(?:FROM|JOIN)\s+(\w+)/i) do |match|
    table_name_match = match[0]
    # Exclude the view itself and common SQL keywords
    if !table_name_match.downcase.in?(%w[select where order group having limit offset]) &&
       tables.exclude?(table_name_match) &&
       table_name_match != table_name
      tables << table_name_match
    end
  end

  {
    type: 'regular',  # SQLite only supports regular views
    updatable: false, # SQLite views are generally read-only
    dependencies: tables.sort
  }
rescue ActiveRecord::StatementInvalid, SQLite3::Exception => e
  RailsLens.logger.debug { "Failed to fetch view metadata for #{table_name}: #{e.message}" }
  nil
end

#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(<<~SQL.squish, 'SQLite View Definition')
    SELECT sql FROM sqlite_master
    WHERE type = 'view' AND name = '#{connection.quote_string(table_name)}'
    LIMIT 1
  SQL

  result.rows.first&.first&.strip
rescue ActiveRecord::StatementInvalid, SQLite3::Exception
  nil
end

#view_dependenciesObject



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

def view_dependencies
  @view_metadata ||= 
  @view_metadata&.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
  @view_metadata ||= 
  @view_metadata&.dig(:type)
end

#view_updatable?Boolean

Returns:

  • (Boolean)


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

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