20#include <config_folders.h>
23#include <rtl/bootstrap.hxx>
24#include <rtl/ustring.hxx>
25#include <osl/diagnose.h>
26#include <osl/process.h>
27#include <osl/thread.hxx>
30#ifdef HAVE_ODBC_SUPPORT
33#define ODBC_LIBRARY "ODBC32.DLL"
37#define ODBC_LIBRARY "libiodbc.dylib"
39#define ODBC_LIBRARY_PLAIN "libodbc.so"
40#define ODBC_LIBRARY_1 "libodbc.so.1"
41#define ODBC_LIBRARY "libodbc.so.2"
49#define ODBC_LIBRARY ""
56#ifdef HAVE_ODBC_SUPPORT
58typedef SQLRETURN (SQL_API*
TSQLAllocHandle) (SQLSMALLINT HandleType, SQLHANDLE InputHandle, SQLHANDLE* OutputHandlePtr);
60typedef SQLRETURN (SQL_API*
TSQLSetEnvAttr) (SQLHENV EnvironmentHandle, SQLINTEGER
Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength);
62 SQLSMALLINT BufferLength1, SQLSMALLINT* NameLength1Ptr, SQLCHAR* Description, SQLSMALLINT BufferLength2, SQLSMALLINT* NameLength2Ptr);
70 m_sLibPath = OUString::createFromAscii(_pLibPath);
71#if defined(HAVE_ODBC_SUPPORT) && !defined(DISABLE_DYNLOADING)
82#if defined(HAVE_ODBC_SUPPORT) && !defined(DISABLE_DYNLOADING)
93 return osl_getFunctionSymbol(
m_pOdbcLib, OUString::createFromAscii(_pFunctionName).
pData);
99#ifdef HAVE_ODBC_SUPPORT
110 ,m_pAllocHandle(nullptr)
111 ,m_pFreeHandle(nullptr)
112 ,m_pSetEnvAttr(nullptr)
113 ,m_pDataSources(nullptr)
122#ifdef ODBC_LIBRARY_PLAIN
130#ifdef HAVE_ODBC_SUPPORT
155 OSL_ENSURE(
isLoaded(),
"OOdbcEnumeration::allocEnv: not loaded!");
159#ifdef HAVE_ODBC_SUPPORT
164 if (SQL_SUCCESS != nResult)
177#ifdef HAVE_ODBC_SUPPORT
180 m_pImpl->hEnvironment =
nullptr;
186 OSL_ENSURE(
isLoaded(),
"OOdbcEnumeration::getDatasourceNames: not loaded!");
192 OSL_FAIL(
"OOdbcEnumeration::getDatasourceNames: could not allocate an ODBC environment!");
196#ifdef HAVE_ODBC_SUPPORT
198 UCHAR szDSN[SQL_MAX_DSN_LENGTH+1];
200 UCHAR szDescription[1024+1];
201 SWORD pcbDescription;
202 SQLRETURN nResult = SQL_SUCCESS;
203 rtl_TextEncoding nTextEncoding = osl_getThreadTextEncoding();
206 sizeof(szDSN), &pcbDSN, szDescription,
sizeof(szDescription)-1, &pcbDescription);
209 sizeof(szDSN), &pcbDSN, szDescription,
sizeof(szDescription)-1, &pcbDescription)
212 if (nResult != SQL_SUCCESS)
217 OUString aCurrentDsn(
reinterpret_cast<const char*
>(szDSN),pcbDSN, nTextEncoding);
218 _rNames.insert(aCurrentDsn);
226#ifdef HAVE_ODBC_ADMINISTRATION
229class ProcessTerminationWait :
public ::osl::Thread
231 oslProcess m_hProcessHandle;
236 ProcessTerminationWait( oslProcess _hProcessHandle,
const Link<void*,void>& _rFinishHdl )
237 : m_hProcessHandle( _hProcessHandle )
238 , m_aFinishHdl( _rFinishHdl )
239 , m_nEventId(nullptr)
243 void disableCallback()
251 m_nEventId =
nullptr;
255 void receivedCallback()
257 m_nEventId =
nullptr;
261 virtual void SAL_CALL
run()
override
263 osl_setThreadName(
"dbaui::ProcessTerminationWait");
265 osl_joinProcess( m_hProcessHandle );
266 osl_freeProcessHandle( m_hProcessHandle );
272OOdbcManagement::OOdbcManagement(
const Link<void*,void>& rAsyncFinishCallback)
273 : m_aAsyncFinishCallback(rAsyncFinishCallback)
277OOdbcManagement::~OOdbcManagement()
280 if ( m_pProcessWait )
281 m_pProcessWait->join();
284bool OOdbcManagement::manageDataSources_async()
286 OSL_PRECOND( !isRunning(),
"OOdbcManagement::manageDataSources_async: still running from the previous call!" );
292 OUString sExecutableName(
"$BRAND_BASE_DIR/" LIBO_LIBEXEC_FOLDER
"/odbcconfig.exe" );
293 ::rtl::Bootstrap::expandMacros( sExecutableName );
294 oslProcess hProcessHandle(
nullptr);
295 oslProcessError eError = osl_executeProcess( sExecutableName.pData,
nullptr, 0, 0,
nullptr,
nullptr,
nullptr, 0, &hProcessHandle );
296 if ( eError != osl_Process_E_None )
299 m_pProcessWait.reset(
new ProcessTerminationWait( hProcessHandle, m_aAsyncFinishCallback ) );
300 m_pProcessWait->create();
304void OOdbcManagement::disableCallback()
307 m_pProcessWait->disableCallback();
310void OOdbcManagement::receivedCallback()
313 m_pProcessWait->receivedCallback();
316bool OOdbcManagement::isRunning()
const
318 return ( m_pProcessWait && m_pProcessWait->isRunning() );
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
static void RemoveUserEvent(ImplSVEvent *nUserEvent)
void freeEnv()
free any allocated ODBC environment
void unload()
unload the lib
std::unique_ptr< OdbcTypesImpl > m_pImpl
void getDatasourceNames(std::set< OUString > &_rNames)
oslGenericFunction loadSymbol(const char *_pFunctionName)
oslGenericFunction m_pDataSources
bool allocEnv()
ensure that an ODBC environment is allocated
oslGenericFunction m_pFreeHandle
bool load(const char *_pLibPath)
load the lib
oslGenericFunction m_pAllocHandle
oslGenericFunction m_pSetEnvAttr
std::unique_ptr< sal_Int32[]> pData
def run(arg=None, arg2=-1)
SQLRETURN(SQL_API * TSQLSetEnvAttr)(SQLHENV EnvironmentHandle, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength)
SQLRETURN(SQL_API * TSQLAllocHandle)(SQLSMALLINT HandleType, SQLHANDLE InputHandle, SQLHANDLE *OutputHandlePtr)
SQLRETURN(SQL_API * TSQLFreeHandle)(SQLSMALLINT HandleType, SQLHANDLE Handle)
SQLRETURN(SQL_API * TSQLDataSources)(SQLHENV EnvironmentHandle, SQLUSMALLINT Direction, SQLCHAR *ServerName, SQLSMALLINT BufferLength1, SQLSMALLINT *NameLength1Ptr, SQLCHAR *Description, SQLSMALLINT BufferLength2, SQLSMALLINT *NameLength2Ptr)
SQLRETURN(SQL_API * TSQLManageDataSource)(SQLHWND hwndParent)
#define ODBC_LIBRARY_PLAIN
#define HAVE_ODBC_SUPPORT