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 :
31 : using namespace dbaccess;
32 : using namespace connectivity;
33 : using namespace ::com::sun::star::uno;
34 : using namespace ::com::sun::star::beans;
35 : using namespace ::com::sun::star::sdbc;
36 : using namespace ::com::sun::star::sdbcx;
37 : using namespace ::com::sun::star::container;
38 : using namespace ::com::sun::star::lang;
39 : using namespace ::osl;
40 :
41 0 : void OStaticSet::fillValueRow(ORowSetRow& _rRow,sal_Int32 /*_nPosition*/)
42 : {
43 0 : _rRow = *m_aSetIter;
44 0 : }
45 :
46 : // ::com::sun::star::sdbcx::XRowLocate
47 0 : Any SAL_CALL OStaticSet::getBookmark() throw(SQLException, RuntimeException)
48 : {
49 0 : return makeAny(getRow());
50 : }
51 :
52 0 : bool SAL_CALL OStaticSet::moveToBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
53 : {
54 0 : m_bInserted = m_bUpdated = m_bDeleted = false;
55 0 : return absolute(::comphelper::getINT32(bookmark));
56 : }
57 :
58 0 : bool SAL_CALL OStaticSet::moveRelativeToBookmark( const Any& bookmark, sal_Int32 rows ) throw(SQLException, RuntimeException)
59 : {
60 0 : m_bInserted = m_bUpdated = m_bDeleted = false;
61 0 : return absolute(::comphelper::getINT32(bookmark)+rows);
62 : }
63 :
64 0 : sal_Int32 SAL_CALL OStaticSet::compareBookmarks( const Any& _first, const Any& _second ) throw(SQLException, RuntimeException)
65 : {
66 0 : sal_Int32 nFirst = 0, nSecond = 0;
67 0 : _first >>= nFirst;
68 0 : _second >>= nSecond;
69 0 : return (nFirst < nSecond) ? CompareBookmark::LESS : ((nFirst > nSecond) ? CompareBookmark::GREATER : CompareBookmark::EQUAL);
70 : }
71 :
72 0 : bool SAL_CALL OStaticSet::hasOrderedBookmarks( ) throw(SQLException, RuntimeException)
73 : {
74 0 : return true;
75 : }
76 :
77 0 : sal_Int32 SAL_CALL OStaticSet::hashBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
78 : {
79 0 : return ::comphelper::getINT32(bookmark);
80 : }
81 :
82 0 : bool OStaticSet::fetchRow()
83 : {
84 0 : bool bRet = false;
85 0 : if ( !m_bEnd && (!m_nMaxRows || sal_Int32(m_aSet.size()) < m_nMaxRows) )
86 0 : bRet = m_xDriverSet->next();
87 0 : if ( bRet )
88 : {
89 0 : m_aSet.push_back(new connectivity::ORowVector< connectivity::ORowSetValue >(m_xSetMetaData->getColumnCount()));
90 0 : m_aSetIter = m_aSet.end() - 1;
91 0 : ((*m_aSetIter)->get())[0] = getRow();
92 0 : OCacheSet::fillValueRow(*m_aSetIter,((*m_aSetIter)->get())[0]);
93 : }
94 : else
95 0 : m_bEnd = true;
96 0 : return bRet;
97 : }
98 :
99 0 : void OStaticSet::fillAllRows()
100 : {
101 0 : if(!m_bEnd)
102 : {
103 0 : sal_Int32 nColumnCount = m_xSetMetaData->getColumnCount();
104 0 : while(m_xDriverSet->next())
105 : {
106 0 : ORowSetRow pRow = new connectivity::ORowVector< connectivity::ORowSetValue >(nColumnCount);
107 0 : m_aSet.push_back(pRow);
108 0 : m_aSetIter = m_aSet.end() - 1;
109 0 : (pRow->get())[0] = getRow();
110 0 : OCacheSet::fillValueRow(pRow,(pRow->get())[0]);
111 0 : }
112 0 : m_bEnd = true;
113 : }
114 0 : }
115 :
116 : // XResultSet
117 0 : bool SAL_CALL OStaticSet::next( ) throw(SQLException, RuntimeException)
118 : {
119 0 : m_bInserted = m_bUpdated = m_bDeleted = false;
120 :
121 0 : if(isAfterLast())
122 0 : return false;
123 0 : if(!m_bEnd) // not yet all records fetched
124 : {
125 0 : ++m_aSetIter;
126 0 : if(m_aSetIter == m_aSet.end() && !fetchRow())
127 0 : m_aSetIter = m_aSet.end();
128 : }
129 0 : else if(!isAfterLast())
130 0 : ++m_aSetIter;
131 0 : return !isAfterLast();
132 : }
133 :
134 0 : bool SAL_CALL OStaticSet::isBeforeFirst( ) throw(SQLException, RuntimeException)
135 : {
136 0 : return m_aSetIter == m_aSet.begin();
137 : }
138 :
139 0 : bool SAL_CALL OStaticSet::isAfterLast( ) throw(SQLException, RuntimeException)
140 : {
141 0 : return m_aSetIter == m_aSet.end() && m_bEnd;
142 : }
143 :
144 0 : bool SAL_CALL OStaticSet::isFirst( ) throw(SQLException, RuntimeException)
145 : {
146 0 : return m_aSetIter == m_aSet.begin()+1;
147 : }
148 :
149 0 : bool SAL_CALL OStaticSet::isLast( ) throw(SQLException, RuntimeException)
150 : {
151 0 : return m_aSetIter == m_aSet.end()-1 && m_bEnd;
152 : }
153 :
154 0 : void SAL_CALL OStaticSet::beforeFirst( ) throw(SQLException, RuntimeException)
155 : {
156 0 : m_bInserted = m_bUpdated = m_bDeleted = false;
157 0 : m_aSetIter = m_aSet.begin();
158 0 : }
159 :
160 0 : void SAL_CALL OStaticSet::afterLast( ) throw(SQLException, RuntimeException)
161 : {
162 0 : m_bInserted = m_bUpdated = m_bDeleted = false;
163 0 : fillAllRows();
164 0 : m_aSetIter = m_aSet.end();
165 0 : }
166 :
167 0 : bool SAL_CALL OStaticSet::first( ) throw(SQLException, RuntimeException)
168 : {
169 0 : m_bInserted = m_bUpdated = m_bDeleted = false;
170 0 : m_aSetIter = m_aSet.begin()+1;
171 0 : if(m_aSetIter == m_aSet.end() && !fetchRow())
172 0 : m_aSetIter = m_aSet.end();
173 :
174 0 : return m_aSetIter != m_aSet.end();
175 : }
176 :
177 0 : bool SAL_CALL OStaticSet::last( ) throw(SQLException, RuntimeException)
178 : {
179 0 : m_bInserted = m_bUpdated = m_bDeleted = false;
180 0 : fillAllRows();
181 0 : m_aSetIter = m_aSet.end()-1;
182 :
183 0 : return !isBeforeFirst() && !isAfterLast();
184 : }
185 :
186 0 : sal_Int32 SAL_CALL OStaticSet::getRow( ) throw(SQLException, RuntimeException)
187 : {
188 : OSL_ENSURE(!isAfterLast(),"getRow is not allowed when afterlast record!");
189 : OSL_ENSURE(!isBeforeFirst(),"getRow is not allowed when beforefirst record!");
190 :
191 0 : sal_Int32 nPos = m_aSet.size() - (m_aSet.end() - m_aSetIter);
192 : OSL_ENSURE(nPos > 0,"RowPos is < 0");
193 0 : return nPos;
194 : }
195 :
196 0 : bool SAL_CALL OStaticSet::absolute( sal_Int32 row ) throw(SQLException, RuntimeException)
197 : {
198 0 : m_bInserted = m_bUpdated = m_bDeleted = false;
199 : OSL_ENSURE(row,"OStaticSet::absolute: INVALID row number!");
200 : // if row greater 0 than count from end - row means last
201 0 : if(row < 0)
202 : {
203 0 : if(!m_bEnd)
204 0 : fillAllRows();
205 :
206 0 : sal_Int32 nRow = getRow();
207 0 : nRow += row;
208 0 : if(nRow <= (sal_Int32)m_aSet.size())
209 0 : m_aSetIter = m_aSet.begin() + nRow;
210 : else
211 0 : m_aSetIter = m_aSet.begin();
212 : }
213 0 : else if(row > 0)
214 : {
215 0 : if(row >= (sal_Int32)m_aSet.size())
216 : {
217 0 : if(!m_bEnd)
218 : {
219 0 : bool bNext = true;
220 0 : for(sal_Int32 i=m_aSet.size()-1;i < row && bNext;++i)
221 0 : bNext = fetchRow();
222 : }
223 :
224 0 : if(row > (sal_Int32)m_aSet.size())
225 0 : m_aSetIter = m_aSet.end(); // check again
226 : else
227 0 : m_aSetIter = m_aSet.begin() + row;
228 : }
229 : else
230 0 : m_aSetIter = m_aSet.begin() + row;
231 : }
232 :
233 0 : return m_aSetIter != m_aSet.end() && m_aSetIter != m_aSet.begin();
234 : }
235 :
236 0 : bool SAL_CALL OStaticSet::relative( sal_Int32 rows ) throw(SQLException, RuntimeException)
237 : {
238 0 : if(!rows)
239 0 : return true;
240 :
241 0 : sal_Int32 nCurPos = getRow();
242 0 : return absolute(nCurPos+rows);
243 : }
244 :
245 0 : bool SAL_CALL OStaticSet::previous( ) throw(SQLException, RuntimeException)
246 : {
247 0 : m_bInserted = m_bUpdated = m_bDeleted = false;
248 :
249 0 : if(m_aSetIter != m_aSet.begin())
250 0 : --m_aSetIter;
251 :
252 0 : return m_aSetIter != m_aSet.begin();
253 : }
254 :
255 0 : void SAL_CALL OStaticSet::refreshRow( ) throw(SQLException, RuntimeException)
256 : {
257 0 : }
258 :
259 0 : bool SAL_CALL OStaticSet::rowUpdated( ) throw(SQLException, RuntimeException)
260 : {
261 0 : return m_bUpdated;
262 : }
263 :
264 0 : bool SAL_CALL OStaticSet::rowInserted( ) throw(SQLException, RuntimeException)
265 : {
266 0 : return m_bInserted;
267 : }
268 :
269 0 : bool SAL_CALL OStaticSet::rowDeleted( ) throw(SQLException, RuntimeException)
270 : {
271 0 : return m_bDeleted;
272 : }
273 :
274 0 : Sequence< sal_Int32 > SAL_CALL OStaticSet::deleteRows( const Sequence< Any >& rows,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
275 : {
276 0 : Sequence< sal_Int32 > aRet(rows.getLength());
277 0 : const Any* pBegin = rows.getConstArray();
278 0 : const Any* pEnd = pBegin + rows.getLength();
279 0 : for(sal_Int32 i=0; pBegin != pEnd; ++pBegin,++i)
280 : {
281 0 : deleteRow(*(m_aSet.begin() + comphelper::getINT32(*pBegin)),_xTable);
282 0 : aRet.getArray()[i] = m_bDeleted ? 1 : 0;
283 : }
284 0 : return aRet;
285 : }
286 :
287 0 : void SAL_CALL OStaticSet::insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
288 : {
289 0 : OCacheSet::insertRow( _rInsertRow,_xTable);
290 0 : if(m_bInserted)
291 : {
292 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
293 0 : m_aSetIter = m_aSet.end() - 1;
294 0 : ((*m_aSetIter)->get())[0] = (_rInsertRow->get())[0] = getBookmark();
295 0 : m_bEnd = false;
296 : }
297 0 : }
298 :
299 0 : void SAL_CALL OStaticSet::updateRow(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOriginalRow,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
300 : {
301 0 : OCacheSet::updateRow( _rInsertRow,_rOriginalRow,_xTable);
302 0 : }
303 :
304 0 : void SAL_CALL OStaticSet::deleteRow(const ORowSetRow& _rDeleteRow ,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
305 : {
306 0 : OCacheSet::deleteRow(_rDeleteRow,_xTable);
307 0 : if(m_bDeleted)
308 : {
309 0 : ORowSetMatrix::iterator aPos = m_aSet.begin()+(_rDeleteRow->get())[0].getInt32();
310 0 : if(aPos == (m_aSet.end()-1))
311 0 : m_aSetIter = m_aSet.end();
312 0 : m_aSet.erase(aPos);
313 : }
314 0 : }
315 :
316 0 : void SAL_CALL OStaticSet::cancelRowUpdates( ) throw(SQLException, RuntimeException)
317 : {
318 0 : }
319 :
320 0 : void SAL_CALL OStaticSet::moveToInsertRow( ) throw(SQLException, RuntimeException)
321 : {
322 0 : }
323 :
324 0 : void SAL_CALL OStaticSet::moveToCurrentRow( ) throw(SQLException, RuntimeException)
325 : {
326 0 : }
327 :
328 0 : void OStaticSet::reset(const Reference< XResultSet> &_xDriverSet)
329 : {
330 0 : OCacheSet::construct(_xDriverSet, m_sRowSetFilter);
331 : {
332 0 : ORowSetMatrix t;
333 0 : m_aSet.swap(t);
334 : }
335 0 : m_aSetIter = m_aSet.end();
336 0 : m_bEnd = false;
337 0 : m_aSet.push_back(NULL); // this is the beforefirst record
338 0 : }
339 :
340 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|