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