Module: RedhillonrailsCore::ActiveRecord::ConnectionAdapters::PostgresqlAdapter
- Defined in:
- lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_index(table_name, column_name, options = {}) ⇒ Object
- #foreign_keys(table_name, name = nil) ⇒ Object
- #indexes(table_name, name = nil) ⇒ Object
- #reverse_foreign_keys(table_name, name = nil) ⇒ Object
- #supports_partial_indexes? ⇒ Boolean
- #view_definition(view_name, name = nil) ⇒ Object
- #views(name = nil) ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 8 9 |
# File 'lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb', line 5 def self.included(base) base.class_eval do remove_method :indexes end end |
Instance Method Details
#add_index(table_name, column_name, options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb', line 11 def add_index(table_name, column_name, = {}) column_name, = [], column_name if column_name.is_a?(Hash) column_names = Array(column_name) if column_names.empty? raise ArgumentError, "No columns and :expression missing from options - cannot create index" if [:expression].blank? raise ArgumentError, "Index name not given. Pass :name option" if [:name].blank? end index_type = [:unique] ? "UNIQUE" : "" index_name = [:name] || index_name(table_name, column_names) conditions = [:conditions] if column_names.empty? then sql = "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} #{[:expression]}" else quoted_column_names = column_names.map { |e| [:case_sensitive] == false && e.to_s !~ /_id$/ ? "LOWER(#{quote_column_name(e)})" : quote_column_name(e) } sql = "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{quoted_column_names.join(", ")})" sql += " WHERE (#{ ::ActiveRecord::Base.send(:sanitize_sql, conditions, quote_table_name(table_name)) })" if conditions end execute sql end |
#foreign_keys(table_name, name = nil) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb', line 78 def foreign_keys(table_name, name = nil) load_foreign_keys(<<-SQL, name) SELECT f.conname, pg_get_constraintdef(f.oid), t.relname FROM pg_class t, pg_constraint f WHERE f.conrelid = t.oid AND f.contype = 'f' AND t.relname = '#{table_name}' SQL end |
#indexes(table_name, name = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb', line 38 def indexes(table_name, name = nil) schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',') result = query(<<-SQL, name) SELECT distinct i.relname, d.indisunique, d.indkey, m.amname, t.oid, pg_get_expr(d.indpred, t.oid), pg_get_expr(d.indexprs, t.oid) FROM pg_class t, pg_class i, pg_index d, pg_am m WHERE i.relkind = 'i' AND i.relam = m.oid AND d.indexrelid = i.oid AND d.indisprimary = 'f' AND t.oid = d.indrelid AND t.relname = '#{table_name}' AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (#{schemas}) ) ORDER BY i.relname SQL result.map do |(index_name, is_unique, indkey, kind, oid, conditions, expression)| unique = (is_unique == 't') index_keys = indkey.split(" ") columns = Hash[query(<<-SQL, "Columns for index #{index_name} on #{table_name}")] SELECT a.attnum, a.attname FROM pg_attribute a WHERE a.attrelid = #{oid} AND a.attnum IN (#{index_keys.join(",")}) SQL column_names = columns.values_at(*index_keys).compact if md = expression.try(:match, /^lower\(\(?([^)]+)\)?(::text)?\)$/i) column_names << md[1] end index = ::ActiveRecord::ConnectionAdapters::IndexDefinition.new(table_name, index_name, unique, column_names) index.conditions = conditions index.case_sensitive = !(expression =~ /lower/i) index.kind = kind unless kind.downcase == "btree" index.expression = expression index end end |
#reverse_foreign_keys(table_name, name = nil) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb', line 88 def reverse_foreign_keys(table_name, name = nil) load_foreign_keys(<<-SQL, name) SELECT f.conname, pg_get_constraintdef(f.oid), t2.relname FROM pg_class t, pg_class t2, pg_constraint f WHERE f.confrelid = t.oid AND f.conrelid = t2.oid AND f.contype = 'f' AND t.relname = '#{table_name}' SQL end |
#supports_partial_indexes? ⇒ Boolean
34 35 36 |
# File 'lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb', line 34 def supports_partial_indexes? true end |
#view_definition(view_name, name = nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb', line 108 def view_definition(view_name, name = nil) result = query(<<-SQL, name) SELECT pg_get_viewdef(oid) FROM pg_class WHERE relkind = 'v' AND relname = '#{view_name}' SQL row = result.first row.first unless row.nil? end |
#views(name = nil) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb', line 99 def views(name = nil) schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',') query(<<-SQL, name).map { |row| row[0] } SELECT viewname FROM pg_views WHERE schemaname IN (#{schemas}) SQL end |