1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  *  The Contents of this file are made available subject to the terms of
5  *  the BSD license.
6  *
7  *  Copyright 2000, 2010 Oracle and/or its affiliates.
8  *  All rights reserved.
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *  1. Redistributions of source code must retain the above copyright
14  *     notice, this list of conditions and the following disclaimer.
15  *  2. Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in the
17  *     documentation and/or other materials provided with the distribution.
18  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
19  *     contributors may be used to endorse or promote products derived
20  *     from this software without specific prior written permission.
21  *
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *************************************************************************/
35 
36 #include <stdio.h>
37 #include "SStatement.hxx"
38 #include "SConnection.hxx"
39 #include "SResultSet.hxx"
40 #include <osl/thread.h>
41 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
42 #include <com/sun/star/sdbc/ResultSetType.hpp>
43 #include <com/sun/star/sdbc/FetchDirection.hpp>
44 #include <com/sun/star/lang/DisposedException.hpp>
45 #include <cppuhelper/typeprovider.hxx>
46 #include <cppuhelper/queryinterface.hxx>
47 #include "propertyids.hxx"
48 
49 using namespace connectivity::skeleton;
50 
51 using namespace com::sun::star::uno;
52 using namespace com::sun::star::lang;
53 using namespace com::sun::star::beans;
54 using namespace com::sun::star::sdbc;
55 using namespace com::sun::star::sdbcx;
56 using namespace com::sun::star::container;
57 using namespace com::sun::star::io;
58 using namespace com::sun::star::util;
59 
OStatement_Base(OConnection * _pConnection)60 OStatement_Base::OStatement_Base(OConnection* _pConnection )
61     : OStatement_BASE(m_aMutex),
62     OPropertySetHelper(OStatement_BASE::rBHelper),
63     rBHelper(OStatement_BASE::rBHelper),
64     m_pConnection(_pConnection)
65 {
66     m_pConnection->acquire();
67 }
68 
~OStatement_Base()69 OStatement_Base::~OStatement_Base()
70 {
71 }
72 
disposeResultSet()73 void OStatement_Base::disposeResultSet()
74 {
75     // free the cursor if alive
76     Reference< XComponent > xComp(m_xResultSet.get(), UNO_QUERY);
77     if (xComp.is())
78         xComp->dispose();
79     m_xResultSet.clear();
80 }
81 
disposing()82 void OStatement_BASE2::disposing()
83 {
84     ::osl::MutexGuard aGuard(m_aMutex);
85 
86     disposeResultSet();
87 
88     if (m_pConnection)
89         m_pConnection->release();
90     m_pConnection = NULL;
91 
92     dispose_ChildImpl();
93     OStatement_Base::disposing();
94 }
95 
release()96 void SAL_CALL OStatement_BASE2::release() throw()
97 {
98     release_ChildImpl();
99 }
100 
queryInterface(const Type & rType)101 Any SAL_CALL OStatement_Base::queryInterface( const Type & rType )
102 {
103     Any aRet = OStatement_BASE::queryInterface(rType);
104     if(!aRet.hasValue())
105         aRet = OPropertySetHelper::queryInterface(rType);
106     return aRet;
107 }
108 
getTypes()109 Sequence< Type > SAL_CALL OStatement_Base::getTypes(  )
110 {
111     ::cppu::OTypeCollection aTypes(
112         ::cppu::UnoType<XMultiPropertySet>::get(),
113         ::cppu::UnoType<XFastPropertySet>::get(),
114         ::cppu::UnoType<XPropertySet>::get());
115 
116     return concatSequences(aTypes.getTypes(),OStatement_BASE::getTypes());
117 }
118 
119 
cancel()120 void SAL_CALL OStatement_Base::cancel(  )
121 {
122     ::osl::MutexGuard aGuard( m_aMutex );
123     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
124     // cancel the current sql statement
125 }
126 
127 
close()128 void SAL_CALL OStatement_Base::close(  )
129 {
130     {
131         ::osl::MutexGuard aGuard( m_aMutex );
132         checkDisposed(OStatement_BASE::rBHelper.bDisposed);
133 
134     }
135     dispose();
136 }
137 
138 
clearBatch()139 void SAL_CALL OStatement::clearBatch(  )
140 {
141     // if you support batches clear it here
142 }
143 
execute(const::rtl::OUString & sql)144 sal_Bool SAL_CALL OStatement_Base::execute( const ::rtl::OUString& sql )
145 {
146     ::osl::MutexGuard aGuard( m_aMutex );
147     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
148 
149     // returns true when a resultset is available
150     return sal_False;
151 }
152 
153 
executeQuery(const::rtl::OUString & sql)154 Reference< XResultSet > SAL_CALL OStatement_Base::executeQuery( const ::rtl::OUString& sql )
155 {
156     ::osl::MutexGuard aGuard( m_aMutex );
157     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
158 
159 
160     Reference< XResultSet > xRS = NULL;
161     // create a resultset as result of executing the sql statement
162     // you have to here something :-)
163     m_xResultSet = xRS; // we need a reference to it for later use
164     return xRS;
165 }
166 
167 
getConnection()168 Reference< XConnection > SAL_CALL OStatement_Base::getConnection(  )
169 {
170     ::osl::MutexGuard aGuard( m_aMutex );
171     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
172 
173     // just return our connection here
174     return (Reference< XConnection >)m_pConnection;
175 }
176 
getUpdateCount()177 sal_Int32 SAL_CALL OStatement_Base::getUpdateCount(  )
178 {
179     return 0;
180 }
181 
182 
queryInterface(const Type & rType)183 Any SAL_CALL OStatement::queryInterface( const Type & rType )
184 {
185     Any aRet = ::cppu::queryInterface(rType,static_cast< XBatchExecution*> (this));
186     if(!aRet.hasValue())
187         aRet = OStatement_Base::queryInterface(rType);
188     return aRet;
189 }
190 
191 
addBatch(const::rtl::OUString & sql)192 void SAL_CALL OStatement::addBatch( const ::rtl::OUString& sql )
193 {
194     ::osl::MutexGuard aGuard( m_aMutex );
195     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
196 
197 
198     m_aBatchVector.push_back(sql);
199 }
200 
executeBatch()201 Sequence< sal_Int32 > SAL_CALL OStatement::executeBatch(  )
202 {
203     ::osl::MutexGuard aGuard( m_aMutex );
204     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
205 
206     return Sequence< sal_Int32 >();
207 }
208 
209 
executeUpdate(const::rtl::OUString & sql)210 sal_Int32 SAL_CALL OStatement_Base::executeUpdate( const ::rtl::OUString& sql )
211 {
212     ::osl::MutexGuard aGuard( m_aMutex );
213     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
214 
215     // the return values gives information about how many rows are affected by executing the sql statement
216     return 0;
217 
218 }
219 
220 
getResultSet()221 Reference< XResultSet > SAL_CALL OStatement_Base::getResultSet(  )
222 {
223     ::osl::MutexGuard aGuard( m_aMutex );
224     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
225 
226 //  return our save resultset here
227     return m_xResultSet;
228 }
229 
230 
getMoreResults()231 sal_Bool SAL_CALL OStatement_Base::getMoreResults(  )
232 {
233     ::osl::MutexGuard aGuard( m_aMutex );
234     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
235 
236     // if your driver supports more than only one resultset
237     // and has one more at this moment return true
238     return sal_False;
239 }
240 
241 
getWarnings()242 Any SAL_CALL OStatement_Base::getWarnings(  )
243 {
244     ::osl::MutexGuard aGuard( m_aMutex );
245     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
246 
247 
248     return makeAny(m_aLastWarning);
249 }
250 
251 
clearWarnings()252 void SAL_CALL OStatement_Base::clearWarnings(  )
253 {
254     ::osl::MutexGuard aGuard( m_aMutex );
255     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
256 
257 
258     m_aLastWarning = SQLWarning();
259 }
260 
createArrayHelper() const261 ::cppu::IPropertyArrayHelper* OStatement_Base::createArrayHelper( ) const
262 {
263     // this properties are define by the service statement
264     // they must in alphabetic order
265     Sequence< Property > aProps(10);
266     Property* pProperties = aProps.getArray();
267     sal_Int32 nPos = 0;
268     pProperties[nPos++] = ::com::sun::star::beans::Property(OPropertyMap::getPropMap().getNameByIndex(PROPERTY_ID_CURSORNAME),
269         PROPERTY_ID_CURSORNAME, ::cppu::UnoType<rtl::OUString>::get(), 0);
270     pProperties[nPos++] = ::com::sun::star::beans::Property(OPropertyMap::getPropMap().getNameByIndex(PROPERTY_ID_ESCAPEPROCESSING),
271         PROPERTY_ID_ESCAPEPROCESSING, cppu::UnoType<bool>::get(), 0);
272     pProperties[nPos++] = ::com::sun::star::beans::Property(OPropertyMap::getPropMap().getNameByIndex(PROPERTY_ID_FETCHDIRECTION),
273         PROPERTY_ID_FETCHDIRECTION, ::cppu::UnoType<sal_Int32>::get(), 0);
274     pProperties[nPos++] = ::com::sun::star::beans::Property(OPropertyMap::getPropMap().getNameByIndex(PROPERTY_ID_FETCHSIZE),
275         PROPERTY_ID_FETCHSIZE, ::cppu::UnoType<sal_Int32>::get(), 0);
276     pProperties[nPos++] = ::com::sun::star::beans::Property(OPropertyMap::getPropMap().getNameByIndex(PROPERTY_ID_MAXFIELDSIZE),
277         PROPERTY_ID_MAXFIELDSIZE, ::cppu::UnoType<sal_Int32>::get(), 0);
278     pProperties[nPos++] = ::com::sun::star::beans::Property(OPropertyMap::getPropMap().getNameByIndex(PROPERTY_ID_MAXROWS),
279         PROPERTY_ID_MAXROWS, ::cppu::UnoType<sal_Int32>::get(), 0);
280     pProperties[nPos++] = ::com::sun::star::beans::Property(OPropertyMap::getPropMap().getNameByIndex(PROPERTY_ID_QUERYTIMEOUT),
281         PROPERTY_ID_QUERYTIMEOUT, ::cppu::UnoType<sal_Int32>::get(), 0);
282     pProperties[nPos++] = ::com::sun::star::beans::Property(OPropertyMap::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETCONCURRENCY),
283         PROPERTY_ID_RESULTSETCONCURRENCY, ::cppu::UnoType<sal_Int32>::get(), 0);
284     pProperties[nPos++] = ::com::sun::star::beans::Property(OPropertyMap::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETTYPE),
285         PROPERTY_ID_RESULTSETTYPE, ::cppu::UnoType<sal_Int32>::get(), 0);
286     pProperties[nPos++] = ::com::sun::star::beans::Property(OPropertyMap::getPropMap().getNameByIndex(PROPERTY_ID_USEBOOKMARKS),
287         PROPERTY_ID_USEBOOKMARKS, cppu::UnoType<bool>::get(), 0);
288 
289     return new ::cppu::OPropertyArrayHelper(aProps);
290 }
291 
292 
getInfoHelper()293 ::cppu::IPropertyArrayHelper & OStatement_Base::getInfoHelper()
294 {
295     return *const_cast<OStatement_Base*>(this)->getArrayHelper();
296 }
297 
convertFastPropertyValue(Any & rConvertedValue,Any & rOldValue,sal_Int32 nHandle,const Any & rValue)298 sal_Bool OStatement_Base::convertFastPropertyValue(
299                             Any & rConvertedValue,
300                             Any & rOldValue,
301                             sal_Int32 nHandle,
302                             const Any& rValue )
303 {
304     sal_Bool bConverted = sal_False;
305     // here we have to try to convert
306     return bConverted;
307 }
308 
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any & rValue)309 void OStatement_Base::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any& rValue)
310 {
311     // set the value to whatever is necessary
312     switch(nHandle)
313     {
314         case PROPERTY_ID_QUERYTIMEOUT:
315         case PROPERTY_ID_MAXFIELDSIZE:
316         case PROPERTY_ID_MAXROWS:
317         case PROPERTY_ID_CURSORNAME:
318         case PROPERTY_ID_RESULTSETCONCURRENCY:
319         case PROPERTY_ID_RESULTSETTYPE:
320         case PROPERTY_ID_FETCHDIRECTION:
321         case PROPERTY_ID_FETCHSIZE:
322         case PROPERTY_ID_ESCAPEPROCESSING:
323         case PROPERTY_ID_USEBOOKMARKS:
324         default:
325             ;
326     }
327 }
328 
getFastPropertyValue(Any & rValue,sal_Int32 nHandle) const329 void OStatement_Base::getFastPropertyValue(Any& rValue,sal_Int32 nHandle) const
330 {
331     switch(nHandle)
332     {
333         case PROPERTY_ID_QUERYTIMEOUT:
334         case PROPERTY_ID_MAXFIELDSIZE:
335         case PROPERTY_ID_MAXROWS:
336         case PROPERTY_ID_CURSORNAME:
337         case PROPERTY_ID_RESULTSETCONCURRENCY:
338         case PROPERTY_ID_RESULTSETTYPE:
339         case PROPERTY_ID_FETCHDIRECTION:
340         case PROPERTY_ID_FETCHSIZE:
341         case PROPERTY_ID_ESCAPEPROCESSING:
342         case PROPERTY_ID_USEBOOKMARKS:
343         default:
344             ;
345     }
346 }
347 
348 IMPLEMENT_SERVICE_INFO(OStatement,"com.sun.star.sdbcx.OStatement","com.sun.star.sdbc.Statement");
349 
acquire()350 void SAL_CALL OStatement_Base::acquire() throw()
351 {
352     OStatement_BASE::acquire();
353 }
354 
release()355 void SAL_CALL OStatement_Base::release() throw()
356 {
357     OStatement_BASE::release();
358 }
359 
acquire()360 void SAL_CALL OStatement::acquire() throw()
361 {
362     OStatement_BASE2::acquire();
363 }
364 
release()365 void SAL_CALL OStatement::release() throw()
366 {
367     OStatement_BASE2::release();
368 }
369 
getPropertySetInfo()370 Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL OStatement_Base::getPropertySetInfo(  )
371 {
372     return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
373 }
374 
375 
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
377