Namespace

Class/Module Index [+]

Quicksearch

JdbcSpec::Derby

Public Class Methods

adapter_matcher(name, *) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 14
def self.adapter_matcher(name, *)
  name =~ /derby/ ? self : false
end
column_selector() click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 18
def self.column_selector
  [/derby/, lambda {|cfg,col| col.extend(::JdbcSpec::Derby::Column)}]
end
extended(*args) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 43
def self.extended(*args)
  monkey_rails
end
included(*args) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 47
def self.included(*args)
  monkey_rails
end
monkey_rails() click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 22
def self.monkey_rails
  unless @already_monkeyd
    # Needed because Rails is broken wrt to quoting of
    # some values. Most databases are nice about it,
    # but not Derby. The real issue is that you can't
    # compare a CHAR value to a NUMBER column.
    ::ActiveRecord::Associations::ClassMethods.module_eval do
      private

      def select_limited_ids_list(options, join_dependency)
        connection.select_all(
                              construct_finder_sql_for_association_limiting(options, join_dependency),
                              "#{name} Load IDs For Limited Eager Loading"
                              ).collect { |row| connection.quote(row[primary_key], columns_hash[primary_key]) }.join(", ")
      end
    end

    @already_monkeyd = true
  end
end

Public Instance Methods

add_column(table_name, column_name, type, options = {}) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 207
def add_column(table_name, column_name, type, options = {})
  if option_not_null = options[:null] == false
    option_not_null = options.delete(:null)
  end
  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)
  if option_not_null
    alter_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} NOT NULL"
  end
end
add_column_options!(sql, options) click to toggle source

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

# File lib/jdbc_adapter/jdbc_derby.rb, line 91
def add_column_options!(sql, options)
  options.delete(:default) if options.has_key?(:default) && options[:default].nil?
  options.delete(:null) if options.has_key?(:null) && (options[:null].nil? || options[:null] == true)
  super
end
add_quotes(name) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 135
def add_quotes(name)
  return name unless name
  %{"#{name}"}
end
auto_increment_stmt(tname, cname) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 187
def auto_increment_stmt(tname, cname)
  stmt = AUTO_INC_STMT2 % [tname, strip_quotes(cname)]
  data = execute(stmt).first
  if data
    start = data['autoincrementstart']
    if start
      coldef = ""
      coldef << " GENERATED " << (data['columndefault'].nil? ? "ALWAYS" : "BY DEFAULT ")
      coldef << "AS IDENTITY (START WITH "
      coldef << start
      coldef << ", INCREMENT BY "
      coldef << data['autoincrementinc']
      coldef << ")"
      return coldef
    end
  end
  ""
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 somehow (supposed to arrive for 10.3.0.0)

# File lib/jdbc_adapter/jdbc_derby.rb, line 317
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 #{table_name} ALTER COLUMN #{column_name} NOT NULL"
    else
      execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} NULL"
    end
  end

  # anything left to do?
  unless options.empty?
    begin
      execute "ALTER TABLE #{table_name} ALTER COLUMN #{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 #{table_name} SET #{temp_new_column_name} = CAST(#{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
classes_for_table_name(table) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 97
def classes_for_table_name(table)
  ActiveRecord::Base.send(:subclasses).select {|klass| klass.table_name == table}
end
columns(table_name, name=nil) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 366
def columns(table_name, name=nil)
  @connection.columns_internal(table_name.to_s, name, derby_schema)
end
create_column(name, refid, colno) click to toggle source

I don't think this method is ever called ??? (stepheneb)

# File lib/jdbc_adapter/jdbc_derby.rb, line 233
def create_column(name, refid, colno)
  stmt = COLUMN_TYPE_STMT % [refid, strip_quotes(name)]
  coldef = ""
  data = execute(stmt).first
  if data
    coldef << add_quotes(expand_double_quotes(strip_quotes(name)))
    coldef << " "
    coldef << data['columndatatype']
    if !reinstate_auto_increment(name, refid, coldef) && data['columndefault']
      coldef << " DEFAULT " << data['columndefault']
    end
  end
  coldef
end
execute(sql, name = nil) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 219
def execute(sql, name = nil)
  if sql =~ /^\s*(UPDATE|INSERT)/
    i = sql =~ /\swhere\s/m
    if i
      sql[i..-1] = sql[i..-1].gsub(/!=\s*NULL/, 'IS NOT NULL').gsub(/=\sNULL/, 'IS NULL')
    end
  else
    sql.gsub!(/= NULL/, 'IS NULL')
  end
  super
end
expand_double_quotes(name) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 146
def expand_double_quotes(name)
  return name unless name && name['"']
  name.gsub(/"/,'""')
end
modify_types(tp) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 82
def modify_types(tp)
  tp[:primary_key] = "int generated by default as identity NOT NULL PRIMARY KEY"
  tp[:integer][:limit] = nil
  tp[:string][:limit] = 256
  tp[:boolean] = {:name => "smallint"}
  tp
end
primary_keys(table_name) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 362
def primary_keys(table_name)
  @connection.primary_keys table_name.to_s.upcase
end
quoted_false() click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 400
def quoted_false
  '0'
end
quoted_true() click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 396
def quoted_true
  '1'
end
recreate_database(db_name) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 374
def recreate_database(db_name)
  tables.each do |t|
    drop_table t
  end
end
reinstate_auto_increment(name, refid, coldef) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 151
def reinstate_auto_increment(name, refid, coldef)
  stmt = AUTO_INC_STMT % [refid, strip_quotes(name)]
  data = execute(stmt).first
  if data
    start = data['autoincrementstart']
    if start
      coldef << " GENERATED " << (data['columndefault'].nil? ? "ALWAYS" : "BY DEFAULT ")
      coldef << "AS IDENTITY (START WITH "
      coldef << start
      coldef << ", INCREMENT BY "
      coldef << data['autoincrementinc']
      coldef << ")"
      return true
    end
  end
  false
end
remove_column(table_name, column_name) click to toggle source

Support for removing columns added via derby bug issue: issues.apache.org/jira/browse/DERBY-1489

This feature has not made it into a formal release and is not in Java 6. If the normal strategy fails we fall back on a strategy by creating a new table without the new column and there after moving the data to the new

# File lib/jdbc_adapter/jdbc_derby.rb, line 299
def remove_column(table_name, column_name)
  begin
    execute "ALTER TABLE #{table_name} DROP COLUMN #{column_name} RESTRICT"
  rescue
    alter_table(table_name) do |definition|
      definition.columns.delete(definition[column_name])
    end
  end
end
rename_table(name, new_name) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 124
def rename_table(name, new_name)
  execute "RENAME TABLE #{name} TO #{new_name}"
end
reset_pk_sequence!(table, pk = nil, sequence = nil) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 107
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/jdbc_adapter/jdbc_derby.rb, line 102
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
select_limited_ids_list(options, join_dependency) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 31
def select_limited_ids_list(options, join_dependency)
  connection.select_all(
                        construct_finder_sql_for_association_limiting(options, join_dependency),
                        "#{name} Load IDs For Limited Eager Loading"
                        ).collect { |row| connection.quote(row[primary_key], columns_hash[primary_key]) }.join(", ")
end
strip_quotes(str) click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 140
def strip_quotes(str)
  return str unless str
  return str unless /^(["']).*\11$$/ =~ str
  str[1..-2]
end
tables() click to toggle source
# File lib/jdbc_adapter/jdbc_derby.rb, line 370
def tables
  @connection.tables(nil, derby_schema)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.