1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: odbcconfig.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef _DBAUI_ODBC_CONFIG_HXX_
35 #include "odbcconfig.hxx"
37 #include <rtl/bootstrap.hxx>
38 #ifndef _RTL_USTRING_HXX_
39 #include <rtl/ustring.hxx>
41 #ifndef _RTL_USTRBUF_HXX_
42 #include <rtl/ustrbuf.hxx>
44 #ifndef _OSL_DIAGNOSE_H_
45 #include <osl/diagnose.h>
47 #ifndef _OSL_PROCESS_H_
48 #include <osl/process.h>
51 #include <osl/thread.hxx>
53 #ifndef _TOOLS_DEBUG_HXX
54 #include <tools/debug.hxx>
57 #include <vcl/svapp.hxx>
60 #ifdef HAVE_ODBC_SUPPORT
62 #if defined(WIN) || defined(OS2)
63 #define ODBC_LIBRARY "ODBC.DLL"
64 #define ODBC_UI_LIBRARY "ODBCINST.DLL"
67 #define ODBC_LIBRARY "ODBC32.DLL"
68 #define ODBC_UI_LIBRARY "ODBCCP32.DLL"
72 #define ODBC_LIBRARY "libiodbc.dylib"
73 #define ODBC_UI_LIBRARY "libiodbcinst.dylib"
75 #define ODBC_LIBRARY_1 "libodbc.so.1"
76 #define ODBC_UI_LIBRARY_1 "libodbcinst.so.1"
77 #define ODBC_LIBRARY "libodbc.so"
78 #define ODBC_UI_LIBRARY "libodbcinst.so"
82 // just to go with calling convention of windows
83 // so don't touch this
84 #if defined(WIN) || defined(WNT)
85 #define SQL_API __stdcall
86 // At least under some circumstances, the below #include <odbc/sqlext.h> re-
87 // defines SQL_API to an empty string, leading to a compiler warning on MSC; to
88 // not break the current behavior, this is worked around by locally disabling
92 #pragma warning(disable: 4005)
94 #endif // defined(WIN) || defined(WNT)
97 #define ALLREADY_HAVE_OS2_TYPES
101 #ifdef SYSTEM_ODBC_HEADERS
105 #include <odbc/sqlext.h>
109 #if defined(WIN) || defined(WNT)
114 #define SQL_API __stdcall
115 #endif // defined(WIN) || defined(WNT)
116 // from here on you can do what you want to
119 #define SQL_API _System
120 #endif // defined(OS2)
124 #define ODBC_LIBRARY ""
125 #define ODBC_UI_LIBRARY ""
127 #endif // HAVE_ODBC_SUPPORT
129 //.........................................................................
132 //.........................................................................
135 #ifdef HAVE_ODBC_SUPPORT
136 typedef SQLRETURN (SQL_API
* TSQLManageDataSource
) (SQLHWND hwndParent
);
137 typedef SQLRETURN (SQL_API
* TSQLAllocHandle
) (SQLSMALLINT HandleType
, SQLHANDLE InputHandle
, SQLHANDLE
* OutputHandlePtr
);
138 typedef SQLRETURN (SQL_API
* TSQLFreeHandle
) (SQLSMALLINT HandleType
, SQLHANDLE Handle
);
139 typedef SQLRETURN (SQL_API
* TSQLSetEnvAttr
) (SQLHENV EnvironmentHandle
, SQLINTEGER Attribute
, SQLPOINTER ValuePtr
, SQLINTEGER StringLength
);
140 typedef SQLRETURN (SQL_API
* TSQLDataSources
) (SQLHENV EnvironmentHandle
, SQLUSMALLINT Direction
, SQLCHAR
* ServerName
,
141 SQLSMALLINT BufferLength1
, SQLSMALLINT
* NameLength1Ptr
, SQLCHAR
* Description
, SQLSMALLINT BufferLength2
, SQLSMALLINT
* NameLength2Ptr
);
143 #define NSQLManageDataSource(a) (*(TSQLManageDataSource)(m_pSQLManageDataSource))(a)
144 #define NSQLAllocHandle(a,b,c) (*(TSQLAllocHandle)(m_pAllocHandle))(a,b,c)
145 #define NSQLFreeHandle(a,b) (*(TSQLFreeHandle)(m_pFreeHandle))(a,b)
146 #define NSQLSetEnvAttr(a,b,c,d) (*(TSQLSetEnvAttr)(m_pSetEnvAttr))(a,b,c,d)
147 #define NSQLDataSources(a,b,c,d,e,f,g,h) (*(TSQLDataSources)(m_pDataSources))(a,b,c,d,e,f,g,h)
150 //=========================================================================
152 //=========================================================================
153 DBG_NAME(OOdbcLibWrapper
)
154 //-------------------------------------------------------------------------
155 #ifdef HAVE_ODBC_SUPPORT
156 OOdbcLibWrapper::OOdbcLibWrapper()
159 DBG_CTOR(OOdbcLibWrapper
,NULL
);
164 //-------------------------------------------------------------------------
165 sal_Bool
OOdbcLibWrapper::load(const sal_Char
* _pLibPath
)
167 m_sLibPath
= ::rtl::OUString::createFromAscii(_pLibPath
);
168 #ifdef HAVE_ODBC_SUPPORT
170 m_pOdbcLib
= osl_loadModule(m_sLibPath
.pData
, SAL_LOADMODULE_NOW
);
171 return (NULL
!= m_pOdbcLib
);
175 //-------------------------------------------------------------------------
176 void OOdbcLibWrapper::unload()
178 #ifdef HAVE_ODBC_SUPPORT
181 osl_unloadModule(m_pOdbcLib
);
187 //-------------------------------------------------------------------------
188 oslGenericFunction
OOdbcLibWrapper::loadSymbol(const sal_Char
* _pFunctionName
)
190 return osl_getFunctionSymbol(m_pOdbcLib
, ::rtl::OUString::createFromAscii(_pFunctionName
).pData
);
193 //-------------------------------------------------------------------------
194 OOdbcLibWrapper::~OOdbcLibWrapper()
198 DBG_DTOR(OOdbcLibWrapper
,NULL
);
201 //=========================================================================
203 //=========================================================================
206 #ifdef HAVE_ODBC_SUPPORT
207 SQLHANDLE hEnvironment
;
208 OdbcTypesImpl() : hEnvironment(0) { }
213 DBG_NAME(OOdbcEnumeration
)
214 //-------------------------------------------------------------------------
215 OOdbcEnumeration::OOdbcEnumeration()
216 #ifdef HAVE_ODBC_SUPPORT
217 :m_pAllocHandle(NULL
)
219 ,m_pDataSources(NULL
)
220 ,m_pImpl(new OdbcTypesImpl
)
223 DBG_CTOR(OOdbcEnumeration
,NULL
);
225 sal_Bool bLoaded
= load(ODBC_LIBRARY
);
226 #ifdef ODBC_LIBRARY_1
228 bLoaded
= load(ODBC_LIBRARY_1
);
233 #ifdef HAVE_ODBC_SUPPORT
234 // load the generic functions
235 m_pAllocHandle
= loadSymbol("SQLAllocHandle");
236 m_pFreeHandle
= loadSymbol("SQLFreeHandle");
237 m_pSetEnvAttr
= loadSymbol("SQLSetEnvAttr");
238 m_pDataSources
= loadSymbol("SQLDataSources");
241 if (!m_pAllocHandle
|| !m_pSetEnvAttr
|| !m_pDataSources
|| !m_pFreeHandle
)
244 m_pAllocHandle
= m_pFreeHandle
= m_pSetEnvAttr
= m_pDataSources
= NULL
;
250 //-------------------------------------------------------------------------
251 OOdbcEnumeration::~OOdbcEnumeration()
256 DBG_DTOR(OOdbcEnumeration
,NULL
);
259 //-------------------------------------------------------------------------
260 sal_Bool
OOdbcEnumeration::allocEnv()
262 OSL_ENSURE(isLoaded(), "OOdbcEnumeration::allocEnv: not loaded!");
266 #ifdef HAVE_ODBC_SUPPORT
267 if (m_pImpl
->hEnvironment
)
270 SQLRETURN nResult
= NSQLAllocHandle(SQL_HANDLE_ENV
, SQL_NULL_HANDLE
, &m_pImpl
->hEnvironment
);
271 if (SQL_SUCCESS
!= nResult
)
272 // can't do anything without environment
275 NSQLSetEnvAttr(m_pImpl
->hEnvironment
, SQL_ATTR_ODBC_VERSION
, (SQLPOINTER
)SQL_OV_ODBC3
, SQL_IS_INTEGER
);
282 //-------------------------------------------------------------------------
283 void OOdbcEnumeration::freeEnv()
285 #ifdef HAVE_ODBC_SUPPORT
286 if (m_pImpl
->hEnvironment
)
287 NSQLFreeHandle(SQL_HANDLE_ENV
, m_pImpl
->hEnvironment
);
288 m_pImpl
->hEnvironment
= 0;
292 //-------------------------------------------------------------------------
293 void OOdbcEnumeration::getDatasourceNames(StringBag
& _rNames
)
295 OSL_ENSURE(isLoaded(), "OOdbcEnumeration::getDatasourceNames: not loaded!");
301 OSL_ENSURE(sal_False
, "OOdbcEnumeration::getDatasourceNames: could not allocate an ODBC environment!");
305 #ifdef HAVE_ODBC_SUPPORT
306 // now that we have an environment collect the data source names
307 UCHAR szDSN
[SQL_MAX_DSN_LENGTH
+1];
309 UCHAR szDescription
[1024+1];
310 SWORD pcbDescription
;
311 SQLRETURN nResult
= SQL_SUCCESS
;
312 rtl_TextEncoding nTextEncoding
= osl_getThreadTextEncoding();
314 for ( nResult
= NSQLDataSources(m_pImpl
->hEnvironment
, SQL_FETCH_FIRST
, szDSN
, sizeof(szDSN
), &pcbDSN
, szDescription
, sizeof(szDescription
)-1, &pcbDescription
);
316 nResult
= NSQLDataSources(m_pImpl
->hEnvironment
, SQL_FETCH_NEXT
, szDSN
, sizeof(szDSN
), &pcbDSN
, szDescription
, sizeof(szDescription
)-1, &pcbDescription
)
319 if (nResult
!= SQL_SUCCESS
)
320 // no further error handling
324 ::rtl::OUString
aCurrentDsn(reinterpret_cast<const char*>(szDSN
),pcbDSN
, nTextEncoding
);
325 _rNames
.insert(aCurrentDsn
);
331 #ifdef HAVE_ODBC_ADMINISTRATION
333 //=========================================================================
334 //= ProcessTerminationWait
335 //=========================================================================
336 class ProcessTerminationWait
: public ::osl::Thread
338 oslProcess m_hProcessHandle
;
342 ProcessTerminationWait( oslProcess _hProcessHandle
, const Link
& _rFinishHdl
)
343 :m_hProcessHandle( _hProcessHandle
)
344 ,m_aFinishHdl( _rFinishHdl
)
349 virtual void SAL_CALL
run()
351 osl_joinProcess( m_hProcessHandle
);
352 osl_freeProcessHandle( m_hProcessHandle
);
353 Application::PostUserEvent( m_aFinishHdl
);
357 //=========================================================================
359 //=========================================================================
360 //-------------------------------------------------------------------------
361 OOdbcManagement::OOdbcManagement( const Link
& _rAsyncFinishCallback
)
362 :m_pProcessWait( NULL
)
363 ,m_aAsyncFinishCallback( _rAsyncFinishCallback
)
367 //-------------------------------------------------------------------------
368 OOdbcManagement::~OOdbcManagement()
370 // wait for our thread to be finished
371 if ( m_pProcessWait
.get() )
372 m_pProcessWait
->join();
375 //-------------------------------------------------------------------------
376 bool OOdbcManagement::manageDataSources_async()
378 OSL_PRECOND( !isRunning(), "OOdbcManagement::manageDataSources_async: still running from the previous call!" );
382 // this is done in an external process, due to #i78733#
383 // (and note this whole functionality is supported on Windows only, ATM)
384 ::rtl::OUString
sExecutableName( RTL_CONSTASCII_USTRINGPARAM( "$OOO_BASE_DIR/program/odbcconfig.exe" ) );
385 ::rtl::Bootstrap::expandMacros( sExecutableName
); //TODO: detect failure
386 oslProcess
hProcessHandle(0);
387 oslProcessError eError
= osl_executeProcess( sExecutableName
.pData
, NULL
, 0, 0, NULL
, NULL
, NULL
, 0, &hProcessHandle
);
388 if ( eError
!= osl_Process_E_None
)
391 m_pProcessWait
.reset( new ProcessTerminationWait( hProcessHandle
, m_aAsyncFinishCallback
) );
392 m_pProcessWait
->create();
396 //-------------------------------------------------------------------------
397 bool OOdbcManagement::isRunning() const
399 return ( m_pProcessWait
.get() && m_pProcessWait
->isRunning() );
402 #endif // HAVE_ODBC_ADMINISTRATION
404 //.........................................................................
406 //.........................................................................