Class: Sequel::IBMDB::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/adapters/ibmdb.rb

Overview

Wraps an underlying connection to DB2 using IBM_DB.

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_param) ⇒ Connection

Create the underlying IBM_DB connection.



46
47
48
49
50
51
52
53
54
55
# File 'lib/sequel/adapters/ibmdb.rb', line 46

def initialize(connection_param)
  @conn = if connection_param.class == String
    IBM_DB.connect(connection_param, '', '')
  else  # connect using catalog 
    IBM_DB.connect(*connection_param)
  end

  self.autocommit = true
  @prepared_statements = {}
end

Instance Attribute Details

#prepared_statementsObject

A hash with prepared statement name symbol keys, where each value is a two element array with an sql string and cached Statement value.



33
34
35
# File 'lib/sequel/adapters/ibmdb.rb', line 33

def prepared_statements
  @prepared_statements
end

Instance Method Details

#autocommitObject

Check whether the connection is in autocommit state or not.



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

def autocommit
  IBM_DB.autocommit(@conn) == 1
end

#autocommit=(value) ⇒ Object

Turn autocommit on or off for the connection.



63
64
65
# File 'lib/sequel/adapters/ibmdb.rb', line 63

def autocommit=(value)
  IBM_DB.autocommit(@conn, value ? IBM_DB::SQL_AUTOCOMMIT_ON : IBM_DB::SQL_AUTOCOMMIT_OFF)
end

#closeObject

Close the connection, disconnecting from DB2.



68
69
70
# File 'lib/sequel/adapters/ibmdb.rb', line 68

def close
  IBM_DB.close(@conn)
end

#commitObject

Commit the currently outstanding transaction on this connection.



73
74
75
# File 'lib/sequel/adapters/ibmdb.rb', line 73

def commit
  IBM_DB.commit(@conn)
end

#error_msgObject

Return the related error message for the connection.



78
79
80
# File 'lib/sequel/adapters/ibmdb.rb', line 78

def error_msg
  IBM_DB.getErrormsg(@conn, IBM_DB::DB_CONN)
end

#error_sqlstateObject

Return the related error message for the connection.



83
84
85
# File 'lib/sequel/adapters/ibmdb.rb', line 83

def error_sqlstate
  IBM_DB.getErrorstate(@conn, IBM_DB::DB_CONN)
end

#execute(sql) ⇒ Object

Execute the given SQL on the database, and return a Statement instance holding the results.

Raises:



89
90
91
92
93
# File 'lib/sequel/adapters/ibmdb.rb', line 89

def execute(sql)
  stmt = IBM_DB.exec(@conn, sql)
  raise Error.new(error_msg, error_sqlstate) unless stmt
  Statement.new(stmt)
end

#execute_prepared(ps_name, *values) ⇒ Object

Execute the related prepared statement on the database with the given arguments.



97
98
99
100
101
102
103
104
# File 'lib/sequel/adapters/ibmdb.rb', line 97

def execute_prepared(ps_name, *values)
  stmt = @prepared_statements[ps_name].last
  res = stmt.execute(*values)
  unless res
    raise Error.new("Error executing statement #{ps_name}: #{error_msg}", error_sqlstate)
  end
  stmt
end

#prepare(sql, ps_name) ⇒ Object

Prepare a statement with the given sql on the database, and cache the prepared statement value by name.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sequel/adapters/ibmdb.rb', line 108

def prepare(sql, ps_name)
  if stmt = IBM_DB.prepare(@conn, sql)
    ps_name = ps_name.to_sym
    stmt = Statement.new(stmt)
    @prepared_statements[ps_name] = [sql, stmt]
  else
    err = error_msg
    err = "Error preparing #{ps_name} with SQL: #{sql}" if error_msg.nil? || error_msg.empty?
    raise Error.new(err, error_sqlstate)
  end
end

#rollbackObject

Rollback the currently outstanding transaction on this connection.



121
122
123
# File 'lib/sequel/adapters/ibmdb.rb', line 121

def rollback
  IBM_DB.rollback(@conn)
end