Class: Sequel::DB2::Database

Inherits:
Sequel::Database show all
Includes:
DatabaseMethods
Defined in:
lib/sequel/adapters/db2.rb

Constant Summary collapse

TEMPORARY =
'GLOBAL TEMPORARY '.freeze
ERROR_MAP =
{}
DatasetClass =
self

Constants included from DatabaseMethods

Sequel::DB2::DatabaseMethods::AUTOINCREMENT, Sequel::DB2::DatabaseMethods::NOT_NULL, Sequel::DB2::DatabaseMethods::NULL

Constants inherited from Sequel::Database

Sequel::Database::ADAPTERS, Sequel::Database::AUTOINCREMENT, Sequel::Database::COLUMN_DEFINITION_ORDER, Sequel::Database::COMMA_SEPARATOR, Sequel::Database::DEFAULT_JOIN_TABLE_COLUMN_OPTIONS, Sequel::Database::MSSQL_DEFAULT_RE, Sequel::Database::MYSQL_TIMESTAMP_RE, Sequel::Database::NOT_NULL, Sequel::Database::NULL, Sequel::Database::POSTGRES_DEFAULT_RE, Sequel::Database::PRIMARY_KEY, Sequel::Database::SQL_BEGIN, Sequel::Database::SQL_COMMIT, Sequel::Database::SQL_RELEASE_SAVEPOINT, Sequel::Database::SQL_ROLLBACK, Sequel::Database::SQL_ROLLBACK_TO_SAVEPOINT, Sequel::Database::SQL_SAVEPOINT, Sequel::Database::STRING_DEFAULT_RE, Sequel::Database::TRANSACTION_BEGIN, Sequel::Database::TRANSACTION_COMMIT, Sequel::Database::TRANSACTION_ISOLATION_LEVELS, Sequel::Database::TRANSACTION_ROLLBACK, Sequel::Database::UNDERSCORE, Sequel::Database::UNIQUE, Sequel::Database::UNSIGNED

Instance Attribute Summary collapse

Attributes inherited from Sequel::Database

#cache_schema, #dataset_class, #default_schema, #log_warn_duration, #loggers, #opts, #pool, #prepared_statements, #sql_log_level, #timezone, #transaction_isolation_level

Instance Method Summary collapse

Methods included from DatabaseMethods

#database_type, #db2_version, #indexes, #schema_parse_table, #tables, #views

Methods inherited from Sequel::Database

#<<, #[], adapter_class, adapter_scheme, #adapter_scheme, #add_column, #add_index, #add_servers, #after_commit, #after_rollback, #alter_table, #call, #cast_type_literal, connect, #create_join_table, #create_or_replace_view, #create_table, #create_table!, #create_table?, #create_view, #database_type, #dataset, #disconnect, #drop_column, #drop_index, #drop_join_table, #drop_table, #drop_table?, #drop_view, #dump_foreign_key_migration, #dump_indexes_migration, #dump_schema_cache, #dump_schema_cache?, #dump_schema_migration, #dump_table_schema, #each_server, #execute_ddl, #execute_dui, #extend_datasets, #fetch, #foreign_key_list, #from, #from_application_timestamp, #get, #identifier_input_method, identifier_input_method, #identifier_input_method=, identifier_input_method=, #identifier_output_method, identifier_output_method, #identifier_output_method=, identifier_output_method=, #in_transaction?, #indexes, #inspect, #literal, #load_schema_cache, #load_schema_cache?, #log_exception, #log_info, #log_yield, #logger=, #prepared_statement, #query, #quote_identifiers=, quote_identifiers=, #quote_identifiers?, #remove_servers, #rename_column, #rename_table, #run, #schema, #select, #serial_primary_key_options, #servers, #set_column_default, #set_column_type, #set_prepared_statement, single_threaded=, #single_threaded?, #supports_create_table_if_not_exists?, #supports_drop_table_if_exists?, #supports_prepared_transactions?, #supports_savepoints?, #supports_savepoints_in_prepared_transactions?, #supports_transaction_isolation_levels?, #supports_transactional_ddl?, #synchronize, #table_exists?, #tables, #test_connection, #to_application_timestamp, #transaction, #typecast_value, #uri, #url, #views

