Module Sequel::DB2::DatabaseMethods
In: lib/sequel/adapters/shared/db2.rb

Methods

Constants

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

Attributes

use_clob_as_blob  [RW]  Whether to use clob as the generic File type, false by default.

Public Instance methods

[Source]

    # File lib/sequel/adapters/shared/db2.rb, line 13
13:       def database_type
14:         :db2
15:       end

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

[Source]

    # File lib/sequel/adapters/shared/db2.rb, line 19
19:       def db2_version
20:         return @db2_version if defined?(@db2_version)
21:         @db2_version = metadata_dataset.with_sql("select service_level from sysibmadm.env_inst_info").first[:service_level]
22:       end

[Source]

    # File lib/sequel/adapters/shared/db2.rb, line 25
25:       def freeze
26:         db2_version
27:         offset_strategy
28:         super
29:       end

Use SYSCAT.INDEXES to get the indexes for the table

[Source]

    # File lib/sequel/adapters/shared/db2.rb, line 70
70:       def indexes(table, opts = OPTS)
71:         m = output_identifier_meth
72:         indexes = {}
73:         metadata_dataset.
74:          from(Sequel[:syscat][:indexes]).
75:          select(:indname, :uniquerule, :colnames).
76:          where(:tabname=>input_identifier_meth.call(table), :system_required=>0).
77:          each do |r|
78:           indexes[m.call(r[:indname])] = {:unique=>(r[:uniquerule]=='U'), :columns=>r[:colnames][1..-1].split('+').map{|v| m.call(v)}}
79:         end
80:         indexes
81:       end

[Source]

    # File lib/sequel/adapters/shared/db2.rb, line 83
83:       def offset_strategy
84:         return @offset_strategy if defined?(@offset_strategy)
85: 
86:         @offset_strategy = case strategy = opts[:offset_strategy].to_s
87:         when "limit_offset", "offset_fetch"
88:           opts[:offset_strategy] = strategy.to_sym
89:         else
90:           opts[:offset_strategy] = :emulate
91:         end
92:       end

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

[Source]

    # File lib/sequel/adapters/shared/db2.rb, line 32
32:       def schema_parse_table(table, opts = OPTS)
33:         m = output_identifier_meth(opts[:dataset])
34:         im = input_identifier_meth(opts[:dataset])
35:         metadata_dataset.with_sql("SELECT * FROM SYSIBM.SYSCOLUMNS WHERE TBNAME = #{literal(im.call(table))} ORDER BY COLNO").
36:           collect do |column| 
37:             column[:db_type] = column.delete(:typename)
38:             if column[:db_type] =~ /\A(VAR)?CHAR\z/
39:               column[:db_type] << "(#{column[:length]})"
40:             end
41:             if column[:db_type] == "DECIMAL"
42:               column[:db_type] << "(#{column[:longlength]},#{column[:scale]})"
43:             end
44:             column[:allow_null] = column.delete(:nulls) == 'Y'
45:             identity = column.delete(:identity) == 'Y'
46:             if column[:primary_key] = identity || !column[:keyseq].nil?
47:               column[:auto_increment] = identity
48:             end
49:             column[:type]        = schema_column_type(column[:db_type])
50:             column[:max_length]  = column[:longlength] if column[:type] == :string
51:             [ m.call(column.delete(:name)), column]
52:           end
53:       end
server_version()

Alias for db2_version

DB2 supports transaction isolation levels.

[Source]

    # File lib/sequel/adapters/shared/db2.rb, line 95
95:       def supports_transaction_isolation_levels?
96:         true
97:       end

On DB2, a table might need to be REORGed if you are testing existence of it. This REORGs automatically if the database raises a specific error that indicates it should be REORGed.

[Source]

     # File lib/sequel/adapters/shared/db2.rb, line 102
102:       def table_exists?(name)
103:         v ||= false # only retry once
104:         sch, table_name = schema_and_table(name)
105:         name = SQL::QualifiedIdentifier.new(sch, table_name) if sch
106:         from(name).first
107:         true
108:       rescue DatabaseError => e
109:         if e.to_s =~ /Operation not allowed for reason code "7" on table/ && v == false
110:           # table probably needs reorg
111:           reorg(name)
112:           v = true
113:           retry 
114:         end
115:         false
116:       end

Use SYSCAT.TABLES to get the tables for the database

[Source]

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

Use SYSCAT.TABLES to get the views for the database

[Source]

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

[Validate]