1  /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 #include "NConnection.hxx"
31 #include "NDatabaseMetaData.hxx"
32 #include "NCatalog.hxx"
33 #include <com/sun/star/lang/DisposedException.hpp>
34 #include <com/sun/star/sdbc/TransactionIsolation.hpp>
35 #include <tools/urlobj.hxx>
36 #include "NPreparedStatement.hxx"
37 #include "NStatement.hxx"
38 #include <comphelper/extract.hxx>
39 #include <connectivity/dbexception.hxx>
40 #include <comphelper/processfactory.hxx>
41 #include <vos/process.hxx>
42 #include <tools/debug.hxx>
43 #include "NDebug.hxx"
44 #include <comphelper/sequence.hxx>
45 
46 using namespace connectivity::evoab;
47 using namespace vos;
48 using namespace dbtools;
49 
50 //------------------------------------------------------------------------------
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::sdbcx;
54 using namespace ::com::sun::star::sdbc;
55 using namespace ::com::sun::star::lang;
56 
57 ::rtl::OUString implGetExceptionMsg( Exception& e, const ::rtl::OUString& aExceptionType_ )
58 {
59      ::rtl::OUString aExceptionType = aExceptionType_;
60      if( aExceptionType.getLength() == 0 )
61          aExceptionType =  ::rtl::OUString::createFromAscii("Unknown" ) ;
62 
63      ::rtl::OUString aTypeLine( RTL_CONSTASCII_USTRINGPARAM("\nType: " ) );
64      aTypeLine += aExceptionType;
65 
66      ::rtl::OUString aMessageLine( RTL_CONSTASCII_USTRINGPARAM("\nMessage: " ) );
67          aMessageLine += ::rtl::OUString( e.Message );
68 
69 	 ::rtl::OUString aMsg(aTypeLine);
70      aMsg += aMessageLine;
71          return aMsg;
72 }
73 
74  // Exception type unknown
75 ::rtl::OUString implGetExceptionMsg( Exception& e )
76 {
77          ::rtl::OUString aMsg = implGetExceptionMsg( e, ::rtl::OUString() );
78          return aMsg;
79 }
80 
81 // --------------------------------------------------------------------------------
82 OEvoabConnection::OEvoabConnection( OEvoabDriver& _rDriver )
83     :OSubComponent<OEvoabConnection, OConnection_BASE>( (::cppu::OWeakObject*)(&_rDriver), this )
84 	,m_rDriver(_rDriver)
85     ,m_xCatalog(NULL)
86 {
87 }
88 //-----------------------------------------------------------------------------
89 OEvoabConnection::~OEvoabConnection()
90 {
91 		::osl::MutexGuard aGuard( m_aMutex );
92 
93 	if(!isClosed()) {
94 		acquire();
95 		close();
96 	}
97 }
98 
99 //-----------------------------------------------------------------------------
100 void SAL_CALL OEvoabConnection::release() throw()
101 {
102 	relase_ChildImpl();
103 }
104 
105 // XServiceInfo
106 // --------------------------------------------------------------------------------
107 IMPLEMENT_SERVICE_INFO(OEvoabConnection, "com.sun.star.sdbc.drivers.evoab.Connection", "com.sun.star.sdbc.Connection")
108 
109 //-----------------------------------------------------------------------------
110 void OEvoabConnection::construct(const ::rtl::OUString& url, const Sequence< PropertyValue >& info)  throw(SQLException)
111 {
112 	osl_incrementInterlockedCount( &m_refCount );
113 	EVO_TRACE_STRING("OEvoabConnection::construct()::url = %s\n", url );
114 
115  	::rtl::OUString sPassword;
116         const char* pPwd                = "password";
117 
118         const PropertyValue *pIter      = info.getConstArray();
119         const PropertyValue *pEnd       = pIter + info.getLength();
120         for(;pIter != pEnd;++pIter)
121         {
122                 if(!pIter->Name.compareToAscii(pPwd))
123                 {
124                         pIter->Value >>= sPassword;
125                         break;
126                 }
127         }
128 
129 	if (url.equalsAscii("sdbc:address:evolution:groupwise"))
130 		setSDBCAddressType(SDBCAddress::EVO_GWISE);
131 	else if (url.equalsAscii("sdbc:address:evolution:ldap"))
132 		setSDBCAddressType(SDBCAddress::EVO_LDAP);
133 	else
134 		setSDBCAddressType(SDBCAddress::EVO_LOCAL);
135 	setURL(url);
136 	setPassword(::rtl::OUStringToOString(sPassword,RTL_TEXTENCODING_UTF8));
137 	osl_decrementInterlockedCount( &m_refCount );
138 }
139 
140 // --------------------------------------------------------------------------------
141 ::rtl::OUString SAL_CALL OEvoabConnection::nativeSQL( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException)
142 {
143 	// when you need to transform SQL92 to you driver specific you can do it here
144 	return _sSql;
145 }
146 // --------------------------------------------------------------------------------
147 Reference< XDatabaseMetaData > SAL_CALL OEvoabConnection::getMetaData(  ) throw(SQLException, RuntimeException)
148 {
149 	::osl::MutexGuard aGuard( m_aMutex );
150 	checkDisposed(OConnection_BASE::rBHelper.bDisposed);
151 
152 	Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
153 	if(!xMetaData.is())
154 	{
155 		xMetaData = new OEvoabDatabaseMetaData(this);
156 		m_xMetaData = xMetaData;
157 	}
158 
159 	return xMetaData;
160 }
161 //------------------------------------------------------------------------------
162 ::com::sun::star::uno::Reference< XTablesSupplier > OEvoabConnection::createCatalog()
163 {
164 	::osl::MutexGuard aGuard( m_aMutex );
165  	Reference< XTablesSupplier > xTab = m_xCatalog;
166  	if(!xTab.is())
167  	{
168  		OEvoabCatalog *pCat = new OEvoabCatalog(this);
169  		xTab = pCat;
170  		m_xCatalog = xTab;
171  	}
172  	return xTab;
173 }
174 // --------------------------------------------------------------------------------
175 Reference< XStatement > SAL_CALL OEvoabConnection::createStatement(  ) throw(SQLException, RuntimeException)
176 {
177 	::osl::MutexGuard aGuard( m_aMutex );
178 	checkDisposed(OConnection_BASE::rBHelper.bDisposed);
179 
180 	OStatement* pStmt = new OStatement(this);
181 
182 	Reference< XStatement > xStmt = pStmt;
183 	m_aStatements.push_back(WeakReferenceHelper(*pStmt));
184 	return xStmt;
185 }
186 // --------------------------------------------------------------------------------
187 Reference< XPreparedStatement > SAL_CALL OEvoabConnection::prepareStatement( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
188 {
189 	::osl::MutexGuard aGuard( m_aMutex );
190 	checkDisposed(OConnection_BASE::rBHelper.bDisposed);
191 
192 	OEvoabPreparedStatement* pStmt = new OEvoabPreparedStatement( this );
193 	Reference< XPreparedStatement > xStmt = pStmt;
194     pStmt->construct( sql );
195 
196 	m_aStatements.push_back(WeakReferenceHelper(*pStmt));
197 	return xStmt;
198 }
199 
200 Reference< XPreparedStatement > SAL_CALL OEvoabConnection::prepareCall( const ::rtl::OUString& /*sql*/ ) throw( SQLException, RuntimeException)
201 {
202     ::dbtools::throwFeatureNotImplementedException( "XConnection::prepareCall", *this );
203     return NULL;
204 }
205 sal_Bool SAL_CALL OEvoabConnection::isClosed(  ) throw(SQLException, RuntimeException)
206 {
207 	::osl::MutexGuard aGuard( m_aMutex );
208 	return OConnection_BASE::rBHelper.bDisposed;
209 }
210 
211 // --------------------------------------------------------------------------------
212 // XCloseable
213 void SAL_CALL OEvoabConnection::close(  ) throw(SQLException, RuntimeException)
214 {
215 	{  // we just dispose us
216 		::osl::MutexGuard aGuard( m_aMutex );
217 		checkDisposed(OConnection_BASE::rBHelper.bDisposed);
218 	}
219 	dispose();
220 }
221 
222 // --------------------------------------------------------------------------------
223 // XWarningsSupplier
224 Any SAL_CALL OEvoabConnection::getWarnings(  ) throw(SQLException, RuntimeException)
225 {
226     return m_aWarnings.getWarnings();
227 }
228 void SAL_CALL OEvoabConnection::clearWarnings(  ) throw(SQLException, RuntimeException)
229 {
230     m_aWarnings.clearWarnings();
231 }
232 //------------------------------------------------------------------------------
233 
234 void OEvoabConnection::disposing()
235 {
236 	// we noticed that we should be destroyed in near future so we have to dispose our statements
237 	::osl::MutexGuard aGuard(m_aMutex);
238     OConnection_BASE::disposing();
239 	dispose_ChildImpl();
240 }
241 
242 // -------------------------------- stubbed methods ------------------------------------------------
243 void SAL_CALL OEvoabConnection::setAutoCommit( sal_Bool /*autoCommit*/ ) throw(SQLException, RuntimeException)
244 {
245     ::dbtools::throwFeatureNotImplementedException( "XConnection::setAutoCommit", *this );
246 }
247 sal_Bool SAL_CALL OEvoabConnection::getAutoCommit(  ) throw(SQLException, RuntimeException)
248 {
249 	return sal_True;
250 }
251 void SAL_CALL OEvoabConnection::commit(  ) throw(SQLException, RuntimeException)
252 {
253 }
254 void SAL_CALL OEvoabConnection::rollback(  ) throw(SQLException, RuntimeException)
255 {
256 }
257 void SAL_CALL OEvoabConnection::setReadOnly( sal_Bool /*readOnly*/ ) throw(SQLException, RuntimeException)
258 {
259     ::dbtools::throwFeatureNotImplementedException( "XConnection::setReadOnly", *this );
260 }
261 sal_Bool SAL_CALL OEvoabConnection::isReadOnly(  ) throw(SQLException, RuntimeException)
262 {
263 	return sal_False;
264 }
265 void SAL_CALL OEvoabConnection::setCatalog( const ::rtl::OUString& /*catalog*/ ) throw(SQLException, RuntimeException)
266 {
267     ::dbtools::throwFeatureNotImplementedException( "XConnection::setCatalog", *this );
268 }
269 
270 ::rtl::OUString SAL_CALL OEvoabConnection::getCatalog(  ) throw(SQLException, RuntimeException)
271 {
272 	return ::rtl::OUString();
273 }
274 void SAL_CALL OEvoabConnection::setTransactionIsolation( sal_Int32 /*level*/ ) throw(SQLException, RuntimeException)
275 {
276     ::dbtools::throwFeatureNotImplementedException( "XConnection::setTransactionIsolation", *this );
277 }
278 
279 sal_Int32 SAL_CALL OEvoabConnection::getTransactionIsolation(  ) throw(SQLException, RuntimeException)
280 {
281 	return TransactionIsolation::NONE;
282 }
283 
284 Reference< ::com::sun::star::container::XNameAccess > SAL_CALL OEvoabConnection::getTypeMap(  ) throw(SQLException, RuntimeException)
285 {
286     ::dbtools::throwFeatureNotImplementedException( "XConnection::getTypeMap", *this );
287 	return NULL;
288 }
289 void SAL_CALL OEvoabConnection::setTypeMap( const Reference< ::com::sun::star::container::XNameAccess >& /*typeMap*/ ) throw(SQLException, RuntimeException)
290 {
291     ::dbtools::throwFeatureNotImplementedException( "XConnection::setTypeMap", *this );
292 }
293