Module: RedhillonrailsCore::ActiveRecord::ConnectionAdapters::Sqlite3Adapter

Defined in:
lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
# File 'lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb', line 5

def self.included(base)
  base.class_eval do
    alias_method_chain :tables, :redhillonrails_core
  end
end

Instance Method Details

#add_foreign_key(from_table_name, from_column_names, to_table_name, to_column_names, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb', line 16

def add_foreign_key(from_table_name, from_column_names, to_table_name, to_column_names, options = {})
  initialize_sqlite3_foreign_key_table
  from_column_names = Array(from_column_names)
  to_column_names = Array(to_column_names)
  fk_name = options[:name] || ["fk", from_table_name, *to_column_names].join("_")

  columns = %w(name from_table_name from_column_names to_table_name to_column_names)
  values = [fk_name, from_table_name, from_column_names.join(","), to_table_name, to_column_names.join(",")]

  quoted_values = values.map { |x| quote(x.to_s) }.join(",")

  # TODO: support options

  insert <<-SQL
INSERT INTO #{sqlite3_foreign_key_table}(#{quoted_columns(columns)})
VALUES (#{quoted_values})
  SQL
end

#foreign_keys(table_name, name = nil) ⇒ Object



54
55
56
# File 'lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb', line 54

def foreign_keys(table_name, name = nil)
  load_foreign_keys("from_table_name", table_name, name)
end

#move_table(from, to, options = {}, &block) ⇒ Object

:nodoc:



11
12
13
14
# File 'lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb', line 11

def move_table(from, to, options = {}, &block) #:nodoc:
  copy_table(from, to, options, &block)
  drop_table(from, options)
end

#remove_foreign_key(table_name, foreign_key_name, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb', line 35

def remove_foreign_key(table_name, foreign_key_name, options = {})
  return if options[:temporary] == true
  initialize_sqlite3_foreign_key_table

  rows_deleted = delete <<-SQL
DELETE FROM #{sqlite3_foreign_key_table}
 WHERE #{quote_column_name("name")} = #{quote(foreign_key_name.to_s)}
   AND #{quote_column_name("from_table_name")} = #{quote(table_name.to_s)}
   SQL

   if rows_deleted != 1
     raise ActiveRecord::ActiveRecordError, "Foreign-key '#{foreign_key_name}' on table '#{table_name}' not found"
   end
end

#reverse_foreign_keys(table_name, name = nil) ⇒ Object



58
59
60
# File 'lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb', line 58

def reverse_foreign_keys(table_name, name = nil)
  load_foreign_keys("to_table_name", table_name, name)
end

#tables_with_redhillonrails_core(name = nil) ⇒ Object



50
51
52
# File 'lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb', line 50

def tables_with_redhillonrails_core(name=nil)
  tables_without_redhillonrails_core.reject{ |name| name == sqlite3_foreign_key_table }
end

#view_definition(view_name, name = nil) ⇒ Object



66
67
68
# File 'lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb', line 66

def view_definition(view_name, name = nil)
  select_one("SELECT sql FROM sqlite_master WHERE type = 'view' AND name = #{quote_table_name(view_name)}")["sql"]
end

#views(name = nil) ⇒ Object



62
63
64
# File 'lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb', line 62

def views(name = nil)
  execute("SELECT name FROM sqlite_master WHERE type = 'view'").collect { |definition| definition["name"] }
end