module Sequel::DB2::DatabaseMethods

  1. lib/sequel/adapters/shared/db2.rb
Parent: DB2

Constants

AUTOINCREMENT = 'GENERATED ALWAYS AS IDENTITY'.freeze  
DATABASE_ERROR_REGEXPS = { /DB2 SQL Error: SQLCODE=-803, SQLSTATE=23505|One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index/ => UniqueConstraintViolation, /DB2 SQL Error: (SQLCODE=-530, SQLSTATE=23503|SQLCODE=-532, SQLSTATE=23504)|The insert or update value of the FOREIGN KEY .+ is not equal to any value of the parent key of the parent table|A parent row cannot be deleted because the relationship .+ restricts the deletion/ => ForeignKeyConstraintViolation, /DB2 SQL Error: SQLCODE=-545, SQLSTATE=23513|The requested operation is not allowed because a row does not satisfy the check constraint/ => CheckConstraintViolation, /DB2 SQL Error: SQLCODE=-407, SQLSTATE=23502|Assignment of a NULL value to a NOT NULL column/ => NotNullConstraintViolation, /DB2 SQL Error: SQLCODE=-911, SQLSTATE=40001|The current transaction has been rolled back because of a deadlock or timeout/ => SerializationFailure, }.freeze  
NOT_NULL = ' NOT NULL'.freeze  
NULL = ''.freeze  

Public Instance Aliases

server_version -> db2_version

Public Instance methods

database_type ()

DB2 always uses :db2 as it's database type

[show source]
# File lib/sequel/adapters/shared/db2.rb, line 20
def database_type
  :db2
end
db2_version ()

Return the database version as a string. Don’t rely on this, it may return an integer in the future.

[show source]
# File lib/sequel/adapters/shared/db2.rb, line 26
def db2_version
  return @db2_version if @db2_version
  @db2_version = metadata_dataset.with_sql("select service_level from sysibmadm.env_inst_info").first[:service_level]
end
indexes (table, opts = OPTS)

Use SYSCAT.INDEXES to get the indexes for the table

[show source]
# File lib/sequel/adapters/shared/db2.rb, line 64
def indexes(table, opts = OPTS)
  m = output_identifier_meth
  indexes = {}
  metadata_dataset.
   from(:syscat__indexes).
   select(:indname, :uniquerule, :colnames).
   where(:tabname=>input_identifier_meth.call(table), :system_required=>0).
   each do |r|
    indexes[m.call(r[:indname])] = {:unique=>(r[:uniquerule]=='U'), :columns=>r[:colnames][1..-1].split('+').map{|v| m.call(v)}}
  end
  indexes
end
schema_parse_table (table, opts = OPTS)

Use SYSIBM.SYSCOLUMNS to get the information on the tables.

[show source]
# File lib/sequel/adapters/shared/db2.rb, line 33
def schema_parse_table(table, opts = OPTS)
  m = output_identifier_meth(opts[:dataset])
  im = input_identifier_meth(opts[:dataset])
  metadata_dataset.with_sql("SELECT * FROM SYSIBM.SYSCOLUMNS WHERE TBNAME = #{literal(im.call(table))} ORDER BY COLNO").
    collect do |column| 
      column[:db_type]     = column.delete(:typename)
      if column[:db_type]  == "DECIMAL"
        column[:db_type] << "(#{column[:longlength]},#{column[:scale]})"
      end
      column[:allow_null]  = column.delete(:nulls) == 'Y'
      column[:primary_key] = column.delete(:identity) == 'Y' || !column[:keyseq].nil?
      column[:type]        = schema_column_type(column[:db_type])
      [ m.call(column.delete(:name)), column]
    end
end
supports_transaction_isolation_levels? ()

DB2 supports transaction isolation levels.

[show source]
# File lib/sequel/adapters/shared/db2.rb, line 78
def supports_transaction_isolation_levels?
  true
end
tables ()

Use SYSCAT.TABLES to get the tables for the database

[show source]
# File lib/sequel/adapters/shared/db2.rb, line 50
def tables
  metadata_dataset.
    with_sql("SELECT TABNAME FROM SYSCAT.TABLES WHERE TYPE='T' AND OWNER = #{literal(input_identifier_meth.call(opts[:user]))}").
    all.map{|h| output_identifier_meth.call(h[:tabname]) }
end
views ()

Use SYSCAT.TABLES to get the views for the database

[show source]
# File lib/sequel/adapters/shared/db2.rb, line 57
def views
  metadata_dataset.
    with_sql("SELECT TABNAME FROM SYSCAT.TABLES WHERE TYPE='V' AND OWNER = #{literal(input_identifier_meth.call(opts[:user]))}").
    all.map{|h| output_identifier_meth.call(h[:tabname]) }
end