lib/arjdbc/postgresql/adapter.rb
def indexes(table_name, name = nil)
      # NOTE: maybe it's better to leave things of to the JDBC API ?!
      result = select_rows(<<-SQL, 'SCHEMA')
        SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
        FROM pg_class t
        INNER JOIN pg_index d ON t.oid = d.indrelid
        INNER JOIN pg_class i ON d.indexrelid = i.oid
        WHERE i.relkind = 'i'
        AND d.indisprimary = 'f'
        AND t.relname = '#{table_name}'
        AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
        ORDER BY i.relname
      SQL

      result.map! do |row|
        index_name = row[0]
        unique = row[1].is_a?(String) ? row[1] == 't' : row[1] # JDBC gets us a boolean
        indkey = row[2].is_a?(Java::OrgPostgresqlUtil::PGobject) ? row[2].value : row[2]
        indkey = indkey.split(" ")
        inddef = row[3]
        oid = row[4]

        columns = select_rows(<<-SQL, "SCHEMA")
          SELECT a.attnum, a.attname
          FROM pg_attribute a
          WHERE a.attrelid = #{oid}
          AND a.attnum IN (#{indkey.join(",")})
        SQL

        columns = Hash[ columns.each { |column| column[0] = column[0].to_s } ]
        column_names = columns.values_at(*indkey).compact

        unless column_names.empty?
          # add info on sort order for columns (only desc order is explicitly specified, asc is the default)
          desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
          orders = desc_order_columns.any? ? Hash[ desc_order_columns.map { |column| [column, :desc] } ] : {}

          if ActiveRecord::VERSION::MAJOR > 3 # AR4 supports `where` and `using` index options
            where = inddef.scan(/WHERE (.+)$/).flatten[0]
            using = inddef.scan(/USING (.+?) /).flatten[0].to_sym

            IndexDefinition.new(table_name, index_name, unique, column_names, [], orders, where, nil, using)
          else
            IndexDefinition.new(table_name, index_name, unique, column_names, [], orders)
          end
        end
      end
      result.compact!
      result
    end
# Returns an array of indexes for the given table.
Returns an array of indexes for the given table.
Returns an array of indexes for the given table .
def indexes(table_name, name = nil)
      result = select_rows(<<-SQL, 'SCHEMA')
        SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
        FROM pg_class t
        INNER JOIN pg_index d ON t.oid = d.indrelid
        INNER JOIN pg_class i ON d.indexrelid = i.oid
        WHERE i.relkind = 'i'
        AND d.indisprimary = 'f'
        AND t.relname = '#{table_name}'
        AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
        ORDER BY i.relname
      SQL
      result.map! do |row|
        index_name = row[0]
        unique = row[1].is_a?(String) ? row[1] == 't' : row[1] 
        indkey = row[2].is_a?(Java::OrgPostgresqlUtil::PGobject) ? row[2].value : row[2]
        indkey = indkey.split(" ")
        inddef = row[3]
        oid = row[4]
        columns = select_rows(<<-SQL, "SCHEMA")
          SELECT a.attnum, a.attname
          FROM pg_attribute a
          WHERE a.attrelid = #{oid}
          AND a.attnum IN (#{indkey.join(",")})
        SQL
        columns = Hash[ columns.each { |column| column[0] = column[0].to_s } ]
        column_names = columns.values_at(*indkey).compact
        unless column_names.empty?
          desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
          orders = desc_order_columns.any? ? Hash[ desc_order_columns.map { |column| [column, :desc] } ] : {}
          if ActiveRecord::VERSION::MAJOR > 3 
            where = inddef.scan(/WHERE (.+)$/).flatten[0]
            using = inddef.scan(/USING (.+?) /).flatten[0].to_sym
            IndexDefinition.new(table_name, index_name, unique, column_names, [], orders, where, nil, using)
          else
            IndexDefinition.new(table_name, index_name, unique, column_names, [], orders)
          end
        end
      end
      result.compact!
      result
    end
def indexes ( table_name , name = nil ) result = select_rows ( <<-SQL , 'SCHEMA' ) 
        SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
        FROM pg_class t
        INNER JOIN pg_index d ON t.oid = d.indrelid
        INNER JOIN pg_class i ON d.indexrelid = i.oid
        WHERE i.relkind = 'i'
        AND d.indisprimary = 'f'
        AND t.relname = ' #{ table_name } '
        AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
        ORDER BY i.relname
       SQL result . map! do | row | index_name = row [ 0 ] unique = row [ 1 ] . is_a? ( String ) ? row [ 1 ] == 't' : row [ 1 ] indkey = row [ 2 ] . is_a? ( Java :: OrgPostgresqlUtil :: PGobject ) ? row [ 2 ] . value : row [ 2 ] indkey = indkey . split ( " " ) inddef = row [ 3 ] oid = row [ 4 ] columns = select_rows ( <<-SQL , "SCHEMA" ) 
          SELECT a.attnum, a.attname
          FROM pg_attribute a
          WHERE a.attrelid =  #{ oid } 
          AND a.attnum IN ( #{ indkey . join ( "," ) } )
         SQL columns = Hash [ columns . each { | column | column [ 0 ] = column [ 0 ] . to_s } ] column_names = columns . values_at ( * indkey ) . compact unless column_names . empty? desc_order_columns = inddef . scan ( / ( \w +) DESC / ) . flatten orders = desc_order_columns . any? ? Hash [ desc_order_columns . map { | column | [ column , :desc ] } ] : { } if ActiveRecord :: VERSION :: MAJOR > 3 where = inddef . scan ( / WHERE (.+)$ / ) . flatten [ 0 ] using = inddef . scan ( / USING (.+?)  / ) . flatten [ 0 ] . to_sym IndexDefinition . new ( table_name , index_name , unique , column_names , [ ] , orders , where , nil , using ) else IndexDefinition . new ( table_name , index_name , unique , column_names , [ ] , orders ) end end end result . compact! result end
Returns an array of indexes for the given table.
Returns an array of indexes for the given table .
# NOTE: maybe it's better to leave things of to the JDBC API ?! # JDBC gets us a boolean # add info on sort order for columns (only desc order is explicitly specified, asc is the default) # AR4 supports `where` and `using` index options
table_name
name