Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / dlg / odbcconfig.cxx
blob0a5c850598bddd84f1b5be9371d63204f5cbecb3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "odbcconfig.hxx"
32 #ifdef SYSTEM_ODBC_HEADERS
33 #include <sqltypes.h>
34 #else
35 #include <odbc/sqltypes.h>
36 #endif
38 #include <rtl/bootstrap.hxx>
39 #include <rtl/ustring.hxx>
40 #include <rtl/ustrbuf.hxx>
41 #include <osl/diagnose.h>
42 #include <osl/process.h>
43 #include <osl/thread.hxx>
44 #include <tools/debug.hxx>
45 #include <vcl/svapp.hxx>
47 #ifdef HAVE_ODBC_SUPPORT
49 #if defined WNT
50 #define ODBC_LIBRARY "ODBC32.DLL"
51 #define ODBC_UI_LIBRARY "ODBCCP32.DLL"
52 #endif
53 #ifdef UNX
54 #ifdef MACOSX
55 #define ODBC_LIBRARY "libiodbc.dylib"
56 #define ODBC_UI_LIBRARY "libiodbcinst.dylib"
57 #else
58 #define ODBC_LIBRARY_1 "libodbc.so.1"
59 #define ODBC_UI_LIBRARY_1 "libodbcinst.so.1"
60 #define ODBC_LIBRARY "libodbc.so"
61 #define ODBC_UI_LIBRARY "libodbcinst.so"
62 #endif
63 #endif
65 // just to go with calling convention of windows
66 // so don't touch this
67 #if defined(WNT)
68 #undef SQL_API
69 #define SQL_API __stdcall
70 // At least under some circumstances, the below #include <odbc/sqlext.h> re-
71 // defines SQL_API to an empty string, leading to a compiler warning on MSC; to
72 // not break the current behavior, this is worked around by locally disabling
73 // that warning:
74 #if defined _MSC_VER
75 #pragma warning(push)
76 #pragma warning(disable: 4005)
77 #endif
78 #endif // defined(WNT)
80 #ifdef SYSTEM_ODBC_HEADERS
81 #include <sqlext.h>
82 #else
83 #include <odbc/sqlext.h>
84 #endif
86 #if defined(WNT)
87 #if defined _MSC_VER
88 #pragma warning(pop)
89 #endif
90 #undef SQL_API
91 #define SQL_API __stdcall
92 #endif // defined(WNT)
93 // from here on you can do what you want to
95 #else
97 #define ODBC_LIBRARY ""
98 #define ODBC_UI_LIBRARY ""
100 #endif // HAVE_ODBC_SUPPORT
102 //.........................................................................
103 namespace dbaui
105 //.........................................................................
108 #ifdef HAVE_ODBC_SUPPORT
109 typedef SQLRETURN (SQL_API* TSQLManageDataSource) (SQLHWND hwndParent);
110 typedef SQLRETURN (SQL_API* TSQLAllocHandle) (SQLSMALLINT HandleType, SQLHANDLE InputHandle, SQLHANDLE* OutputHandlePtr);
111 typedef SQLRETURN (SQL_API* TSQLFreeHandle) (SQLSMALLINT HandleType, SQLHANDLE Handle);
112 typedef SQLRETURN (SQL_API* TSQLSetEnvAttr) (SQLHENV EnvironmentHandle, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength);
113 typedef SQLRETURN (SQL_API* TSQLDataSources) (SQLHENV EnvironmentHandle, SQLUSMALLINT Direction, SQLCHAR* ServerName,
114 SQLSMALLINT BufferLength1, SQLSMALLINT* NameLength1Ptr, SQLCHAR* Description, SQLSMALLINT BufferLength2, SQLSMALLINT* NameLength2Ptr);
116 #define NSQLManageDataSource(a) (*(TSQLManageDataSource)(m_pSQLManageDataSource))(a)
117 #define NSQLAllocHandle(a,b,c) (*(TSQLAllocHandle)(m_pAllocHandle))(a,b,c)
118 #define NSQLFreeHandle(a,b) (*(TSQLFreeHandle)(m_pFreeHandle))(a,b)
119 #define NSQLSetEnvAttr(a,b,c,d) (*(TSQLSetEnvAttr)(m_pSetEnvAttr))(a,b,c,d)
120 #define NSQLDataSources(a,b,c,d,e,f,g,h) (*(TSQLDataSources)(m_pDataSources))(a,b,c,d,e,f,g,h)
121 #endif
123 //=========================================================================
124 //= OOdbcLibWrapper
125 //=========================================================================
126 DBG_NAME(OOdbcLibWrapper)
127 //-------------------------------------------------------------------------
128 #ifdef HAVE_ODBC_SUPPORT
129 OOdbcLibWrapper::OOdbcLibWrapper()
130 :m_pOdbcLib(NULL)
132 DBG_CTOR(OOdbcLibWrapper,NULL);
135 #endif
137 //-------------------------------------------------------------------------
138 sal_Bool OOdbcLibWrapper::load(const sal_Char* _pLibPath)
140 m_sLibPath = ::rtl::OUString::createFromAscii(_pLibPath);
141 #ifdef HAVE_ODBC_SUPPORT
142 // load the module
143 m_pOdbcLib = osl_loadModule(m_sLibPath.pData, SAL_LOADMODULE_NOW);
144 return (NULL != m_pOdbcLib);
145 #endif
148 //-------------------------------------------------------------------------
149 void OOdbcLibWrapper::unload()
151 #ifdef HAVE_ODBC_SUPPORT
152 if (isLoaded())
154 osl_unloadModule(m_pOdbcLib);
155 m_pOdbcLib = NULL;
157 #endif
160 //-------------------------------------------------------------------------
161 oslGenericFunction OOdbcLibWrapper::loadSymbol(const sal_Char* _pFunctionName)
163 return osl_getFunctionSymbol(m_pOdbcLib, ::rtl::OUString::createFromAscii(_pFunctionName).pData);
166 //-------------------------------------------------------------------------
167 OOdbcLibWrapper::~OOdbcLibWrapper()
169 unload();
171 DBG_DTOR(OOdbcLibWrapper,NULL);
174 //=========================================================================
175 //= OOdbcEnumeration
176 //=========================================================================
177 struct OdbcTypesImpl
179 #ifdef HAVE_ODBC_SUPPORT
180 SQLHANDLE hEnvironment;
181 OdbcTypesImpl() : hEnvironment(0) { }
182 #else
183 void* pDummy;
184 #endif
186 DBG_NAME(OOdbcEnumeration)
187 //-------------------------------------------------------------------------
188 OOdbcEnumeration::OOdbcEnumeration()
189 #ifdef HAVE_ODBC_SUPPORT
190 :m_pAllocHandle(NULL)
191 ,m_pSetEnvAttr(NULL)
192 ,m_pDataSources(NULL)
193 ,m_pImpl(new OdbcTypesImpl)
194 #endif
196 DBG_CTOR(OOdbcEnumeration,NULL);
198 sal_Bool bLoaded = load(ODBC_LIBRARY);
199 #ifdef ODBC_LIBRARY_1
200 if ( !bLoaded )
201 bLoaded = load(ODBC_LIBRARY_1);
202 #endif
204 if ( bLoaded )
206 #ifdef HAVE_ODBC_SUPPORT
207 // load the generic functions
208 m_pAllocHandle = loadSymbol("SQLAllocHandle");
209 m_pFreeHandle = loadSymbol("SQLFreeHandle");
210 m_pSetEnvAttr = loadSymbol("SQLSetEnvAttr");
211 m_pDataSources = loadSymbol("SQLDataSources");
213 // all or nothing
214 if (!m_pAllocHandle || !m_pSetEnvAttr || !m_pDataSources || !m_pFreeHandle)
216 unload();
217 m_pAllocHandle = m_pFreeHandle = m_pSetEnvAttr = m_pDataSources = NULL;
219 #endif
223 //-------------------------------------------------------------------------
224 OOdbcEnumeration::~OOdbcEnumeration()
226 freeEnv();
227 delete m_pImpl;
229 DBG_DTOR(OOdbcEnumeration,NULL);
232 //-------------------------------------------------------------------------
233 sal_Bool OOdbcEnumeration::allocEnv()
235 OSL_ENSURE(isLoaded(), "OOdbcEnumeration::allocEnv: not loaded!");
236 if (!isLoaded())
237 return sal_False;
239 #ifdef HAVE_ODBC_SUPPORT
240 if (m_pImpl->hEnvironment)
241 // nothing to do
242 return sal_True;
243 SQLRETURN nResult = NSQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_pImpl->hEnvironment);
244 if (SQL_SUCCESS != nResult)
245 // can't do anything without environment
246 return sal_False;
248 NSQLSetEnvAttr(m_pImpl->hEnvironment, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
249 return sal_True;
250 #else
251 return sal_False;
252 #endif
255 //-------------------------------------------------------------------------
256 void OOdbcEnumeration::freeEnv()
258 #ifdef HAVE_ODBC_SUPPORT
259 if (m_pImpl->hEnvironment)
260 NSQLFreeHandle(SQL_HANDLE_ENV, m_pImpl->hEnvironment);
261 m_pImpl->hEnvironment = 0;
262 #endif
265 //-------------------------------------------------------------------------
266 void OOdbcEnumeration::getDatasourceNames(StringBag& _rNames)
268 OSL_ENSURE(isLoaded(), "OOdbcEnumeration::getDatasourceNames: not loaded!");
269 if (!isLoaded())
270 return;
272 if (!allocEnv())
274 OSL_FAIL("OOdbcEnumeration::getDatasourceNames: could not allocate an ODBC environment!");
275 return;
278 #ifdef HAVE_ODBC_SUPPORT
279 // now that we have an environment collect the data source names
280 UCHAR szDSN[SQL_MAX_DSN_LENGTH+1];
281 SWORD pcbDSN;
282 UCHAR szDescription[1024+1];
283 SWORD pcbDescription;
284 SQLRETURN nResult = SQL_SUCCESS;
285 rtl_TextEncoding nTextEncoding = osl_getThreadTextEncoding();
287 for ( nResult = NSQLDataSources(m_pImpl->hEnvironment, SQL_FETCH_FIRST, szDSN, sizeof(szDSN), &pcbDSN, szDescription, sizeof(szDescription)-1, &pcbDescription);
289 nResult = NSQLDataSources(m_pImpl->hEnvironment, SQL_FETCH_NEXT, szDSN, sizeof(szDSN), &pcbDSN, szDescription, sizeof(szDescription)-1, &pcbDescription)
292 if (nResult != SQL_SUCCESS)
293 // no further error handling
294 break;
295 else
297 ::rtl::OUString aCurrentDsn(reinterpret_cast<const char*>(szDSN),pcbDSN, nTextEncoding);
298 _rNames.insert(aCurrentDsn);
301 #endif
304 #ifdef HAVE_ODBC_ADMINISTRATION
306 //=========================================================================
307 //= ProcessTerminationWait
308 //=========================================================================
309 class ProcessTerminationWait : public ::osl::Thread
311 oslProcess m_hProcessHandle;
312 Link m_aFinishHdl;
314 public:
315 ProcessTerminationWait( oslProcess _hProcessHandle, const Link& _rFinishHdl )
316 :m_hProcessHandle( _hProcessHandle )
317 ,m_aFinishHdl( _rFinishHdl )
321 protected:
322 virtual void SAL_CALL run()
324 osl_joinProcess( m_hProcessHandle );
325 osl_freeProcessHandle( m_hProcessHandle );
326 Application::PostUserEvent( m_aFinishHdl );
330 //=========================================================================
331 //= OOdbcManagement
332 //=========================================================================
333 //-------------------------------------------------------------------------
334 OOdbcManagement::OOdbcManagement( const Link& _rAsyncFinishCallback )
335 :m_pProcessWait( NULL )
336 ,m_aAsyncFinishCallback( _rAsyncFinishCallback )
340 //-------------------------------------------------------------------------
341 OOdbcManagement::~OOdbcManagement()
343 // wait for our thread to be finished
344 if ( m_pProcessWait.get() )
345 m_pProcessWait->join();
348 //-------------------------------------------------------------------------
349 bool OOdbcManagement::manageDataSources_async()
351 OSL_PRECOND( !isRunning(), "OOdbcManagement::manageDataSources_async: still running from the previous call!" );
352 if ( isRunning() )
353 return false;
355 // this is done in an external process, due to #i78733#
356 // (and note this whole functionality is supported on Windows only, ATM)
357 ::rtl::OUString sExecutableName( RTL_CONSTASCII_USTRINGPARAM( "$BRAND_BASE_DIR/program/odbcconfig.exe" ) );
358 ::rtl::Bootstrap::expandMacros( sExecutableName ); //TODO: detect failure
359 oslProcess hProcessHandle(0);
360 oslProcessError eError = osl_executeProcess( sExecutableName.pData, NULL, 0, 0, NULL, NULL, NULL, 0, &hProcessHandle );
361 if ( eError != osl_Process_E_None )
362 return false;
364 m_pProcessWait.reset( new ProcessTerminationWait( hProcessHandle, m_aAsyncFinishCallback ) );
365 m_pProcessWait->create();
366 return true;
369 //-------------------------------------------------------------------------
370 bool OOdbcManagement::isRunning() const
372 return ( m_pProcessWait.get() && m_pProcessWait->isRunning() );
375 #endif // HAVE_ODBC_ADMINISTRATION
377 //.........................................................................
378 } // namespace dbaui
379 //.........................................................................
381 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */