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
35 #include <odbc/sqltypes.h>
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
50 #define ODBC_LIBRARY "ODBC32.DLL"
51 #define ODBC_UI_LIBRARY "ODBCCP32.DLL"
55 #define ODBC_LIBRARY "libiodbc.dylib"
56 #define ODBC_UI_LIBRARY "libiodbcinst.dylib"
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"
65 // just to go with calling convention of windows
66 // so don't touch this
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
76 #pragma warning(disable: 4005)
78 #endif // defined(WNT)
80 #ifdef SYSTEM_ODBC_HEADERS
83 #include <odbc/sqlext.h>
91 #define SQL_API __stdcall
92 #endif // defined(WNT)
93 // from here on you can do what you want to
97 #define ODBC_LIBRARY ""
98 #define ODBC_UI_LIBRARY ""
100 #endif // HAVE_ODBC_SUPPORT
102 //.........................................................................
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)
123 //=========================================================================
125 //=========================================================================
126 DBG_NAME(OOdbcLibWrapper
)
127 //-------------------------------------------------------------------------
128 #ifdef HAVE_ODBC_SUPPORT
129 OOdbcLibWrapper::OOdbcLibWrapper()
132 DBG_CTOR(OOdbcLibWrapper
,NULL
);
137 //-------------------------------------------------------------------------
138 sal_Bool
OOdbcLibWrapper::load(const sal_Char
* _pLibPath
)
140 m_sLibPath
= ::rtl::OUString::createFromAscii(_pLibPath
);
141 #ifdef HAVE_ODBC_SUPPORT
143 m_pOdbcLib
= osl_loadModule(m_sLibPath
.pData
, SAL_LOADMODULE_NOW
);
144 return (NULL
!= m_pOdbcLib
);
148 //-------------------------------------------------------------------------
149 void OOdbcLibWrapper::unload()
151 #ifdef HAVE_ODBC_SUPPORT
154 osl_unloadModule(m_pOdbcLib
);
160 //-------------------------------------------------------------------------
161 oslGenericFunction
OOdbcLibWrapper::loadSymbol(const sal_Char
* _pFunctionName
)
163 return osl_getFunctionSymbol(m_pOdbcLib
, ::rtl::OUString::createFromAscii(_pFunctionName
).pData
);
166 //-------------------------------------------------------------------------
167 OOdbcLibWrapper::~OOdbcLibWrapper()
171 DBG_DTOR(OOdbcLibWrapper
,NULL
);
174 //=========================================================================
176 //=========================================================================
179 #ifdef HAVE_ODBC_SUPPORT
180 SQLHANDLE hEnvironment
;
181 OdbcTypesImpl() : hEnvironment(0) { }
186 DBG_NAME(OOdbcEnumeration
)
187 //-------------------------------------------------------------------------
188 OOdbcEnumeration::OOdbcEnumeration()
189 #ifdef HAVE_ODBC_SUPPORT
190 :m_pAllocHandle(NULL
)
192 ,m_pDataSources(NULL
)
193 ,m_pImpl(new OdbcTypesImpl
)
196 DBG_CTOR(OOdbcEnumeration
,NULL
);
198 sal_Bool bLoaded
= load(ODBC_LIBRARY
);
199 #ifdef ODBC_LIBRARY_1
201 bLoaded
= load(ODBC_LIBRARY_1
);
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");
214 if (!m_pAllocHandle
|| !m_pSetEnvAttr
|| !m_pDataSources
|| !m_pFreeHandle
)
217 m_pAllocHandle
= m_pFreeHandle
= m_pSetEnvAttr
= m_pDataSources
= NULL
;
223 //-------------------------------------------------------------------------
224 OOdbcEnumeration::~OOdbcEnumeration()
229 DBG_DTOR(OOdbcEnumeration
,NULL
);
232 //-------------------------------------------------------------------------
233 sal_Bool
OOdbcEnumeration::allocEnv()
235 OSL_ENSURE(isLoaded(), "OOdbcEnumeration::allocEnv: not loaded!");
239 #ifdef HAVE_ODBC_SUPPORT
240 if (m_pImpl
->hEnvironment
)
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
248 NSQLSetEnvAttr(m_pImpl
->hEnvironment
, SQL_ATTR_ODBC_VERSION
, (SQLPOINTER
)SQL_OV_ODBC3
, SQL_IS_INTEGER
);
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;
265 //-------------------------------------------------------------------------
266 void OOdbcEnumeration::getDatasourceNames(StringBag
& _rNames
)
268 OSL_ENSURE(isLoaded(), "OOdbcEnumeration::getDatasourceNames: not loaded!");
274 OSL_FAIL("OOdbcEnumeration::getDatasourceNames: could not allocate an ODBC environment!");
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];
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
297 ::rtl::OUString
aCurrentDsn(reinterpret_cast<const char*>(szDSN
),pcbDSN
, nTextEncoding
);
298 _rNames
.insert(aCurrentDsn
);
304 #ifdef HAVE_ODBC_ADMINISTRATION
306 //=========================================================================
307 //= ProcessTerminationWait
308 //=========================================================================
309 class ProcessTerminationWait
: public ::osl::Thread
311 oslProcess m_hProcessHandle
;
315 ProcessTerminationWait( oslProcess _hProcessHandle
, const Link
& _rFinishHdl
)
316 :m_hProcessHandle( _hProcessHandle
)
317 ,m_aFinishHdl( _rFinishHdl
)
322 virtual void SAL_CALL
run()
324 osl_joinProcess( m_hProcessHandle
);
325 osl_freeProcessHandle( m_hProcessHandle
);
326 Application::PostUserEvent( m_aFinishHdl
);
330 //=========================================================================
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!" );
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
)
364 m_pProcessWait
.reset( new ProcessTerminationWait( hProcessHandle
, m_aAsyncFinishCallback
) );
365 m_pProcessWait
->create();
369 //-------------------------------------------------------------------------
370 bool OOdbcManagement::isRunning() const
372 return ( m_pProcessWait
.get() && m_pProcessWait
->isRunning() );
375 #endif // HAVE_ODBC_ADMINISTRATION
377 //.........................................................................
379 //.........................................................................
381 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */