Class: Sequel::Firebird::Database

Class | Sequel::Firebird::Database
In: | lib/sequel_core/adapters/firebird.rb
Parent: | Sequel::Database

### Methods

alter_table_sql auto_increment_sql connect create_sequence_sql create_table create_table_sql_list create_trigger create_trigger_sql dataset drop_sequence drop_sequence_sql execute new primary_key primary_key_for_table restart_sequence restart_sequence_sql sequences tables transaction

### Constants

AUTO_INCREMENT | = | ''.freeze

### Public Class methods

new(*args)

Add the primary_keys and primary_key_sequences instance variables, so we can get the correct return values for inserted rows.

[Source]

        # File lib/sequel_core/adapters/firebird.rb, line 14
    14:       def initialize(*args)
    15:         super
    16:         @primary_keys = {}
    17:         @primary_key_sequences = {}
    18:       end

### Public Instance methods

alter_table_sql(table, op)

Use Firebird specific syntax for add column

[Source]

        # File lib/sequel_core/adapters/firebird.rb, line 21
    21:       def alter_table_sql(table, op)
    22:         case op[:op]
    23:         when :add_column
    24:           "ALTER TABLE #{quote_schema_table(table)} ADD #{column_definition_sql(op)}"
    25:         when :drop_column
    26:           "ALTER TABLE #{quote_schema_table(table)} DROP #{column_definition_sql(op)}"
    27:         when :rename_column
    28:           "ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name])}"
    29:         when :set_column_type
    30:           "ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} TYPE #{type_literal(op)}"
    31:         else
    32:           super(table, op)
    33:         end
    34:       end

auto_increment_sql()

[Source]

        # File lib/sequel_core/adapters/firebird.rb, line 36
    36:       def auto_increment_sql()
    37:         AUTO_INCREMENT
    38:       end

connect(server)

[Source]

        # File lib/sequel_core/adapters/firebird.rb, line 40
    40:       def connect(server)
    41:         opts = server_opts(server)
    42:
    43:         db = Fb::Database.new(
    44:           :database => "#{opts[:host]}:#{opts[:database]}",
    45:           :username => opts[:user],
    46:           :password => opts[:password])
    47:         conn = db.connect
    48:         conn.downcase_names = true
    49:         conn
    50:       end

create_sequence_sql(name, opts={})

[Source]

        # File lib/sequel_core/adapters/firebird.rb, line 52
    52:       def create_sequence_sql(name, opts={})
    53:         "CREATE SEQUENCE #{quote_identifier(name)}"
    54:       end

create_table(name, options={}, &block)

Creates a table with the columns given in the provided block:

      DB.create_table :posts do
        primary_key :id, :serial
        column :title, :text
        column :content, :text
        index :title
      end

See Schema::Generator. Firebird gets an override because of the mess of creating a generator for auto-incrementing primary keys.

[Source]

        # File lib/sequel_core/adapters/firebird.rb, line 68
    68:       def create_table(name, options={}, &block)
    69:         options = {:generator=>options} if options.is_a?(Schema::Generator)
    70:         statements = create_table_sql_list(name, *((options[:generator] ||= Schema::Generator.new(self, &block)).create_info << options))
    71:         begin
    72:           execute_ddl(statements[1])
    73:         rescue
    74:           nil
    75:         end if statements[1]
    76:         statements[0].flatten.each {|sql| execute_ddl(sql)}
    77:       end

create_table_sql_list(name, columns, indexes = nil, options={})

[Source]

        # File lib/sequel_core/adapters/firebird.rb, line 79
    79:       def create_table_sql_list(name, columns, indexes = nil, options={})
    80:         statements = super
    81:         drop_seq_statement = nil
    82:         columns.each do |c|
    83:           if c[:auto_increment]
    84:             c[:sequence_name] ||= "seq_#{name}_#{c[:name]}"
    85:             unless c[:create_sequence] == false
    86:               drop_seq_statement = drop_sequence_sql(c[:sequence_name])
    87:               statements << create_sequence_sql(c[:sequence_name])
    88:               statements << restart_sequence_sql(c[:sequence_name], {:restart_position => c[:sequence_start_position]}) if c[:sequence_start_position]
    89:             end
    90:             unless c[:create_trigger] == false
    91:               c[:trigger_name] ||= "BI_#{name}_#{c[:name]}"
    92:               c[:quoted_name] = quote_identifier(c[:name])
    93:               trigger_definition = "begin\nif ((new.\#{c[:quoted_name]} is null) or (new.\#{c[:quoted_name]} = 0)) then\nbegin\nnew.\#{c[:quoted_name]} = next value for \#{c[:sequence_name]};\nend\nend\n"
    94:               statements << create_trigger_sql(name, c[:trigger_name], trigger_definition, {:events => [:insert]})
    95:             end
    96:           end
    97:         end
    98:         [statements, drop_seq_statement]
    99:       end

