xref: /trunk/main/odk/examples/DevelopersGuide/Database/DriverSkeleton/SStatement.cxx (revision 9d37da743abb7db0a497688391f6e55a19f27c44)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <stdio.h>
25 #include <osl/diagnose.h>
26 #include "SStatement.hxx"
27 #include "SConnection.hxx"
28 #include "SResultSet.hxx"
29 #include <osl/thread.h>
30 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
31 #include <com/sun/star/sdbc/ResultSetType.hpp>
32 #include <com/sun/star/sdbc/FetchDirection.hpp>
33 #include <com/sun/star/lang/DisposedException.hpp>
34 #include <cppuhelper/typeprovider.hxx>
35 #include "propertyids.hxx"
36 
37 using namespace connectivity::skeleton;
38 //------------------------------------------------------------------------------
39 using namespace com::sun::star::uno;
40 using namespace com::sun::star::lang;
41 using namespace com::sun::star::beans;
42 using namespace com::sun::star::sdbc;
43 using namespace com::sun::star::sdbcx;
44 using namespace com::sun::star::container;
45 using namespace com::sun::star::io;
46 using namespace com::sun::star::util;
47 //------------------------------------------------------------------------------
48 OStatement_Base::OStatement_Base(OConnection* _pConnection )
49     : OStatement_BASE(m_aMutex),
50     OPropertySetHelper(OStatement_BASE::rBHelper),
51     rBHelper(OStatement_BASE::rBHelper),
52     m_pConnection(_pConnection)
53 {
54     m_pConnection->acquire();
55 }
56 // -----------------------------------------------------------------------------
57 OStatement_Base::~OStatement_Base()
58 {
59 }
60 //------------------------------------------------------------------------------
61 void OStatement_Base::disposeResultSet()
62 {
63     // free the cursor if alive
64     Reference< XComponent > xComp(m_xResultSet.get(), UNO_QUERY);
65     if (xComp.is())
66         xComp->dispose();
67     m_xResultSet = Reference< XResultSet>();
68 }
69 //------------------------------------------------------------------------------
70 void OStatement_BASE2::disposing()
71 {
72     ::osl::MutexGuard aGuard(m_aMutex);
73 
74     disposeResultSet();
75 
76     if (m_pConnection)
77         m_pConnection->release();
78     m_pConnection = NULL;
79 
80     dispose_ChildImpl();
81     OStatement_Base::disposing();
82 }
83 //-----------------------------------------------------------------------------
84 void SAL_CALL OStatement_BASE2::release() throw()
85 {
86     relase_ChildImpl();
87 }
88 //-----------------------------------------------------------------------------
89 Any SAL_CALL OStatement_Base::queryInterface( const Type & rType )
90 {
91     Any aRet = OStatement_BASE::queryInterface(rType);
92     if(!aRet.hasValue())
93         aRet = OPropertySetHelper::queryInterface(rType);
94     return aRet;
95 }
96 // -------------------------------------------------------------------------
97 Sequence< Type > SAL_CALL OStatement_Base::getTypes(  )
98 {
99     ::cppu::OTypeCollection aTypes(
100         ::cppu::UnoType< Reference< XMultiPropertySet > >::get(),
101         ::cppu::UnoType< Reference< XFastPropertySet > >::get(),
102         ::cppu::UnoType< Reference< XPropertySet > >::get());
103 
104     return concatSequences(aTypes.getTypes(),OStatement_BASE::getTypes());
105 }
106 // -------------------------------------------------------------------------
107 
108 void SAL_CALL OStatement_Base::cancel(  )
109 {
110     ::osl::MutexGuard aGuard( m_aMutex );
111     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
112     // cancel the current sql statement
113 }
114 // -------------------------------------------------------------------------
115 
116 void SAL_CALL OStatement_Base::close(  )
117 {
118     {
119         ::osl::MutexGuard aGuard( m_aMutex );
120         checkDisposed(OStatement_BASE::rBHelper.bDisposed);
121 
122     }
123     dispose();
124 }
125 // -------------------------------------------------------------------------
126 
127 void SAL_CALL OStatement::clearBatch(  )
128 {
129     // if you support batches clear it here
130 }
131 // -------------------------------------------------------------------------
132 sal_Bool SAL_CALL OStatement_Base::execute( const ::rtl::OUString& sql )
133 {
134     ::osl::MutexGuard aGuard( m_aMutex );
135     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
136 
137     // returns true when a resultset is available
138     return sal_False;
139 }
140 // -------------------------------------------------------------------------
141 
142 Reference< XResultSet > SAL_CALL OStatement_Base::executeQuery( const ::rtl::OUString& sql )
143 {
144     ::osl::MutexGuard aGuard( m_aMutex );
145     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
146 
147 
148     Reference< XResultSet > xRS = NULL;
149     // create a resultset as result of executing the sql statement
150     // you have to here something :-)
151     m_xResultSet = xRS; // we nedd a reference to it for later use
152     return xRS;
153 }
154 // -------------------------------------------------------------------------
155 
156 Reference< XConnection > SAL_CALL OStatement_Base::getConnection(  )
157 {
158     ::osl::MutexGuard aGuard( m_aMutex );
159     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
160 
161     // just return our connection here
162     return (Reference< XConnection >)m_pConnection;
163 }
164 // -----------------------------------------------------------------------------
165 sal_Int32 SAL_CALL OStatement_Base::getUpdateCount(  )
166 {
167     return 0;
168 }
169 // -------------------------------------------------------------------------
170 
171 Any SAL_CALL OStatement::queryInterface( const Type & rType )
172 {
173     Any aRet = ::cppu::queryInterface(rType,static_cast< XBatchExecution*> (this));
174     if(!aRet.hasValue())
175         aRet = OStatement_Base::queryInterface(rType);
176     return aRet;
177 }
178 // -------------------------------------------------------------------------
179 
180 void SAL_CALL OStatement::addBatch( const ::rtl::OUString& sql )
181 {
182     ::osl::MutexGuard aGuard( m_aMutex );
183     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
184 
185 
186     m_aBatchList.push_back(sql);
187 }
188 // -------------------------------------------------------------------------
189 Sequence< sal_Int32 > SAL_CALL OStatement::executeBatch(  )
190 {
191     ::osl::MutexGuard aGuard( m_aMutex );
192     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
193 
194     return Sequence< sal_Int32 >();
195 }
196 // -------------------------------------------------------------------------
197 
198 
199 sal_Int32 SAL_CALL OStatement_Base::executeUpdate( const ::rtl::OUString& sql )
200 {
201     ::osl::MutexGuard aGuard( m_aMutex );
202     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
203 
204     // the return values gives information about how many rows are affected by executing the sql statement
205     return 0;
206 
207 }
208 // -------------------------------------------------------------------------
209 
210 Reference< XResultSet > SAL_CALL OStatement_Base::getResultSet(  )
211 {
212     ::osl::MutexGuard aGuard( m_aMutex );
213     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
214 
215 //  return our save resultset here
216     return m_xResultSet;
217 }
218 // -------------------------------------------------------------------------
219 
220 sal_Bool SAL_CALL OStatement_Base::getMoreResults(  )
221 {
222     ::osl::MutexGuard aGuard( m_aMutex );
223     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
224 
225     // if your driver supports more than only one resultset
226     // and has one more at this moment return true
227     return sal_False;
228 }
229 // -------------------------------------------------------------------------
230 
231 // -------------------------------------------------------------------------
232 Any SAL_CALL OStatement_Base::getWarnings(  )
233 {
234     ::osl::MutexGuard aGuard( m_aMutex );
235     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
236 
237 
238     return makeAny(m_aLastWarning);
239 }
240 // -------------------------------------------------------------------------
241 
242 // -------------------------------------------------------------------------
243 void SAL_CALL OStatement_Base::clearWarnings(  )
244 {
245     ::osl::MutexGuard aGuard( m_aMutex );
246     checkDisposed(OStatement_BASE::rBHelper.bDisposed);
247 
248 
249     m_aLastWarning = SQLWarning();
250 }
251 // -------------------------------------------------------------------------
252 ::cppu::IPropertyArrayHelper* OStatement_Base::createArrayHelper( ) const
253 {
254     // this properties are define by the service statement
255     // they must in alphabetic order
256     Sequence< Property > aProps(10);
257     Property* pProperties = aProps.getArray();
258     sal_Int32 nPos = 0;
259     DECL_PROP0(CURSORNAME,  ::rtl::OUString);
260     DECL_BOOL_PROP0(ESCAPEPROCESSING);
261     DECL_PROP0(FETCHDIRECTION,sal_Int32);
262     DECL_PROP0(FETCHSIZE,   sal_Int32);
263     DECL_PROP0(MAXFIELDSIZE,sal_Int32);
264     DECL_PROP0(MAXROWS,     sal_Int32);
265     DECL_PROP0(QUERYTIMEOUT,sal_Int32);
266     DECL_PROP0(RESULTSETCONCURRENCY,sal_Int32);
267     DECL_PROP0(RESULTSETTYPE,sal_Int32);
268     DECL_BOOL_PROP0(USEBOOKMARKS);
269 
270     return new ::cppu::OPropertyArrayHelper(aProps);
271 }
272 
273 // -------------------------------------------------------------------------
274 ::cppu::IPropertyArrayHelper & OStatement_Base::getInfoHelper()
275 {
276     return *const_cast<OStatement_Base*>(this)->getArrayHelper();
277 }
278 // -------------------------------------------------------------------------
279 sal_Bool OStatement_Base::convertFastPropertyValue(
280                             Any & rConvertedValue,
281                             Any & rOldValue,
282                             sal_Int32 nHandle,
283                             const Any& rValue )
284 {
285     sal_Bool bConverted = sal_False;
286     // here we have to try to convert
287     return bConverted;
288 }
289 // -------------------------------------------------------------------------
290 void OStatement_Base::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any& rValue)
291 {
292     // set the value to what ever is necessary
293     switch(nHandle)
294     {
295         case PROPERTY_ID_QUERYTIMEOUT:
296         case PROPERTY_ID_MAXFIELDSIZE:
297         case PROPERTY_ID_MAXROWS:
298         case PROPERTY_ID_CURSORNAME:
299         case PROPERTY_ID_RESULTSETCONCURRENCY:
300         case PROPERTY_ID_RESULTSETTYPE:
301         case PROPERTY_ID_FETCHDIRECTION:
302         case PROPERTY_ID_FETCHSIZE:
303         case PROPERTY_ID_ESCAPEPROCESSING:
304         case PROPERTY_ID_USEBOOKMARKS:
305         default:
306             ;
307     }
308 }
309 // -------------------------------------------------------------------------
310 void OStatement_Base::getFastPropertyValue(Any& rValue,sal_Int32 nHandle) const
311 {
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 // -------------------------------------------------------------------------
329 IMPLEMENT_SERVICE_INFO(OStatement,"com.sun.star.sdbcx.OStatement","com.sun.star.sdbc.Statement");
330 // -----------------------------------------------------------------------------
331 void SAL_CALL OStatement_Base::acquire() throw()
332 {
333     OStatement_BASE::acquire();
334 }
335 // -----------------------------------------------------------------------------
336 void SAL_CALL OStatement_Base::release() throw()
337 {
338     OStatement_BASE::release();
339 }
340 // -----------------------------------------------------------------------------
341 void SAL_CALL OStatement::acquire() throw()
342 {
343     OStatement_BASE2::acquire();
344 }
345 // -----------------------------------------------------------------------------
346 void SAL_CALL OStatement::release() throw()
347 {
348     OStatement_BASE2::release();
349 }
350 // -----------------------------------------------------------------------------
351 Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL OStatement_Base::getPropertySetInfo(  )
352 {
353     return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
354 }
355 // -----------------------------------------------------------------------------
356