1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "StaticSet.hxx"
22 #include <com/sun/star/sdbcx/CompareBookmark.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <connectivity/CommonTools.hxx>
25 #include <comphelper/types.hxx>
26 #include <o3tl/safeint.hxx>
27 #include <osl/diagnose.h>
29 using namespace dbaccess
;
30 using namespace connectivity
;
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::beans
;
33 using namespace ::com::sun::star::sdbc
;
34 using namespace ::com::sun::star::sdbcx
;
35 using namespace ::com::sun::star::container
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::osl
;
39 void OStaticSet::fillValueRow(ORowSetRow
& _rRow
,sal_Int32
/*_nPosition*/)
44 // css::sdbcx::XRowLocate
45 Any
OStaticSet::getBookmark()
50 bool OStaticSet::moveToBookmark( const Any
& bookmark
)
52 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
53 return absolute(::comphelper::getINT32(bookmark
));
56 sal_Int32
OStaticSet::compareBookmarks( const Any
& _first
, const Any
& _second
)
58 sal_Int32 nFirst
= 0, nSecond
= 0;
61 return (nFirst
< nSecond
) ? CompareBookmark::LESS
: ((nFirst
> nSecond
) ? CompareBookmark::GREATER
: CompareBookmark::EQUAL
);
64 bool OStaticSet::hasOrderedBookmarks( )
69 sal_Int32
OStaticSet::hashBookmark( const Any
& bookmark
)
71 return ::comphelper::getINT32(bookmark
);
74 bool OStaticSet::fetchRow()
77 if ( !m_bEnd
&& (!m_nMaxRows
|| sal_Int32(m_aSet
.size()) < m_nMaxRows
) )
78 bRet
= m_xDriverSet
->next();
81 m_aSet
.push_back(new connectivity::ORowVector
< connectivity::ORowSetValue
>(m_xSetMetaData
->getColumnCount()));
82 m_aSetIter
= m_aSet
.end() - 1;
83 (**m_aSetIter
)[0] = getRow();
84 OCacheSet::fillValueRow(*m_aSetIter
,(**m_aSetIter
)[0].getInt32());
91 void OStaticSet::fillAllRows()
96 sal_Int32 nColumnCount
= m_xSetMetaData
->getColumnCount();
97 while(m_xDriverSet
->next())
99 ORowSetRow pRow
= new connectivity::ORowVector
< connectivity::ORowSetValue
>(nColumnCount
);
100 m_aSet
.push_back(pRow
);
101 m_aSetIter
= m_aSet
.end() - 1;
102 (*pRow
)[0] = getRow();
103 OCacheSet::fillValueRow(pRow
,(*pRow
)[0].getInt32());
109 bool OStaticSet::next()
111 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
115 if(!m_bEnd
) // not yet all records fetched
118 if(m_aSetIter
== m_aSet
.end() && !fetchRow())
119 m_aSetIter
= m_aSet
.end();
121 else if(!isAfterLast())
123 return !isAfterLast();
126 bool OStaticSet::isBeforeFirst( )
128 return m_aSetIter
== m_aSet
.begin();
131 bool OStaticSet::isAfterLast( )
133 return m_aSetIter
== m_aSet
.end() && m_bEnd
;
136 void OStaticSet::beforeFirst( )
138 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
139 m_aSetIter
= m_aSet
.begin();
142 void OStaticSet::afterLast( )
144 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
146 m_aSetIter
= m_aSet
.end();
149 bool OStaticSet::first()
151 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
152 m_aSetIter
= m_aSet
.begin()+1;
153 if(m_aSetIter
== m_aSet
.end() && !fetchRow())
154 m_aSetIter
= m_aSet
.end();
156 return m_aSetIter
!= m_aSet
.end();
159 bool OStaticSet::last()
161 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
163 m_aSetIter
= m_aSet
.end()-1;
165 return !isBeforeFirst() && !isAfterLast();
168 sal_Int32
OStaticSet::getRow( )
170 OSL_ENSURE(!isAfterLast(),"getRow is not allowed when afterlast record!");
171 OSL_ENSURE(!isBeforeFirst(),"getRow is not allowed when beforefirst record!");
173 sal_Int32 nPos
= m_aSet
.size() - (m_aSet
.end() - m_aSetIter
);
174 OSL_ENSURE(nPos
> 0,"RowPos is < 0");
178 bool OStaticSet::absolute( sal_Int32 row
)
180 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
181 OSL_ENSURE(row
,"OStaticSet::absolute: INVALID row number!");
182 // if row greater 0 than count from end - row means last
188 sal_Int32 nRow
= getRow();
190 if(nRow
<= static_cast<sal_Int32
>(m_aSet
.size()))
191 m_aSetIter
= m_aSet
.begin() + nRow
;
193 m_aSetIter
= m_aSet
.begin();
197 if(o3tl::make_unsigned(row
) >= m_aSet
.size())
202 for(sal_Int32 i
=m_aSet
.size()-1;i
< row
&& bNext
;++i
)
206 if(o3tl::make_unsigned(row
) > m_aSet
.size())
207 m_aSetIter
= m_aSet
.end(); // check again
209 m_aSetIter
= m_aSet
.begin() + row
;
212 m_aSetIter
= m_aSet
.begin() + row
;
215 return m_aSetIter
!= m_aSet
.end() && m_aSetIter
!= m_aSet
.begin();
218 bool OStaticSet::previous( )
220 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
222 if(m_aSetIter
!= m_aSet
.begin())
225 return m_aSetIter
!= m_aSet
.begin();
228 void OStaticSet::refreshRow( )
232 bool OStaticSet::rowUpdated( )
237 bool OStaticSet::rowInserted( )
242 bool OStaticSet::rowDeleted( )
247 void OStaticSet::insertRow( const ORowSetRow
& _rInsertRow
,const connectivity::OSQLTable
& _xTable
)
249 OCacheSet::insertRow( _rInsertRow
,_xTable
);
252 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
253 m_aSetIter
= m_aSet
.end() - 1;
254 (**m_aSetIter
)[0] = (*_rInsertRow
)[0] = getBookmark();
259 void OStaticSet::deleteRow(const ORowSetRow
& _rDeleteRow
,const connectivity::OSQLTable
& _xTable
)
261 OCacheSet::deleteRow(_rDeleteRow
,_xTable
);
264 ORowSetMatrix::iterator aPos
= m_aSet
.begin()+(*_rDeleteRow
)[0].getInt32();
265 if(aPos
== (m_aSet
.end()-1))
266 m_aSetIter
= m_aSet
.end();
271 void OStaticSet::reset(const Reference
< XResultSet
> &_xDriverSet
)
273 OCacheSet::construct(_xDriverSet
, m_sRowSetFilter
);
274 ORowSetMatrix().swap(m_aSet
);
275 m_aSetIter
= m_aSet
.end();
277 m_aSet
.emplace_back(nullptr); // this is the beforefirst record
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */