# File lib/sequel/adapters/shared/firebird.rb, line 26 def primary_key(table) t = dataset.send(:input_identifier, table) @primary_keys.fetch(t) do pk = fetch("SELECT RDB$FIELD_NAME FROM RDB$INDEX_SEGMENTS NATURAL JOIN RDB$RELATION_CONSTRAINTS WHERE RDB$CONSTRAINT_TYPE = 'PRIMARY KEY' AND RDB$RELATION_NAME = ?", t).single_value @primary_keys[t] = dataset.send(:output_identifier, pk.rstrip) if pk end end
module Sequel::Firebird::DatabaseMethods
Constants
- AUTO_INCREMENT
- TEMPORARY
Public Instance Methods
clear_primary_key(*tables)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 9 def clear_primary_key(*tables) tables.each{|t| @primary_keys.delete(dataset.send(:input_identifier, t))} end
create_trigger(*args)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 13 def create_trigger(*args) self << create_trigger_sql(*args) end
database_type()
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 17 def database_type :firebird end
drop_sequence(name)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 21 def drop_sequence(name) self << drop_sequence_sql(name) end
primary_key(table)
click to toggle source
Return primary key for the given table.
restart_sequence(*args)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 34 def restart_sequence(*args) self << restart_sequence_sql(*args) end
sequences(opts=OPTS) { |ds| ... }
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 38 def sequences(opts=OPTS) ds = self[:"rdb$generators"].server(opts[:server]).filter(:"rdb$system_flag" => 0).select(:"rdb$generator_name") block_given? ? yield(ds) : ds.map{|r| ds.send(:output_identifier, r[:"rdb$generator_name"])} end
tables(opts=OPTS)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 43 def tables(opts=OPTS) tables_or_views(0, opts) end
views(opts=OPTS)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 47 def views(opts=OPTS) tables_or_views(1, opts) end
Private Instance Methods
alter_table_sql(table, op)
click to toggle source
Use Firebird specific syntax for add column
Calls superclass method
# File lib/sequel/adapters/shared/firebird.rb, line 54 def alter_table_sql(table, op) case op[:op] when :add_column "ALTER TABLE #{quote_schema_table(table)} ADD #{column_definition_sql(op)}" when :drop_column "ALTER TABLE #{quote_schema_table(table)} DROP #{column_definition_sql(op)}" when :rename_column "ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name])}" when :set_column_type "ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} TYPE #{type_literal(op)}" else super(table, op) end end
auto_increment_sql()
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 69 def auto_increment_sql() AUTO_INCREMENT end
create_sequence_sql(name, opts=OPTS)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 73 def create_sequence_sql(name, opts=OPTS) "CREATE SEQUENCE #{quote_identifier(name)}" end
create_table_from_generator(name, generator, options)
click to toggle source
Firebird gets an override because of the mess of creating a sequence and trigger for auto-incrementing primary keys.
# File lib/sequel/adapters/shared/firebird.rb, line 79 def create_table_from_generator(name, generator, options) drop_statement, create_statements = create_table_sql_list(name, generator, options) (execute_ddl(drop_statement) rescue nil) if drop_statement create_statements.each{|sql| execute_ddl(sql)} end
create_table_sql_list(name, generator, options=OPTS)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 85 def create_table_sql_list(name, generator, options=OPTS) statements = [create_table_sql(name, generator, options)] drop_seq_statement = nil generator.columns.each do |c| if c[:auto_increment] c[:sequence_name] ||= "seq_#{name}_#{c[:name]}" unless c[:create_sequence] == false drop_seq_statement = drop_sequence_sql(c[:sequence_name]) statements << create_sequence_sql(c[:sequence_name]) statements << restart_sequence_sql(c[:sequence_name], {:restart_position => c[:sequence_start_position]}) if c[:sequence_start_position] end unless c[:create_trigger] == false c[:trigger_name] ||= "BI_#{name}_#{c[:name]}" c[:quoted_name] = quote_identifier(c[:name]) trigger_definition = <<-END begin if ((new.#{c[:quoted_name]} is null) or (new.#{c[:quoted_name]} = 0)) then begin new.#{c[:quoted_name]} = next value for #{c[:sequence_name]}; end end END statements << create_trigger_sql(name, c[:trigger_name], trigger_definition, {:events => [:insert]}) end end end [drop_seq_statement, statements] end
create_trigger_sql(table, name, definition, opts=OPTS)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 114 def create_trigger_sql(table, name, definition, opts=OPTS) events = opts[:events] ? Array(opts[:events]) : [:insert, :update, :delete] whence = opts[:after] ? 'AFTER' : 'BEFORE' inactive = opts[:inactive] ? 'INACTIVE' : 'ACTIVE' position = opts.fetch(:position, 0) sql = <<-end_sql CREATE TRIGGER #{quote_identifier(name)} for #{quote_identifier(table)} #{inactive} #{whence} #{events.map{|e| e.to_s.upcase}.join(' OR ')} position #{position} as #{definition} end_sql sql end
drop_sequence_sql(name)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 127 def drop_sequence_sql(name) "DROP SEQUENCE #{quote_identifier(name)}" end
remove_cached_schema(table)
click to toggle source
Calls superclass method
# File lib/sequel/adapters/shared/firebird.rb, line 131 def remove_cached_schema(table) clear_primary_key(table) super end
restart_sequence_sql(name, opts=OPTS)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 136 def restart_sequence_sql(name, opts=OPTS) seq_name = quote_identifier(name) "ALTER SEQUENCE #{seq_name} RESTART WITH #{opts[:restart_position]}" end
tables_or_views(type, opts)
click to toggle source
# File lib/sequel/adapters/shared/firebird.rb, line 141 def tables_or_views(type, opts) ds = self[:"rdb$relations"].server(opts[:server]).filter(:"rdb$relation_type" => type, Sequel::SQL::Function.new(:COALESCE, :"rdb$system_flag", 0) => 0).select(:"rdb$relation_name") ds.map{|r| ds.send(:output_identifier, r[:"rdb$relation_name"].rstrip)} end
type_literal_generic_string(column)
click to toggle source
Calls superclass method
# File lib/sequel/adapters/shared/firebird.rb, line 146 def type_literal_generic_string(column) column[:text] ? :"BLOB SUB_TYPE TEXT" : super end
view_with_check_option_support()
click to toggle source
Firebird supports views with check option, but not local.
# File lib/sequel/adapters/shared/firebird.rb, line 151 def view_with_check_option_support true end