1//
2// EnvironmentHandle.cpp
3//
4// Library: Data/ODBC
5// Package: ODBC
6// Module: EnvironmentHandle
7//
8// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/Data/ODBC/EnvironmentHandle.h"
16#include "Poco/Data/ODBC/Utility.h"
17#include "Poco/Data/ODBC/ODBCException.h"
18
19
20namespace Poco {
21namespace Data {
22namespace ODBC {
23
24
25EnvironmentHandle::EnvironmentHandle(): _henv(SQL_NULL_HENV),
26 _isOwner(false)
27{
28 init();
29}
30
31EnvironmentHandle::EnvironmentHandle(const SQLHENV* henv) : _henv(SQL_NULL_HENV),
32 _isOwner(false)
33{
34 if (!henv || *henv == SQL_NULL_HENV)
35 init();
36 else
37 _henv = *henv;
38}
39
40void EnvironmentHandle::init()
41{
42 if (Utility::isError(SQLAllocHandle(SQL_HANDLE_ENV,
43 SQL_NULL_HANDLE,
44 &_henv)) ||
45 Utility::isError(SQLSetEnvAttr(_henv,
46 SQL_ATTR_ODBC_VERSION,
47 (SQLPOINTER)SQL_OV_ODBC3,
48 0)))
49 {
50 throw ODBCException("Could not initialize environment.");
51 }
52 _isOwner = true;
53}
54
55EnvironmentHandle::~EnvironmentHandle()
56{
57 try
58 {
59 if (_isOwner && _henv != SQL_NULL_HENV)
60 {
61 SQLRETURN rc = SQLFreeHandle(SQL_HANDLE_ENV, _henv);
62 _henv = SQL_NULL_HENV;
63 poco_assert(!Utility::isError(rc));
64 }
65 }
66 catch (...)
67 {
68 poco_unexpected();
69 }
70}
71
72
73} } } // namespace Poco::Data::ODBC
74