Class Index [+]

Quicksearch

Sequel::DB2::DatabaseMethods

Constants

AUTOINCREMENT
NOT_NULL
NULL

Public Instance Methods

database_type() click to toggle source

DB2 always uses :db2 as it’s database type

    # File lib/sequel/adapters/shared/db2.rb, line 18
18:       def database_type
19:         :db2
20:       end
db2_version() click to toggle source

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

    # File lib/sequel/adapters/shared/db2.rb, line 24
24:       def db2_version
25:         return @db2_version if @db2_version
26:         @db2_version = metadata_dataset.with_sql("select service_level from sysibmadm.env_inst_info").first[:service_level]
27:       end
Also aliased as: server_version
indexes(table, opts = {}) click to toggle source

Use SYSCAT.INDEXES to get the indexes for the table

    # File lib/sequel/adapters/shared/db2.rb, line 62
62:       def indexes(table, opts = {})
63:         m = output_identifier_meth
64:         indexes = {}
65:         metadata_dataset.
66:          from(:syscat__indexes).
67:          select(:indname, :uniquerule, :colnames).
68:          where(:tabname=>input_identifier_meth.call(table), :system_required=>0).
69:          each do |r|
70:           indexes[m.call(r[:indname])] = {:unique=>(r[:uniquerule]=='U'), :columns=>r[:colnames][1..1].split('+').map{|v| m.call(v)}}
71:         end
72:         indexes
73:       end
schema_parse_table(table, opts = {}) click to toggle source

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

    # File lib/sequel/adapters/shared/db2.rb, line 31
31:       def schema_parse_table(table, opts = {})
32:         m = output_identifier_meth(opts[:dataset])
33:         im = input_identifier_meth(opts[:dataset])
34:         metadata_dataset.with_sql("SELECT * FROM SYSIBM.SYSCOLUMNS WHERE TBNAME = #{literal(im.call(table))} ORDER BY COLNO").
35:           collect do |column| 
36:             column[:db_type]     = column.delete(:typename)
37:             if column[:db_type]  == "DECIMAL"
38:               column[:db_type] << "(#{column[:longlength]},#{column[:scale]})"
39:             end
40:             column[:allow_null]  = column.delete(:nulls) == 'Y'
41:             column[:primary_key] = column.delete(:identity) == 'Y' || !column[:keyseq].nil?
42:             column[:type]        = schema_column_type(column[:db_type])
43:             [ m.call(column.delete(:name)), column]
44:           end
45:       end
server_version() click to toggle source
Alias for: db2_version
tables() click to toggle source

Use SYSCAT.TABLES to get the tables for the database

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

Use SYSCAT.TABLES to get the views for the database

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

Private Instance Methods

alter_table_sql(table, op) click to toggle source

Handle DB2 specific alter table operations.

     # File lib/sequel/adapters/shared/db2.rb, line 78
 78:       def alter_table_sql(table, op)
 79:         case op[:op]
 80:         when :add_column
 81:           if op[:primary_key] && op[:auto_increment] && op[:type] == Integer
 82:             [
 83:             "ALTER TABLE #{quote_schema_table(table)} ADD #{column_definition_sql(op.merge(:auto_increment=>false, :primary_key=>false, :default=>0, :null=>false))}",
 84:             "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{literal(op[:name])} DROP DEFAULT",
 85:             "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{literal(op[:name])} SET #{AUTOINCREMENT}"
 86:             ]
 87:           else
 88:             "ALTER TABLE #{quote_schema_table(table)} ADD #{column_definition_sql(op)}"
 89:           end
 90:         when :drop_column
 91:           "ALTER TABLE #{quote_schema_table(table)} DROP #{column_definition_sql(op)}"
 92:         when :rename_column       # renaming is only possible after db2 v9.7
 93:           "ALTER TABLE #{quote_schema_table(table)} RENAME COLUMN #{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name])}"
 94:         when :set_column_type
 95:           "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} SET DATA TYPE #{type_literal(op)}"
 96:         when :set_column_default
 97:           "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} SET DEFAULT #{literal(op[:default])}"
 98:         when :add_constraint
 99:           if op[:type] == :unique
100:             sqls = op[:columns].map{|c| ["ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(c)} SET NOT NULL", reorg_sql(table)]}
101:             sqls << super
102:             sqls.flatten
103:           else
104:             super
105:           end
106:         else
107:           super
108:         end
109:       end
auto_increment_sql() click to toggle source

