xref: /trunk/main/odk/examples/DevelopersGuide/Database/DriverSkeleton/SConnection.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 #include "SConnection.hxx"
36 
37 #include "SDatabaseMetaData.hxx"
38 #include "SDriver.hxx"
39 #include "SStatement.hxx"
40 #include "SPreparedStatement.hxx"
41 #include <com/sun/star/sdbc/ColumnValue.hpp>
42 #include <com/sun/star/sdbc/XRow.hpp>
43 #include <com/sun/star/sdbc/TransactionIsolation.hpp>
44 #include <com/sun/star/lang/DisposedException.hpp>
45 
46 using namespace connectivity::skeleton;
47 
48 //------------------------------------------------------------------------------
49 using namespace com::sun::star::uno;
50 using namespace com::sun::star::lang;
51 using namespace com::sun::star::beans;
52 using namespace com::sun::star::sdbc;
53 // --------------------------------------------------------------------------------
54 OConnection::OConnection(SkeletonDriver*    _pDriver)
55                          : OSubComponent<OConnection, OConnection_BASE>((::cppu::OWeakObject*)_pDriver, this),
56                          OMetaConnection_BASE(m_aMutex),
57                          m_pDriver(_pDriver),
58                          m_bClosed(sal_False),
59                          m_xMetaData(NULL),
60                          m_bUseCatalog(sal_False),
61                          m_bUseOldDateFormat(sal_False)
62 {
63     m_pDriver->acquire();
64 }
65 //-----------------------------------------------------------------------------
66 OConnection::~OConnection()
67 {
68     if(!isClosed())
69         close();
70     m_pDriver->release();
71     m_pDriver = NULL;
72 }
73 //-----------------------------------------------------------------------------
74 void SAL_CALL OConnection::release() throw()
75 {
76     relase_ChildImpl();
77 }
78 // -----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 void OConnection::construct(const ::rtl::OUString& url,const Sequence< PropertyValue >& info)  throw(SQLException)
81 {
82     osl_incrementInterlockedCount( &m_refCount );
83 
84     // some example code how to get the information out of the sequence
85 
86     sal_Int32 nLen = url.indexOf(':');
87     nLen = url.indexOf(':',nLen+1);
88     ::rtl::OUString aDSN(RTL_CONSTASCII_USTRINGPARAM("DSN=")), aUID, aPWD, aSysDrvSettings;
89     aDSN += url.copy(nLen+1);
90 
91     const char* pUser       = "user";
92     const char* pTimeout    = "Timeout";
93     const char* pSilent     = "Silent";
94     const char* pPwd        = "password";
95     const char* pUseCatalog = "UseCatalog";
96     const char* pSysDrv     = "SystemDriverSettings";
97 
98     sal_Int32 nTimeout = 20;
99     sal_Bool bSilent = sal_True;
100     const PropertyValue *pBegin = info.getConstArray();
101     const PropertyValue *pEnd   = pBegin + info.getLength();
102     for(;pBegin != pEnd;++pBegin)
103     {
104         if(!pBegin->Name.compareToAscii(pTimeout))
105             pBegin->Value >>= nTimeout;
106         else if(!pBegin->Name.compareToAscii(pSilent))
107             pBegin->Value >>= bSilent;
108         else if(!pBegin->Name.compareToAscii(pUser))
109         {
110             pBegin->Value >>= aUID;
111             aDSN = aDSN + ::rtl::OUString::createFromAscii(";UID=") + aUID;
112         }
113         else if(!pBegin->Name.compareToAscii(pPwd))
114         {
115             pBegin->Value >>= aPWD;
116             aDSN = aDSN + ::rtl::OUString::createFromAscii(";PWD=") + aPWD;
117         }
118         else if(!pBegin->Name.compareToAscii(pUseCatalog))
119         {
120             pBegin->Value >>= m_bUseCatalog;
121         }
122         else if(!pBegin->Name.compareToAscii(pSysDrv))
123         {
124             pBegin->Value >>= aSysDrvSettings;
125             aDSN += ::rtl::OUString::createFromAscii(";");
126             aDSN += aSysDrvSettings;
127         }
128     }
129     m_sUser = aUID;
130 
131     osl_decrementInterlockedCount( &m_refCount );
132 }
133 // XServiceInfo
134 // --------------------------------------------------------------------------------
135 IMPLEMENT_SERVICE_INFO(OConnection, "com.sun.star.sdbc.drivers.skeleton.OConnection", "com.sun.star.sdbc.Connection")
136 
137 // --------------------------------------------------------------------------------
138 Reference< XStatement > SAL_CALL OConnection::createStatement(  ) throw(SQLException, RuntimeException)
139 {
140     ::osl::MutexGuard aGuard( m_aMutex );
141     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
142 
143     // create a statement
144     // the statement can only be executed once
145     Reference< XStatement > xReturn = new OStatement(this);
146     m_aStatements.push_back(WeakReferenceHelper(xReturn));
147     return xReturn;
148 }
149 // --------------------------------------------------------------------------------
150 Reference< XPreparedStatement > SAL_CALL OConnection::prepareStatement( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException)
151 {
152     ::osl::MutexGuard aGuard( m_aMutex );
153     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
154 
155     // the pre
156     if(m_aTypeInfo.empty())
157         buildTypeInfo();
158 
159     // create a statement
160     // the statement can only be executed more than once
161     Reference< XPreparedStatement > xReturn = new OPreparedStatement(this,m_aTypeInfo,_sSql);
162     m_aStatements.push_back(WeakReferenceHelper(xReturn));
163     return xReturn;
164 }
165 // --------------------------------------------------------------------------------
166 Reference< XPreparedStatement > SAL_CALL OConnection::prepareCall( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException)
167 {
168     ::osl::MutexGuard aGuard( m_aMutex );
169     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
170 
171     // not implemented yet :-) a task to do
172     return NULL;
173 }
174 // --------------------------------------------------------------------------------
175 ::rtl::OUString SAL_CALL OConnection::nativeSQL( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException)
176 {
177     ::osl::MutexGuard aGuard( m_aMutex );
178     // when you need to transform SQL92 to you driver specific you can do it here
179 
180     return _sSql;
181 }
182 // --------------------------------------------------------------------------------
183 void SAL_CALL OConnection::setAutoCommit( sal_Bool autoCommit ) throw(SQLException, RuntimeException)
184 {
185     ::osl::MutexGuard aGuard( m_aMutex );
186     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
187     // here you  have to set your commit mode please have a look at the jdbc documentation to get a clear explanation
188 }
189 // --------------------------------------------------------------------------------
190 sal_Bool SAL_CALL OConnection::getAutoCommit(  ) throw(SQLException, RuntimeException)
191 {
192     ::osl::MutexGuard aGuard( m_aMutex );
193     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
194     // you have to distinguish which if you are in autocommit mode or not
195     // at normal case true should be fine here
196 
197     return sal_True;
198 }
199 // --------------------------------------------------------------------------------
200 void SAL_CALL OConnection::commit(  ) throw(SQLException, RuntimeException)
201 {
202     ::osl::MutexGuard aGuard( m_aMutex );
203     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
204 
205     // when you database does support transactions you should commit here
206 }
207 // --------------------------------------------------------------------------------
208 void SAL_CALL OConnection::rollback(  ) throw(SQLException, RuntimeException)
209 {
210     ::osl::MutexGuard aGuard( m_aMutex );
211     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
212 
213 
214     // same as commit but for the other case
215 }
216 // --------------------------------------------------------------------------------
217 sal_Bool SAL_CALL OConnection::isClosed(  ) throw(SQLException, RuntimeException)
218 {
219     ::osl::MutexGuard aGuard( m_aMutex );
220 
221     // just simple -> we are close when we are disposed taht means someone called dispose(); (XComponent)
222     return OConnection_BASE::rBHelper.bDisposed;
223 }
224 // --------------------------------------------------------------------------------
225 Reference< XDatabaseMetaData > SAL_CALL OConnection::getMetaData(  ) throw(SQLException, RuntimeException)
226 {
227     ::osl::MutexGuard aGuard( m_aMutex );
228     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
229 
230     // here we have to create the class with biggest interface
231     // The answer is 42 :-)
232     Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
233     if(!xMetaData.is())
234     {
235         xMetaData = new ODatabaseMetaData(this); // need the connection because it can return it
236         m_xMetaData = xMetaData;
237     }
238 
239     return xMetaData;
240 }
241 // --------------------------------------------------------------------------------
242 void SAL_CALL OConnection::setReadOnly( sal_Bool readOnly ) throw(SQLException, RuntimeException)
243 {
244     ::osl::MutexGuard aGuard( m_aMutex );
245     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
246 
247     // set you connection to readonly
248 }
249 // --------------------------------------------------------------------------------
250 sal_Bool SAL_CALL OConnection::isReadOnly(  ) throw(SQLException, RuntimeException)
251 {
252     ::osl::MutexGuard aGuard( m_aMutex );
253     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
254 
255     // return if your connection to readonly
256     return sal_False;
257 }
258 // --------------------------------------------------------------------------------
259 void SAL_CALL OConnection::setCatalog( const ::rtl::OUString& catalog ) throw(SQLException, RuntimeException)
260 {
261     ::osl::MutexGuard aGuard( m_aMutex );
262     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
263 
264     // if your database doesn't work with catalogs you go to next method otherwise you kjnow what to do
265 }
266 // --------------------------------------------------------------------------------
267 ::rtl::OUString SAL_CALL OConnection::getCatalog(  ) throw(SQLException, RuntimeException)
268 {
269     ::osl::MutexGuard aGuard( m_aMutex );
270     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
271 
272 
273     // return your current catalog
274     return ::rtl::OUString();
275 }
276 // --------------------------------------------------------------------------------
277 void SAL_CALL OConnection::setTransactionIsolation( sal_Int32 level ) throw(SQLException, RuntimeException)
278 {
279     ::osl::MutexGuard aGuard( m_aMutex );
280     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
281 
282     // set your isolation level
283     // please have a look at @see com.sun.star.sdbc.TransactionIsolation
284 }
285 // --------------------------------------------------------------------------------
286 sal_Int32 SAL_CALL OConnection::getTransactionIsolation(  ) throw(SQLException, RuntimeException)
287 {
288     ::osl::MutexGuard aGuard( m_aMutex );
289     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
290 
291 
292     // please have a look at @see com.sun.star.sdbc.TransactionIsolation
293     return TransactionIsolation::NONE;
294 }
295 // --------------------------------------------------------------------------------
296 Reference< ::com::sun::star::container::XNameAccess > SAL_CALL OConnection::getTypeMap(  ) throw(SQLException, RuntimeException)
297 {
298     ::osl::MutexGuard aGuard( m_aMutex );
299     checkDisposed(OConnection_BASE::rBHelper.bDisposed);
300 
301     // if your driver has special database types you can return it here
302 
303     return NULL;
304 }
305 // --------------------------------------------------------------------------------
306 void SAL_CALL OConnection::setTypeMap( const Reference< ::com::sun::star::container::XNameAccess >& typeMap ) throw(SQLException, RuntimeException)
307 {
308     // the other way around
309 }
310 // --------------------------------------------------------------------------------
311 // XCloseable
312 void SAL_CALL OConnection::close(  ) throw(SQLException, RuntimeException)
313 {
314     // we just dispose us
315     {
316         ::osl::MutexGuard aGuard( m_aMutex );
317         checkDisposed(OConnection_BASE::rBHelper.bDisposed);
318 
319     }
320     dispose();
321 }
322 // --------------------------------------------------------------------------------
323 // XWarningsSupplier
324 Any SAL_CALL OConnection::getWarnings(  ) throw(SQLException, RuntimeException)
325 {
326     // when you collected some warnings -> return it
327     return Any();
328 }
329 // --------------------------------------------------------------------------------
330 void SAL_CALL OConnection::clearWarnings(  ) throw(SQLException, RuntimeException)
331 {
332     // you should clear your collected warnings here
333 }
334 //--------------------------------------------------------------------
335 void OConnection::buildTypeInfo() throw( SQLException)
336 {
337     ::osl::MutexGuard aGuard( m_aMutex );
338 
339     Reference< XResultSet> xRs = getMetaData ()->getTypeInfo ();
340     Reference< XRow> xRow(xRs,UNO_QUERY);
341     // Information for a single SQL type
342 
343     // Loop on the result set until we reach end of file
344 
345     while (xRs->next ())
346     {
347         OTypeInfo aInfo;
348         aInfo.aTypeName         = xRow->getString   (1);
349         aInfo.nType             = xRow->getShort    (2);
350         aInfo.nPrecision        = xRow->getInt      (3);
351         aInfo.aLiteralPrefix    = xRow->getString   (4);
352         aInfo.aLiteralSuffix    = xRow->getString   (5);
353         aInfo.aCreateParams     = xRow->getString   (6);
354         aInfo.bNullable         = xRow->getBoolean  (7) == ColumnValue::NULLABLE;
355         aInfo.bCaseSensitive    = xRow->getBoolean  (8);
356         aInfo.nSearchType       = xRow->getShort    (9);
357         aInfo.bUnsigned         = xRow->getBoolean  (10);
358         aInfo.bCurrency         = xRow->getBoolean  (11);
359         aInfo.bAutoIncrement    = xRow->getBoolean  (12);
360         aInfo.aLocalTypeName    = xRow->getString   (13);
361         aInfo.nMinimumScale     = xRow->getShort    (14);
362         aInfo.nMaximumScale     = xRow->getShort    (15);
363         aInfo.nNumPrecRadix     = (sal_Int16)xRow->getInt(18);
364 
365 
366 
367         // Now that we have the type info, save it
368         // in the Hashtable if we don't already have an
369         // entry for this SQL type.
370 
371         m_aTypeInfo.push_back(aInfo);
372     }
373 
374     // Close the result set/statement.
375 
376     Reference< XCloseable> xClose(xRs,UNO_QUERY);
377     xClose->close();
378 }
379 //------------------------------------------------------------------------------
380 void OConnection::disposing()
381 {
382     // we noticed that we should be destroied in near future so we have to dispose our statements
383     ::osl::MutexGuard aGuard(m_aMutex);
384 
385     for (OWeakRefArray::iterator i = m_aStatements.begin(); m_aStatements.end() != i; ++i)
386     {
387         Reference< XComponent > xComp(i->get(), UNO_QUERY);
388         if (xComp.is())
389             xComp->dispose();
390     }
391     m_aStatements.clear();
392 
393     m_bClosed   = sal_True;
394     m_xMetaData = ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XDatabaseMetaData>();
395 
396     dispose_ChildImpl();
397     OConnection_BASE::disposing();
398 }
399 // -----------------------------------------------------------------------------
400 
401 
402 
403