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 <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>
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
;
41 void OStaticSet::fillValueRow(ORowSetRow
& _rRow
,sal_Int32
/*_nPosition*/)
46 // ::com::sun::star::sdbcx::XRowLocate
47 Any SAL_CALL
OStaticSet::getBookmark() throw(SQLException
, RuntimeException
)
49 return makeAny(getRow());
52 bool SAL_CALL
OStaticSet::moveToBookmark( const Any
& bookmark
) throw(SQLException
, RuntimeException
)
54 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
55 return absolute(::comphelper::getINT32(bookmark
));
58 bool SAL_CALL
OStaticSet::moveRelativeToBookmark( const Any
& bookmark
, sal_Int32 rows
) throw(SQLException
, RuntimeException
)
60 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
61 return absolute(::comphelper::getINT32(bookmark
)+rows
);
64 sal_Int32 SAL_CALL
OStaticSet::compareBookmarks( const Any
& _first
, const Any
& _second
) throw(SQLException
, RuntimeException
)
66 sal_Int32 nFirst
= 0, nSecond
= 0;
69 return (nFirst
< nSecond
) ? CompareBookmark::LESS
: ((nFirst
> nSecond
) ? CompareBookmark::GREATER
: CompareBookmark::EQUAL
);
72 bool SAL_CALL
OStaticSet::hasOrderedBookmarks( ) throw(SQLException
, RuntimeException
)
77 sal_Int32 SAL_CALL
OStaticSet::hashBookmark( const Any
& bookmark
) throw(SQLException
, RuntimeException
)
79 return ::comphelper::getINT32(bookmark
);
82 bool OStaticSet::fetchRow()
85 if ( !m_bEnd
&& (!m_nMaxRows
|| sal_Int32(m_aSet
.size()) < m_nMaxRows
) )
86 bRet
= m_xDriverSet
->next();
89 m_aSet
.push_back(new connectivity::ORowVector
< connectivity::ORowSetValue
>(m_xSetMetaData
->getColumnCount()));
90 m_aSetIter
= m_aSet
.end() - 1;
91 ((*m_aSetIter
)->get())[0] = getRow();
92 OCacheSet::fillValueRow(*m_aSetIter
,((*m_aSetIter
)->get())[0]);
99 void OStaticSet::fillAllRows()
103 sal_Int32 nColumnCount
= m_xSetMetaData
->getColumnCount();
104 while(m_xDriverSet
->next())
106 ORowSetRow pRow
= new connectivity::ORowVector
< connectivity::ORowSetValue
>(nColumnCount
);
107 m_aSet
.push_back(pRow
);
108 m_aSetIter
= m_aSet
.end() - 1;
109 (pRow
->get())[0] = getRow();
110 OCacheSet::fillValueRow(pRow
,(pRow
->get())[0]);
117 bool SAL_CALL
OStaticSet::next( ) throw(SQLException
, RuntimeException
)
119 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
123 if(!m_bEnd
) // not yet all records fetched
126 if(m_aSetIter
== m_aSet
.end() && !fetchRow())
127 m_aSetIter
= m_aSet
.end();
129 else if(!isAfterLast())
131 return !isAfterLast();
134 bool SAL_CALL
OStaticSet::isBeforeFirst( ) throw(SQLException
, RuntimeException
)
136 return m_aSetIter
== m_aSet
.begin();
139 bool SAL_CALL
OStaticSet::isAfterLast( ) throw(SQLException
, RuntimeException
)
141 return m_aSetIter
== m_aSet
.end() && m_bEnd
;
144 bool SAL_CALL
OStaticSet::isFirst( ) throw(SQLException
, RuntimeException
)
146 return m_aSetIter
== m_aSet
.begin()+1;
149 bool SAL_CALL
OStaticSet::isLast( ) throw(SQLException
, RuntimeException
)
151 return m_aSetIter
== m_aSet
.end()-1 && m_bEnd
;
154 void SAL_CALL
OStaticSet::beforeFirst( ) throw(SQLException
, RuntimeException
)
156 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
157 m_aSetIter
= m_aSet
.begin();
160 void SAL_CALL
OStaticSet::afterLast( ) throw(SQLException
, RuntimeException
)
162 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
164 m_aSetIter
= m_aSet
.end();
167 bool SAL_CALL
OStaticSet::first( ) throw(SQLException
, RuntimeException
)
169 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
170 m_aSetIter
= m_aSet
.begin()+1;
171 if(m_aSetIter
== m_aSet
.end() && !fetchRow())
172 m_aSetIter
= m_aSet
.end();
174 return m_aSetIter
!= m_aSet
.end();
177 bool SAL_CALL
OStaticSet::last( ) throw(SQLException
, RuntimeException
)
179 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
181 m_aSetIter
= m_aSet
.end()-1;
183 return !isBeforeFirst() && !isAfterLast();
186 sal_Int32 SAL_CALL
OStaticSet::getRow( ) throw(SQLException
, RuntimeException
)
188 OSL_ENSURE(!isAfterLast(),"getRow is not allowed when afterlast record!");
189 OSL_ENSURE(!isBeforeFirst(),"getRow is not allowed when beforefirst record!");
191 sal_Int32 nPos
= m_aSet
.size() - (m_aSet
.end() - m_aSetIter
);
192 OSL_ENSURE(nPos
> 0,"RowPos is < 0");
196 bool SAL_CALL
OStaticSet::absolute( sal_Int32 row
) throw(SQLException
, RuntimeException
)
198 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
206 sal_Int32 nRow
= getRow();
208 if(nRow
<= (sal_Int32
)m_aSet
.size())
209 m_aSetIter
= m_aSet
.begin() + nRow
;
211 m_aSetIter
= m_aSet
.begin();
215 if(row
>= (sal_Int32
)m_aSet
.size())
220 for(sal_Int32 i
=m_aSet
.size()-1;i
< row
&& bNext
;++i
)
224 if(row
> (sal_Int32
)m_aSet
.size())
225 m_aSetIter
= m_aSet
.end(); // check again
227 m_aSetIter
= m_aSet
.begin() + row
;
230 m_aSetIter
= m_aSet
.begin() + row
;
233 return m_aSetIter
!= m_aSet
.end() && m_aSetIter
!= m_aSet
.begin();
236 bool SAL_CALL
OStaticSet::relative( sal_Int32 rows
) throw(SQLException
, RuntimeException
)
241 sal_Int32 nCurPos
= getRow();
242 return absolute(nCurPos
+rows
);
245 bool SAL_CALL
OStaticSet::previous( ) throw(SQLException
, RuntimeException
)
247 m_bInserted
= m_bUpdated
= m_bDeleted
= false;
249 if(m_aSetIter
!= m_aSet
.begin())
252 return m_aSetIter
!= m_aSet
.begin();
255 void SAL_CALL
OStaticSet::refreshRow( ) throw(SQLException
, RuntimeException
)
259 bool SAL_CALL
OStaticSet::rowUpdated( ) throw(SQLException
, RuntimeException
)
264 bool SAL_CALL
OStaticSet::rowInserted( ) throw(SQLException
, RuntimeException
)
269 bool SAL_CALL
OStaticSet::rowDeleted( ) throw(SQLException
, RuntimeException
)
274 Sequence
< sal_Int32
> SAL_CALL
OStaticSet::deleteRows( const Sequence
< Any
>& rows
,const connectivity::OSQLTable
& _xTable
) throw(SQLException
, RuntimeException
)
276 Sequence
< sal_Int32
> aRet(rows
.getLength());
277 const Any
* pBegin
= rows
.getConstArray();
278 const Any
* pEnd
= pBegin
+ rows
.getLength();
279 for(sal_Int32 i
=0; pBegin
!= pEnd
; ++pBegin
,++i
)
281 deleteRow(*(m_aSet
.begin() + comphelper::getINT32(*pBegin
)),_xTable
);
282 aRet
.getArray()[i
] = m_bDeleted
? 1 : 0;
287 void SAL_CALL
OStaticSet::insertRow( const ORowSetRow
& _rInsertRow
,const connectivity::OSQLTable
& _xTable
) throw(::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
)
289 OCacheSet::insertRow( _rInsertRow
,_xTable
);
292 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 m_aSetIter
= m_aSet
.end() - 1;
294 ((*m_aSetIter
)->get())[0] = (_rInsertRow
->get())[0] = getBookmark();
299 void SAL_CALL
OStaticSet::updateRow(const ORowSetRow
& _rInsertRow
,const ORowSetRow
& _rOriginalRow
,const connectivity::OSQLTable
& _xTable
) throw(SQLException
, RuntimeException
)
301 OCacheSet::updateRow( _rInsertRow
,_rOriginalRow
,_xTable
);
304 void SAL_CALL
OStaticSet::deleteRow(const ORowSetRow
& _rDeleteRow
,const connectivity::OSQLTable
& _xTable
) throw(SQLException
, RuntimeException
)
306 OCacheSet::deleteRow(_rDeleteRow
,_xTable
);
309 ORowSetMatrix::iterator aPos
= m_aSet
.begin()+(_rDeleteRow
->get())[0].getInt32();
310 if(aPos
== (m_aSet
.end()-1))
311 m_aSetIter
= m_aSet
.end();
316 void SAL_CALL
OStaticSet::cancelRowUpdates( ) throw(SQLException
, RuntimeException
)
320 void SAL_CALL
OStaticSet::moveToInsertRow( ) throw(SQLException
, RuntimeException
)
324 void SAL_CALL
OStaticSet::moveToCurrentRow( ) throw(SQLException
, RuntimeException
)
328 void OStaticSet::reset(const Reference
< XResultSet
> &_xDriverSet
)
330 OCacheSet::construct(_xDriverSet
, m_sRowSetFilter
);
335 m_aSetIter
= m_aSet
.end();
337 m_aSet
.push_back(NULL
); // this is the beforefirst record
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */