Skip to content
Browse files

Refactor error checking in db2 adapter to use a block

This also uses a constant instead of a class variable for the null
handle, and removes the override of test_connection since it is
the same as the default.
  • Loading branch information...
1 parent 8cf5cf0 commit 4db74e0e1c5260fa0edff30e1690512d6b9466d2 @jeremyevans jeremyevans committed Oct 14, 2011
Showing with 25 additions and 40 deletions.
  1. +25 −40 lib/sequel/adapters/db2.rb
View
65 lib/sequel/adapters/db2.rb
@@ -9,26 +9,16 @@ class Database < Sequel::Database
set_adapter_scheme :db2
TEMPORARY = 'GLOBAL TEMPORARY '.freeze
+ rc, NullHandle = DB2CLI.SQLAllocHandle(DB2CLI::SQL_HANDLE_ENV, DB2CLI::SQL_NULL_HANDLE)
- rc, @@env = DB2CLI.SQLAllocHandle(DB2CLI::SQL_HANDLE_ENV, DB2CLI::SQL_NULL_HANDLE)
- #check_error(rc, "Could not allocate DB2 environment")
def connect(server)
opts = server_opts(server)
- rc, dbc = DB2CLI.SQLAllocHandle(DB2CLI::SQL_HANDLE_DBC, @@env)
- check_error(rc, "Could not allocate database connection")
-
- rc = DB2CLI.SQLConnect(dbc, opts[:database], opts[:user], opts[:password])
- check_error(rc, "Could not connect to database")
-
+ 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
- def test_connection(server=nil)
- synchronize(server){|conn|}
- true
- end
-
def dataset(opts = nil)
DB2::Dataset.new(self, opts)
end
@@ -43,11 +33,9 @@ def execute_insert(sql, opts={})
log_connection_execute(conn, sql)
sql = "SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1"
log_connection_execute(conn, sql) do |sth|
- rc, name, buflen, datatype, size, digits, nullable = DB2CLI.SQLDescribeCol(sth, 1, 256)
- check_error(rc, "Could not describe column")
- if (rc = DB2CLI.SQLFetch(sth)) != DB2CLI::SQL_NO_DATA_FOUND
- rc, v = DB2CLI.SQLGetData(sth, 1, datatype, size)
- check_error(rc, "Could not get data")
+ 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
@@ -73,6 +61,12 @@ def check_error(rc, msg)
end
end
+ def checked_error(msg)
+ rc, *ary= yield
+ check_error(rc, msg)
+ ary.length <= 1 ? ary.first : ary
+ end
+
private
def begin_transaction(conn, opts={})
@@ -94,30 +88,24 @@ def commit_transaction(conn, opts={})
end
def log_connection_execute(conn, sql)
- rc, sth = DB2CLI.SQLAllocHandle(DB2CLI::SQL_HANDLE_STMT, conn)
- check_error(rc, "Could not allocate statement")
+ sth = checked_error("Could not allocate statement"){DB2CLI.SQLAllocHandle(DB2CLI::SQL_HANDLE_STMT, conn)}
begin
- rc = log_yield(sql){DB2CLI.SQLExecDirect(sth, sql)}
- check_error(rc, "Could not execute statement: #{sql}")
+ checked_error("Could not execute statement: #{sql}"){log_yield(sql){DB2CLI.SQLExecDirect(sth, sql)}}
- yield(sth) if block_given?
-
- rc, rpc = DB2CLI.SQLRowCount(sth)
- check_error(rc, "Could not get RPC")
- rpc
+ if block_given?
+ yield(sth)
+ else
+ checked_error("Could not get RPC"){DB2CLI.SQLRowCount(sth)}
+ end
ensure
- rc = DB2CLI.SQLFreeHandle(DB2CLI::SQL_HANDLE_STMT, sth)
- check_error(rc, "Could not free statement")
+ checked_error("Could not free statement"){DB2CLI.SQLFreeHandle(DB2CLI::SQL_HANDLE_STMT, sth)}
end
end
def disconnect_connection(conn)
- rc = DB2CLI.SQLDisconnect(conn)
- check_error(rc, "Could not disconnect from database")
-
- rc = DB2CLI.SQLFreeHandle(DB2CLI::SQL_HANDLE_DBC, conn)
- check_error(rc, "Could not free Database handle")
+ checked_error("Could not disconnect from database"){DB2CLI.SQLDisconnect(conn)}
+ checked_error("Could not free Database handle"){DB2CLI.SQLFreeHandle(DB2CLI::SQL_HANDLE_DBC, conn)}
end
end
@@ -139,8 +127,7 @@ def fetch_rows(sql)
db.check_error(rc, "Could not fetch row")
row = {}
column_info.each do |i, c, t, s|
- rc, v = DB2CLI.SQLGetData(sth, i, t, s)
- db.check_error(rc, "Could not get data")
+ v, _ = db.checked_error("Could not get data"){DB2CLI.SQLGetData(sth, i, t, s)}
row[c] = convert_type(v)
end
row.delete(row_number_column) if offset
@@ -154,12 +141,10 @@ def fetch_rows(sql)
def get_column_info(sth)
db = @db
- rc, column_count = DB2CLI.SQLNumResultCols(sth)
- db.check_error(rc, "Could not get number of result columns")
+ column_count = db.checked_error("Could not get number of result columns"){DB2CLI.SQLNumResultCols(sth)}
(1..column_count).map do |i|
- rc, name, buflen, datatype, size, digits, nullable = DB2CLI.SQLDescribeCol(sth, i, MAX_COL_SIZE)
- db.check_error(rc, "Could not describe column")
+ name, buflen, datatype, size, digits, nullable = db.checked_error("Could not describe column"){DB2CLI.SQLDescribeCol(sth, i, MAX_COL_SIZE)}
[i, output_identifier(name), datatype, size]
end
end

0 comments on commit 4db74e0

Please sign in to comment.
Something went wrong with that request. Please try again.