lib/updateable_views_inheritance/postgresql_adapter.rb
# Creates Single Table Inheritanche-like aggregate view called +sti_aggregate_view+
# for +parent_relation+ and all its descendants. <i>The view isn't updateable.</i>
# The order of all or just the first few columns in the aggregate view can be explicitly set
# by passing array of column names as third argument.
# If there are columns with the same name but different types in two or more relations
# they will appear as a single column of type +text+ in the view.
Creates Single Table Inheritanche-like aggregate view called +sti_aggregate_view+
for +parent_relation+ and all its descendants. The view isn't updateable.
The order of all or just the first few columns in the aggregate view can be explicitly set
by passing array of column names as third argument.
If there are columns with the same name but different types in two or more relations
they will appear as a single column of type +text+ in the view.
Creates Single Table Inheritanche - like aggregate view called + sti_aggregate_view + for + parent_relation + and all its descendants . The view isn ' t updateable . The order of all or just the first few columns in the aggregate view can be explicitly set by passing array of column names as third argument . If there are columns with the same name but different types in two or more relations they will appear as a single column of type + text + in the view .
def create_single_table_inheritance_view(sti_aggregate_view, parent_relation, columns_for_view = nil)
          columns_for_view ||= []
          relations_heirarchy = get_view_hierarchy_for(parent_relation)
          relations = relations_heirarchy.flatten
          leaves_relations = get_leaves_relations(relations_heirarchy)
          all_columns = leaves_relations.map{|rel| columns(rel)}.flatten
          columns_hash = {}
          conflict_column_names = []
          all_columns.each do |col|
            c = columns_hash[col.name]
            if(c && col.sql_type != c.sql_type)
              conflict_column_names << col.name
            else
              columns_hash[col.name] = col
            end
          end
          conflict_column_names = conflict_column_names.uniq.sort if !conflict_column_names.empty?
          sorted_column_names = (columns_for_view + columns_hash.keys.sort).uniq
          parent_klass_name = Tutuf::ClassTableReflection.get_klass_for_table(parent_relation)
          quoted_inheritance_column = quote_column_name(parent_klass_name.inheritance_column)
          queries = relations.map{|rel| generate_single_table_inheritanche_union_clause(rel, sorted_column_names, conflict_column_names, columns_hash, quoted_inheritance_column)}
          unioin_clauses = queries.join("\n UNION ")
          execute <<-end_sql
            CREATE VIEW #{sti_aggregate_view} AS (
              #{unioin_clauses}
            )
          end_sql
        end
def create_single_table_inheritance_view ( sti_aggregate_view , parent_relation , columns_for_view = nil ) columns_for_view ||= [ ] relations_heirarchy = get_view_hierarchy_for ( parent_relation ) relations = relations_heirarchy . flatten leaves_relations = get_leaves_relations ( relations_heirarchy ) all_columns = leaves_relations . map { | rel | columns ( rel ) } . flatten columns_hash = { } conflict_column_names = [ ] all_columns . each do | col | c = columns_hash [ col . name ] if ( c && col . sql_type != c . sql_type ) conflict_column_names << col . name else columns_hash [ col . name ] = col end end conflict_column_names = conflict_column_names . uniq . sort if ! conflict_column_names . empty? sorted_column_names = ( columns_for_view + columns_hash . keys . sort ) . uniq parent_klass_name = Tutuf :: ClassTableReflection . get_klass_for_table ( parent_relation ) quoted_inheritance_column = quote_column_name ( parent_klass_name . inheritance_column ) queries = relations . map { | rel | generate_single_table_inheritanche_union_clause ( rel , sorted_column_names , conflict_column_names , columns_hash , quoted_inheritance_column ) } unioin_clauses = queries . join ( "\n UNION " ) execute <<-end_sql 
            CREATE VIEW  #{ sti_aggregate_view }  AS (
               #{ unioin_clauses } 
            )
           end_sql end
Creates Single Table Inheritanche-like aggregate view called +sti_aggregate_view+
for +parent_relation+ and all its descendants.
Creates Single Table Inheritanche - like aggregate view called + sti_aggregate_view + for + parent_relation + and all its descendants .
sti_aggregate_view
<not_specific>
parent_relation
<not_specific>
columns_for_view
<not_specific>