xref: /AOO41X/main/connectivity/source/drivers/adabas/BConnection.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
31*cdf0e10cSrcweir #include "adabas/BConnection.hxx"
32*cdf0e10cSrcweir #include "adabas/BDriver.hxx"
33*cdf0e10cSrcweir #include "adabas/BCatalog.hxx"
34*cdf0e10cSrcweir #include "odbc/OFunctions.hxx"
35*cdf0e10cSrcweir #include "odbc/OTools.hxx"
36*cdf0e10cSrcweir #ifndef _CONNECTIVITY_ODBC_ODATABASEMETADATA_HXX_
37*cdf0e10cSrcweir #include "adabas/BDatabaseMetaData.hxx"
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir #include "adabas/BStatement.hxx"
40*cdf0e10cSrcweir #include "adabas/BPreparedStatement.hxx"
41*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
42*cdf0e10cSrcweir #include <connectivity/dbcharset.hxx>
43*cdf0e10cSrcweir #include "connectivity/sqliterator.hxx"
44*cdf0e10cSrcweir #include <connectivity/sqlparse.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <string.h>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir using namespace connectivity::adabas;
49*cdf0e10cSrcweir using namespace connectivity;
50*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
51*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
52*cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
53*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
54*cdf0e10cSrcweir using namespace ::com::sun::star::container;
55*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir //------------------------------------------------------------------------------
59*cdf0e10cSrcweir namespace starlang	= ::com::sun::star::lang;
60*cdf0e10cSrcweir // --------------------------------------------------------------------------------
61*cdf0e10cSrcweir OAdabasConnection::OAdabasConnection(const SQLHANDLE _pDriverHandle, connectivity::odbc::ODBCDriver*        _pDriver)
62*cdf0e10cSrcweir                                                  : OConnection_BASE2(_pDriverHandle,_pDriver)
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir 	m_bUseOldDateFormat = sal_True;
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir //-----------------------------------------------------------------------------
67*cdf0e10cSrcweir SQLRETURN OAdabasConnection::Construct( const ::rtl::OUString& url,const Sequence< PropertyValue >& info) throw(SQLException)
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	m_aConnectionHandle  = SQL_NULL_HANDLE;
72*cdf0e10cSrcweir 	setURL(url);
73*cdf0e10cSrcweir 	setConnectionInfo(info);
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	// Connection allozieren
76*cdf0e10cSrcweir 	N3SQLAllocHandle(SQL_HANDLE_DBC,m_pDriverHandleCopy,&m_aConnectionHandle);
77*cdf0e10cSrcweir 	if(m_aConnectionHandle == SQL_NULL_HANDLE)
78*cdf0e10cSrcweir 		throw SQLException();
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	const PropertyValue *pBegin	= info.getConstArray();
81*cdf0e10cSrcweir 	const PropertyValue *pEnd	= pBegin + info.getLength();
82*cdf0e10cSrcweir 	::rtl::OUString sHostName;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 	sal_Int32 nLen = url.indexOf(':');
85*cdf0e10cSrcweir 	nLen = url.indexOf(':',nLen+1);
86*cdf0e10cSrcweir 	::rtl::OUString aDSN(url.copy(nLen+1)),aUID,aPWD;
87*cdf0e10cSrcweir 	sal_Int32 nTimeout = 20;
88*cdf0e10cSrcweir 	for(;pBegin != pEnd;++pBegin)
89*cdf0e10cSrcweir 	{
90*cdf0e10cSrcweir 		if ( !pBegin->Name.compareToAscii("Timeout") )
91*cdf0e10cSrcweir 			pBegin->Value >>= nTimeout;
92*cdf0e10cSrcweir 		else if(!pBegin->Name.compareToAscii("user"))
93*cdf0e10cSrcweir 			pBegin->Value >>= aUID;
94*cdf0e10cSrcweir 		else if(!pBegin->Name.compareToAscii("password"))
95*cdf0e10cSrcweir 			pBegin->Value >>= aPWD;
96*cdf0e10cSrcweir 		else if(!pBegin->Name.compareToAscii("HostName"))
97*cdf0e10cSrcweir 			pBegin->Value >>= sHostName;
98*cdf0e10cSrcweir 		else if(0 == pBegin->Name.compareToAscii("CharSet"))
99*cdf0e10cSrcweir 		{
100*cdf0e10cSrcweir 			::rtl::OUString sIanaName;
101*cdf0e10cSrcweir 			OSL_VERIFY( pBegin->Value >>= sIanaName );
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 			::dbtools::OCharsetMap aLookupIanaName;
104*cdf0e10cSrcweir 			::dbtools::OCharsetMap::const_iterator aLookup = aLookupIanaName.find(sIanaName, ::dbtools::OCharsetMap::IANA());
105*cdf0e10cSrcweir 			if (aLookup != aLookupIanaName.end())
106*cdf0e10cSrcweir 				m_nTextEncoding = (*aLookup).getEncoding();
107*cdf0e10cSrcweir 			else
108*cdf0e10cSrcweir 				m_nTextEncoding = RTL_TEXTENCODING_DONTKNOW;
109*cdf0e10cSrcweir 			if(m_nTextEncoding == RTL_TEXTENCODING_DONTKNOW)
110*cdf0e10cSrcweir 				m_nTextEncoding = osl_getThreadTextEncoding();
111*cdf0e10cSrcweir 		}
112*cdf0e10cSrcweir 	}
113*cdf0e10cSrcweir 	m_sUser = aUID;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 	if ( sHostName.getLength() )
116*cdf0e10cSrcweir 		aDSN = sHostName + ':' + aDSN;
117*cdf0e10cSrcweir 	SQLRETURN nSQLRETURN = openConnectionWithAuth(aDSN,nTimeout, aUID,aPWD);
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	return nSQLRETURN;
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir //-----------------------------------------------------------------------------
122*cdf0e10cSrcweir SQLRETURN OAdabasConnection::openConnectionWithAuth(const ::rtl::OUString& aConnectStr,sal_Int32 nTimeOut, const ::rtl::OUString& _uid,const ::rtl::OUString& _pwd)
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir 	if (m_aConnectionHandle == SQL_NULL_HANDLE)
125*cdf0e10cSrcweir 		return -1;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 	SQLRETURN nSQLRETURN = 0;
128*cdf0e10cSrcweir 	SDB_ODBC_CHAR szDSN[4096];
129*cdf0e10cSrcweir 	SDB_ODBC_CHAR szUID[20];
130*cdf0e10cSrcweir 	SDB_ODBC_CHAR szPWD[20];
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	memset(szDSN,'\0',4096);
133*cdf0e10cSrcweir 	memset(szUID,'\0',20);
134*cdf0e10cSrcweir 	memset(szPWD,'\0',20);
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	::rtl::OString aConStr(::rtl::OUStringToOString(aConnectStr,getTextEncoding()));
137*cdf0e10cSrcweir 	::rtl::OString aUID(::rtl::OUStringToOString(_uid,getTextEncoding()));
138*cdf0e10cSrcweir 	::rtl::OString aPWD(::rtl::OUStringToOString(_pwd,getTextEncoding()));
139*cdf0e10cSrcweir 	memcpy(szDSN, (SDB_ODBC_CHAR*) aConStr.getStr(), ::std::min<sal_Int32>((sal_Int32)2048,aConStr.getLength()));
140*cdf0e10cSrcweir 	memcpy(szUID, (SDB_ODBC_CHAR*) aUID.getStr(), ::std::min<sal_Int32>((sal_Int32)20,aUID.getLength()));
141*cdf0e10cSrcweir 	memcpy(szPWD, (SDB_ODBC_CHAR*) aPWD.getStr(), ::std::min<sal_Int32>((sal_Int32)20,aPWD.getLength()));
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	N3SQLSetConnectAttr(m_aConnectionHandle,SQL_ATTR_LOGIN_TIMEOUT,(SQLPOINTER)nTimeOut,SQL_IS_INTEGER);
146*cdf0e10cSrcweir 	// Verbindung aufbauen
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 	nSQLRETURN = N3SQLConnect(m_aConnectionHandle,
149*cdf0e10cSrcweir 					  szDSN,
150*cdf0e10cSrcweir 					  (SQLSMALLINT) ::std::min<sal_Int32>((sal_Int32)2048,aConStr.getLength()),
151*cdf0e10cSrcweir 					  szUID,
152*cdf0e10cSrcweir 					  (SQLSMALLINT) ::std::min<sal_Int32>((sal_Int32)20,aUID.getLength()),
153*cdf0e10cSrcweir 					  szPWD,
154*cdf0e10cSrcweir 					  (SQLSMALLINT) ::std::min<sal_Int32>((sal_Int32)20,aPWD.getLength()));
155*cdf0e10cSrcweir 	if (nSQLRETURN == SQL_ERROR || nSQLRETURN == SQL_NO_DATA)
156*cdf0e10cSrcweir 		return nSQLRETURN;
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 	m_bClosed = sal_False;
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	// autocoomit ist immer default
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 	N3SQLSetConnectAttr(m_aConnectionHandle,SQL_ATTR_AUTOCOMMIT,(SQLPOINTER)SQL_AUTOCOMMIT_ON,SQL_IS_INTEGER);
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 	return nSQLRETURN;
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir //------------------------------------------------------------------------------
168*cdf0e10cSrcweir void OAdabasConnection::disposing()
169*cdf0e10cSrcweir {
170*cdf0e10cSrcweir 	::osl::MutexGuard aGuard(m_aMutex);
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 	Reference< XTablesSupplier > xTableSupplier(m_xCatalog);
173*cdf0e10cSrcweir     ::comphelper::disposeComponent(xTableSupplier);
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 	m_xCatalog = WeakReference< XTablesSupplier >();
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir     OConnection_BASE2::disposing();
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir //------------------------------------------------------------------------------
180*cdf0e10cSrcweir Reference< XTablesSupplier > OAdabasConnection::createCatalog()
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
183*cdf0e10cSrcweir 	Reference< XTablesSupplier > xTab = m_xCatalog;
184*cdf0e10cSrcweir 	if(!xTab.is())
185*cdf0e10cSrcweir 	{
186*cdf0e10cSrcweir 		xTab = new OAdabasCatalog(m_aConnectionHandle,this);
187*cdf0e10cSrcweir 		m_xCatalog = xTab;
188*cdf0e10cSrcweir 	}
189*cdf0e10cSrcweir 	return xTab;
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir // --------------------------------------------------------------------------------
192*cdf0e10cSrcweir Reference< XDatabaseMetaData > SAL_CALL OAdabasConnection::getMetaData(  ) throw(SQLException, RuntimeException)
193*cdf0e10cSrcweir {
194*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
195*cdf0e10cSrcweir 	checkDisposed(OConnection_BASE2::rBHelper.bDisposed);
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 	Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
199*cdf0e10cSrcweir 	if(!xMetaData.is())
200*cdf0e10cSrcweir 	{
201*cdf0e10cSrcweir 		xMetaData = new OAdabasDatabaseMetaData(m_aConnectionHandle,this);
202*cdf0e10cSrcweir 		m_xMetaData = xMetaData;
203*cdf0e10cSrcweir 	}
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir 	return xMetaData;
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir // --------------------------------------------------------------------------------
208*cdf0e10cSrcweir Reference< XStatement > SAL_CALL OAdabasConnection::createStatement(  ) throw(SQLException, RuntimeException)
209*cdf0e10cSrcweir {
210*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
211*cdf0e10cSrcweir 	checkDisposed(OConnection_BASE2::rBHelper.bDisposed);
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 	Reference< XStatement > xReturn = new OAdabasStatement(this);
214*cdf0e10cSrcweir 	m_aStatements.push_back(WeakReferenceHelper(xReturn));
215*cdf0e10cSrcweir 	return xReturn;
216*cdf0e10cSrcweir }
217*cdf0e10cSrcweir // --------------------------------------------------------------------------------
218*cdf0e10cSrcweir Reference< XPreparedStatement > SAL_CALL OAdabasConnection::prepareStatement( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
219*cdf0e10cSrcweir {
220*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
221*cdf0e10cSrcweir 	checkDisposed(OConnection_BASE2::rBHelper.bDisposed);
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 	Reference< XPreparedStatement > xReturn = new OAdabasPreparedStatement(this,sql);
224*cdf0e10cSrcweir 	m_aStatements.push_back(WeakReferenceHelper(xReturn));
225*cdf0e10cSrcweir 	return xReturn;
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir // -----------------------------------------------------------------------------
228*cdf0e10cSrcweir sal_Int64 SAL_CALL OAdabasConnection::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rId ) throw (::com::sun::star::uno::RuntimeException)
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir 	return (rId.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
231*cdf0e10cSrcweir 				? reinterpret_cast< sal_Int64 >( this )
232*cdf0e10cSrcweir 				: OConnection_BASE2::getSomething(rId);
233*cdf0e10cSrcweir }
234*cdf0e10cSrcweir // -----------------------------------------------------------------------------
235*cdf0e10cSrcweir Sequence< sal_Int8 > OAdabasConnection::getUnoTunnelImplementationId()
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir 	static ::cppu::OImplementationId * pId = 0;
238*cdf0e10cSrcweir 	if (! pId)
239*cdf0e10cSrcweir 	{
240*cdf0e10cSrcweir 		::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
241*cdf0e10cSrcweir 		if (! pId)
242*cdf0e10cSrcweir 		{
243*cdf0e10cSrcweir 			static ::cppu::OImplementationId aId;
244*cdf0e10cSrcweir 			pId = &aId;
245*cdf0e10cSrcweir 		}
246*cdf0e10cSrcweir 	}
247*cdf0e10cSrcweir 	return pId->getImplementationId();
248*cdf0e10cSrcweir }
249*cdf0e10cSrcweir // -----------------------------------------------------------------------------
250*cdf0e10cSrcweir ::connectivity::odbc::OConnection* OAdabasConnection::cloneConnection()
251*cdf0e10cSrcweir {
252*cdf0e10cSrcweir 	return new OAdabasConnection(m_pDriverHandleCopy,m_pDriver);
253*cdf0e10cSrcweir }
254*cdf0e10cSrcweir // -----------------------------------------------------------------------------
255*cdf0e10cSrcweir ::vos::ORef<OSQLColumns> OAdabasConnection::createSelectColumns(const ::rtl::OUString& _rSql)
256*cdf0e10cSrcweir {
257*cdf0e10cSrcweir 	::vos::ORef<OSQLColumns> aRet;
258*cdf0e10cSrcweir 	OSQLParser aParser(getDriver()->getORB());
259*cdf0e10cSrcweir 	::rtl::OUString sErrorMessage;
260*cdf0e10cSrcweir 	OSQLParseNode* pNode = aParser.parseTree(sErrorMessage,_rSql);
261*cdf0e10cSrcweir 	if(pNode)
262*cdf0e10cSrcweir 	{
263*cdf0e10cSrcweir 		Reference< XTablesSupplier> xCata = createCatalog();
264*cdf0e10cSrcweir 		OSQLParseTreeIterator aParseIter(this, xCata->getTables(),
265*cdf0e10cSrcweir 										aParser, pNode);
266*cdf0e10cSrcweir 		aParseIter.traverseAll();
267*cdf0e10cSrcweir 		aRet = aParseIter.getSelectColumns();
268*cdf0e10cSrcweir 	}
269*cdf0e10cSrcweir 	return aRet;
270*cdf0e10cSrcweir }
271*cdf0e10cSrcweir // -----------------------------------------------------------------------------
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir 
275