1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include <string.h>
29cdf0e10cSrcweir #include <osl/diagnose.h>
30cdf0e10cSrcweir #include "odbc/OPreparedStatement.hxx"
31cdf0e10cSrcweir #include "odbc/OBoundParam.hxx"
32cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp>
33cdf0e10cSrcweir #include "odbc/OTools.hxx"
34cdf0e10cSrcweir #include "odbc/ODriver.hxx"
35cdf0e10cSrcweir #include "odbc/OResultSet.hxx"
36cdf0e10cSrcweir #include "odbc/OResultSetMetaData.hxx"
37cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
38cdf0e10cSrcweir #include <comphelper/sequence.hxx>
39cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
40cdf0e10cSrcweir #include "connectivity/dbtools.hxx"
41cdf0e10cSrcweir #include <comphelper/types.hxx>
42cdf0e10cSrcweir #include "connectivity/FValue.hxx"
43cdf0e10cSrcweir #include "resource/common_res.hrc"
44cdf0e10cSrcweir #include "connectivity/sqlparse.hxx"
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace ::comphelper;
47cdf0e10cSrcweir using namespace connectivity;
48cdf0e10cSrcweir using namespace connectivity::odbc;
49cdf0e10cSrcweir using namespace com::sun::star::uno;
50cdf0e10cSrcweir using namespace com::sun::star::lang;
51cdf0e10cSrcweir using namespace com::sun::star::beans;
52cdf0e10cSrcweir using namespace com::sun::star::sdbc;
53cdf0e10cSrcweir using namespace com::sun::star::sdbcx;
54cdf0e10cSrcweir using namespace com::sun::star::container;
55cdf0e10cSrcweir using namespace com::sun::star::io;
56cdf0e10cSrcweir using namespace com::sun::star::util;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir IMPLEMENT_SERVICE_INFO(OPreparedStatement,"com.sun.star.sdbcx.OPreparedStatement","com.sun.star.sdbc.PreparedStatement");
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 
OPreparedStatement(OConnection * _pConnection,const::rtl::OUString & sql)61cdf0e10cSrcweir OPreparedStatement::OPreparedStatement( OConnection* _pConnection,const ::rtl::OUString& sql)
62cdf0e10cSrcweir 	:OStatement_BASE2(_pConnection)
63cdf0e10cSrcweir     ,numParams(0)
64cdf0e10cSrcweir 	,boundParams(NULL)
65cdf0e10cSrcweir 	,m_bPrepared(sal_False)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir 	m_sSqlStatement = sql;
68cdf0e10cSrcweir 	try
69cdf0e10cSrcweir 	{
70cdf0e10cSrcweir 		if(_pConnection->isParameterSubstitutionEnabled())
71cdf0e10cSrcweir 		{
72cdf0e10cSrcweir 			OSQLParser aParser(_pConnection->getDriver()->getORB());
73cdf0e10cSrcweir 			::rtl::OUString sErrorMessage;
74cdf0e10cSrcweir 			::rtl::OUString sNewSql;
75cdf0e10cSrcweir             ::std::auto_ptr<OSQLParseNode> pNode( aParser.parseTree(sErrorMessage,sql) );
76cdf0e10cSrcweir 			if ( pNode.get() )
77cdf0e10cSrcweir 			{	// special handling for parameters
78cdf0e10cSrcweir 				OSQLParseNode::substituteParameterNames(pNode.get());
79cdf0e10cSrcweir 				pNode->parseNodeToStr( sNewSql, _pConnection );
80cdf0e10cSrcweir 				m_sSqlStatement = sNewSql;
81cdf0e10cSrcweir 			}
82cdf0e10cSrcweir 		}
83cdf0e10cSrcweir 	}
84cdf0e10cSrcweir 	catch(Exception&)
85cdf0e10cSrcweir 	{
86cdf0e10cSrcweir 	}
87cdf0e10cSrcweir }
88cdf0e10cSrcweir // -----------------------------------------------------------------------------
acquire()89cdf0e10cSrcweir void SAL_CALL OPreparedStatement::acquire() throw()
90cdf0e10cSrcweir {
91cdf0e10cSrcweir 	OStatement_BASE2::acquire();
92cdf0e10cSrcweir }
93cdf0e10cSrcweir // -----------------------------------------------------------------------------
release()94cdf0e10cSrcweir void SAL_CALL OPreparedStatement::release() throw()
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	OStatement_BASE2::release();
97cdf0e10cSrcweir }
98cdf0e10cSrcweir // -----------------------------------------------------------------------------
queryInterface(const Type & rType)99cdf0e10cSrcweir Any SAL_CALL OPreparedStatement::queryInterface( const Type & rType ) throw(RuntimeException)
100cdf0e10cSrcweir {
101cdf0e10cSrcweir 	Any aRet = OStatement_BASE2::queryInterface(rType);
102cdf0e10cSrcweir 	return aRet.hasValue() ? aRet : OPreparedStatement_BASE::queryInterface(rType);
103cdf0e10cSrcweir }
104cdf0e10cSrcweir // -------------------------------------------------------------------------
getTypes()105cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL OPreparedStatement::getTypes(  ) throw(::com::sun::star::uno::RuntimeException)
106cdf0e10cSrcweir {
107cdf0e10cSrcweir 	return ::comphelper::concatSequences(OPreparedStatement_BASE::getTypes(),OStatement_BASE2::getTypes());
108cdf0e10cSrcweir }
109cdf0e10cSrcweir // -------------------------------------------------------------------------
110cdf0e10cSrcweir 
getMetaData()111cdf0e10cSrcweir Reference< XResultSetMetaData > SAL_CALL OPreparedStatement::getMetaData(  ) throw(SQLException, RuntimeException)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
114cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	prepareStatement();
118cdf0e10cSrcweir 	OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
119cdf0e10cSrcweir 	if(!m_xMetaData.is())
120cdf0e10cSrcweir 		m_xMetaData = new OResultSetMetaData(getOwnConnection(),m_aStatementHandle);
121cdf0e10cSrcweir 	return m_xMetaData;
122cdf0e10cSrcweir }
123cdf0e10cSrcweir // -------------------------------------------------------------------------
124cdf0e10cSrcweir 
close()125cdf0e10cSrcweir void SAL_CALL OPreparedStatement::close(  ) throw(SQLException, RuntimeException)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
128cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	// Close/clear our result set
132cdf0e10cSrcweir 	clearMyResultSet ();
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	// Reset last warning message
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	try {
137cdf0e10cSrcweir 		clearWarnings ();
138cdf0e10cSrcweir 		OStatement_BASE2::close();
139cdf0e10cSrcweir 		FreeParams();
140cdf0e10cSrcweir 	}
141cdf0e10cSrcweir 	catch (SQLException &) {
142cdf0e10cSrcweir 		// If we get an error, ignore
143cdf0e10cSrcweir 	}
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 	// Remove this Statement object from the Connection object's
146cdf0e10cSrcweir 	// list
147cdf0e10cSrcweir }
148cdf0e10cSrcweir // -------------------------------------------------------------------------
149cdf0e10cSrcweir 
execute()150cdf0e10cSrcweir sal_Bool SAL_CALL OPreparedStatement::execute(  ) throw(SQLException, RuntimeException)
151cdf0e10cSrcweir {
152cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
153cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	sal_Bool needData = sal_False;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 	// Reset warnings
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	clearWarnings ();
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	// Reset the statement handle, warning and saved Resultset
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	reset();
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 	// Call SQLExecute
167cdf0e10cSrcweir 	prepareStatement();
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
170cdf0e10cSrcweir 	try
171cdf0e10cSrcweir 	{
172cdf0e10cSrcweir 		SQLRETURN nReturn = N3SQLExecute(m_aStatementHandle);
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 		OTools::ThrowException(m_pConnection,nReturn,m_aStatementHandle,SQL_HANDLE_STMT,*this);
175cdf0e10cSrcweir 		needData = nReturn == SQL_NEED_DATA;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 		// Now loop while more data is needed (i.e. a data-at-
178cdf0e10cSrcweir 		// execution parameter was given).  For each parameter
179cdf0e10cSrcweir 		// that needs data, put the data from the input stream.
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 		while (needData) {
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 			// Get the parameter number that requires data
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 			sal_Int32* paramIndex = 0;
186cdf0e10cSrcweir 			nReturn = N3SQLParamData(m_aStatementHandle,(SQLPOINTER*)&paramIndex);
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 			// If the parameter index is -1, there is no
189cdf0e10cSrcweir 			// more data required
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 			if ( !paramIndex || ( *paramIndex == -1 ) )
192cdf0e10cSrcweir 				needData = sal_False;
193cdf0e10cSrcweir 			else
194cdf0e10cSrcweir 			{
195cdf0e10cSrcweir 				// Now we have the proper parameter
196cdf0e10cSrcweir 				// index, get the data from the input
197cdf0e10cSrcweir 				// stream and do a SQLPutData
198cdf0e10cSrcweir 				putParamData (*paramIndex);
199cdf0e10cSrcweir 			}
200cdf0e10cSrcweir 		}
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 	}
203cdf0e10cSrcweir 	catch (const SQLWarning&)
204cdf0e10cSrcweir 	{
205cdf0e10cSrcweir 	}
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 	// Now loop while more data is needed (i.e. a data-at-
208cdf0e10cSrcweir 	// execution parameter was given).  For each parameter
209cdf0e10cSrcweir 	// that needs data, put the data from the input stream.
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 	while (needData) {
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 		// Get the parameter number that requires data
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 		sal_Int32* paramIndex = 0;
216cdf0e10cSrcweir 		N3SQLParamData (m_aStatementHandle,(SQLPOINTER*)&paramIndex);
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 		// If the parameter index is -1, there is no more
219cdf0e10cSrcweir 		// data required
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 		if (*paramIndex == -1) {
222cdf0e10cSrcweir 			needData = sal_False;
223cdf0e10cSrcweir 		}
224cdf0e10cSrcweir 		else {
225cdf0e10cSrcweir 			// Now we have the proper parameter index,
226cdf0e10cSrcweir 			// get the data from the input stream
227cdf0e10cSrcweir 			// and do a SQLPutData
228cdf0e10cSrcweir 			putParamData(*paramIndex);
229cdf0e10cSrcweir 		}
230cdf0e10cSrcweir 	}
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 	// Now determine if there is a result set associated with
233cdf0e10cSrcweir 	// the SQL statement that was executed.  Get the column
234cdf0e10cSrcweir 	// count, and if it is not zero, there is a result set.
235cdf0e10cSrcweir 
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 	return getColumnCount() > 0;
238cdf0e10cSrcweir }
239cdf0e10cSrcweir // -------------------------------------------------------------------------
240cdf0e10cSrcweir 
executeUpdate()241cdf0e10cSrcweir sal_Int32 SAL_CALL OPreparedStatement::executeUpdate(  ) throw(SQLException, RuntimeException)
242cdf0e10cSrcweir {
243cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
244cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	sal_Int32 numRows = -1;
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 	prepareStatement();
249cdf0e10cSrcweir 	// Execute the statement.  If execute returns sal_False, a
250cdf0e10cSrcweir 	// row count exists.
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 	if (!execute())
253cdf0e10cSrcweir 		numRows = getUpdateCount ();
254cdf0e10cSrcweir 	else
255cdf0e10cSrcweir     {
256cdf0e10cSrcweir 		// No update count was produced (a ResultSet was).  Raise
257cdf0e10cSrcweir 		// an exception
258cdf0e10cSrcweir 		m_pConnection->throwGenericSQLException(STR_NO_ROWCOUNT,*this);
259cdf0e10cSrcweir 	}
260cdf0e10cSrcweir 	return numRows;
261cdf0e10cSrcweir }
262cdf0e10cSrcweir // -------------------------------------------------------------------------
263cdf0e10cSrcweir 
setString(sal_Int32 parameterIndex,const::rtl::OUString & x)264cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setString( sal_Int32 parameterIndex, const ::rtl::OUString& x ) throw(SQLException, RuntimeException)
265cdf0e10cSrcweir {
266cdf0e10cSrcweir 	::rtl::OString aString(::rtl::OUStringToOString(x,getOwnConnection()->getTextEncoding()));
267cdf0e10cSrcweir 	setParameter(parameterIndex,DataType::CHAR,aString.getLength(),(void*)&x);
268cdf0e10cSrcweir }
269cdf0e10cSrcweir // -------------------------------------------------------------------------
270cdf0e10cSrcweir 
getConnection()271cdf0e10cSrcweir Reference< XConnection > SAL_CALL OPreparedStatement::getConnection(  ) throw(SQLException, RuntimeException)
272cdf0e10cSrcweir {
273cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
274cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 	return (Reference< XConnection >)m_pConnection;
277cdf0e10cSrcweir }
278cdf0e10cSrcweir // -------------------------------------------------------------------------
279cdf0e10cSrcweir 
executeQuery()280cdf0e10cSrcweir Reference< XResultSet > SAL_CALL OPreparedStatement::executeQuery(  ) throw(SQLException, RuntimeException)
281cdf0e10cSrcweir {
282cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
283cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 	Reference< XResultSet > rs = NULL;
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 	prepareStatement();
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 	if (execute())
290cdf0e10cSrcweir 		rs = getResultSet(sal_False);
291cdf0e10cSrcweir 	else
292cdf0e10cSrcweir     {
293cdf0e10cSrcweir 		// No ResultSet was produced.  Raise an exception
294cdf0e10cSrcweir         m_pConnection->throwGenericSQLException(STR_NO_RESULTSET,*this);
295cdf0e10cSrcweir 	}
296cdf0e10cSrcweir 	return rs;
297cdf0e10cSrcweir }
298cdf0e10cSrcweir // -------------------------------------------------------------------------
299cdf0e10cSrcweir 
setBoolean(sal_Int32 parameterIndex,sal_Bool x)300cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x ) throw(SQLException, RuntimeException)
301cdf0e10cSrcweir {
302cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
303cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
304cdf0e10cSrcweir 
305cdf0e10cSrcweir 
306cdf0e10cSrcweir 	sal_Int32 value = 0;
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 	// If the parameter is sal_True, set the value to 1
309cdf0e10cSrcweir 	if (x) {
310cdf0e10cSrcweir 		value = 1;
311cdf0e10cSrcweir 	}
312cdf0e10cSrcweir 
313cdf0e10cSrcweir 	// Set the parameter as if it were an integer
314cdf0e10cSrcweir 	setInt (parameterIndex, value);
315cdf0e10cSrcweir }
316cdf0e10cSrcweir // -------------------------------------------------------------------------
setParameter(sal_Int32 parameterIndex,sal_Int32 _nType,sal_Int32 _nSize,void * _pData)317cdf0e10cSrcweir void OPreparedStatement::setParameter(sal_Int32 parameterIndex,sal_Int32 _nType,sal_Int32 _nSize,void* _pData)
318cdf0e10cSrcweir {
319cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
320cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 	prepareStatement();
323cdf0e10cSrcweir 	// Allocate a buffer to be used in binding.  This will be
324cdf0e10cSrcweir 		// a 'permanent' buffer that the bridge will fill in with
325cdf0e10cSrcweir 		// the bound data in native format.
326cdf0e10cSrcweir 
327cdf0e10cSrcweir 
328cdf0e10cSrcweir 	checkParameterIndex(parameterIndex);
329cdf0e10cSrcweir 	sal_Int32 nRealSize = _nSize;
330cdf0e10cSrcweir 	SQLSMALLINT fSqlType = static_cast<SQLSMALLINT>(OTools::jdbcTypeToOdbc(_nType));
331cdf0e10cSrcweir 	switch(fSqlType)
332cdf0e10cSrcweir 	{
333cdf0e10cSrcweir 		case SQL_CHAR:
334cdf0e10cSrcweir 		case SQL_VARCHAR:
335cdf0e10cSrcweir 		case SQL_DECIMAL:
336cdf0e10cSrcweir 		case SQL_NUMERIC:
337cdf0e10cSrcweir 			++nRealSize;
338cdf0e10cSrcweir 			break;
339cdf0e10cSrcweir 		case SQL_BINARY:
340cdf0e10cSrcweir 		case SQL_VARBINARY:
341cdf0e10cSrcweir 			nRealSize=1;    //dummy buffer, binary data isn't copied
342cdf0e10cSrcweir 			break;
343cdf0e10cSrcweir 		default:
344cdf0e10cSrcweir 			break;
345cdf0e10cSrcweir 	}
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 	sal_Int8* bindBuf = allocBindBuf(parameterIndex, nRealSize);
348cdf0e10cSrcweir 
349cdf0e10cSrcweir 	OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
350cdf0e10cSrcweir 	OTools::bindParameter(	m_pConnection,
351cdf0e10cSrcweir 							m_aStatementHandle,
352cdf0e10cSrcweir 							parameterIndex,
353cdf0e10cSrcweir 							bindBuf,
354cdf0e10cSrcweir 							getLengthBuf(parameterIndex),
355cdf0e10cSrcweir 							fSqlType,
356cdf0e10cSrcweir 							sal_False,
357cdf0e10cSrcweir 							m_pConnection->useOldDateFormat(),
358cdf0e10cSrcweir 							_pData,
359cdf0e10cSrcweir 							(Reference <XInterface>)*this,
360cdf0e10cSrcweir 							getOwnConnection()->getTextEncoding());
361cdf0e10cSrcweir }
362cdf0e10cSrcweir // -----------------------------------------------------------------------------
setByte(sal_Int32 parameterIndex,sal_Int8 x)363cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setByte( sal_Int32 parameterIndex, sal_Int8 x ) throw(SQLException, RuntimeException)
364cdf0e10cSrcweir {
365cdf0e10cSrcweir 	setParameter(parameterIndex,DataType::TINYINT,sizeof(sal_Int8),&x);
366cdf0e10cSrcweir }
367cdf0e10cSrcweir // -------------------------------------------------------------------------
368cdf0e10cSrcweir 
setDate(sal_Int32 parameterIndex,const Date & aData)369cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setDate( sal_Int32 parameterIndex, const Date& aData ) throw(SQLException, RuntimeException)
370cdf0e10cSrcweir {
371cdf0e10cSrcweir 	DATE_STRUCT x = OTools::DateToOdbcDate(aData);
372cdf0e10cSrcweir 	setParameter(parameterIndex,DataType::DATE,sizeof(DATE_STRUCT),&x);
373cdf0e10cSrcweir }
374cdf0e10cSrcweir // -------------------------------------------------------------------------
375cdf0e10cSrcweir 
376cdf0e10cSrcweir 
setTime(sal_Int32 parameterIndex,const Time & aVal)377cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setTime( sal_Int32 parameterIndex, const Time& aVal ) throw(SQLException, RuntimeException)
378cdf0e10cSrcweir {
379cdf0e10cSrcweir 	TIME_STRUCT x = OTools::TimeToOdbcTime(aVal);
380cdf0e10cSrcweir 	setParameter(parameterIndex,DataType::TIME,sizeof(TIME_STRUCT),&x);
381cdf0e10cSrcweir }
382cdf0e10cSrcweir // -------------------------------------------------------------------------
383cdf0e10cSrcweir 
setTimestamp(sal_Int32 parameterIndex,const DateTime & aVal)384cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setTimestamp( sal_Int32 parameterIndex, const DateTime& aVal ) throw(SQLException, RuntimeException)
385cdf0e10cSrcweir {
386cdf0e10cSrcweir 	TIMESTAMP_STRUCT x = OTools::DateTimeToTimestamp(aVal);
387cdf0e10cSrcweir 	setParameter(parameterIndex,DataType::TIMESTAMP,sizeof(TIMESTAMP_STRUCT),&x);
388cdf0e10cSrcweir }
389cdf0e10cSrcweir // -------------------------------------------------------------------------
390cdf0e10cSrcweir 
setDouble(sal_Int32 parameterIndex,double x)391cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setDouble( sal_Int32 parameterIndex, double x ) throw(SQLException, RuntimeException)
392cdf0e10cSrcweir {
393cdf0e10cSrcweir 	setParameter(parameterIndex,DataType::DOUBLE,sizeof(double),&x);
394cdf0e10cSrcweir }
395cdf0e10cSrcweir 
396cdf0e10cSrcweir // -------------------------------------------------------------------------
397cdf0e10cSrcweir 
setFloat(sal_Int32 parameterIndex,float x)398cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setFloat( sal_Int32 parameterIndex, float x ) throw(SQLException, RuntimeException)
399cdf0e10cSrcweir {
400cdf0e10cSrcweir 	setParameter(parameterIndex,DataType::FLOAT,sizeof(float),&x);
401cdf0e10cSrcweir }
402cdf0e10cSrcweir // -------------------------------------------------------------------------
403cdf0e10cSrcweir 
setInt(sal_Int32 parameterIndex,sal_Int32 x)404cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x ) throw(SQLException, RuntimeException)
405cdf0e10cSrcweir {
406cdf0e10cSrcweir 	setParameter(parameterIndex,DataType::INTEGER,sizeof(sal_Int32),&x);
407cdf0e10cSrcweir }
408cdf0e10cSrcweir // -------------------------------------------------------------------------
409cdf0e10cSrcweir 
setLong(sal_Int32 parameterIndex,sal_Int64 x)410cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setLong( sal_Int32 parameterIndex, sal_Int64 x ) throw(SQLException, RuntimeException)
411cdf0e10cSrcweir {
412cdf0e10cSrcweir 	try
413cdf0e10cSrcweir 	{
414cdf0e10cSrcweir 		setParameter(parameterIndex,DataType::BIGINT,sizeof(sal_Int64),&x);
415cdf0e10cSrcweir 	}
416cdf0e10cSrcweir 	catch(SQLException&)
417cdf0e10cSrcweir 	{
418cdf0e10cSrcweir 		setString(parameterIndex,ORowSetValue(x));
419cdf0e10cSrcweir 	}
420cdf0e10cSrcweir }
421cdf0e10cSrcweir // -------------------------------------------------------------------------
422cdf0e10cSrcweir 
setNull(sal_Int32 parameterIndex,sal_Int32 sqlType)423cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setNull( sal_Int32 parameterIndex, sal_Int32 sqlType ) throw(SQLException, RuntimeException)
424cdf0e10cSrcweir {
425cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
426cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
427cdf0e10cSrcweir 
428cdf0e10cSrcweir 
429cdf0e10cSrcweir 	prepareStatement();
430cdf0e10cSrcweir 	// Get the buffer needed for the length
431cdf0e10cSrcweir 	checkParameterIndex(parameterIndex);
432cdf0e10cSrcweir 
433cdf0e10cSrcweir 	sal_Int8* lenBuf = getLengthBuf (parameterIndex);
434cdf0e10cSrcweir 	*(SQLLEN*)lenBuf = SQL_NULL_DATA;
435cdf0e10cSrcweir 
436cdf0e10cSrcweir 
437cdf0e10cSrcweir 	SQLLEN prec = 0;
438cdf0e10cSrcweir 	SQLULEN nColumnSize = 0;
439cdf0e10cSrcweir 	if (sqlType == SQL_CHAR || sqlType == SQL_VARCHAR || sqlType == SQL_LONGVARCHAR)
440cdf0e10cSrcweir 	{
441cdf0e10cSrcweir 		prec = 1;
442cdf0e10cSrcweir 		nColumnSize = 1;
443cdf0e10cSrcweir 	}
444cdf0e10cSrcweir 
445cdf0e10cSrcweir 	SQLSMALLINT fCType = 0;
446cdf0e10cSrcweir 	SQLSMALLINT fSqlType = 0;
447cdf0e10cSrcweir 
448cdf0e10cSrcweir 	SQLSMALLINT nDecimalDigits = 0;
449cdf0e10cSrcweir 	OTools::getBindTypes(	sal_False,
450cdf0e10cSrcweir 							m_pConnection->useOldDateFormat(),
451cdf0e10cSrcweir                             (SQLSMALLINT)sqlType,
452cdf0e10cSrcweir 							fCType,
453cdf0e10cSrcweir 							fSqlType);
454cdf0e10cSrcweir 
455cdf0e10cSrcweir 	SQLRETURN nReturn = N3SQLBindParameter(	m_aStatementHandle,
456cdf0e10cSrcweir 											(SQLUSMALLINT)parameterIndex,
457cdf0e10cSrcweir 											(SQLSMALLINT)SQL_PARAM_INPUT,
458cdf0e10cSrcweir 											fCType,
459cdf0e10cSrcweir 											fSqlType,
460cdf0e10cSrcweir 											nColumnSize,
461cdf0e10cSrcweir 											nDecimalDigits,
462cdf0e10cSrcweir 											NULL,
463cdf0e10cSrcweir 											prec,
464cdf0e10cSrcweir 											(SQLLEN*)lenBuf
465cdf0e10cSrcweir 											);
466cdf0e10cSrcweir 	OTools::ThrowException(m_pConnection,nReturn,m_aStatementHandle,SQL_HANDLE_STMT,*this);
467cdf0e10cSrcweir }
468cdf0e10cSrcweir // -------------------------------------------------------------------------
469cdf0e10cSrcweir 
setClob(sal_Int32 parameterIndex,const Reference<XClob> & x)470cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setClob( sal_Int32 parameterIndex, const Reference< XClob >& x ) throw(SQLException, RuntimeException)
471cdf0e10cSrcweir {
472cdf0e10cSrcweir 	if ( x.is() )
473cdf0e10cSrcweir 		setStream(parameterIndex, x->getCharacterStream(), (SQLLEN)x->length(), DataType::LONGVARCHAR);
474cdf0e10cSrcweir }
475cdf0e10cSrcweir // -------------------------------------------------------------------------
476cdf0e10cSrcweir 
setBlob(sal_Int32 parameterIndex,const Reference<XBlob> & x)477cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setBlob( sal_Int32 parameterIndex, const Reference< XBlob >& x ) throw(SQLException, RuntimeException)
478cdf0e10cSrcweir {
479cdf0e10cSrcweir     if ( x.is() )
480cdf0e10cSrcweir 		setStream(parameterIndex, x->getBinaryStream(), (SQLLEN)x->length(), DataType::LONGVARCHAR);
481cdf0e10cSrcweir }
482cdf0e10cSrcweir // -------------------------------------------------------------------------
483cdf0e10cSrcweir 
setArray(sal_Int32,const Reference<XArray> &)484cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setArray( sal_Int32 /*parameterIndex*/, const Reference< XArray >& /*x*/ ) throw(SQLException, RuntimeException)
485cdf0e10cSrcweir {
486cdf0e10cSrcweir     ::dbtools::throwFunctionNotSupportedException( "XParameters::setArray", *this );
487cdf0e10cSrcweir }
488cdf0e10cSrcweir // -------------------------------------------------------------------------
489cdf0e10cSrcweir 
setRef(sal_Int32,const Reference<XRef> &)490cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setRef( sal_Int32 /*parameterIndex*/, const Reference< XRef >& /*x*/ ) throw(SQLException, RuntimeException)
491cdf0e10cSrcweir {
492cdf0e10cSrcweir     ::dbtools::throwFunctionNotSupportedException( "XParameters::setRef", *this );
493cdf0e10cSrcweir }
494cdf0e10cSrcweir // -------------------------------------------------------------------------
setDecimal(sal_Int32 parameterIndex,const::rtl::OUString & x)495cdf0e10cSrcweir void OPreparedStatement::setDecimal( sal_Int32 parameterIndex, const ::rtl::OUString& x )
496cdf0e10cSrcweir {
497cdf0e10cSrcweir 	::rtl::OString aString(::rtl::OUStringToOString(x,getOwnConnection()->getTextEncoding()));
498cdf0e10cSrcweir 	setParameter(parameterIndex,DataType::DECIMAL,aString.getLength(),(void*)&x);
499cdf0e10cSrcweir }
500cdf0e10cSrcweir // -------------------------------------------------------------------------
setObjectWithInfo(sal_Int32 parameterIndex,const Any & x,sal_Int32 sqlType,sal_Int32 scale)501cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const Any& x, sal_Int32 sqlType, sal_Int32 scale ) throw(SQLException, RuntimeException)
502cdf0e10cSrcweir {
503cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
504cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
505cdf0e10cSrcweir 
506cdf0e10cSrcweir 	prepareStatement();
507cdf0e10cSrcweir 	// For each known SQL Type, call the appropriate
508cdf0e10cSrcweir 		// set routine
509cdf0e10cSrcweir 
510cdf0e10cSrcweir 	switch (sqlType)
511cdf0e10cSrcweir 	{
512cdf0e10cSrcweir 		case DataType::VARCHAR:
513cdf0e10cSrcweir 		case DataType::LONGVARCHAR:
514cdf0e10cSrcweir 			if(x.hasValue())
515cdf0e10cSrcweir 			{
516cdf0e10cSrcweir 				::rtl::OUString sStr;
517cdf0e10cSrcweir 				x >>= sStr;
518cdf0e10cSrcweir 				::rtl::OString aString(::rtl::OUStringToOString(sStr,getOwnConnection()->getTextEncoding()));
519cdf0e10cSrcweir 				setParameter(parameterIndex,sqlType,aString.getLength(),&aString);
520cdf0e10cSrcweir 			}
521cdf0e10cSrcweir 			else
522cdf0e10cSrcweir 				setNull(parameterIndex,sqlType);
523cdf0e10cSrcweir 			break;
524cdf0e10cSrcweir         case DataType::DECIMAL:
525cdf0e10cSrcweir             {
526cdf0e10cSrcweir                 ORowSetValue aValue;
527cdf0e10cSrcweir                 aValue.fill(x);
528cdf0e10cSrcweir                 setDecimal(parameterIndex,aValue);
529cdf0e10cSrcweir             }
530cdf0e10cSrcweir             break;
531cdf0e10cSrcweir         case DataType::NUMERIC:
532cdf0e10cSrcweir             {
533cdf0e10cSrcweir                 ORowSetValue aValue;
534cdf0e10cSrcweir                 aValue.fill(x);
535cdf0e10cSrcweir                 setString(parameterIndex,aValue);
536cdf0e10cSrcweir             }
537cdf0e10cSrcweir             break;
538cdf0e10cSrcweir 		default:
539cdf0e10cSrcweir 			::dbtools::setObjectWithInfo(this,parameterIndex,x,sqlType,scale);
540cdf0e10cSrcweir 		}
541cdf0e10cSrcweir }
542cdf0e10cSrcweir // -------------------------------------------------------------------------
543cdf0e10cSrcweir 
setObjectNull(sal_Int32 parameterIndex,sal_Int32 sqlType,const::rtl::OUString &)544cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 sqlType, const ::rtl::OUString& /*typeName*/ ) throw(SQLException, RuntimeException)
545cdf0e10cSrcweir {
546cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
547cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 	setNull(parameterIndex,sqlType);
550cdf0e10cSrcweir }
551cdf0e10cSrcweir // -------------------------------------------------------------------------
552cdf0e10cSrcweir 
setObject(sal_Int32 parameterIndex,const Any & x)553cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setObject( sal_Int32 parameterIndex, const Any& x ) throw(SQLException, RuntimeException)
554cdf0e10cSrcweir {
555cdf0e10cSrcweir 	if (!::dbtools::implSetObject(this, parameterIndex, x))
556cdf0e10cSrcweir 	{	// there is no other setXXX call which can handle the value in x
557cdf0e10cSrcweir 		throw SQLException();
558cdf0e10cSrcweir 	}
559cdf0e10cSrcweir }
560cdf0e10cSrcweir // -------------------------------------------------------------------------
561cdf0e10cSrcweir 
setShort(sal_Int32 parameterIndex,sal_Int16 x)562cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x ) throw(SQLException, RuntimeException)
563cdf0e10cSrcweir {
564cdf0e10cSrcweir 	setParameter(parameterIndex,DataType::SMALLINT,sizeof(sal_Int16),&x);
565cdf0e10cSrcweir }
566cdf0e10cSrcweir // -------------------------------------------------------------------------
567cdf0e10cSrcweir 
setBytes(sal_Int32 parameterIndex,const Sequence<sal_Int8> & x)568cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setBytes( sal_Int32 parameterIndex, const Sequence< sal_Int8 >& x ) throw(SQLException, RuntimeException)
569cdf0e10cSrcweir {
570cdf0e10cSrcweir 	setParameter(parameterIndex,DataType::BINARY,x.getLength(),(void*)&x);
571cdf0e10cSrcweir 	boundParams[parameterIndex-1].setSequence(x); // this assures that the sequence stays alive
572cdf0e10cSrcweir }
573cdf0e10cSrcweir // -------------------------------------------------------------------------
574cdf0e10cSrcweir 
575cdf0e10cSrcweir 
setCharacterStream(sal_Int32 parameterIndex,const Reference<::com::sun::star::io::XInputStream> & x,sal_Int32 length)576cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException)
577cdf0e10cSrcweir {
578cdf0e10cSrcweir 	setStream(parameterIndex, x, length, DataType::LONGVARCHAR);
579cdf0e10cSrcweir }
580cdf0e10cSrcweir // -------------------------------------------------------------------------
581cdf0e10cSrcweir 
setBinaryStream(sal_Int32 parameterIndex,const Reference<::com::sun::star::io::XInputStream> & x,sal_Int32 length)582cdf0e10cSrcweir void SAL_CALL OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException)
583cdf0e10cSrcweir {
584cdf0e10cSrcweir 	setStream(parameterIndex, x, length, DataType::LONGVARBINARY);
585cdf0e10cSrcweir }
586cdf0e10cSrcweir // -------------------------------------------------------------------------
587cdf0e10cSrcweir 
clearParameters()588cdf0e10cSrcweir void SAL_CALL OPreparedStatement::clearParameters(  ) throw(SQLException, RuntimeException)
589cdf0e10cSrcweir {
590cdf0e10cSrcweir 	prepareStatement();
591cdf0e10cSrcweir 	OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
592cdf0e10cSrcweir 	SQLRETURN nRet = N3SQLFreeStmt (m_aStatementHandle, SQL_RESET_PARAMS);
593cdf0e10cSrcweir 	nRet = N3SQLFreeStmt (m_aStatementHandle, SQL_UNBIND);
594cdf0e10cSrcweir }
595cdf0e10cSrcweir // -------------------------------------------------------------------------
clearBatch()596cdf0e10cSrcweir void SAL_CALL OPreparedStatement::clearBatch(  ) throw(SQLException, RuntimeException)
597cdf0e10cSrcweir {
598cdf0e10cSrcweir 	//	clearParameters(  );
599cdf0e10cSrcweir 	//	m_aBatchList.erase();
600cdf0e10cSrcweir }
601cdf0e10cSrcweir // -------------------------------------------------------------------------
602cdf0e10cSrcweir 
addBatch()603cdf0e10cSrcweir void SAL_CALL OPreparedStatement::addBatch( ) throw(SQLException, RuntimeException)
604cdf0e10cSrcweir {
605cdf0e10cSrcweir }
606cdf0e10cSrcweir // -------------------------------------------------------------------------
607cdf0e10cSrcweir 
executeBatch()608cdf0e10cSrcweir Sequence< sal_Int32 > SAL_CALL OPreparedStatement::executeBatch(  ) throw(SQLException, RuntimeException)
609cdf0e10cSrcweir {
610cdf0e10cSrcweir 	return Sequence< sal_Int32 > ();
611cdf0e10cSrcweir }
612cdf0e10cSrcweir // -------------------------------------------------------------------------
613cdf0e10cSrcweir 
614cdf0e10cSrcweir //====================================================================
615cdf0e10cSrcweir // methods
616cdf0e10cSrcweir //====================================================================
617cdf0e10cSrcweir 
618cdf0e10cSrcweir //--------------------------------------------------------------------
619cdf0e10cSrcweir // initBoundParam
620cdf0e10cSrcweir // Initialize the bound parameter objects
621cdf0e10cSrcweir //--------------------------------------------------------------------
622cdf0e10cSrcweir 
initBoundParam()623cdf0e10cSrcweir void OPreparedStatement::initBoundParam () throw(SQLException)
624cdf0e10cSrcweir {
625cdf0e10cSrcweir 	OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
626cdf0e10cSrcweir 	// Get the number of parameters
627cdf0e10cSrcweir 	numParams = 0;
628cdf0e10cSrcweir 	N3SQLNumParams (m_aStatementHandle,&numParams);
629cdf0e10cSrcweir 
630cdf0e10cSrcweir 	// There are parameter markers, allocate the bound
631cdf0e10cSrcweir 	// parameter objects
632cdf0e10cSrcweir 
633cdf0e10cSrcweir 	if (numParams > 0)
634cdf0e10cSrcweir 	{
635cdf0e10cSrcweir 		// Allocate an array of bound parameter objects
636cdf0e10cSrcweir 
637cdf0e10cSrcweir 		boundParams = new OBoundParam[numParams];
638cdf0e10cSrcweir 
639cdf0e10cSrcweir 		// Allocate and initialize each bound parameter
640cdf0e10cSrcweir 
641cdf0e10cSrcweir 		for (sal_Int32 i = 0; i < numParams; i++)
642cdf0e10cSrcweir 		{
643cdf0e10cSrcweir 			boundParams[i] = OBoundParam();
644cdf0e10cSrcweir 			boundParams[i].initialize ();
645cdf0e10cSrcweir 		}
646cdf0e10cSrcweir 	}
647cdf0e10cSrcweir }
648cdf0e10cSrcweir // -------------------------------------------------------------------------
649cdf0e10cSrcweir 
650cdf0e10cSrcweir //--------------------------------------------------------------------
651cdf0e10cSrcweir // allocBindBuf
652cdf0e10cSrcweir // Allocate storage for the permanent data buffer for the bound
653cdf0e10cSrcweir // parameter.
654cdf0e10cSrcweir //--------------------------------------------------------------------
655cdf0e10cSrcweir 
allocBindBuf(sal_Int32 index,sal_Int32 bufLen)656cdf0e10cSrcweir sal_Int8* OPreparedStatement::allocBindBuf(	sal_Int32 index,sal_Int32 bufLen)
657cdf0e10cSrcweir {
658cdf0e10cSrcweir 	sal_Int8* b = NULL;
659cdf0e10cSrcweir 
660cdf0e10cSrcweir 	// Sanity check the parameter number
661cdf0e10cSrcweir 
662cdf0e10cSrcweir 	if ((index >= 1) &&
663cdf0e10cSrcweir 		(index <= numParams) && bufLen > 0 )
664cdf0e10cSrcweir 	{
665cdf0e10cSrcweir 		b = boundParams[index - 1].allocBindDataBuffer(bufLen);
666cdf0e10cSrcweir 	}
667cdf0e10cSrcweir 
668cdf0e10cSrcweir 	return b;
669cdf0e10cSrcweir }
670cdf0e10cSrcweir // -------------------------------------------------------------------------
671cdf0e10cSrcweir 
672cdf0e10cSrcweir //--------------------------------------------------------------------
673cdf0e10cSrcweir // getDataBuf
674cdf0e10cSrcweir // Gets the data buffer for the given parameter index
675cdf0e10cSrcweir //--------------------------------------------------------------------
676cdf0e10cSrcweir 
getDataBuf(sal_Int32 index)677cdf0e10cSrcweir sal_Int8* OPreparedStatement::getDataBuf (sal_Int32 index)
678cdf0e10cSrcweir {
679cdf0e10cSrcweir 	sal_Int8* b = NULL;
680cdf0e10cSrcweir 
681cdf0e10cSrcweir 	// Sanity check the parameter number
682cdf0e10cSrcweir 
683cdf0e10cSrcweir 	if ((index >= 1) &&
684cdf0e10cSrcweir 		(index <= numParams))
685cdf0e10cSrcweir 	{
686cdf0e10cSrcweir 		b = boundParams[index - 1].getBindDataBuffer ();
687cdf0e10cSrcweir 	}
688cdf0e10cSrcweir 
689cdf0e10cSrcweir 	return b;
690cdf0e10cSrcweir }
691cdf0e10cSrcweir // -------------------------------------------------------------------------
692cdf0e10cSrcweir 
693cdf0e10cSrcweir //--------------------------------------------------------------------
694cdf0e10cSrcweir // getLengthBuf
695cdf0e10cSrcweir // Gets the length buffer for the given parameter index
696cdf0e10cSrcweir //--------------------------------------------------------------------
697cdf0e10cSrcweir 
getLengthBuf(sal_Int32 index)698cdf0e10cSrcweir sal_Int8* OPreparedStatement::getLengthBuf (sal_Int32 index)
699cdf0e10cSrcweir {
700cdf0e10cSrcweir 	sal_Int8* b = NULL;
701cdf0e10cSrcweir 
702cdf0e10cSrcweir 	// Sanity check the parameter number
703cdf0e10cSrcweir 
704cdf0e10cSrcweir 	if ((index >= 1) &&
705cdf0e10cSrcweir 		(index <= numParams))
706cdf0e10cSrcweir 	{
707cdf0e10cSrcweir 		b = boundParams[index - 1].getBindLengthBuffer ();
708cdf0e10cSrcweir 	}
709cdf0e10cSrcweir 
710cdf0e10cSrcweir 	return b;
711cdf0e10cSrcweir }
712cdf0e10cSrcweir // -------------------------------------------------------------------------
713cdf0e10cSrcweir 
714cdf0e10cSrcweir //--------------------------------------------------------------------
715cdf0e10cSrcweir // getParamLength
716cdf0e10cSrcweir // Returns the length of the given parameter number.  When each
717cdf0e10cSrcweir // parameter was bound, a 4-sal_Int8 buffer was given to hold the
718cdf0e10cSrcweir // length (stored in native format).  Get the buffer, convert the
719cdf0e10cSrcweir // buffer from native format, and return it.  If the length is -1,
720cdf0e10cSrcweir // the column is considered to be NULL.
721cdf0e10cSrcweir //--------------------------------------------------------------------
722cdf0e10cSrcweir 
getParamLength(sal_Int32 index)723cdf0e10cSrcweir sal_Int32 OPreparedStatement::getParamLength (	sal_Int32 index)
724cdf0e10cSrcweir {
725cdf0e10cSrcweir 	sal_Int32 paramLen = SQL_NULL_DATA;
726cdf0e10cSrcweir 
727cdf0e10cSrcweir 	// Sanity check the parameter number
728cdf0e10cSrcweir 
729cdf0e10cSrcweir 	if ((index >= 1) &&
730cdf0e10cSrcweir 		(index <= numParams)) {
731cdf0e10cSrcweir 
732cdf0e10cSrcweir 		// Now get the length of the parameter from the
733cdf0e10cSrcweir 		// bound param array.  -1 is returned if it is NULL.
734cdf0e10cSrcweir 		long n = 0;
735cdf0e10cSrcweir 		memcpy (&n, boundParams[index -1].getBindLengthBuffer (), sizeof (n));
736cdf0e10cSrcweir 		paramLen = n;
737cdf0e10cSrcweir 	}
738cdf0e10cSrcweir 	return paramLen;
739cdf0e10cSrcweir }
740cdf0e10cSrcweir // -------------------------------------------------------------------------
741cdf0e10cSrcweir 
742cdf0e10cSrcweir //--------------------------------------------------------------------
743cdf0e10cSrcweir // putParamData
744cdf0e10cSrcweir // Puts parameter data from a previously bound input stream.  The
745cdf0e10cSrcweir // input stream was bound using SQL_LEN_DATA_AT_EXEC.
746cdf0e10cSrcweir //--------------------------------------------------------------------
747cdf0e10cSrcweir 
putParamData(sal_Int32 index)748cdf0e10cSrcweir void OPreparedStatement::putParamData (sal_Int32 index) throw(SQLException)
749cdf0e10cSrcweir {
750cdf0e10cSrcweir 	// Sanity check the parameter index
751cdf0e10cSrcweir 	if ((index < 1) ||
752cdf0e10cSrcweir 		(index > numParams))
753cdf0e10cSrcweir 	{
754cdf0e10cSrcweir 		return;
755cdf0e10cSrcweir 	}
756cdf0e10cSrcweir 
757cdf0e10cSrcweir 	// We'll transfer up to MAX_PUT_DATA_LENGTH at a time
758cdf0e10cSrcweir     Sequence< sal_Int8 > buf( MAX_PUT_DATA_LENGTH );
759cdf0e10cSrcweir 
760cdf0e10cSrcweir 	// Get the information about the input stream
761cdf0e10cSrcweir 
762cdf0e10cSrcweir 	Reference< XInputStream> inputStream =	boundParams[index - 1].getInputStream ();
763cdf0e10cSrcweir 	if ( !inputStream.is() )
764cdf0e10cSrcweir 	{
765cdf0e10cSrcweir         ::connectivity::SharedResources aResources;
766cdf0e10cSrcweir         const ::rtl::OUString sError( aResources.getResourceString(STR_NO_INPUTSTREAM));
767cdf0e10cSrcweir 		throw SQLException (sError,	*this,::rtl::OUString(),0,Any());
768cdf0e10cSrcweir 	}
769cdf0e10cSrcweir 
770cdf0e10cSrcweir 	sal_Int32 maxBytesLeft = boundParams[index - 1].getInputStreamLen ();
771cdf0e10cSrcweir 
772cdf0e10cSrcweir 	// Loop while more data from the input stream
773cdf0e10cSrcweir     sal_Int32 haveRead = 0;
774cdf0e10cSrcweir 	try
775cdf0e10cSrcweir 	{
776cdf0e10cSrcweir 
777cdf0e10cSrcweir 		do
778cdf0e10cSrcweir 		{
779cdf0e10cSrcweir             sal_Int32 toReadThisRound = ::std::min( MAX_PUT_DATA_LENGTH, maxBytesLeft );
780cdf0e10cSrcweir 
781cdf0e10cSrcweir 			// Read some data from the input stream
782cdf0e10cSrcweir 			haveRead = inputStream->readBytes( buf, toReadThisRound );
783cdf0e10cSrcweir             OSL_ENSURE( haveRead == buf.getLength(), "OPreparedStatement::putParamData: inconsistency!" );
784cdf0e10cSrcweir 
785cdf0e10cSrcweir             if ( !haveRead )
786cdf0e10cSrcweir                 // no more data in the stream - the given stream length was a maximum which could not be
787cdf0e10cSrcweir                 // fulfilled by the stream
788cdf0e10cSrcweir                 break;
789cdf0e10cSrcweir 
790cdf0e10cSrcweir 		    // Put the data
791cdf0e10cSrcweir             OSL_ENSURE( m_aStatementHandle, "OPreparedStatement::putParamData: StatementHandle is null!" );
792cdf0e10cSrcweir 		    N3SQLPutData ( m_aStatementHandle, buf.getArray(), buf.getLength() );
793cdf0e10cSrcweir 
794cdf0e10cSrcweir 			// decrement the number of bytes still needed
795cdf0e10cSrcweir 			maxBytesLeft -= haveRead;
796cdf0e10cSrcweir 		}
797cdf0e10cSrcweir         while ( maxBytesLeft > 0 );
798cdf0e10cSrcweir 	}
799cdf0e10cSrcweir 	catch (const IOException& ex)
800cdf0e10cSrcweir 	{
801cdf0e10cSrcweir 
802cdf0e10cSrcweir 		// If an I/O exception was generated, turn
803cdf0e10cSrcweir 		// it into a SQLException
804cdf0e10cSrcweir 
805cdf0e10cSrcweir 		throw SQLException(ex.Message,*this,::rtl::OUString(),0,Any());
806cdf0e10cSrcweir 	}
807cdf0e10cSrcweir }
808cdf0e10cSrcweir // -------------------------------------------------------------------------
809cdf0e10cSrcweir //--------------------------------------------------------------------
810cdf0e10cSrcweir // getPrecision
811cdf0e10cSrcweir // Given a SQL type, return the maximum precision for the column.
812cdf0e10cSrcweir // Returns -1 if not known
813cdf0e10cSrcweir //--------------------------------------------------------------------
814cdf0e10cSrcweir 
getPrecision(sal_Int32 sqlType)815cdf0e10cSrcweir sal_Int32 OPreparedStatement::getPrecision ( sal_Int32 sqlType)
816cdf0e10cSrcweir {
817cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
818cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
819cdf0e10cSrcweir 
820cdf0e10cSrcweir 	sal_Int32 prec = -1;
821cdf0e10cSrcweir     const TTypeInfoVector& rTypeInfo = m_pConnection->getTypeInfo();
822cdf0e10cSrcweir     if ( !rTypeInfo.empty() )
823cdf0e10cSrcweir     {
824cdf0e10cSrcweir         m_pConnection->buildTypeInfo();
825cdf0e10cSrcweir     }
826cdf0e10cSrcweir 
827cdf0e10cSrcweir 	if ( !rTypeInfo.empty() )
828cdf0e10cSrcweir 	{
829cdf0e10cSrcweir 		OTypeInfo aInfo;
830cdf0e10cSrcweir 		aInfo.nType = (sal_Int16)sqlType;
831cdf0e10cSrcweir 		TTypeInfoVector::const_iterator aIter = ::std::find(rTypeInfo.begin(),rTypeInfo.end(),aInfo);
832cdf0e10cSrcweir 		if(aIter != rTypeInfo.end())
833cdf0e10cSrcweir 			prec = (*aIter).nPrecision;
834cdf0e10cSrcweir 	}
835cdf0e10cSrcweir 	return prec;
836cdf0e10cSrcweir }
837cdf0e10cSrcweir 
838cdf0e10cSrcweir //--------------------------------------------------------------------
839cdf0e10cSrcweir // setStream
840cdf0e10cSrcweir // Sets an input stream as a parameter, using the given SQL type
841cdf0e10cSrcweir //--------------------------------------------------------------------
842cdf0e10cSrcweir 
setStream(sal_Int32 ParameterIndex,const Reference<XInputStream> & x,SQLLEN length,sal_Int32 SQLtype)843cdf0e10cSrcweir void OPreparedStatement::setStream(
844cdf0e10cSrcweir 									sal_Int32 ParameterIndex,
845cdf0e10cSrcweir 									const Reference< XInputStream>& x,
846cdf0e10cSrcweir 									SQLLEN length,
847cdf0e10cSrcweir 									sal_Int32 SQLtype)
848cdf0e10cSrcweir 									throw(SQLException)
849cdf0e10cSrcweir {
850cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
851cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
852cdf0e10cSrcweir 
853cdf0e10cSrcweir 
854cdf0e10cSrcweir 	prepareStatement();
855cdf0e10cSrcweir 
856cdf0e10cSrcweir 	checkParameterIndex(ParameterIndex);
857cdf0e10cSrcweir 	// Get the buffer needed for the length
858cdf0e10cSrcweir 
859cdf0e10cSrcweir 	sal_Int8* lenBuf = getLengthBuf(ParameterIndex);
860cdf0e10cSrcweir 
861cdf0e10cSrcweir 	// Allocate a new buffer for the parameter data.  This buffer
862cdf0e10cSrcweir 	// will be returned by SQLParamData (it is set to the parameter
863cdf0e10cSrcweir 	// number, a 4-sal_Int8 integer)
864cdf0e10cSrcweir 
865cdf0e10cSrcweir 	sal_Int8* dataBuf = allocBindBuf (ParameterIndex, 4);
866cdf0e10cSrcweir 
867cdf0e10cSrcweir     // Bind the parameter with SQL_LEN_DATA_AT_EXEC
868cdf0e10cSrcweir 	SQLSMALLINT   Ctype = SQL_C_CHAR;
869cdf0e10cSrcweir 	SQLLEN	atExec = SQL_LEN_DATA_AT_EXEC (length);
870cdf0e10cSrcweir 	memcpy (dataBuf, &ParameterIndex, sizeof(ParameterIndex));
871cdf0e10cSrcweir 	memcpy (lenBuf, &atExec, sizeof (atExec));
872cdf0e10cSrcweir 
873cdf0e10cSrcweir 	if ((SQLtype == SQL_BINARY) || (SQLtype == SQL_VARBINARY) || (SQLtype == SQL_LONGVARBINARY))
874cdf0e10cSrcweir 		Ctype = SQL_C_BINARY;
875cdf0e10cSrcweir 
876cdf0e10cSrcweir 
877cdf0e10cSrcweir 	OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
878cdf0e10cSrcweir 	N3SQLBindParameter(m_aStatementHandle,
879cdf0e10cSrcweir 						(SQLUSMALLINT)ParameterIndex,
880cdf0e10cSrcweir 						(SQLUSMALLINT)SQL_PARAM_INPUT,
881cdf0e10cSrcweir 						Ctype,
882cdf0e10cSrcweir 						(SQLSMALLINT)SQLtype,
883cdf0e10cSrcweir 						(SQLULEN)length,
884cdf0e10cSrcweir 						0,
885cdf0e10cSrcweir 						dataBuf,
886cdf0e10cSrcweir 						sizeof(ParameterIndex),
887cdf0e10cSrcweir 						(SQLLEN*)lenBuf);
888cdf0e10cSrcweir 
889cdf0e10cSrcweir 	// Save the input stream
890cdf0e10cSrcweir 	boundParams[ParameterIndex - 1].setInputStream (x, length);
891cdf0e10cSrcweir }
892cdf0e10cSrcweir // -------------------------------------------------------------------------
893cdf0e10cSrcweir 
894cdf0e10cSrcweir // -------------------------------------------------------------------------
895cdf0e10cSrcweir 
FreeParams()896cdf0e10cSrcweir void OPreparedStatement::FreeParams()
897cdf0e10cSrcweir {
898cdf0e10cSrcweir     numParams = 0;
899cdf0e10cSrcweir 	delete [] boundParams;
900cdf0e10cSrcweir 	boundParams = NULL;
901cdf0e10cSrcweir }
902cdf0e10cSrcweir // -------------------------------------------------------------------------
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any & rValue)903cdf0e10cSrcweir void OPreparedStatement::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any& rValue) throw (Exception)
904cdf0e10cSrcweir {
905cdf0e10cSrcweir 	try
906cdf0e10cSrcweir 	{
907cdf0e10cSrcweir 		switch(nHandle)
908cdf0e10cSrcweir 		{
909cdf0e10cSrcweir 			case PROPERTY_ID_RESULTSETCONCURRENCY:
910cdf0e10cSrcweir 				if(!isPrepared())
911cdf0e10cSrcweir 					setResultSetConcurrency(comphelper::getINT32(rValue));
912cdf0e10cSrcweir 				break;
913cdf0e10cSrcweir 			case PROPERTY_ID_RESULTSETTYPE:
914cdf0e10cSrcweir 				if(!isPrepared())
915cdf0e10cSrcweir 					setResultSetType(comphelper::getINT32(rValue));
916cdf0e10cSrcweir 				break;
917cdf0e10cSrcweir 			case PROPERTY_ID_FETCHDIRECTION:
918cdf0e10cSrcweir 				if(!isPrepared())
919cdf0e10cSrcweir 					setFetchDirection(comphelper::getINT32(rValue));
920cdf0e10cSrcweir 				break;
921cdf0e10cSrcweir 			case PROPERTY_ID_USEBOOKMARKS:
922cdf0e10cSrcweir 				if(!isPrepared())
923cdf0e10cSrcweir 					setUsingBookmarks(comphelper::getBOOL(rValue));
924cdf0e10cSrcweir 				break;
925cdf0e10cSrcweir 			default:
926cdf0e10cSrcweir 				OStatement_Base::setFastPropertyValue_NoBroadcast(nHandle,rValue);
927cdf0e10cSrcweir 		}
928cdf0e10cSrcweir 	}
929cdf0e10cSrcweir 	catch(const SQLException&)
930cdf0e10cSrcweir 	{
931cdf0e10cSrcweir 		//	throw Exception(e.Message,*this);
932cdf0e10cSrcweir 	}
933cdf0e10cSrcweir }
934cdf0e10cSrcweir // -----------------------------------------------------------------------------
prepareStatement()935cdf0e10cSrcweir void OPreparedStatement::prepareStatement()
936cdf0e10cSrcweir {
937cdf0e10cSrcweir 	if(!isPrepared())
938cdf0e10cSrcweir 	{
939cdf0e10cSrcweir 		OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
940cdf0e10cSrcweir 		::rtl::OString aSql(::rtl::OUStringToOString(m_sSqlStatement,getOwnConnection()->getTextEncoding()));
941cdf0e10cSrcweir 		SQLRETURN nReturn = N3SQLPrepare(m_aStatementHandle,(SDB_ODBC_CHAR *) aSql.getStr(),aSql.getLength());
942cdf0e10cSrcweir 		OTools::ThrowException(m_pConnection,nReturn,m_aStatementHandle,SQL_HANDLE_STMT,*this);
943cdf0e10cSrcweir         m_bPrepared = sal_True;
944cdf0e10cSrcweir 		initBoundParam();
945cdf0e10cSrcweir 	}
946cdf0e10cSrcweir }
947cdf0e10cSrcweir // -----------------------------------------------------------------------------
checkParameterIndex(sal_Int32 _parameterIndex)948cdf0e10cSrcweir void OPreparedStatement::checkParameterIndex(sal_Int32 _parameterIndex)
949cdf0e10cSrcweir {
950cdf0e10cSrcweir 	if(	!_parameterIndex || _parameterIndex > numParams)
951cdf0e10cSrcweir 	{
952cdf0e10cSrcweir         ::connectivity::SharedResources aResources;
953cdf0e10cSrcweir         const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(STR_WRONG_PARAM_INDEX,
954cdf0e10cSrcweir             "$pos$", ::rtl::OUString::valueOf(_parameterIndex),
955cdf0e10cSrcweir             "$count$", ::rtl::OUString::valueOf((sal_Int32)numParams)
956cdf0e10cSrcweir             ));
957cdf0e10cSrcweir         SQLException aNext(sError,*this, ::rtl::OUString(),0,Any());
958cdf0e10cSrcweir 
959cdf0e10cSrcweir         ::dbtools::throwInvalidIndexException(*this,makeAny(aNext));
960cdf0e10cSrcweir 	}
961cdf0e10cSrcweir }
962cdf0e10cSrcweir // -----------------------------------------------------------------------------
createResulSet()963cdf0e10cSrcweir OResultSet* OPreparedStatement::createResulSet()
964cdf0e10cSrcweir {
965cdf0e10cSrcweir     OResultSet* pReturn = new OResultSet(m_aStatementHandle,this);
966cdf0e10cSrcweir     pReturn->setMetaData(getMetaData());
967cdf0e10cSrcweir 	return pReturn;
968cdf0e10cSrcweir }
969cdf0e10cSrcweir // -----------------------------------------------------------------------------
970