Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "StaticSet.hxx"
22 : #include <com/sun/star/sdbcx/CompareBookmark.hpp>
23 : #include <com/sun/star/beans/XPropertySet.hpp>
24 : #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
25 : #include <com/sun/star/sdbc/XPreparedStatement.hpp>
26 : #include "dbastrings.hrc"
27 : #include "apitools.hxx"
28 : #include <connectivity/CommonTools.hxx>
29 : #include <comphelper/types.hxx>
30 : #include <rtl/logfile.hxx>
31 :
32 : using namespace dbaccess;
33 : using namespace connectivity;
34 : using namespace ::com::sun::star::uno;
35 : using namespace ::com::sun::star::beans;
36 : using namespace ::com::sun::star::sdbc;
37 : using namespace ::com::sun::star::sdbcx;
38 : using namespace ::com::sun::star::container;
39 : using namespace ::com::sun::star::lang;
40 : using namespace ::osl;
41 :
42 0 : void OStaticSet::fillValueRow(ORowSetRow& _rRow,sal_Int32 /*_nPosition*/)
43 : {
44 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::fillValueRow" );
45 0 : _rRow = *m_aSetIter;
46 0 : }
47 :
48 : // ::com::sun::star::sdbcx::XRowLocate
49 0 : Any SAL_CALL OStaticSet::getBookmark() throw(SQLException, RuntimeException)
50 : {
51 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::getBookmark" );
52 0 : return makeAny(getRow());
53 : }
54 :
55 0 : sal_Bool SAL_CALL OStaticSet::moveToBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
56 : {
57 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveToBookmark" );
58 0 : m_bInserted = m_bUpdated = m_bDeleted = sal_False;
59 0 : return absolute(::comphelper::getINT32(bookmark));
60 : }
61 :
62 0 : sal_Bool SAL_CALL OStaticSet::moveRelativeToBookmark( const Any& bookmark, sal_Int32 rows ) throw(SQLException, RuntimeException)
63 : {
64 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveRelativeToBookmark" );
65 0 : m_bInserted = m_bUpdated = m_bDeleted = sal_False;
66 0 : return absolute(::comphelper::getINT32(bookmark)+rows);
67 : }
68 :
69 0 : sal_Int32 SAL_CALL OStaticSet::compareBookmarks( const Any& _first, const Any& _second ) throw(SQLException, RuntimeException)
70 : {
71 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::compareBookmarks" );
72 0 : sal_Int32 nFirst = 0, nSecond = 0;
73 0 : _first >>= nFirst;
74 0 : _second >>= nSecond;
75 0 : return (nFirst < nSecond) ? CompareBookmark::LESS : ((nFirst > nSecond) ? CompareBookmark::GREATER : CompareBookmark::EQUAL);
76 : }
77 :
78 0 : sal_Bool SAL_CALL OStaticSet::hasOrderedBookmarks( ) throw(SQLException, RuntimeException)
79 : {
80 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::hasOrderedBookmarks" );
81 0 : return sal_True;
82 : }
83 :
84 0 : sal_Int32 SAL_CALL OStaticSet::hashBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
85 : {
86 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::hashBookmark" );
87 0 : return ::comphelper::getINT32(bookmark);
88 : }
89 :
90 0 : sal_Bool OStaticSet::fetchRow()
91 : {
92 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::fetchRow" );
93 0 : sal_Bool bRet = sal_False;
94 0 : if ( !m_bEnd && (!m_nMaxRows || sal_Int32(m_aSet.size()) < m_nMaxRows) )
95 0 : bRet = m_xDriverSet->next();
96 0 : if ( bRet )
97 : {
98 0 : m_aSet.push_back(new connectivity::ORowVector< connectivity::ORowSetValue >(m_xSetMetaData->getColumnCount()));
99 0 : m_aSetIter = m_aSet.end() - 1;
100 0 : ((*m_aSetIter)->get())[0] = getRow();
101 0 : OCacheSet::fillValueRow(*m_aSetIter,((*m_aSetIter)->get())[0]);
102 : }
103 : else
104 0 : m_bEnd = sal_True;
105 0 : return bRet;
106 : }
107 :
108 0 : void OStaticSet::fillAllRows()
109 : {
110 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::fillAllRows" );
111 0 : if(!m_bEnd)
112 : {
113 0 : sal_Int32 nColumnCount = m_xSetMetaData->getColumnCount();
114 0 : while(m_xDriverSet->next())
115 : {
116 0 : ORowSetRow pRow = new connectivity::ORowVector< connectivity::ORowSetValue >(nColumnCount);
117 0 : m_aSet.push_back(pRow);
118 0 : m_aSetIter = m_aSet.end() - 1;
119 0 : (pRow->get())[0] = getRow();
120 0 : OCacheSet::fillValueRow(pRow,(pRow->get())[0]);
121 0 : }
122 0 : m_bEnd = sal_True;
123 : }
124 0 : }
125 :
126 : // XResultSet
127 0 : sal_Bool SAL_CALL OStaticSet::next( ) throw(SQLException, RuntimeException)
128 : {
129 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::next" );
130 0 : m_bInserted = m_bUpdated = m_bDeleted = sal_False;
131 :
132 0 : if(isAfterLast())
133 0 : return sal_False;
134 0 : if(!m_bEnd) // not yet all records fetched
135 : {
136 0 : ++m_aSetIter;
137 0 : if(m_aSetIter == m_aSet.end() && !fetchRow())
138 0 : m_aSetIter = m_aSet.end();
139 : }
140 0 : else if(!isAfterLast())
141 0 : ++m_aSetIter;
142 0 : return !isAfterLast();
143 : }
144 :
145 0 : sal_Bool SAL_CALL OStaticSet::isBeforeFirst( ) throw(SQLException, RuntimeException)
146 : {
147 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isBeforeFirst" );
148 0 : return m_aSetIter == m_aSet.begin();
149 : }
150 :
151 0 : sal_Bool SAL_CALL OStaticSet::isAfterLast( ) throw(SQLException, RuntimeException)
152 : {
153 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isAfterLast" );
154 0 : return m_aSetIter == m_aSet.end() && m_bEnd;
155 : }
156 :
157 0 : sal_Bool SAL_CALL OStaticSet::isFirst( ) throw(SQLException, RuntimeException)
158 : {
159 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isFirst" );
160 0 : return m_aSetIter == m_aSet.begin()+1;
161 : }
162 :
163 0 : sal_Bool SAL_CALL OStaticSet::isLast( ) throw(SQLException, RuntimeException)
164 : {
165 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isLast" );
166 0 : return m_aSetIter == m_aSet.end()-1 && m_bEnd;
167 : }
168 :
169 0 : void SAL_CALL OStaticSet::beforeFirst( ) throw(SQLException, RuntimeException)
170 : {
171 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::beforeFirst" );
172 0 : m_bInserted = m_bUpdated = m_bDeleted = sal_False;
173 0 : m_aSetIter = m_aSet.begin();
174 0 : }
175 :
176 0 : void SAL_CALL OStaticSet::afterLast( ) throw(SQLException, RuntimeException)
177 : {
178 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::afterLast" );
179 0 : m_bInserted = m_bUpdated = m_bDeleted = sal_False;
180 0 : fillAllRows();
181 0 : m_aSetIter = m_aSet.end();
182 0 : }
183 :
184 0 : sal_Bool SAL_CALL OStaticSet::first( ) throw(SQLException, RuntimeException)
185 : {
186 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::first" );
187 0 : m_bInserted = m_bUpdated = m_bDeleted = sal_False;
188 0 : m_aSetIter = m_aSet.begin()+1;
189 0 : if(m_aSetIter == m_aSet.end() && !fetchRow())
190 0 : m_aSetIter = m_aSet.end();
191 :
192 0 : return m_aSetIter != m_aSet.end();
193 : }
194 :
195 0 : sal_Bool SAL_CALL OStaticSet::last( ) throw(SQLException, RuntimeException)
196 : {
197 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::last" );
198 0 : m_bInserted = m_bUpdated = m_bDeleted = sal_False;
199 0 : fillAllRows();
200 0 : m_aSetIter = m_aSet.end()-1;
201 :
202 0 : return !isBeforeFirst() && !isAfterLast();
203 : }
204 :
205 0 : sal_Int32 SAL_CALL OStaticSet::getRow( ) throw(SQLException, RuntimeException)
206 : {
207 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::getRow" );
208 : OSL_ENSURE(!isAfterLast(),"getRow is not allowed when afterlast record!");
209 : OSL_ENSURE(!isBeforeFirst(),"getRow is not allowed when beforefirst record!");
210 :
211 0 : sal_Int32 nPos = m_aSet.size() - (m_aSet.end() - m_aSetIter);
212 : OSL_ENSURE(nPos > 0,"RowPos is < 0");
213 0 : return nPos;
214 : }
215 :
216 0 : sal_Bool SAL_CALL OStaticSet::absolute( sal_Int32 row ) throw(SQLException, RuntimeException)
217 : {
218 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::absolute" );
219 0 : m_bInserted = m_bUpdated = m_bDeleted = sal_False;
220 : OSL_ENSURE(row,"OStaticSet::absolute: INVALID row number!");
221 : // if row greater 0 than count from end - row means last
222 0 : if(row < 0)
223 : {
224 0 : if(!m_bEnd)
225 0 : fillAllRows();
226 :
227 0 : sal_Int32 nRow = getRow();
228 0 : nRow += row;
229 0 : if(nRow <= (sal_Int32)m_aSet.size())
230 0 : m_aSetIter = m_aSet.begin() + nRow;
231 : else
232 0 : m_aSetIter = m_aSet.begin();
233 : }
234 0 : else if(row > 0)
235 : {
236 0 : if(row >= (sal_Int32)m_aSet.size())
237 : {
238 0 : if(!m_bEnd)
239 : {
240 0 : sal_Bool bNext = sal_True;
241 0 : for(sal_Int32 i=m_aSet.size()-1;i < row && bNext;++i)
242 0 : bNext = fetchRow();
243 : }
244 :
245 0 : if(row > (sal_Int32)m_aSet.size())
246 0 : m_aSetIter = m_aSet.end(); // check again
247 : else
248 0 : m_aSetIter = m_aSet.begin() + row;
249 : }
250 : else
251 0 : m_aSetIter = m_aSet.begin() + row;
252 : }
253 :
254 0 : return m_aSetIter != m_aSet.end() && m_aSetIter != m_aSet.begin();
255 : }
256 :
257 0 : sal_Bool SAL_CALL OStaticSet::relative( sal_Int32 rows ) throw(SQLException, RuntimeException)
258 : {
259 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::relative" );
260 0 : if(!rows)
261 0 : return sal_True;
262 :
263 0 : sal_Int32 nCurPos = getRow();
264 0 : return absolute(nCurPos+rows);
265 : }
266 :
267 0 : sal_Bool SAL_CALL OStaticSet::previous( ) throw(SQLException, RuntimeException)
268 : {
269 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::previous" );
270 0 : m_bInserted = m_bUpdated = m_bDeleted = sal_False;
271 :
272 0 : if(m_aSetIter != m_aSet.begin())
273 0 : --m_aSetIter;
274 :
275 0 : return m_aSetIter != m_aSet.begin();
276 : }
277 :
278 0 : void SAL_CALL OStaticSet::refreshRow( ) throw(SQLException, RuntimeException)
279 : {
280 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::refreshRow" );
281 0 : }
282 :
283 0 : sal_Bool SAL_CALL OStaticSet::rowUpdated( ) throw(SQLException, RuntimeException)
284 : {
285 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::rowUpdated" );
286 0 : return m_bUpdated;
287 : }
288 :
289 0 : sal_Bool SAL_CALL OStaticSet::rowInserted( ) throw(SQLException, RuntimeException)
290 : {
291 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::rowInserted" );
292 0 : return m_bInserted;
293 : }
294 :
295 0 : sal_Bool SAL_CALL OStaticSet::rowDeleted( ) throw(SQLException, RuntimeException)
296 : {
297 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::rowDeleted" );
298 0 : return m_bDeleted;
299 : }
300 :
301 0 : Sequence< sal_Int32 > SAL_CALL OStaticSet::deleteRows( const Sequence< Any >& rows,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
302 : {
303 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::deleteRows" );
304 0 : Sequence< sal_Int32 > aRet(rows.getLength());
305 0 : const Any* pBegin = rows.getConstArray();
306 0 : const Any* pEnd = pBegin + rows.getLength();
307 0 : for(sal_Int32 i=0;pBegin != pEnd; ++pBegin,++i)
308 : {
309 0 : deleteRow(*(m_aSet.begin() + comphelper::getINT32(*pBegin)),_xTable);
310 0 : aRet.getArray()[i] = m_bDeleted;
311 : }
312 0 : return aRet;
313 : }
314 :
315 0 : void SAL_CALL OStaticSet::insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
316 : {
317 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::insertRow" );
318 0 : OCacheSet::insertRow( _rInsertRow,_xTable);
319 0 : if(m_bInserted)
320 : {
321 0 : m_aSet.push_back(new ORowVector< ORowSetValue >(*_rInsertRow)); // we don't know where the new row is so we append it to the current rows
322 0 : m_aSetIter = m_aSet.end() - 1;
323 0 : ((*m_aSetIter)->get())[0] = (_rInsertRow->get())[0] = getBookmark();
324 0 : m_bEnd = sal_False;
325 : }
326 0 : }
327 :
328 0 : void SAL_CALL OStaticSet::updateRow(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOrginalRow,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
329 : {
330 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::updateRow" );
331 0 : OCacheSet::updateRow( _rInsertRow,_rOrginalRow,_xTable);
332 0 : }
333 :
334 0 : void SAL_CALL OStaticSet::deleteRow(const ORowSetRow& _rDeleteRow ,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
335 : {
336 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::deleteRow" );
337 0 : OCacheSet::deleteRow(_rDeleteRow,_xTable);
338 0 : if(m_bDeleted)
339 : {
340 0 : ORowSetMatrix::iterator aPos = m_aSet.begin()+(_rDeleteRow->get())[0].getInt32();
341 0 : if(aPos == (m_aSet.end()-1))
342 0 : m_aSetIter = m_aSet.end();
343 0 : m_aSet.erase(aPos);
344 : }
345 0 : }
346 :
347 0 : void SAL_CALL OStaticSet::cancelRowUpdates( ) throw(SQLException, RuntimeException)
348 : {
349 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::cancelRowUpdates" );
350 0 : }
351 :
352 0 : void SAL_CALL OStaticSet::moveToInsertRow( ) throw(SQLException, RuntimeException)
353 : {
354 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveToInsertRow" );
355 0 : }
356 :
357 0 : void SAL_CALL OStaticSet::moveToCurrentRow( ) throw(SQLException, RuntimeException)
358 : {
359 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveToCurrentRow" );
360 0 : }
361 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|