Namespace

Files

Class/Module Index [+]

Quicksearch

ArJdbc::Derby

Public Class Methods

arel2_visitors(config) click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 76
def self.arel2_visitors(config)
  require 'arel/visitors/derby'
  {
    'derby' => ::Arel::Visitors::Derby,
    'jdbcderby' => ::Arel::Visitors::Derby,
  }
end
column_selector() click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 15
def self.column_selector
  [ /derby/, lambda { |cfg, column| column.extend(::ArJdbc::Derby::Column) } ]
end
extended(adapter) click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 6
def self.extended(adapter)
  require 'arjdbc/derby/active_record_patch'
  adapter.configure_connection
end
included(*args) click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 11
def self.included(*args)
  require 'arjdbc/derby/active_record_patch'
end

Public Instance Methods

add_column(table_name, column_name, type, options = {}) click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 171
def add_column(table_name, column_name, type, options = {})
  add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
  add_column_options!(add_column_sql, options)
  execute(add_column_sql)
end
add_column_options!(sql, options) click to toggle source

Override default -- fix case where ActiveRecord passes :default => nil, :null => true

# File lib/arjdbc/derby/adapter.rb, line 137
def add_column_options!(sql, options)
  options.delete(:default) if options.has_key?(:default) && options[:default].nil?
  sql << " DEFAULT #{quote(options.delete(:default))}" if options.has_key?(:default)
  super
end
change_column(table_name, column_name, type, options = {}) click to toggle source

Notes about changing in Derby:

http://db.apache.org/derby/docs/10.2/ref/rrefsqlj81859.html#rrefsqlj81859__rrefsqlj37860)

We support changing columns using the strategy outlined in:

https://issues.apache.org/jira/browse/DERBY-1515

This feature has not made it into a formal release and is not in Java 6. We will need to conditionally support this (supposed to arrive for 10.3.0.0).

# File lib/arjdbc/derby/adapter.rb, line 291
def change_column(table_name, column_name, type, options = {})
  # null/not nulling is easy, handle that separately
  if options.include?(:null)
    # This seems to only work with 10.2 of Derby
    if options.delete(:null) == false
      execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} NOT NULL"
    else
      execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} NULL"
    end
  end

  # anything left to do?
  unless options.empty?
    begin
      execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DATA TYPE #{type_to_sql(type, options[:limit])}"
    rescue
      transaction do
        temp_new_column_name = "#{column_name}_newtype"
        # 1) ALTER TABLE t ADD COLUMN c1_newtype NEWTYPE;
        add_column table_name, temp_new_column_name, type, options
        # 2) UPDATE t SET c1_newtype = c1;
        execute "UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(temp_new_column_name)} = CAST(#{quote_column_name(column_name)} AS #{type_to_sql(type, options[:limit])})"
        # 3) ALTER TABLE t DROP COLUMN c1;
        remove_column table_name, column_name
        # 4) ALTER TABLE t RENAME COLUMN c1_newtype to c1;
        rename_column table_name, temp_new_column_name, column_name
      end
    end
  end
end
columns(table_name, name = nil) click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 330
def columns(table_name, name = nil)
  @connection.columns_internal(table_name.to_s, name, derby_schema)
end
configure_connection() click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 19
def configure_connection
  execute("SET ISOLATION = SERIALIZABLE")
  # must be done or SELECT...FOR UPDATE won't work how we expect
end
distinct(columns, order_by) click to toggle source

SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.

Derby requires the ORDER BY columns in the select list for distinct queries, and requires that the ORDER BY include the distinct column.

distinct("posts.id", "posts.created_at desc")

Based on distinct method for PostgreSQL Adapter

# File lib/arjdbc/derby/adapter.rb, line 200
def distinct(columns, order_by)
  return "DISTINCT #{columns}" if order_by.blank?

  # construct a clean list of column names from the ORDER BY clause, removing
  # any asc/desc modifiers
  order_columns = [order_by].flatten.map{|o| o.split(',').collect { |s| s.split.first } }.flatten.reject(&:blank?)
  order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }

  # return a DISTINCT clause that's distinct on the columns we want but includes
  # all the required columns for the ORDER BY to work properly
  sql = "DISTINCT #{columns}, #{order_columns * ', '}"
  sql
end
execute(sql, name = nil, binds = []) click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 177
def execute(sql, name = nil, binds = [])
  sql = to_sql(sql)
  if sql =~ /\A\s*(UPDATE|INSERT)/
    if ( i = sql =~ /\sWHERE\s/m )
      where_part = sql[i..-1]; sql = sql.dup
      where_part.gsub!(/!=\s*NULL/, 'IS NOT NULL')
      where_part.gsub!(/=\sNULL/, 'IS NULL')
      sql[i..-1] = where_part
    end
  else
    sql = sql.gsub(/=\sNULL/, 'IS NULL')
  end
  super(sql, name, binds)
end
index_name_length() click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 86
def index_name_length
  128
end
modify_types(types) click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 114
def modify_types(types)
  super(types)
  types[:primary_key] = NATIVE_DATABASE_TYPES[:primary_key]
  [ :string, :float, :decimal, :numeric, :integer, 
    :smallint, :bigint, :real, :double ].each do |type|
    types[type] = NATIVE_DATABASE_TYPES[type].dup
  end
  types[:boolean] = NATIVE_DATABASE_TYPES[:boolean].dup
  types
end
native_database_types() click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 110
def native_database_types
  super.merge NATIVE_DATABASE_TYPES
end
primary_keys(table_name) click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 326
def primary_keys(table_name)
  @connection.primary_keys table_name.to_s.upcase
end
recreate_database(db_name, options = {}) click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 338
def recreate_database(db_name, options = {})
  tables.each { |table| drop_table table }
end
rename_table(name, new_name) click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 167
def rename_table(name, new_name)
  execute "RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}"
end
reset_pk_sequence!(table, pk = nil, sequence = nil) click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 149
def reset_pk_sequence!(table, pk = nil, sequence = nil)
  klasses = classes_for_table_name(table)
  klass   = klasses.nil? ? nil : klasses.first
  pk      = klass.primary_key unless klass.nil?
  if pk && klass.columns_hash[pk].type == :integer
    reset_sequence!(klass.table_name, pk)
  end
end
reset_sequence!(table, column, sequence = nil) click to toggle source

Set the sequence to the max value of the table's column.

# File lib/arjdbc/derby/adapter.rb, line 144
def reset_sequence!(table, column, sequence = nil)
  mpk = select_value("SELECT MAX(#{quote_column_name(column)}) FROM #{quote_table_name(table)}")
  execute("ALTER TABLE #{quote_table_name(table)} ALTER COLUMN #{quote_column_name(column)} RESTART WITH #{mpk.to_i + 1}")
end
tables() click to toggle source
# File lib/arjdbc/derby/adapter.rb, line 334
def tables
  @connection.tables(nil, derby_schema)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.