1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 #include "WrappedResultSet.hxx"
27 #include "core_resource.hxx"
28 #include "core_resource.hrc"
29 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
30 #include <connectivity/dbexception.hxx>
31 #include <rtl/logfile.hxx>
32 
33 #include <limits>
34 
35 using namespace dbaccess;
36 using namespace ::connectivity;
37 using namespace ::dbtools;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::sdbc;
41 //	using namespace ::com::sun::star::sdb;
42 using namespace ::com::sun::star::sdbcx;
43 using namespace ::com::sun::star::container;
44 using namespace ::com::sun::star::lang;
45 //	using namespace ::cppu;
46 using namespace ::osl;
47 
construct(const Reference<XResultSet> & _xDriverSet,const::rtl::OUString & i_sRowSetFilter)48 void WrappedResultSet::construct(const Reference< XResultSet>& _xDriverSet,const ::rtl::OUString& i_sRowSetFilter)
49 {
50     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::construct" );
51 	OCacheSet::construct(_xDriverSet,i_sRowSetFilter);
52     m_xUpd.set(_xDriverSet,UNO_QUERY_THROW);
53 	m_xRowLocate.set(_xDriverSet,UNO_QUERY_THROW);
54     m_xUpdRow.set(_xDriverSet,UNO_QUERY_THROW);
55 }
56 // -----------------------------------------------------------------------------
getBookmark()57 Any SAL_CALL WrappedResultSet::getBookmark() throw(SQLException, RuntimeException)
58 {
59     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::getBookmark" );
60     if ( m_xRowLocate.is() )
61     {
62         return m_xRowLocate->getBookmark( );
63     }
64 	return makeAny(m_xDriverSet->getRow());
65 }
66 // -------------------------------------------------------------------------
moveToBookmark(const Any & bookmark)67 sal_Bool SAL_CALL WrappedResultSet::moveToBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
68 {
69     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::moveToBookmark" );
70     return m_xRowLocate->moveToBookmark( bookmark );
71 }
72 // -------------------------------------------------------------------------
moveRelativeToBookmark(const Any & bookmark,sal_Int32 rows)73 sal_Bool SAL_CALL WrappedResultSet::moveRelativeToBookmark( const Any& bookmark, sal_Int32 rows ) throw(SQLException, RuntimeException)
74 {
75     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::moveRelativeToBookmark" );
76     return m_xRowLocate->moveRelativeToBookmark( bookmark,rows );
77 }
78 // -------------------------------------------------------------------------
compareBookmarks(const Any & _first,const Any & _second)79 sal_Int32 SAL_CALL WrappedResultSet::compareBookmarks( const Any& _first, const Any& _second ) throw(SQLException, RuntimeException)
80 {
81     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::compareBookmarks" );
82 	return m_xRowLocate->compareBookmarks( _first,_second );
83 }
84 // -------------------------------------------------------------------------
hasOrderedBookmarks()85 sal_Bool SAL_CALL WrappedResultSet::hasOrderedBookmarks(  ) throw(SQLException, RuntimeException)
86 {
87     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::hasOrderedBookmarks" );
88 	return m_xRowLocate->hasOrderedBookmarks();
89 }
90 // -------------------------------------------------------------------------
hashBookmark(const Any & bookmark)91 sal_Int32 SAL_CALL WrappedResultSet::hashBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
92 {
93     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::hashBookmark" );
94     return m_xRowLocate->hashBookmark(bookmark);
95 }
96 // -------------------------------------------------------------------------
97 // ::com::sun::star::sdbcx::XDeleteRows
deleteRows(const Sequence<Any> & rows,const connectivity::OSQLTable &)98 Sequence< sal_Int32 > SAL_CALL WrappedResultSet::deleteRows( const Sequence< Any >& rows ,const connectivity::OSQLTable& /*_xTable*/) throw(SQLException, RuntimeException)
99 {
100     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::deleteRows" );
101 	Reference< ::com::sun::star::sdbcx::XDeleteRows> xDeleteRow(m_xRowLocate,UNO_QUERY);
102 	if(xDeleteRow.is())
103 	{
104 		return xDeleteRow->deleteRows(rows);
105 	}
106 	return Sequence< sal_Int32 >();
107 }
108 // -------------------------------------------------------------------------
insertRow(const ORowSetRow & _rInsertRow,const connectivity::OSQLTable &)109 void SAL_CALL WrappedResultSet::insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& /*_xTable*/ ) throw(SQLException, RuntimeException)
110 {
111     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::insertRow" );
112 
113 	m_xUpd->moveToInsertRow();
114 	sal_Int32 i = 1;
115     connectivity::ORowVector< ORowSetValue > ::Vector::iterator aEnd = _rInsertRow->get().end();
116 	for(connectivity::ORowVector< ORowSetValue > ::Vector::iterator aIter = _rInsertRow->get().begin()+1;aIter != aEnd;++aIter,++i)
117 	{
118 		aIter->setSigned(m_aSignedFlags[i-1]);
119 		updateColumn(i,m_xUpdRow,*aIter);
120 	}
121 	m_xUpd->insertRow();
122 	(*_rInsertRow->get().begin()) = getBookmark();
123 }
124 // -------------------------------------------------------------------------
updateRow(const ORowSetRow & _rInsertRow,const ORowSetRow & _rOrginalRow,const connectivity::OSQLTable &)125 void SAL_CALL WrappedResultSet::updateRow(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOrginalRow,const connectivity::OSQLTable& /*_xTable*/  ) throw(SQLException, RuntimeException)
126 {
127     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::updateRow" );
128 	sal_Int32 i = 1;
129 	connectivity::ORowVector< ORowSetValue > ::Vector::const_iterator aOrgIter = _rOrginalRow->get().begin()+1;
130     connectivity::ORowVector< ORowSetValue > ::Vector::iterator aEnd = _rInsertRow->get().end();
131 	for(connectivity::ORowVector< ORowSetValue > ::Vector::iterator aIter = _rInsertRow->get().begin()+1;aIter != aEnd;++aIter,++i,++aOrgIter)
132 	{
133 		aIter->setSigned(aOrgIter->isSigned());
134 		updateColumn(i,m_xUpdRow,*aIter);
135 	}
136 	m_xUpd->updateRow();
137 }
138 // -------------------------------------------------------------------------
deleteRow(const ORowSetRow &,const connectivity::OSQLTable &)139 void SAL_CALL WrappedResultSet::deleteRow(const ORowSetRow& /*_rDeleteRow*/ ,const connectivity::OSQLTable& /*_xTable*/  ) throw(SQLException, RuntimeException)
140 {
141     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::deleteRow" );
142 	m_xUpd->deleteRow();
143 }
144 // -------------------------------------------------------------------------
cancelRowUpdates()145 void SAL_CALL WrappedResultSet::cancelRowUpdates(  ) throw(SQLException, RuntimeException)
146 {
147     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::cancelRowUpdates" );
148     m_xUpd->cancelRowUpdates();
149 }
150 // -------------------------------------------------------------------------
moveToInsertRow()151 void SAL_CALL WrappedResultSet::moveToInsertRow(  ) throw(SQLException, RuntimeException)
152 {
153     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::moveToInsertRow" );
154 	m_xUpd->moveToInsertRow();
155 }
156 // -------------------------------------------------------------------------
moveToCurrentRow()157 void SAL_CALL WrappedResultSet::moveToCurrentRow(  ) throw(SQLException, RuntimeException)
158 {
159     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::moveToCurrentRow" );
160     m_xUpd->moveToCurrentRow();
161 }
162 // -------------------------------------------------------------------------
fillValueRow(ORowSetRow & _rRow,sal_Int32 _nPosition)163 void WrappedResultSet::fillValueRow(ORowSetRow& _rRow,sal_Int32 _nPosition)
164 {
165     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::fillValueRow" );
166 	OCacheSet::fillValueRow(_rRow,_nPosition);
167 }
168 // -------------------------------------------------------------------------
updateColumn(sal_Int32 nPos,Reference<XRowUpdate> _xParameter,const ORowSetValue & _rValue)169 void WrappedResultSet::updateColumn(sal_Int32 nPos,Reference< XRowUpdate > _xParameter,const ORowSetValue& _rValue)
170 {
171     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "WrappedResultSet::updateColumn" );
172 	if(_rValue.isBound() && _rValue.isModified())
173 	{
174 		if(_rValue.isNull())
175 			_xParameter->updateNull(nPos);
176 		else
177 		{
178 
179 			switch(_rValue.getTypeKind())
180 			{
181                 case DataType::DECIMAL:
182                 case DataType::NUMERIC:
183                     _xParameter->updateNumericObject(nPos,_rValue.makeAny(),m_xSetMetaData->getScale(nPos));
184                     break;
185 				case DataType::CHAR:
186 				case DataType::VARCHAR:
187 				//case DataType::DECIMAL:
188 				//case DataType::NUMERIC:
189 					_xParameter->updateString(nPos,_rValue);
190 					break;
191 				case DataType::BIGINT:
192 					if ( _rValue.isSigned() )
193 						_xParameter->updateLong(nPos,_rValue);
194 					else
195 						_xParameter->updateString(nPos,_rValue);
196 					break;
197 				case DataType::BIT:
198 				case DataType::BOOLEAN:
199 					_xParameter->updateBoolean(nPos,_rValue);
200 					break;
201 				case DataType::TINYINT:
202 					if ( _rValue.isSigned() )
203 						_xParameter->updateByte(nPos,_rValue);
204 					else
205 						_xParameter->updateShort(nPos,_rValue);
206 					break;
207 				case DataType::SMALLINT:
208 					if ( _rValue.isSigned() )
209 						_xParameter->updateShort(nPos,_rValue);
210 					else
211 						_xParameter->updateInt(nPos,_rValue);
212 					break;
213 				case DataType::INTEGER:
214 					if ( _rValue.isSigned() )
215 						_xParameter->updateInt(nPos,_rValue);
216 					else
217 						_xParameter->updateLong(nPos,_rValue);
218 					break;
219 				case DataType::FLOAT:
220 					_xParameter->updateFloat(nPos,_rValue);
221 					break;
222 				case DataType::DOUBLE:
223 				case DataType::REAL:
224 					_xParameter->updateDouble(nPos,_rValue);
225 					break;
226 				case DataType::DATE:
227 					_xParameter->updateDate(nPos,_rValue);
228 					break;
229 				case DataType::TIME:
230 					_xParameter->updateTime(nPos,_rValue);
231 					break;
232 				case DataType::TIMESTAMP:
233 					_xParameter->updateTimestamp(nPos,_rValue);
234 					break;
235 				case DataType::BINARY:
236 				case DataType::VARBINARY:
237 				case DataType::LONGVARBINARY:
238 					_xParameter->updateBytes(nPos,_rValue);
239 					break;
240 				case DataType::BLOB:
241 				case DataType::CLOB:
242 					_xParameter->updateObject(nPos,_rValue.getAny());
243 					break;
244 			}
245 		}
246 	}
247 }
248 
249