DB2 uses an identity column for autoincrement.

     # File lib/sequel/adapters/shared/db2.rb, line 112
112:       def auto_increment_sql
113:         AUTOINCREMENT
114:       end
column_definition_null_sql(sql, column) click to toggle source

Add null/not null SQL fragment to column creation SQL.

     # File lib/sequel/adapters/shared/db2.rb, line 117
117:       def column_definition_null_sql(sql, column)
118:         null = column.fetch(:null, column[:allow_null]) 
119:         null = false  if column[:primary_key]
120: 
121:         sql << NOT_NULL if null == false
122:         sql << NULL if null == true
123:       end
column_list_sql(g) click to toggle source

Supply columns with NOT NULL if they are part of a composite primary/foreign key

     # File lib/sequel/adapters/shared/db2.rb, line 127
127:       def column_list_sql(g)
128:         ks = []
129:         g.constraints.each{|c| ks = c[:columns] if [:primary_key, :foreign_key].include? c[:type]} 
130:         g.columns.each{|c| c[:null] = false if ks.include?(c[:name]) }
131:         super
132:       end
create_table_as(name, sql, options) click to toggle source

Insert data from the current table into the new table after creating the table, since it is not possible to do it in one step.

     # File lib/sequel/adapters/shared/db2.rb, line 136
136:       def create_table_as(name, sql, options)
137:         super
138:         from(name).insert(sql.is_a?(Dataset) ? sql : dataset.with_sql(sql))
139:       end
create_table_as_sql(name, sql, options) click to toggle source

DB2 requires parens around the SELECT, and DEFINITION ONLY at the end.

     # File lib/sequel/adapters/shared/db2.rb, line 142
142:       def create_table_as_sql(name, sql, options)
143:         "#{create_table_prefix_sql(name, options)} AS (#{sql}) DEFINITION ONLY"
144:       end
create_table_prefix_sql(name, options) click to toggle source

Here we use DGTT which has most backward compatibility, which uses DECLARE instead of CREATE. CGTT can only be used after version 9.7. www.ibm.com/developerworks/data/library/techarticle/dm-0912globaltemptable/

     # File lib/sequel/adapters/shared/db2.rb, line 149
149:       def create_table_prefix_sql(name, options)
150:         if options[:temp]
151:           "DECLARE GLOBAL TEMPORARY TABLE #{quote_identifier(name)}"
152:         else
153:           super
154:         end
155:       end
quote_identifiers_default() click to toggle source

DB2 has issues with quoted identifiers, so turn off database quoting by default.

     # File lib/sequel/adapters/shared/db2.rb, line 159
159:       def quote_identifiers_default
160:         false
161:       end
rename_table_sql(name, new_name) click to toggle source

DB2 uses RENAME TABLE to rename tables.

     # File lib/sequel/adapters/shared/db2.rb, line 164
164:       def rename_table_sql(name, new_name)
165:         "RENAME TABLE #{quote_schema_table(name)} TO #{quote_schema_table(new_name)}"
166:       end
reorg(table) click to toggle source

Run the REORG TABLE command for the table, necessary when the table has been altered.

     # File lib/sequel/adapters/shared/db2.rb, line 170
170:       def reorg(table)
171:         synchronize(opts[:server]){|c| c.execute(reorg_sql(table))}
172:       end
reorg_sql(table) click to toggle source

The SQL to use for REORGing a table.

     # File lib/sequel/adapters/shared/db2.rb, line 175
175:       def reorg_sql(table)
176:         "CALL ADMIN_CMD(#{literal("REORG TABLE #{table}")})"
177:       end
type_literal_generic_falseclass(column) click to toggle source
type_literal_generic_file(column) click to toggle source

We uses the clob type by default for Files. Note: if user select to use blob, then insert statement should use use this for blob value:

    cast(X'fffefdfcfbfa' as blob(2G))
     # File lib/sequel/adapters/shared/db2.rb, line 183
183:       def type_literal_generic_file(column)
184:         ::Sequel::DB2::use_clob_as_blob ? :clob : :blob
185:       end
type_literal_generic_trueclass(column) click to toggle source

DB2 uses smallint to store booleans.

     # File lib/sequel/adapters/shared/db2.rb, line 188
188:       def type_literal_generic_trueclass(column)
189:         :smallint
190:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.