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::COLUMN_SCHEMA_DATETIME_TYPES, Sequel::Database::COLUMN_SCHEMA_STRING_TYPES, Sequel::Database::COMBINABLE_ALTER_TABLE_OPS, Sequel::Database::COMMA_SEPARATOR, Sequel::Database::CURRENT_TIMESTAMP_RE, Sequel::Database::DEFAULT_DATABASE_ERROR_REGEXPS, Sequel::Database::DEFAULT_JOIN_TABLE_COLUMN_OPTIONS, Sequel::Database::DEFAULT_STRING_COLUMN_SIZE, Sequel::Database::EXTENSIONS, Sequel::Database::NOT_NULL, Sequel::Database::NULL, Sequel::Database::OPTS, Sequel::Database::PRIMARY_KEY, Sequel::Database::SCHEMA_TYPE_CLASSES, 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_string_column_size, #identifier_input_method, #identifier_output_method, #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, #supports_transaction_isolation_levels?, #tables, #views

Methods included from Sequel::Database::ResetIdentifierMangling

#extended

Methods inherited from Sequel::Database

#<<, #[], adapter_class, #adapter_scheme, adapter_scheme, #add_column, #add_index, #add_servers, #after_commit, after_initialize, #after_rollback, #alter_table, #alter_table_generator, #call, #cast_type_literal, connect, #create_join_table, #create_or_replace_view, #create_table, #create_table!, #create_table?, #create_table_generator, #create_view, #database_type, #dataset, #disconnect, #drop_column, #drop_index, #drop_join_table, #drop_table, #drop_table?, #drop_view, #each_server, #execute_ddl, #execute_dui, #extend_datasets, extension, #extension, #fetch, #from, #from_application_timestamp, #get, #global_index_namespace?, #in_transaction?, #initialize, #inspect, #literal, #log_exception, #log_info, #log_yield, #logger=, #prepared_statement, #quote_identifier, #quote_identifiers=, #quote_identifiers?, register_extension, #remove_servers, #rename_column, #rename_table, #run, run_after_initialize, #schema, #schema_type_class, #select, #serial_primary_key_options, #servers, #set_column_default, #set_column_type, #set_prepared_statement, #single_threaded?, #supports_create_table_if_not_exists?, #supports_deferrable_constraints?, #supports_deferrable_foreign_key_constraints?, #supports_drop_table_if_exists?, #supports_foreign_key_parsing?, #supports_index_parsing?, #supports_partial_indexes?, #supports_prepared_transactions?, #supports_savepoints?, #supports_savepoints_in_prepared_transactions?, #supports_schema_parsing?, #supports_table_listing?, #supports_transaction_isolation_levels?, #supports_transactional_ddl?, #supports_view_listing?, #synchronize, #table_exists?, #test_connection, #to_application_timestamp, #transaction, #typecast_value, #uri, #url, #valid_connection?

Methods included from Metaprogramming

#meta_def

Constructor Details

This class inherits a constructor from Sequel::Database

Instance Attribute Details

#conversion_procsObject (readonly)

Hash of connection procs for converting



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

def conversion_procs
  @conversion_procs
end

Instance Method Details

#check_error(rc, msg) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sequel/adapters/db2.rb', line 84

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



99
100
101
102
103
# File 'lib/sequel/adapters/db2.rb', line 99

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

#connect(server) ⇒ Object



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

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

#disconnect_connection(conn) ⇒ Object



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

def disconnect_connection(conn)
  DB2CLI.SQLDisconnect(conn)
  DB2CLI.SQLFreeHandle(DB2CLI::SQL_HANDLE_DBC, conn)
end

#execute(sql, opts = OPTS, &block) ⇒ Object



58
59
60
# File 'lib/sequel/adapters/db2.rb', line 58

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

#execute_insert(sql, opts = OPTS) ⇒ Object



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

def execute_insert(sql, opts=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



105
106
107
# File 'lib/sequel/adapters/db2.rb', line 105

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