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
# 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
# File lib/arjdbc/derby/adapter.rb, line 15 def self.column_selector [ /derby/, lambda { |cfg, column| column.extend(::ArJdbc::Derby::Column) } ] end
# 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
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
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
# 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
# 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
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
# 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
# File lib/arjdbc/derby/adapter.rb, line 86 def index_name_length 128 end
# 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
# File lib/arjdbc/derby/adapter.rb, line 110 def native_database_types super.merge NATIVE_DATABASE_TYPES end
# File lib/arjdbc/derby/adapter.rb, line 326 def primary_keys(table_name) @connection.primary_keys table_name.to_s.upcase end
# File lib/arjdbc/derby/adapter.rb, line 338 def recreate_database(db_name, options = {}) tables.each { |table| drop_table table } end
# 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
# 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
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
Generated with the Darkfish Rdoc Generator 2.