update dev300-m58
[ooovba.git] / connectivity / source / drivers / adabas / BConnection.cxx
blob151b886fd73c5ecc7cff902732c1644ad7f6b5fa
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: BConnection.cxx,v $
10 * $Revision: 1.27 $
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_connectivity.hxx"
33 #include <cppuhelper/typeprovider.hxx>
34 #include "adabas/BConnection.hxx"
35 #include "adabas/BDriver.hxx"
36 #include "adabas/BCatalog.hxx"
37 #include "odbc/OFunctions.hxx"
38 #include "odbc/OTools.hxx"
39 #ifndef _CONNECTIVITY_ODBC_ODATABASEMETADATA_HXX_
40 #include "adabas/BDatabaseMetaData.hxx"
41 #endif
42 #include "adabas/BStatement.hxx"
43 #include "adabas/BPreparedStatement.hxx"
44 #include <com/sun/star/lang/DisposedException.hpp>
45 #include <connectivity/dbcharset.hxx>
46 #include "connectivity/sqliterator.hxx"
47 #include <connectivity/sqlparse.hxx>
49 #include <string.h>
51 using namespace connectivity::adabas;
52 using namespace connectivity;
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::beans;
55 using namespace ::com::sun::star::sdbcx;
56 using namespace ::com::sun::star::sdbc;
57 using namespace ::com::sun::star::container;
58 using namespace ::com::sun::star::lang;
61 //------------------------------------------------------------------------------
62 namespace starlang = ::com::sun::star::lang;
63 // --------------------------------------------------------------------------------
64 OAdabasConnection::OAdabasConnection(const SQLHANDLE _pDriverHandle, connectivity::odbc::ODBCDriver* _pDriver)
65 : OConnection_BASE2(_pDriverHandle,_pDriver)
67 m_bUseOldDateFormat = sal_True;
69 //-----------------------------------------------------------------------------
70 SQLRETURN OAdabasConnection::Construct( const ::rtl::OUString& url,const Sequence< PropertyValue >& info) throw(SQLException)
72 ::osl::MutexGuard aGuard( m_aMutex );
74 m_aConnectionHandle = SQL_NULL_HANDLE;
75 setURL(url);
76 setConnectionInfo(info);
78 // Connection allozieren
79 N3SQLAllocHandle(SQL_HANDLE_DBC,m_pDriverHandleCopy,&m_aConnectionHandle);
80 if(m_aConnectionHandle == SQL_NULL_HANDLE)
81 throw SQLException();
83 const PropertyValue *pBegin = info.getConstArray();
84 const PropertyValue *pEnd = pBegin + info.getLength();
85 ::rtl::OUString sHostName;
87 sal_Int32 nLen = url.indexOf(':');
88 nLen = url.indexOf(':',nLen+1);
89 ::rtl::OUString aDSN(url.copy(nLen+1)),aUID,aPWD;
90 sal_Int32 nTimeout = 20;
91 for(;pBegin != pEnd;++pBegin)
93 if ( !pBegin->Name.compareToAscii("Timeout") )
94 pBegin->Value >>= nTimeout;
95 else if(!pBegin->Name.compareToAscii("user"))
96 pBegin->Value >>= aUID;
97 else if(!pBegin->Name.compareToAscii("password"))
98 pBegin->Value >>= aPWD;
99 else if(!pBegin->Name.compareToAscii("HostName"))
100 pBegin->Value >>= sHostName;
101 else if(0 == pBegin->Name.compareToAscii("CharSet"))
103 ::rtl::OUString sIanaName;
104 OSL_VERIFY( pBegin->Value >>= sIanaName );
106 ::dbtools::OCharsetMap aLookupIanaName;
107 ::dbtools::OCharsetMap::const_iterator aLookup = aLookupIanaName.find(sIanaName, ::dbtools::OCharsetMap::IANA());
108 if (aLookup != aLookupIanaName.end())
109 m_nTextEncoding = (*aLookup).getEncoding();
110 else
111 m_nTextEncoding = RTL_TEXTENCODING_DONTKNOW;
112 if(m_nTextEncoding == RTL_TEXTENCODING_DONTKNOW)
113 m_nTextEncoding = osl_getThreadTextEncoding();
116 m_sUser = aUID;
118 if ( sHostName.getLength() )
119 aDSN = sHostName + ':' + aDSN;
120 SQLRETURN nSQLRETURN = openConnectionWithAuth(aDSN,nTimeout, aUID,aPWD);
122 return nSQLRETURN;
124 //-----------------------------------------------------------------------------
125 SQLRETURN OAdabasConnection::openConnectionWithAuth(const ::rtl::OUString& aConnectStr,sal_Int32 nTimeOut, const ::rtl::OUString& _uid,const ::rtl::OUString& _pwd)
127 if (m_aConnectionHandle == SQL_NULL_HANDLE)
128 return -1;
130 SQLRETURN nSQLRETURN = 0;
131 SDB_ODBC_CHAR szDSN[4096];
132 SDB_ODBC_CHAR szUID[20];
133 SDB_ODBC_CHAR szPWD[20];
135 memset(szDSN,'\0',4096);
136 memset(szUID,'\0',20);
137 memset(szPWD,'\0',20);
139 ::rtl::OString aConStr(::rtl::OUStringToOString(aConnectStr,getTextEncoding()));
140 ::rtl::OString aUID(::rtl::OUStringToOString(_uid,getTextEncoding()));
141 ::rtl::OString aPWD(::rtl::OUStringToOString(_pwd,getTextEncoding()));
142 memcpy(szDSN, (SDB_ODBC_CHAR*) aConStr.getStr(), ::std::min<sal_Int32>((sal_Int32)2048,aConStr.getLength()));
143 memcpy(szUID, (SDB_ODBC_CHAR*) aUID.getStr(), ::std::min<sal_Int32>((sal_Int32)20,aUID.getLength()));
144 memcpy(szPWD, (SDB_ODBC_CHAR*) aPWD.getStr(), ::std::min<sal_Int32>((sal_Int32)20,aPWD.getLength()));
148 N3SQLSetConnectAttr(m_aConnectionHandle,SQL_ATTR_LOGIN_TIMEOUT,(SQLPOINTER)nTimeOut,SQL_IS_INTEGER);
149 // Verbindung aufbauen
151 nSQLRETURN = N3SQLConnect(m_aConnectionHandle,
152 szDSN,
153 (SQLSMALLINT) ::std::min<sal_Int32>((sal_Int32)2048,aConStr.getLength()),
154 szUID,
155 (SQLSMALLINT) ::std::min<sal_Int32>((sal_Int32)20,aUID.getLength()),
156 szPWD,
157 (SQLSMALLINT) ::std::min<sal_Int32>((sal_Int32)20,aPWD.getLength()));
158 if (nSQLRETURN == SQL_ERROR || nSQLRETURN == SQL_NO_DATA)
159 return nSQLRETURN;
161 m_bClosed = sal_False;
163 // autocoomit ist immer default
165 N3SQLSetConnectAttr(m_aConnectionHandle,SQL_ATTR_AUTOCOMMIT,(SQLPOINTER)SQL_AUTOCOMMIT_ON,SQL_IS_INTEGER);
167 return nSQLRETURN;
170 //------------------------------------------------------------------------------
171 void OAdabasConnection::disposing()
173 ::osl::MutexGuard aGuard(m_aMutex);
175 Reference< XTablesSupplier > xTableSupplier(m_xCatalog);
176 ::comphelper::disposeComponent(xTableSupplier);
178 m_xCatalog = WeakReference< XTablesSupplier >();
180 OConnection_BASE2::disposing();
182 //------------------------------------------------------------------------------
183 Reference< XTablesSupplier > OAdabasConnection::createCatalog()
185 ::osl::MutexGuard aGuard( m_aMutex );
186 Reference< XTablesSupplier > xTab = m_xCatalog;
187 if(!xTab.is())
189 xTab = new OAdabasCatalog(m_aConnectionHandle,this);
190 m_xCatalog = xTab;
192 return xTab;
194 // --------------------------------------------------------------------------------
195 Reference< XDatabaseMetaData > SAL_CALL OAdabasConnection::getMetaData( ) throw(SQLException, RuntimeException)
197 ::osl::MutexGuard aGuard( m_aMutex );
198 checkDisposed(OConnection_BASE2::rBHelper.bDisposed);
201 Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
202 if(!xMetaData.is())
204 xMetaData = new OAdabasDatabaseMetaData(m_aConnectionHandle,this);
205 m_xMetaData = xMetaData;
208 return xMetaData;
210 // --------------------------------------------------------------------------------
211 Reference< XStatement > SAL_CALL OAdabasConnection::createStatement( ) throw(SQLException, RuntimeException)
213 ::osl::MutexGuard aGuard( m_aMutex );
214 checkDisposed(OConnection_BASE2::rBHelper.bDisposed);
216 Reference< XStatement > xReturn = new OAdabasStatement(this);
217 m_aStatements.push_back(WeakReferenceHelper(xReturn));
218 return xReturn;
220 // --------------------------------------------------------------------------------
221 Reference< XPreparedStatement > SAL_CALL OAdabasConnection::prepareStatement( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
223 ::osl::MutexGuard aGuard( m_aMutex );
224 checkDisposed(OConnection_BASE2::rBHelper.bDisposed);
226 Reference< XPreparedStatement > xReturn = new OAdabasPreparedStatement(this,sql);
227 m_aStatements.push_back(WeakReferenceHelper(xReturn));
228 return xReturn;
230 // -----------------------------------------------------------------------------
231 sal_Int64 SAL_CALL OAdabasConnection::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rId ) throw (::com::sun::star::uno::RuntimeException)
233 return (rId.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
234 ? reinterpret_cast< sal_Int64 >( this )
235 : OConnection_BASE2::getSomething(rId);
237 // -----------------------------------------------------------------------------
238 Sequence< sal_Int8 > OAdabasConnection::getUnoTunnelImplementationId()
240 static ::cppu::OImplementationId * pId = 0;
241 if (! pId)
243 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
244 if (! pId)
246 static ::cppu::OImplementationId aId;
247 pId = &aId;
250 return pId->getImplementationId();
252 // -----------------------------------------------------------------------------
253 ::connectivity::odbc::OConnection* OAdabasConnection::cloneConnection()
255 return new OAdabasConnection(m_pDriverHandleCopy,m_pDriver);
257 // -----------------------------------------------------------------------------
258 ::vos::ORef<OSQLColumns> OAdabasConnection::createSelectColumns(const ::rtl::OUString& _rSql)
260 ::vos::ORef<OSQLColumns> aRet;
261 OSQLParser aParser(getDriver()->getORB());
262 ::rtl::OUString sErrorMessage;
263 OSQLParseNode* pNode = aParser.parseTree(sErrorMessage,_rSql);
264 if(pNode)
266 Reference< XTablesSupplier> xCata = createCatalog();
267 OSQLParseTreeIterator aParseIter(this, xCata->getTables(),
268 aParser, pNode);
269 aParseIter.traverseAll();
270 aRet = aParseIter.getSelectColumns();
272 return aRet;
274 // -----------------------------------------------------------------------------