1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: StaticSet.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef DBACCESS_CORE_API_STATICSET_HXX
35 #include "StaticSet.hxx"
37 #ifndef _COM_SUN_STAR_SDBCX_COMPAREBOOKMARK_HPP_
38 #include <com/sun/star/sdbcx/CompareBookmark.hpp>
40 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
41 #include <com/sun/star/beans/XPropertySet.hpp>
43 #ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
44 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
46 #ifndef _COM_SUN_STAR_SDBC_XPREPAREDSTATEMENT_HPP_
47 #include <com/sun/star/sdbc/XPreparedStatement.hpp>
49 #ifndef DBACCESS_SHARED_DBASTRINGS_HRC
50 #include "dbastrings.hrc"
52 #ifndef _DBASHARED_APITOOLS_HXX_
53 #include "apitools.hxx"
55 #ifndef _CONNECTIVITY_COMMONTOOLS_HXX_
56 #include <connectivity/CommonTools.hxx>
58 #ifndef _COMPHELPER_TYPES_HXX_
59 #include <comphelper/types.hxx>
61 #include <rtl/logfile.hxx>
63 using namespace dbaccess
;
64 using namespace connectivity
;
65 using namespace ::com::sun::star::uno
;
66 using namespace ::com::sun::star::beans
;
67 using namespace ::com::sun::star::sdbc
;
68 // using namespace ::com::sun::star::sdb;
69 using namespace ::com::sun::star::sdbcx
;
70 using namespace ::com::sun::star::container
;
71 using namespace ::com::sun::star::lang
;
72 // using namespace ::cppu;
73 using namespace ::osl
;
75 // -------------------------------------------------------------------------
76 void OStaticSet::fillValueRow(ORowSetRow
& _rRow
,sal_Int32
/*_nPosition*/)
78 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::fillValueRow" );
81 // -------------------------------------------------------------------------
82 // ::com::sun::star::sdbcx::XRowLocate
83 Any SAL_CALL
OStaticSet::getBookmark() throw(SQLException
, RuntimeException
)
85 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::getBookmark" );
86 return makeAny(getRow());
88 // -------------------------------------------------------------------------
89 sal_Bool SAL_CALL
OStaticSet::moveToBookmark( const Any
& bookmark
) throw(SQLException
, RuntimeException
)
91 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveToBookmark" );
92 m_bInserted
= m_bUpdated
= m_bDeleted
= sal_False
;
93 return absolute(::comphelper::getINT32(bookmark
));
95 // -------------------------------------------------------------------------
96 sal_Bool SAL_CALL
OStaticSet::moveRelativeToBookmark( const Any
& bookmark
, sal_Int32 rows
) throw(SQLException
, RuntimeException
)
98 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveRelativeToBookmark" );
99 m_bInserted
= m_bUpdated
= m_bDeleted
= sal_False
;
100 return absolute(::comphelper::getINT32(bookmark
)+rows
);
102 // -------------------------------------------------------------------------
103 sal_Int32 SAL_CALL
OStaticSet::compareBookmarks( const Any
& _first
, const Any
& _second
) throw(SQLException
, RuntimeException
)
105 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::compareBookmarks" );
106 sal_Int32 nFirst
= 0, nSecond
= 0;
109 return (nFirst
< nSecond
) ? CompareBookmark::LESS
: ((nFirst
> nSecond
) ? CompareBookmark::GREATER
: CompareBookmark::EQUAL
);
111 // -------------------------------------------------------------------------
112 sal_Bool SAL_CALL
OStaticSet::hasOrderedBookmarks( ) throw(SQLException
, RuntimeException
)
114 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::hasOrderedBookmarks" );
117 // -------------------------------------------------------------------------
118 sal_Int32 SAL_CALL
OStaticSet::hashBookmark( const Any
& bookmark
) throw(SQLException
, RuntimeException
)
120 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::hashBookmark" );
121 return ::comphelper::getINT32(bookmark
);
123 // -------------------------------------------------------------------------
124 sal_Bool
OStaticSet::fetchRow()
126 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::fetchRow" );
127 sal_Bool bRet
= sal_False
;
129 bRet
= m_xDriverSet
->next();
132 m_aSet
.push_back(new connectivity::ORowVector
< connectivity::ORowSetValue
>(m_xSetMetaData
->getColumnCount()));
133 m_aSetIter
= m_aSet
.end() - 1;
134 ((*m_aSetIter
)->get())[0] = getRow();
135 OCacheSet::fillValueRow(*m_aSetIter
,((*m_aSetIter
)->get())[0]);
141 // -------------------------------------------------------------------------
142 void OStaticSet::fillAllRows()
144 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::fillAllRows" );
147 while(m_xDriverSet
->next())
149 ORowSetRow pRow
= new connectivity::ORowVector
< connectivity::ORowSetValue
>(m_xSetMetaData
->getColumnCount());
150 m_aSet
.push_back(pRow
);
151 m_aSetIter
= m_aSet
.end() - 1;
152 (pRow
->get())[0] = getRow();
153 OCacheSet::fillValueRow(pRow
,(pRow
->get())[0]);
158 // -------------------------------------------------------------------------
160 sal_Bool SAL_CALL
OStaticSet::next( ) throw(SQLException
, RuntimeException
)
162 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::next" );
163 m_bInserted
= m_bUpdated
= m_bDeleted
= sal_False
;
167 if(!m_bEnd
) // not yet all records fetched
170 if(m_aSetIter
== m_aSet
.end() && !fetchRow())
171 m_aSetIter
= m_aSet
.end();
173 else if(!isAfterLast())
175 return !isAfterLast();
177 // -------------------------------------------------------------------------
178 sal_Bool SAL_CALL
OStaticSet::isBeforeFirst( ) throw(SQLException
, RuntimeException
)
180 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isBeforeFirst" );
181 return m_aSetIter
== m_aSet
.begin();
183 // -------------------------------------------------------------------------
184 sal_Bool SAL_CALL
OStaticSet::isAfterLast( ) throw(SQLException
, RuntimeException
)
186 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isAfterLast" );
187 return m_aSetIter
== m_aSet
.end() && m_bEnd
;
189 // -------------------------------------------------------------------------
190 sal_Bool SAL_CALL
OStaticSet::isFirst( ) throw(SQLException
, RuntimeException
)
192 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isFirst" );
193 return m_aSetIter
== m_aSet
.begin()+1;
195 // -------------------------------------------------------------------------
196 sal_Bool SAL_CALL
OStaticSet::isLast( ) throw(SQLException
, RuntimeException
)
198 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isLast" );
199 return m_aSetIter
== m_aSet
.end()-1 && m_bEnd
;
201 // -------------------------------------------------------------------------
202 void SAL_CALL
OStaticSet::beforeFirst( ) throw(SQLException
, RuntimeException
)
204 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::beforeFirst" );
205 m_bInserted
= m_bUpdated
= m_bDeleted
= sal_False
;
206 m_aSetIter
= m_aSet
.begin();
208 // -------------------------------------------------------------------------
209 void SAL_CALL
OStaticSet::afterLast( ) throw(SQLException
, RuntimeException
)
211 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::afterLast" );
212 m_bInserted
= m_bUpdated
= m_bDeleted
= sal_False
;
214 m_aSetIter
= m_aSet
.end();
216 // -------------------------------------------------------------------------
217 sal_Bool SAL_CALL
OStaticSet::first( ) throw(SQLException
, RuntimeException
)
219 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::first" );
220 m_bInserted
= m_bUpdated
= m_bDeleted
= sal_False
;
221 m_aSetIter
= m_aSet
.begin()+1;
222 if(m_aSetIter
== m_aSet
.end() && !fetchRow())
223 m_aSetIter
= m_aSet
.end();
225 return m_aSetIter
!= m_aSet
.end();
227 // -------------------------------------------------------------------------
228 sal_Bool SAL_CALL
OStaticSet::last( ) throw(SQLException
, RuntimeException
)
230 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::last" );
231 m_bInserted
= m_bUpdated
= m_bDeleted
= sal_False
;
233 m_aSetIter
= m_aSet
.end()-1;
235 return !isBeforeFirst() && !isAfterLast();
237 // -------------------------------------------------------------------------
238 sal_Int32 SAL_CALL
OStaticSet::getRow( ) throw(SQLException
, RuntimeException
)
240 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::getRow" );
241 OSL_ENSURE(!isAfterLast(),"getRow is not allowed when afterlast record!");
242 OSL_ENSURE(!isBeforeFirst(),"getRow is not allowed when beforefirst record!");
244 sal_Int32 nPos
= m_aSet
.size() - (m_aSet
.end() - m_aSetIter
);
245 OSL_ENSURE(nPos
> 0,"RowPos is < 0");
248 // -------------------------------------------------------------------------
249 sal_Bool SAL_CALL
OStaticSet::absolute( sal_Int32 row
) throw(SQLException
, RuntimeException
)
251 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::absolute" );
252 m_bInserted
= m_bUpdated
= m_bDeleted
= sal_False
;
253 OSL_ENSURE(row
,"OStaticSet::absolute: INVALID row number!");
254 // if row greater 0 than count from end - row means last
260 sal_Int32 nRow
= getRow();
262 if(nRow
<= (sal_Int32
)m_aSet
.size())
263 m_aSetIter
= m_aSet
.begin() + nRow
;
265 m_aSetIter
= m_aSet
.begin();
269 if(row
>= (sal_Int32
)m_aSet
.size())
273 sal_Bool bNext
= sal_True
;
274 for(sal_Int32 i
=m_aSet
.size()-1;i
< row
&& bNext
;++i
)
278 if(row
> (sal_Int32
)m_aSet
.size())
279 m_aSetIter
= m_aSet
.end(); // check again
281 m_aSetIter
= m_aSet
.begin() + row
;
284 m_aSetIter
= m_aSet
.begin() + row
;
287 return m_aSetIter
!= m_aSet
.end() && m_aSetIter
!= m_aSet
.begin();
289 // -------------------------------------------------------------------------
290 sal_Bool SAL_CALL
OStaticSet::relative( sal_Int32 rows
) throw(SQLException
, RuntimeException
)
292 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::relative" );
296 sal_Int32 nCurPos
= getRow();
297 return absolute(nCurPos
+rows
);
299 // -------------------------------------------------------------------------
300 sal_Bool SAL_CALL
OStaticSet::previous( ) throw(SQLException
, RuntimeException
)
302 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::previous" );
303 m_bInserted
= m_bUpdated
= m_bDeleted
= sal_False
;
305 if(m_aSetIter
!= m_aSet
.begin())
308 return m_aSetIter
!= m_aSet
.begin();
310 // -------------------------------------------------------------------------
311 void SAL_CALL
OStaticSet::refreshRow( ) throw(SQLException
, RuntimeException
)
313 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::refreshRow" );
315 // -------------------------------------------------------------------------
316 sal_Bool SAL_CALL
OStaticSet::rowUpdated( ) throw(SQLException
, RuntimeException
)
318 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::rowUpdated" );
321 // -------------------------------------------------------------------------
322 sal_Bool SAL_CALL
OStaticSet::rowInserted( ) throw(SQLException
, RuntimeException
)
324 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::rowInserted" );
327 // -------------------------------------------------------------------------
328 sal_Bool SAL_CALL
OStaticSet::rowDeleted( ) throw(SQLException
, RuntimeException
)
330 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::rowDeleted" );
333 // -------------------------------------------------------------------------
334 Sequence
< sal_Int32
> SAL_CALL
OStaticSet::deleteRows( const Sequence
< Any
>& rows
,const connectivity::OSQLTable
& _xTable
) throw(SQLException
, RuntimeException
)
336 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::deleteRows" );
337 Sequence
< sal_Int32
> aRet(rows
.getLength());
338 const Any
* pBegin
= rows
.getConstArray();
339 const Any
* pEnd
= pBegin
+ rows
.getLength();
340 for(sal_Int32 i
=0;pBegin
!= pEnd
; ++pBegin
,++i
)
342 deleteRow(*(m_aSet
.begin() + comphelper::getINT32(*pBegin
)),_xTable
);
343 aRet
.getArray()[i
] = m_bDeleted
;
347 // -------------------------------------------------------------------------
348 void SAL_CALL
OStaticSet::insertRow( const ORowSetRow
& _rInsertRow
,const connectivity::OSQLTable
& _xTable
) throw(::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
)
350 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::insertRow" );
351 OCacheSet::insertRow( _rInsertRow
,_xTable
);
354 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
355 m_aSetIter
= m_aSet
.end() - 1;
356 ((*m_aSetIter
)->get())[0] = (_rInsertRow
->get())[0] = getBookmark();
360 // -------------------------------------------------------------------------
361 void SAL_CALL
OStaticSet::updateRow(const ORowSetRow
& _rInsertRow
,const ORowSetRow
& _rOrginalRow
,const connectivity::OSQLTable
& _xTable
) throw(SQLException
, RuntimeException
)
363 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::updateRow" );
364 OCacheSet::updateRow( _rInsertRow
,_rOrginalRow
,_xTable
);
366 // -------------------------------------------------------------------------
367 void SAL_CALL
OStaticSet::deleteRow(const ORowSetRow
& _rDeleteRow
,const connectivity::OSQLTable
& _xTable
) throw(SQLException
, RuntimeException
)
369 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::deleteRow" );
370 OCacheSet::deleteRow(_rDeleteRow
,_xTable
);
373 ORowSetMatrix::iterator aPos
= m_aSet
.begin()+(_rDeleteRow
->get())[0].getInt32();
374 if(aPos
== (m_aSet
.end()-1))
375 m_aSetIter
= m_aSet
.end();
379 // -------------------------------------------------------------------------
380 void SAL_CALL
OStaticSet::cancelRowUpdates( ) throw(SQLException
, RuntimeException
)
382 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::cancelRowUpdates" );
384 // -------------------------------------------------------------------------
385 void SAL_CALL
OStaticSet::moveToInsertRow( ) throw(SQLException
, RuntimeException
)
387 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveToInsertRow" );
389 // -------------------------------------------------------------------------
390 void SAL_CALL
OStaticSet::moveToCurrentRow( ) throw(SQLException
, RuntimeException
)
392 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveToCurrentRow" );
394 // -------------------------------------------------------------------------