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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 
27 #include "KPreparedStatement.hxx"
28 #include "propertyids.hxx"
29 #include <connectivity/dbexception.hxx>
30 #include <connectivity/dbtools.hxx>
31 #include "resource/kab_res.hrc"
32 #include "resource/sharedresources.hxx"
33 
34 using namespace connectivity::kab;
35 using namespace com::sun::star::uno;
36 using namespace com::sun::star::lang;
37 using namespace com::sun::star::sdbc;
38 using namespace com::sun::star::util;
39 
40 IMPLEMENT_SERVICE_INFO(KabPreparedStatement, "com.sun.star.sdbc.drivers.KabPreparedStatement", "com.sun.star.sdbc.PreparedStatement");
41 // -------------------------------------------------------------------------
checkAndResizeParameters(sal_Int32 nParams)42 void KabPreparedStatement::checkAndResizeParameters(sal_Int32 nParams) throw(SQLException)
43 {
44 	if ( !m_aParameterRow.isValid() )
45 		m_aParameterRow = new OValueVector();
46 
47 	if (nParams < 1)
48         ::dbtools::throwInvalidIndexException(*(KabPreparedStatement *) this,Any());
49 
50 	if (nParams >= (sal_Int32) (m_aParameterRow->get()).size())
51 		(m_aParameterRow->get()).resize(nParams);
52 }
53 // -------------------------------------------------------------------------
setKabFields() const54 void KabPreparedStatement::setKabFields() const throw(SQLException)
55 {
56 	::vos::ORef<connectivity::OSQLColumns> xColumns;	// selected columns
57 
58 	xColumns = m_aSQLIterator.getSelectColumns();
59 	if (!xColumns.isValid())
60 	{
61         ::connectivity::SharedResources aResources;
62         const ::rtl::OUString sError( aResources.getResourceString(
63                 STR_INVALID_COLUMN_SELECTION
64              ) );
65 	    ::dbtools::throwGenericSQLException(sError,NULL);
66 	}
67 	m_xMetaData->setKabFields(xColumns);
68 }
69 // -------------------------------------------------------------------------
resetParameters() const70 void KabPreparedStatement::resetParameters() const throw(SQLException)
71 {
72 	m_nParameterIndex = 0;
73 }
74 // -------------------------------------------------------------------------
getNextParameter(::rtl::OUString & rParameter) const75 void KabPreparedStatement::getNextParameter(::rtl::OUString &rParameter) const throw(SQLException)
76 {
77 	if (m_nParameterIndex >= (sal_Int32) (m_aParameterRow->get()).size())
78     {
79         ::connectivity::SharedResources aResources;
80         const ::rtl::OUString sError( aResources.getResourceString(
81                 STR_INVALID_PARA_COUNT
82              ) );
83 	    ::dbtools::throwGenericSQLException(sError,*(KabPreparedStatement *) this);
84     } // if (m_nParameterIndex >= (sal_Int32) (*m_aParameterRow).size())
85 
86 	rParameter = (m_aParameterRow->get())[m_nParameterIndex];
87 
88 	m_nParameterIndex++;
89 }
90 // -------------------------------------------------------------------------
KabPreparedStatement(KabConnection * _pConnection,const::rtl::OUString & sql)91 KabPreparedStatement::KabPreparedStatement(
92 	KabConnection* _pConnection,
93 	const ::rtl::OUString& sql)
94 	: KabPreparedStatement_BASE(_pConnection),
95 	  m_sSqlStatement(sql),
96 	  m_bPrepared(sal_False),
97 	  m_nParameterIndex(0),
98 	  m_aParameterRow()
99 {
100 }
101 // -------------------------------------------------------------------------
~KabPreparedStatement()102 KabPreparedStatement::~KabPreparedStatement()
103 {
104 }
105 // -------------------------------------------------------------------------
disposing()106 void KabPreparedStatement::disposing()
107 {
108 	KabPreparedStatement_BASE::disposing();
109 
110 	if (m_aParameterRow.isValid())
111 	{
112 		m_aParameterRow->get().clear();
113 		m_aParameterRow = NULL;
114 	}
115 }
116 // -------------------------------------------------------------------------
getMetaData()117 Reference< XResultSetMetaData > SAL_CALL KabPreparedStatement::getMetaData() throw(SQLException, RuntimeException)
118 {
119 	::osl::MutexGuard aGuard( m_aMutex );
120 	checkDisposed(KabCommonStatement_BASE::rBHelper.bDisposed);
121 
122 	if (!m_xMetaData.is())
123 	{
124 		m_xMetaData = new KabResultSetMetaData(getOwnConnection());
125 		setKabFields();
126 	}
127 	Reference< XResultSetMetaData > xMetaData = m_xMetaData.get();
128 	return xMetaData;
129 }
130 // -------------------------------------------------------------------------
close()131 void SAL_CALL KabPreparedStatement::close() throw(SQLException, RuntimeException)
132 {
133 	::osl::MutexGuard aGuard( m_aMutex );
134 	checkDisposed(KabCommonStatement_BASE::rBHelper.bDisposed);
135 
136 	// Reset last warning message
137 	try {
138 		clearWarnings ();
139 		KabCommonStatement::close();
140 	}
141 	catch (SQLException &) {
142 		// If we get an error, ignore
143 	}
144 
145 	// Remove this Statement object from the Connection object's
146 	// list
147 }
148 // -------------------------------------------------------------------------
execute()149 sal_Bool SAL_CALL KabPreparedStatement::execute() throw(SQLException, RuntimeException)
150 {
151 	::osl::MutexGuard aGuard( m_aMutex );
152 	checkDisposed(KabCommonStatement_BASE::rBHelper.bDisposed);
153 
154 	Reference< XResultSet> xRS = KabCommonStatement::executeQuery(m_sSqlStatement);
155 
156 	return xRS.is();
157 }
158 // -------------------------------------------------------------------------
executeUpdate()159 sal_Int32 SAL_CALL KabPreparedStatement::executeUpdate() throw(SQLException, RuntimeException)
160 {
161 	::osl::MutexGuard aGuard( m_aMutex );
162 	checkDisposed(KabCommonStatement_BASE::rBHelper.bDisposed);
163 
164 	// same as in statement with the difference that this statement also can contain parameter
165 	return 0;
166 }
167 // -------------------------------------------------------------------------
getConnection()168 Reference< XConnection > SAL_CALL KabPreparedStatement::getConnection() throw(SQLException, RuntimeException)
169 {
170 	::osl::MutexGuard aGuard( m_aMutex );
171 	checkDisposed(KabCommonStatement_BASE::rBHelper.bDisposed);
172 
173 	return (Reference< XConnection >) m_pConnection;
174 }
175 // -------------------------------------------------------------------------
executeQuery()176 Reference< XResultSet > SAL_CALL KabPreparedStatement::executeQuery() throw(SQLException, RuntimeException)
177 {
178 	::osl::MutexGuard aGuard( m_aMutex );
179 	checkDisposed(KabCommonStatement_BASE::rBHelper.bDisposed);
180 
181 	Reference< XResultSet > rs = KabCommonStatement::executeQuery(m_sSqlStatement);
182 
183 	return rs;
184 }
185 // -------------------------------------------------------------------------
setNull(sal_Int32 parameterIndex,sal_Int32)186 void SAL_CALL KabPreparedStatement::setNull(sal_Int32 parameterIndex, sal_Int32) throw(SQLException, RuntimeException)
187 {
188 	::osl::MutexGuard aGuard( m_aMutex );
189 	checkDisposed(KabCommonStatement_BASE::rBHelper.bDisposed);
190 
191 	checkAndResizeParameters(parameterIndex);
192 
193 	(m_aParameterRow->get())[parameterIndex - 1].setNull();
194 }
195 // -------------------------------------------------------------------------
setObjectNull(sal_Int32,sal_Int32,const::rtl::OUString &)196 void SAL_CALL KabPreparedStatement::setObjectNull(sal_Int32, sal_Int32, const ::rtl::OUString&) throw(SQLException, RuntimeException)
197 {
198 
199 
200 
201 ::dbtools::throwFunctionNotSupportedException("setObjectNull", NULL);
202 }
203 // -------------------------------------------------------------------------
setBoolean(sal_Int32,sal_Bool)204 void SAL_CALL KabPreparedStatement::setBoolean(sal_Int32, sal_Bool) throw(SQLException, RuntimeException)
205 {
206 
207 
208 
209 ::dbtools::throwFunctionNotSupportedException("setBoolean", NULL);
210 }
211 // -------------------------------------------------------------------------
setByte(sal_Int32,sal_Int8)212 void SAL_CALL KabPreparedStatement::setByte(sal_Int32, sal_Int8) throw(SQLException, RuntimeException)
213 {
214 
215 
216 
217 ::dbtools::throwFunctionNotSupportedException("setByte", NULL);
218 }
219 // -------------------------------------------------------------------------
setShort(sal_Int32,sal_Int16)220 void SAL_CALL KabPreparedStatement::setShort(sal_Int32, sal_Int16) throw(SQLException, RuntimeException)
221 {
222 
223 
224 
225 ::dbtools::throwFunctionNotSupportedException("setShort", NULL);
226 }
227 // -------------------------------------------------------------------------
setInt(sal_Int32,sal_Int32)228 void SAL_CALL KabPreparedStatement::setInt(sal_Int32, sal_Int32) throw(SQLException, RuntimeException)
229 {
230 
231 
232 
233 ::dbtools::throwFunctionNotSupportedException("setInt", NULL);
234 }
235 // -------------------------------------------------------------------------
setLong(sal_Int32,sal_Int64)236 void SAL_CALL KabPreparedStatement::setLong(sal_Int32, sal_Int64) throw(SQLException, RuntimeException)
237 {
238 
239 
240 
241 ::dbtools::throwFunctionNotSupportedException("", NULL);
242 }
243 // -------------------------------------------------------------------------
setFloat(sal_Int32,float)244 void SAL_CALL KabPreparedStatement::setFloat(sal_Int32, float) throw(SQLException, RuntimeException)
245 {
246 
247 
248 
249 ::dbtools::throwFunctionNotSupportedException("setFloat", NULL);
250 }
251 // -------------------------------------------------------------------------
setDouble(sal_Int32,double)252 void SAL_CALL KabPreparedStatement::setDouble(sal_Int32, double) throw(SQLException, RuntimeException)
253 {
254 
255 
256 
257 ::dbtools::throwFunctionNotSupportedException("setDouble", NULL);
258 }
259 // -------------------------------------------------------------------------
setString(sal_Int32 parameterIndex,const::rtl::OUString & x)260 void SAL_CALL KabPreparedStatement::setString(sal_Int32 parameterIndex, const ::rtl::OUString &x) throw(SQLException, RuntimeException)
261 {
262 	::osl::MutexGuard aGuard( m_aMutex );
263 	checkDisposed(KabCommonStatement_BASE::rBHelper.bDisposed);
264 
265 	checkAndResizeParameters(parameterIndex);
266 
267 	(m_aParameterRow->get())[parameterIndex - 1] = x;
268 }
269 // -------------------------------------------------------------------------
setBytes(sal_Int32,const Sequence<sal_Int8> &)270 void SAL_CALL KabPreparedStatement::setBytes(sal_Int32, const Sequence< sal_Int8 >&) throw(SQLException, RuntimeException)
271 {
272 
273 
274 
275 ::dbtools::throwFunctionNotSupportedException("setBytes", NULL);
276 }
277 // -------------------------------------------------------------------------
setDate(sal_Int32,const Date &)278 void SAL_CALL KabPreparedStatement::setDate(sal_Int32, const Date&) throw(SQLException, RuntimeException)
279 {
280 
281 
282 
283 ::dbtools::throwFunctionNotSupportedException("setDate", NULL);
284 }
285 // -------------------------------------------------------------------------
setTime(sal_Int32,const Time &)286 void SAL_CALL KabPreparedStatement::setTime(sal_Int32, const Time&) throw(SQLException, RuntimeException)
287 {
288 
289 
290 
291 ::dbtools::throwFunctionNotSupportedException("setTime", NULL);
292 }
293 // -------------------------------------------------------------------------
setTimestamp(sal_Int32,const DateTime &)294 void SAL_CALL KabPreparedStatement::setTimestamp(sal_Int32, const DateTime&) throw(SQLException, RuntimeException)
295 {
296 
297 
298 
299 ::dbtools::throwFunctionNotSupportedException("setTimestamp", NULL);
300 }
301 // -------------------------------------------------------------------------
setBinaryStream(sal_Int32,const Reference<::com::sun::star::io::XInputStream> &,sal_Int32)302 void SAL_CALL KabPreparedStatement::setBinaryStream(sal_Int32, const Reference< ::com::sun::star::io::XInputStream >&, sal_Int32) throw(SQLException, RuntimeException)
303 {
304 
305 
306 
307 ::dbtools::throwFunctionNotSupportedException("setBinaryStream", NULL);
308 }
309 // -------------------------------------------------------------------------
setCharacterStream(sal_Int32,const Reference<::com::sun::star::io::XInputStream> &,sal_Int32)310 void SAL_CALL KabPreparedStatement::setCharacterStream(sal_Int32, const Reference< ::com::sun::star::io::XInputStream >&, sal_Int32) throw(SQLException, RuntimeException)
311 {
312 
313 
314 
315 ::dbtools::throwFunctionNotSupportedException("setCharacterStream", NULL);
316 }
317 // -------------------------------------------------------------------------
setObject(sal_Int32 parameterIndex,const Any & x)318 void SAL_CALL KabPreparedStatement::setObject(sal_Int32 parameterIndex, const Any& x) throw(SQLException, RuntimeException)
319 {
320     if(!::dbtools::implSetObject(this,parameterIndex,x))
321 	{
322 		throw SQLException();
323 	}
324 }
325 // -------------------------------------------------------------------------
setObjectWithInfo(sal_Int32,const Any &,sal_Int32,sal_Int32)326 void SAL_CALL KabPreparedStatement::setObjectWithInfo(sal_Int32, const Any&, sal_Int32, sal_Int32) throw(SQLException, RuntimeException)
327 {
328 
329 
330 
331 ::dbtools::throwFunctionNotSupportedException("setObjectWithInfo", NULL);
332 }
333 // -------------------------------------------------------------------------
setRef(sal_Int32,const Reference<XRef> &)334 void SAL_CALL KabPreparedStatement::setRef(sal_Int32, const Reference< XRef >&) throw(SQLException, RuntimeException)
335 {
336 
337 
338 
339 ::dbtools::throwFunctionNotSupportedException("setRef", NULL);
340 }
341 // -------------------------------------------------------------------------
setBlob(sal_Int32,const Reference<XBlob> &)342 void SAL_CALL KabPreparedStatement::setBlob(sal_Int32, const Reference< XBlob >&) throw(SQLException, RuntimeException)
343 {
344 
345 
346 
347 ::dbtools::throwFunctionNotSupportedException("setBlob", NULL);
348 }
349 // -------------------------------------------------------------------------
setClob(sal_Int32,const Reference<XClob> &)350 void SAL_CALL KabPreparedStatement::setClob(sal_Int32, const Reference< XClob >&) throw(SQLException, RuntimeException)
351 {
352 
353 
354 
355 ::dbtools::throwFunctionNotSupportedException("setClob", NULL);
356 }
357 // -------------------------------------------------------------------------
setArray(sal_Int32,const Reference<XArray> &)358 void SAL_CALL KabPreparedStatement::setArray(sal_Int32, const Reference< XArray >&) throw(SQLException, RuntimeException)
359 {
360 
361 
362 
363 ::dbtools::throwFunctionNotSupportedException("setArray", NULL);
364 }
365 // -------------------------------------------------------------------------
clearParameters()366 void SAL_CALL KabPreparedStatement::clearParameters() throw(SQLException, RuntimeException)
367 {
368 ::dbtools::throwFunctionNotSupportedException("clearParameters", NULL);
369 }
370 // -------------------------------------------------------------------------
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any & rValue)371 void KabPreparedStatement::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any& rValue) throw (Exception)
372 {
373 	switch (nHandle)
374 	{
375 		case PROPERTY_ID_RESULTSETCONCURRENCY:
376 			break;
377 		case PROPERTY_ID_RESULTSETTYPE:
378 			break;
379 		case PROPERTY_ID_FETCHDIRECTION:
380 			break;
381 		case PROPERTY_ID_USEBOOKMARKS:
382 			break;
383 		default:
384 			KabCommonStatement::setFastPropertyValue_NoBroadcast(nHandle,rValue);
385 	}
386 }
387