create_trigger(*args)

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 109
    109:       def create_trigger(*args)
    110:         self << create_trigger_sql(*args)
    111:       end

create_trigger_sql(table, name, definition, opts={})

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 113
    113:       def create_trigger_sql(table, name, definition, opts={})
    114:         events = opts[:events] ? Array(opts[:events]) : [:insert, :update, :delete]
    115:         whence = opts[:after] ? 'AFTER' : 'BEFORE'
    116:         inactive = opts[:inactive] ? 'INACTIVE' : 'ACTIVE'
    117:         position = opts[:position] ? opts[:position] : 0
    118:         sql = "CREATE TRIGGER \#{quote_identifier(name)} for \#{quote_identifier(table)}\n\#{inactive} \#{whence} \#{events.map{|e| e.to_s.upcase}.join(' OR ')} position \#{position}\nas \#{definition}\n"
    119:         sql
    120:       end

dataset(opts = nil)

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 127
    127:       def dataset(opts = nil)
    128:         Firebird::Dataset.new(self, opts)
    129:       end

drop_sequence(name)

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 131
    131:       def drop_sequence(name)
    132:         self << drop_sequence_sql(name)
    133:       end

drop_sequence_sql(name)

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 135
    135:       def drop_sequence_sql(name)
    136:         "DROP SEQUENCE #{quote_identifier(name)}"
    137:       end

execute(sql, opts={}) {|r| ...}

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 139
    139:       def execute(sql, opts={})
    140:         log_info(sql)
    141:         begin
    142:           synchronize(opts[:server]) do |conn|
    143:             r = conn.execute(sql)
    144:             yield(r) if block_given?
    145:             r
    146:           end
    147:         rescue => e
    148:           log_info(e.message)
    149:           raise_error(e, :classes=>[Fb::Error])
    150:         end
    151:       end

primary_key(table, server=nil)

Return primary key for the given table.

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 154
    154:       def primary_key(table, server=nil)
    155:         synchronize(server){|conn| primary_key_for_table(conn, table)}
    156:       end

primary_key_for_table(conn, table)

Returns primary key for the given table. This information is cached, and if the primary key for a table is changed, the @primary_keys instance variable should be reset manually.

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 161
    161:       def primary_key_for_table(conn, table)
    162:         @primary_keys[quote_identifier(table)] ||= conn.table_primary_key(quote_identifier(table))
    163:       end

restart_sequence(*args)

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 165
    165:       def restart_sequence(*args)
    166:         self << restart_sequence_sql(*args)
    167:       end

restart_sequence_sql(name, opts={})

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 169
    169:       def restart_sequence_sql(name, opts={})
    170:         seq_name = quote_identifier(name)
    171:         "ALTER SEQUENCE #{seq_name} RESTART WITH #{opts[:restart_position]}"
    172:       end

sequences(opts={}) {|ds| ...}

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 174
    174:       def sequences(opts={})
    175:         ds = self["rdb$generators""rdb$generators"].server(opts[:server]).filter("rdb$system_flag""rdb$system_flag" => 0).select("rdb$generator_name""rdb$generator_name")
    176:         block_given? ? yield(ds) : ds.map{|r| ds.send(:output_identifier, r["rdb$generator_name""rdb$generator_name"])}
    177:       end

tables(opts={}) {|ds| ...}

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 179
    179:       def tables(opts={})
    180:         ds = self["rdb$relations""rdb$relations"].server(opts[:server]).filter("rdb$view_blr""rdb$view_blr" => nil, Sequel::SQL::Function.new(:COALESCE, "rdb$system_flag""rdb$system_flag", 0) => 0).select("rdb$relation_name""rdb$relation_name")
    181:         block_given? ? yield(ds) : ds.map{|r| ds.send(:output_identifier, r["rdb$relation_name""rdb$relation_name"])}
    182:       end

transaction(server=nil) {|conn| ...}

[Source]

         # File lib/sequel_core/adapters/firebird.rb, line 184
    184:       def transaction(server=nil)
    185:         synchronize(server) do |conn|
    186:           return yield(conn) if @transactions.include?(Thread.current)
    187:           log_info("Transaction.begin")
    188:           conn.transaction
    189:           begin
    190:             @transactions << Thread.current
    191:             yield(conn)
    192:           rescue ::Exception => e
    193:             log_info("Transaction.rollback")
    194:             conn.rollback
    195:             transaction_error(e, Fb::Error)
    196:           ensure
    197:             unless e
    198:               log_info("Transaction.commit")
    199:               conn.commit
    200:             end
    201:             @transactions.delete(Thread.current)
    202:           end
    203:         end
    204:       end

[Validate]