19b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59b5730f6SAndrew Rist  * distributed with this work for additional information
69b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
99b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
109b5730f6SAndrew Rist  *
119b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129b5730f6SAndrew Rist  *
139b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149b5730f6SAndrew Rist  * software distributed under the License is distributed on an
159b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
179b5730f6SAndrew Rist  * specific language governing permissions and limitations
189b5730f6SAndrew Rist  * under the License.
199b5730f6SAndrew Rist  *
209b5730f6SAndrew Rist  *************************************************************/
219b5730f6SAndrew Rist 
229b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
27cdf0e10cSrcweir #include "adabas/BConnection.hxx"
28cdf0e10cSrcweir #include "adabas/BDriver.hxx"
29cdf0e10cSrcweir #include "adabas/BCatalog.hxx"
30cdf0e10cSrcweir #include "odbc/OFunctions.hxx"
31cdf0e10cSrcweir #include "odbc/OTools.hxx"
32cdf0e10cSrcweir #ifndef _CONNECTIVITY_ODBC_ODATABASEMETADATA_HXX_
33cdf0e10cSrcweir #include "adabas/BDatabaseMetaData.hxx"
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir #include "adabas/BStatement.hxx"
36cdf0e10cSrcweir #include "adabas/BPreparedStatement.hxx"
37cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
38cdf0e10cSrcweir #include <connectivity/dbcharset.hxx>
39cdf0e10cSrcweir #include "connectivity/sqliterator.hxx"
40cdf0e10cSrcweir #include <connectivity/sqlparse.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <string.h>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace connectivity::adabas;
45cdf0e10cSrcweir using namespace connectivity;
46cdf0e10cSrcweir using namespace ::com::sun::star::uno;
47cdf0e10cSrcweir using namespace ::com::sun::star::beans;
48cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
49cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
50cdf0e10cSrcweir using namespace ::com::sun::star::container;
51cdf0e10cSrcweir using namespace ::com::sun::star::lang;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
54cdf0e10cSrcweir //------------------------------------------------------------------------------
55cdf0e10cSrcweir namespace starlang	= ::com::sun::star::lang;
56cdf0e10cSrcweir // --------------------------------------------------------------------------------
OAdabasConnection(const SQLHANDLE _pDriverHandle,connectivity::odbc::ODBCDriver * _pDriver)57cdf0e10cSrcweir OAdabasConnection::OAdabasConnection(const SQLHANDLE _pDriverHandle, connectivity::odbc::ODBCDriver*        _pDriver)
58cdf0e10cSrcweir                                                  : OConnection_BASE2(_pDriverHandle,_pDriver)
59cdf0e10cSrcweir {
60cdf0e10cSrcweir 	m_bUseOldDateFormat = sal_True;
61cdf0e10cSrcweir }
62cdf0e10cSrcweir //-----------------------------------------------------------------------------
Construct(const::rtl::OUString & url,const Sequence<PropertyValue> & info)63cdf0e10cSrcweir SQLRETURN OAdabasConnection::Construct( const ::rtl::OUString& url,const Sequence< PropertyValue >& info) throw(SQLException)
64cdf0e10cSrcweir {
65cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	m_aConnectionHandle  = SQL_NULL_HANDLE;
68cdf0e10cSrcweir 	setURL(url);
69cdf0e10cSrcweir 	setConnectionInfo(info);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	// Connection allozieren
72cdf0e10cSrcweir 	N3SQLAllocHandle(SQL_HANDLE_DBC,m_pDriverHandleCopy,&m_aConnectionHandle);
73cdf0e10cSrcweir 	if(m_aConnectionHandle == SQL_NULL_HANDLE)
74cdf0e10cSrcweir 		throw SQLException();
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 	const PropertyValue *pBegin	= info.getConstArray();
77cdf0e10cSrcweir 	const PropertyValue *pEnd	= pBegin + info.getLength();
78cdf0e10cSrcweir 	::rtl::OUString sHostName;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	sal_Int32 nLen = url.indexOf(':');
81cdf0e10cSrcweir 	nLen = url.indexOf(':',nLen+1);
82cdf0e10cSrcweir 	::rtl::OUString aDSN(url.copy(nLen+1)),aUID,aPWD;
83cdf0e10cSrcweir 	sal_Int32 nTimeout = 20;
84cdf0e10cSrcweir 	for(;pBegin != pEnd;++pBegin)
85cdf0e10cSrcweir 	{
86cdf0e10cSrcweir 		if ( !pBegin->Name.compareToAscii("Timeout") )
87cdf0e10cSrcweir 			pBegin->Value >>= nTimeout;
88cdf0e10cSrcweir 		else if(!pBegin->Name.compareToAscii("user"))
89cdf0e10cSrcweir 			pBegin->Value >>= aUID;
90cdf0e10cSrcweir 		else if(!pBegin->Name.compareToAscii("password"))
91cdf0e10cSrcweir 			pBegin->Value >>= aPWD;
92cdf0e10cSrcweir 		else if(!pBegin->Name.compareToAscii("HostName"))
93cdf0e10cSrcweir 			pBegin->Value >>= sHostName;
94cdf0e10cSrcweir 		else if(0 == pBegin->Name.compareToAscii("CharSet"))
95cdf0e10cSrcweir 		{
96cdf0e10cSrcweir 			::rtl::OUString sIanaName;
97cdf0e10cSrcweir 			OSL_VERIFY( pBegin->Value >>= sIanaName );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 			::dbtools::OCharsetMap aLookupIanaName;
100cdf0e10cSrcweir 			::dbtools::OCharsetMap::const_iterator aLookup = aLookupIanaName.find(sIanaName, ::dbtools::OCharsetMap::IANA());
101cdf0e10cSrcweir 			if (aLookup != aLookupIanaName.end())
102cdf0e10cSrcweir 				m_nTextEncoding = (*aLookup).getEncoding();
103cdf0e10cSrcweir 			else
104cdf0e10cSrcweir 				m_nTextEncoding = RTL_TEXTENCODING_DONTKNOW;
105cdf0e10cSrcweir 			if(m_nTextEncoding == RTL_TEXTENCODING_DONTKNOW)
106cdf0e10cSrcweir 				m_nTextEncoding = osl_getThreadTextEncoding();
107cdf0e10cSrcweir 		}
108cdf0e10cSrcweir 	}
109cdf0e10cSrcweir 	m_sUser = aUID;
110cdf0e10cSrcweir 
111*24c56ab9SHerbert Dürr 	if( !sHostName.isEmpty() )
112*24c56ab9SHerbert Dürr 		aDSN = sHostName + ::rtl::OUString::createFromAscii(":") + aDSN;
113cdf0e10cSrcweir 	SQLRETURN nSQLRETURN = openConnectionWithAuth(aDSN,nTimeout, aUID,aPWD);
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 	return nSQLRETURN;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir //-----------------------------------------------------------------------------
openConnectionWithAuth(const::rtl::OUString & aConnectStr,sal_Int32 nTimeOut,const::rtl::OUString & _uid,const::rtl::OUString & _pwd)118cdf0e10cSrcweir SQLRETURN OAdabasConnection::openConnectionWithAuth(const ::rtl::OUString& aConnectStr,sal_Int32 nTimeOut, const ::rtl::OUString& _uid,const ::rtl::OUString& _pwd)
119cdf0e10cSrcweir {
120cdf0e10cSrcweir 	if (m_aConnectionHandle == SQL_NULL_HANDLE)
121cdf0e10cSrcweir 		return -1;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	SQLRETURN nSQLRETURN = 0;
124cdf0e10cSrcweir 	SDB_ODBC_CHAR szDSN[4096];
125cdf0e10cSrcweir 	SDB_ODBC_CHAR szUID[20];
126cdf0e10cSrcweir 	SDB_ODBC_CHAR szPWD[20];
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	memset(szDSN,'\0',4096);
129cdf0e10cSrcweir 	memset(szUID,'\0',20);
130cdf0e10cSrcweir 	memset(szPWD,'\0',20);
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 	::rtl::OString aConStr(::rtl::OUStringToOString(aConnectStr,getTextEncoding()));
133cdf0e10cSrcweir 	::rtl::OString aUID(::rtl::OUStringToOString(_uid,getTextEncoding()));
134cdf0e10cSrcweir 	::rtl::OString aPWD(::rtl::OUStringToOString(_pwd,getTextEncoding()));
135cdf0e10cSrcweir 	memcpy(szDSN, (SDB_ODBC_CHAR*) aConStr.getStr(), ::std::min<sal_Int32>((sal_Int32)2048,aConStr.getLength()));
136cdf0e10cSrcweir 	memcpy(szUID, (SDB_ODBC_CHAR*) aUID.getStr(), ::std::min<sal_Int32>((sal_Int32)20,aUID.getLength()));
137cdf0e10cSrcweir 	memcpy(szPWD, (SDB_ODBC_CHAR*) aPWD.getStr(), ::std::min<sal_Int32>((sal_Int32)20,aPWD.getLength()));
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	N3SQLSetConnectAttr(m_aConnectionHandle,SQL_ATTR_LOGIN_TIMEOUT,(SQLPOINTER)nTimeOut,SQL_IS_INTEGER);
142cdf0e10cSrcweir 	// Verbindung aufbauen
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	nSQLRETURN = N3SQLConnect(m_aConnectionHandle,
145cdf0e10cSrcweir 					  szDSN,
146cdf0e10cSrcweir 					  (SQLSMALLINT) ::std::min<sal_Int32>((sal_Int32)2048,aConStr.getLength()),
147cdf0e10cSrcweir 					  szUID,
148cdf0e10cSrcweir 					  (SQLSMALLINT) ::std::min<sal_Int32>((sal_Int32)20,aUID.getLength()),
149cdf0e10cSrcweir 					  szPWD,
150cdf0e10cSrcweir 					  (SQLSMALLINT) ::std::min<sal_Int32>((sal_Int32)20,aPWD.getLength()));
151cdf0e10cSrcweir 	if (nSQLRETURN == SQL_ERROR || nSQLRETURN == SQL_NO_DATA)
152cdf0e10cSrcweir 		return nSQLRETURN;
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	m_bClosed = sal_False;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	// autocoomit ist immer default
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 	N3SQLSetConnectAttr(m_aConnectionHandle,SQL_ATTR_AUTOCOMMIT,(SQLPOINTER)SQL_AUTOCOMMIT_ON,SQL_IS_INTEGER);
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	return nSQLRETURN;
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir //------------------------------------------------------------------------------
disposing()164cdf0e10cSrcweir void OAdabasConnection::disposing()
165cdf0e10cSrcweir {
166cdf0e10cSrcweir 	::osl::MutexGuard aGuard(m_aMutex);
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 	Reference< XTablesSupplier > xTableSupplier(m_xCatalog);
169cdf0e10cSrcweir     ::comphelper::disposeComponent(xTableSupplier);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	m_xCatalog = WeakReference< XTablesSupplier >();
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     OConnection_BASE2::disposing();
174cdf0e10cSrcweir }
175cdf0e10cSrcweir //------------------------------------------------------------------------------
createCatalog()176cdf0e10cSrcweir Reference< XTablesSupplier > OAdabasConnection::createCatalog()
177cdf0e10cSrcweir {
178cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
179cdf0e10cSrcweir 	Reference< XTablesSupplier > xTab = m_xCatalog;
180cdf0e10cSrcweir 	if(!xTab.is())
181cdf0e10cSrcweir 	{
182cdf0e10cSrcweir 		xTab = new OAdabasCatalog(m_aConnectionHandle,this);
183cdf0e10cSrcweir 		m_xCatalog = xTab;
184cdf0e10cSrcweir 	}
185cdf0e10cSrcweir 	return xTab;
186cdf0e10cSrcweir }
187cdf0e10cSrcweir // --------------------------------------------------------------------------------
getMetaData()188cdf0e10cSrcweir Reference< XDatabaseMetaData > SAL_CALL OAdabasConnection::getMetaData(  ) throw(SQLException, RuntimeException)
189cdf0e10cSrcweir {
190cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
191cdf0e10cSrcweir 	checkDisposed(OConnection_BASE2::rBHelper.bDisposed);
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 	Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
195cdf0e10cSrcweir 	if(!xMetaData.is())
196cdf0e10cSrcweir 	{
197cdf0e10cSrcweir 		xMetaData = new OAdabasDatabaseMetaData(m_aConnectionHandle,this);
198cdf0e10cSrcweir 		m_xMetaData = xMetaData;
199cdf0e10cSrcweir 	}
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 	return xMetaData;
202cdf0e10cSrcweir }
203cdf0e10cSrcweir // --------------------------------------------------------------------------------
createStatement()204cdf0e10cSrcweir Reference< XStatement > SAL_CALL OAdabasConnection::createStatement(  ) throw(SQLException, RuntimeException)
205cdf0e10cSrcweir {
206cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
207cdf0e10cSrcweir 	checkDisposed(OConnection_BASE2::rBHelper.bDisposed);
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 	Reference< XStatement > xReturn = new OAdabasStatement(this);
210cdf0e10cSrcweir 	m_aStatements.push_back(WeakReferenceHelper(xReturn));
211cdf0e10cSrcweir 	return xReturn;
212cdf0e10cSrcweir }
213cdf0e10cSrcweir // --------------------------------------------------------------------------------
prepareStatement(const::rtl::OUString & sql)214cdf0e10cSrcweir Reference< XPreparedStatement > SAL_CALL OAdabasConnection::prepareStatement( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
215cdf0e10cSrcweir {
216cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
217cdf0e10cSrcweir 	checkDisposed(OConnection_BASE2::rBHelper.bDisposed);
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 	Reference< XPreparedStatement > xReturn = new OAdabasPreparedStatement(this,sql);
220cdf0e10cSrcweir 	m_aStatements.push_back(WeakReferenceHelper(xReturn));
221cdf0e10cSrcweir 	return xReturn;
222cdf0e10cSrcweir }
223cdf0e10cSrcweir // -----------------------------------------------------------------------------
getSomething(const::com::sun::star::uno::Sequence<sal_Int8> & rId)224cdf0e10cSrcweir sal_Int64 SAL_CALL OAdabasConnection::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rId ) throw (::com::sun::star::uno::RuntimeException)
225cdf0e10cSrcweir {
226cdf0e10cSrcweir 	return (rId.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
227cdf0e10cSrcweir 				? reinterpret_cast< sal_Int64 >( this )
228cdf0e10cSrcweir 				: OConnection_BASE2::getSomething(rId);
229cdf0e10cSrcweir }
230cdf0e10cSrcweir // -----------------------------------------------------------------------------
getUnoTunnelImplementationId()231cdf0e10cSrcweir Sequence< sal_Int8 > OAdabasConnection::getUnoTunnelImplementationId()
232cdf0e10cSrcweir {
233cdf0e10cSrcweir 	static ::cppu::OImplementationId * pId = 0;
234cdf0e10cSrcweir 	if (! pId)
235cdf0e10cSrcweir 	{
236cdf0e10cSrcweir 		::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
237cdf0e10cSrcweir 		if (! pId)
238cdf0e10cSrcweir 		{
239cdf0e10cSrcweir 			static ::cppu::OImplementationId aId;
240cdf0e10cSrcweir 			pId = &aId;
241cdf0e10cSrcweir 		}
242cdf0e10cSrcweir 	}
243cdf0e10cSrcweir 	return pId->getImplementationId();
244cdf0e10cSrcweir }
245cdf0e10cSrcweir // -----------------------------------------------------------------------------
cloneConnection()246cdf0e10cSrcweir ::connectivity::odbc::OConnection* OAdabasConnection::cloneConnection()
247cdf0e10cSrcweir {
248cdf0e10cSrcweir 	return new OAdabasConnection(m_pDriverHandleCopy,m_pDriver);
249cdf0e10cSrcweir }
250cdf0e10cSrcweir // -----------------------------------------------------------------------------
createSelectColumns(const::rtl::OUString & _rSql)251cdf0e10cSrcweir ::vos::ORef<OSQLColumns> OAdabasConnection::createSelectColumns(const ::rtl::OUString& _rSql)
252cdf0e10cSrcweir {
253cdf0e10cSrcweir 	::vos::ORef<OSQLColumns> aRet;
254cdf0e10cSrcweir 	OSQLParser aParser(getDriver()->getORB());
255cdf0e10cSrcweir 	::rtl::OUString sErrorMessage;
256cdf0e10cSrcweir 	OSQLParseNode* pNode = aParser.parseTree(sErrorMessage,_rSql);
257cdf0e10cSrcweir 	if(pNode)
258cdf0e10cSrcweir 	{
259cdf0e10cSrcweir 		Reference< XTablesSupplier> xCata = createCatalog();
260cdf0e10cSrcweir 		OSQLParseTreeIterator aParseIter(this, xCata->getTables(),
261cdf0e10cSrcweir 										aParser, pNode);
262cdf0e10cSrcweir 		aParseIter.traverseAll();
263cdf0e10cSrcweir 		aRet = aParseIter.getSelectColumns();
264cdf0e10cSrcweir 	}
265cdf0e10cSrcweir 	return aRet;
266cdf0e10cSrcweir }
267cdf0e10cSrcweir // -----------------------------------------------------------------------------
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 
271