Class/Module Index [+]

Quicksearch

RR::ConnectionExtenders::PostgreSQLExtender

Provides various PostgreSQL specific functionality required by Rubyrep.

Public Instance Methods

columns(table_name, name = nil) click to toggle source

*** Moneky patch*** Returns the column objects for the named table. Fixes JRuby schema support

# File lib/rubyrep/connection_extenders/postgresql_extender.rb, line 197
def columns(table_name, name = nil)
  jdbc_connection = @connection.connection # the actual JDBC DatabaseConnection
  @unquoted_schema ||= select_one("show search_path")['search_path']

  # check if table exists
  table_results = jdbc_connection.meta_data.get_tables(
    jdbc_connection.catalog,
    @unquoted_schema,
    table_name,
    ["TABLE","VIEW","SYNONYM"].to_java(:string)
  )
  table_exists = table_results.next
  table_results.close
  raise "table '#{table_name}' not found" unless table_exists

  # get ResultSet for columns of table
  column_results = jdbc_connection.meta_data.get_columns(
    jdbc_connection.catalog,
    @unquoted_schema,
    table_name,
    nil
  )
  
  # create the Column objects
  columns = []
  while column_results.next
    
    # generate type clause
    type_clause = column_results.get_string('TYPE_NAME')
    precision = column_results.get_int('COLUMN_SIZE')
    scale = column_results.get_int('DECIMAL_DIGITS')
    if precision > 0
      type_clause += "(#{precision}#{scale > 0 ? ",#{scale}" : ""})"
    end

    # create column
    columns << ::ActiveRecord::ConnectionAdapters::JdbcColumn.new(
      @config,
      column_results.get_string('COLUMN_NAME'),
      column_results.get_string('COLUMN_DEF'),
      type_clause,
      column_results.get_string('IS_NULLABLE').strip == "NO"
    )
  end
  column_results.close
  
  columns
end
extract_pg_identifier_from_name(name) click to toggle source

Disables schema extraction from table names by overwriting the according ActiveRecord method. Necessary to support table names containing dots ("."). (This is possible as rubyrep exclusively uses the search_path setting to support PostgreSQL schemas.)

# File lib/rubyrep/connection_extenders/postgresql_extender.rb, line 115
def extract_pg_identifier_from_name(name)
  return name, nil
end
initialize_search_path() click to toggle source

Sets the schema search path as per configuration parameters

# File lib/rubyrep/connection_extenders/postgresql_extender.rb, line 190
def initialize_search_path
  execute "SET search_path TO #{config[:schema_search_path] || 'public'}"
end
primary_key_names(table) click to toggle source

Returns an ordered list of primary key column names of the given table

# File lib/rubyrep/connection_extenders/postgresql_extender.rb, line 120
def primary_key_names(table)
  row = self.select_one(          SELECT relname          FROM pg_class          WHERE relname = '#{table}' and relnamespace IN            (SELECT oid FROM pg_namespace WHERE nspname in (#{schemas})))
  raise "table '#{table}' does not exist" if row.nil?
  
  row = self.select_one(          SELECT cons.conkey           FROM pg_class           rel          JOIN pg_constraint      cons ON (rel.oid = cons.conrelid)          WHERE cons.contype = 'p' AND rel.relname = '#{table}' AND rel.relnamespace IN            (SELECT oid FROM pg_namespace WHERE nspname in (#{schemas})))
  return [] if row.nil?
  column_parray = row['conkey']
  
  # Change a Postgres Array of attribute numbers
  # (returned in String form, e. g.: "{1,2}") into an array of Integers
  column_ids = column_parray.sub(/^\{(.*)\}$/,'\1').split(',').map {|a| a.to_i}

  columns = {}
  rows = self.select_all(          SELECT attnum, attname          FROM pg_class           rel          JOIN pg_constraint      cons ON (rel.oid = cons.conrelid)          JOIN pg_attribute       attr ON (rel.oid = attr.attrelid and attr.attnum = any (cons.conkey))          WHERE cons.contype = 'p' AND rel.relname = '#{table}' AND rel.relnamespace IN            (SELECT oid FROM pg_namespace WHERE nspname in (#{schemas})))
  sorted_columns = []
  if not rows.nil?
    rows.each() {|r| columns[r['attnum'].to_i] = r['attname']}
    sorted_columns = column_ids.map {|column_id| columns[column_id]}
  end
  sorted_columns
end
referenced_tables(tables) click to toggle source

Returns for each given table, which other tables it references via foreign key constraints.

  • tables: an array of table names

Returns: a hash with

  • key: name of the referencing table

  • value: an array of names of referenced tables

# File lib/rubyrep/connection_extenders/postgresql_extender.rb, line 166
def referenced_tables(tables)
  rows = self.select_all(          select distinct referencing.relname as referencing_table, referenced.relname as referenced_table          from pg_class referencing          left join pg_constraint on referencing.oid = pg_constraint.conrelid          left join pg_class referenced on pg_constraint.confrelid = referenced.oid          where referencing.relkind='r'          and referencing.relname in ('#{tables.join("', '")}')          and referencing.relnamespace IN            (SELECT oid FROM pg_namespace WHERE nspname in (#{schemas})))
  result = {}
  rows.each do |row|
    unless result.include? row['referencing_table']
      result[row['referencing_table']] = []
    end
    if row['referenced_table'] != nil
      result[row['referencing_table']] << row['referenced_table']
    end
  end
  result
end
schemas() click to toggle source

Returns an array of schemas in the current search path.

# File lib/rubyrep/connection_extenders/postgresql_extender.rb, line 89
def schemas
  unless @schemas
    search_path = select_one("show search_path")['search_path']
    @schemas = search_path.split(/,/).map { |p| quote(p.strip) }.join(',')
  end
  @schemas
end
tables(name = nil) click to toggle source

*** Monkey patch*** Returns the list of all tables in the schema search path or a specified schema. This overwrites the according ActiveRecord::PostgreSQLAdapter method to make sure that also search paths with spaces work (E. g. 'public, rr' instead of only 'public,rr')

# File lib/rubyrep/connection_extenders/postgresql_extender.rb, line 102
def tables(name = nil)
  select_all(          SELECT tablename            FROM pg_tables           WHERE schemaname IN (#{schemas}), name).map { |row| row['tablename'] }
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.