Module: SchemaPlus::ActiveRecord::ConnectionAdapters::Sqlite3Adapter
- Defined in:
- lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb
Overview
SchemaPlus includes an Sqlite3 implementation of the AbstractAdapter extensions.
Class Method Summary collapse
-
.included(base) ⇒ Object
:enddoc:.
Instance Method Summary collapse
- #add_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {}) ⇒ Object
- #drop_table(name, options = {}) ⇒ Object
- #foreign_keys(table_name, name = nil) ⇒ Object
- #indexes_with_schema_plus(table_name, name = nil) ⇒ Object
- #initialize(*args) ⇒ Object
- #remove_foreign_key(table_name, foreign_key_name) ⇒ Object
-
#rename_table_with_schema_plus(oldname, newname) ⇒ Object
:nodoc:.
- #reverse_foreign_keys(table_name, name = nil) ⇒ Object
- #view_definition(view_name, name = nil) ⇒ Object
- #views(name = nil) ⇒ Object
Class Method Details
.included(base) ⇒ Object
:enddoc:
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 19 def self.included(base) base.class_eval do alias_method_chain :indexes, :schema_plus alias_method_chain :rename_table, :schema_plus end if ::ActiveRecord::VERSION::MAJOR.to_i < 4 ::ActiveRecord::ConnectionAdapters::SQLiteColumn.send(:include, SQLiteColumn) unless ::ActiveRecord::ConnectionAdapters::SQLiteColumn.include?(SQLiteColumn) else ::ActiveRecord::ConnectionAdapters::SQLite3Column.send(:include, SQLiteColumn) unless ::ActiveRecord::ConnectionAdapters::SQLite3Column.include?(SQLiteColumn) end end |
Instance Method Details
#add_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {}) ⇒ Object
52 53 54 |
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 52 def add_foreign_key(table_name, column_names, references_table_name, references_column_names, = {}) raise NotImplementedError, "Sqlite3 does not support altering a table to add foreign key constraints (table #{table_name.inspect} column #{column_names.inspect})" end |
#drop_table(name, options = {}) ⇒ Object
60 61 62 |
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 60 def drop_table(name, ={}) super(name, .except(:cascade)) end |
#foreign_keys(table_name, name = nil) ⇒ Object
64 65 66 |
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 64 def foreign_keys(table_name, name = nil) get_foreign_keys(table_name, name) end |
#indexes_with_schema_plus(table_name, name = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 36 def indexes_with_schema_plus(table_name, name = nil) indexes = indexes_without_schema_plus(table_name, name) exec_query("SELECT name, sql FROM sqlite_master WHERE type = 'index'").map do |row| if (desc_columns = row['sql'].scan(/['"`]?(\w+)['"`]? DESC\b/).flatten).any? index = indexes.detect{ |i| i.name == row['name'] } index.orders = Hash[index.columns.map {|column| [column, desc_columns.include?(column) ? :desc : :asc]}] end end indexes end |
#initialize(*args) ⇒ Object
31 32 33 34 |
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 31 def initialize(*args) super execute('PRAGMA FOREIGN_KEYS = ON') end |
#remove_foreign_key(table_name, foreign_key_name) ⇒ Object
56 57 58 |
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 56 def remove_foreign_key(table_name, foreign_key_name) raise NotImplementedError, "Sqlite3 does not support altering a table to remove foreign key constraints (table #{table_name.inspect} constraint #{foreign_key_name.inspect})" end |
#rename_table_with_schema_plus(oldname, newname) ⇒ Object
:nodoc:
47 48 49 50 |
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 47 def rename_table_with_schema_plus(oldname, newname) #:nodoc: rename_table_without_schema_plus(oldname, newname) rename_indexes_and_foreign_keys(oldname, newname) end |
#reverse_foreign_keys(table_name, name = nil) ⇒ Object
68 69 70 |
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 68 def reverse_foreign_keys(table_name, name = nil) get_foreign_keys(nil, name).select{|definition| definition.references_table_name == table_name} end |
#view_definition(view_name, name = nil) ⇒ Object
76 77 78 79 |
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 76 def view_definition(view_name, name = nil) sql = execute("SELECT sql FROM sqlite_master WHERE type='view' AND name=#{quote(view_name)}", name).collect{|row| row["sql"]}.first sql.sub(/^CREATE VIEW \S* AS\s+/im, '') unless sql.nil? end |
#views(name = nil) ⇒ Object
72 73 74 |
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 72 def views(name = nil) execute("SELECT name FROM sqlite_master WHERE type='view'", name).collect{|row| row["name"]} end |