Methods included from Metaprogramming

#meta_def

Constructor Details

#initialize(opts = {}) ⇒ Database

Returns a new instance of Database.



47
48
49
50
51
# File 'lib/sequel/adapters/db2.rb', line 47

def initialize(opts={})
  super
  @conversion_procs = DB2_TYPES.dup
  @conversion_procs[DB2CLI::SQL_TYPE_TIMESTAMP] = method(:to_application_timestamp_db2)
end

Instance Attribute Details

#conversion_procsObject (readonly)

Hash of connection procs for converting



45
46
47
# File 'lib/sequel/adapters/db2.rb', line 45

def conversion_procs
  @conversion_procs
end

Instance Method Details

#check_error(rc, msg) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sequel/adapters/db2.rb', line 87

def check_error(rc, msg)
  case rc
  when DB2CLI::SQL_SUCCESS, DB2CLI::SQL_SUCCESS_WITH_INFO, DB2CLI::SQL_NO_DATA_FOUND
    nil
  when DB2CLI::SQL_INVALID_HANDLE, DB2CLI::SQL_STILL_EXECUTING
    e = DB2Error.new("#{ERROR_MAP[rc]}: #{msg}")
    e.set_backtrace(caller)
    raise_error(e, :disconnect=>true)
  else
    e = DB2Error.new("#{ERROR_MAP[rc] || "Error code #{rc}"}: #{msg}")
    e.set_backtrace(caller)
    raise_error(e, :disconnect=>true)
  end
end

#checked_error(msg) ⇒ Object



102
103
104
105
106
# File 'lib/sequel/adapters/db2.rb', line 102

def checked_error(msg)
  rc, *ary= yield
  check_error(rc, msg)
  ary.length <= 1 ? ary.first : ary
end

#connect(server) ⇒ Object



53
54
55
56
57
58
# File 'lib/sequel/adapters/db2.rb', line 53

def connect(server)
  opts = server_opts(server)
  dbc = checked_error("Could not allocate database connection"){DB2CLI.SQLAllocHandle(DB2CLI::SQL_HANDLE_DBC, NullHandle)}
  checked_error("Could not connect to database"){DB2CLI.SQLConnect(dbc, opts[:database], opts[:user], opts[:password])}
  dbc
end

#execute(sql, opts = {}, &block) ⇒ Object Also known as: do



60
61
62
# File 'lib/sequel/adapters/db2.rb', line 60

def execute(sql, opts={}, &block)
  synchronize(opts[:server]){|conn| log_connection_execute(conn, sql, &block)}
end

#execute_insert(sql, opts = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sequel/adapters/db2.rb', line 65

def execute_insert(sql, opts={})
  synchronize(opts[:server]) do |conn|
    log_connection_execute(conn, sql)
    sql = "SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1"
    log_connection_execute(conn, sql) do |sth|
      name, buflen, datatype, size, digits, nullable = checked_error("Could not describe column"){DB2CLI.SQLDescribeCol(sth, 1, 256)}
      if DB2CLI.SQLFetch(sth) != DB2CLI::SQL_NO_DATA_FOUND
        v, _ = checked_error("Could not get data"){DB2CLI.SQLGetData(sth, 1, datatype, size)}
        if v.is_a?(String) 
          return v.to_i
        else
          return nil
        end
      end
    end
  end
end

#to_application_timestamp_db2(v) ⇒ Object



108
109
110
# File 'lib/sequel/adapters/db2.rb', line 108

def to_application_timestamp_db2(v)
  to_application_timestamp(v.to_s)
end