merge the formfield patch from ooo-build
[ooovba.git] / dbaccess / source / ui / misc / RowSetDrop.cxx
blob9e27554d9d700b407fbcbf86b26f15ec6fcf16c3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: RowSetDrop.cxx,v $
10 * $Revision: 1.14 $
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 #include "TokenWriter.hxx"
35 #include <com/sun/star/sdbc/XColumnLocate.hpp>
36 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
37 #include "dbu_misc.hrc"
38 #include "sqlmessage.hxx"
39 #include <vcl/msgbox.hxx>
40 #include "dbustrings.hrc"
41 #include <com/sun/star/sdbc/XRowUpdate.hpp>
42 #include <functional>
43 #include <rtl/logfile.hxx>
45 using namespace dbaui;
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::beans;
48 using namespace ::com::sun::star::container;
49 using namespace ::com::sun::star::util;
50 using namespace ::com::sun::star::sdbc;
51 using namespace ::com::sun::star::sdb;
52 using namespace ::com::sun::star::lang;
53 // using namespace ::com::sun::star::sdbcx;
55 // export data
56 ORowSetImportExport::ORowSetImportExport( Window* _pParent,
57 const Reference< XResultSetUpdate >& _xResultSetUpdate,
58 const ::svx::ODataAccessDescriptor& _aDataDescriptor,
59 const Reference< XMultiServiceFactory >& _rM,
60 const String& rExchange
62 : ODatabaseImportExport(_aDataDescriptor,_rM,NULL,rExchange)
63 ,m_xTargetResultSetUpdate(_xResultSetUpdate)
64 ,m_xTargetRowUpdate(_xResultSetUpdate,UNO_QUERY)
65 ,m_pParent(_pParent)
66 ,m_bAlreadyAsked(sal_False)
68 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::ORowSetImportExport" );
69 OSL_ENSURE(_pParent,"Window can't be null!");
71 // -----------------------------------------------------------------------------
72 void ORowSetImportExport::initialize()
74 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::initialize" );
75 ODatabaseImportExport::initialize();
76 // do namemapping
77 Reference<XColumnLocate> xColumnLocate(m_xResultSet,UNO_QUERY);
78 OSL_ENSURE(xColumnLocate.is(),"The rowset normally should support this");
80 m_xTargetResultSetMetaData = Reference<XResultSetMetaDataSupplier>(m_xTargetResultSetUpdate,UNO_QUERY)->getMetaData();
81 if(!m_xTargetResultSetMetaData.is() || !xColumnLocate.is() || !m_xResultSetMetaData.is() )
82 throw SQLException(String(ModuleRes(STR_UNEXPECTED_ERROR)),*this,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")) ,0,Any());
84 sal_Int32 nCount = m_xTargetResultSetMetaData->getColumnCount();
85 m_aColumnMapping.reserve(nCount);
86 m_aColumnTypes.reserve(nCount);
87 for (sal_Int32 i = 1;i <= nCount; ++i)
89 sal_Int32 nPos = -1; // -1 means column is autoincrement or doesn't exists
90 if(!m_xTargetResultSetMetaData->isAutoIncrement(i))
92 try
94 ::rtl::OUString sColumnName = m_xTargetResultSetMetaData->getColumnName(i);
95 nPos = xColumnLocate->findColumn(sColumnName);
97 catch(const SQLException&)
99 if(m_xTargetResultSetMetaData->isNullable(i))
100 nPos = 0; // column doesn't exists but we could set it to null
104 m_aColumnMapping.push_back(nPos);
105 if(nPos > 0)
106 m_aColumnTypes.push_back(m_xResultSetMetaData->getColumnType(nPos));
107 else
108 m_aColumnTypes.push_back(DataType::OTHER);
111 // -----------------------------------------------------------------------------
112 BOOL ORowSetImportExport::Write()
114 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::Write" );
115 return TRUE;
117 // -----------------------------------------------------------------------------
118 BOOL ORowSetImportExport::Read()
120 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::Read" );
121 // check if there is any column to copy
122 if(::std::find_if(m_aColumnMapping.begin(),m_aColumnMapping.end(),
123 ::std::bind2nd(::std::greater<sal_Int32>(),0)) == m_aColumnMapping.end())
124 return FALSE;
125 sal_Int32 nCurrentRow = 0;
126 sal_Int32 nRowFilterIndex = 0;
127 sal_Bool bContinue = sal_True;
128 if(m_aSelection.getLength())
130 const Any* pBegin = m_aSelection.getConstArray();
131 const Any* pEnd = pBegin + m_aSelection.getLength();
132 for(;pBegin != pEnd && bContinue;++pBegin)
134 sal_Int32 nPos = -1;
135 *pBegin >>= nPos;
136 OSL_ENSURE(nPos != -1,"Invalid posiotion!");
137 bContinue = (m_xResultSet.is() && m_xResultSet->absolute(nPos) && insertNewRow());
140 else
142 Reference<XPropertySet> xProp(m_xResultSet,UNO_QUERY);
143 sal_Int32 nRowCount = 0;
144 if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_ISROWCOUNTFINAL) )
146 sal_Bool bFinal = sal_False;
147 xProp->getPropertyValue(PROPERTY_ISROWCOUNTFINAL) >>= bFinal;
148 if ( !bFinal )
149 m_xResultSet->afterLast();
150 xProp->getPropertyValue(PROPERTY_ROWCOUNT) >>= nRowCount;
152 if ( !nRowCount )
154 m_xResultSet->afterLast();
155 nRowCount = m_xResultSet->getRow();
157 OSL_ENSURE(nRowCount,"RowCount is 0!");
158 m_xResultSet->beforeFirst();
159 while(m_xResultSet.is() && m_xResultSet->next() && bContinue && nRowCount )
161 --nRowCount;
162 ++nCurrentRow;
163 if(!m_pRowMarker || m_pRowMarker[nRowFilterIndex] == nCurrentRow)
165 ++nRowFilterIndex;
166 bContinue = insertNewRow();
170 return TRUE;
172 // -----------------------------------------------------------------------------
173 sal_Bool ORowSetImportExport::insertNewRow()
175 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::insertNewRow" );
178 m_xTargetResultSetUpdate->moveToInsertRow();
179 sal_Int32 i = 1;
180 ::std::vector<sal_Int32>::iterator aEnd = m_aColumnMapping.end();
181 for (::std::vector<sal_Int32>::iterator aIter = m_aColumnMapping.begin(); aIter != aEnd ;++aIter,++i )
183 if(*aIter > 0)
185 Any aValue;
186 switch(m_aColumnTypes[i-1])
188 case DataType::CHAR:
189 case DataType::VARCHAR:
190 aValue <<= m_xRow->getString(*aIter);
191 break;
192 case DataType::DECIMAL:
193 case DataType::NUMERIC:
194 aValue <<= m_xRow->getDouble(*aIter);
195 break;
196 case DataType::BIGINT:
197 aValue <<= m_xRow->getLong(*aIter);
198 break;
199 case DataType::FLOAT:
200 aValue <<= m_xRow->getFloat(*aIter);
201 break;
202 case DataType::DOUBLE:
203 aValue <<= m_xRow->getDouble(*aIter);
204 break;
205 case DataType::LONGVARCHAR:
206 aValue <<= m_xRow->getString(*aIter);
207 break;
208 case DataType::LONGVARBINARY:
209 aValue <<= m_xRow->getBytes(*aIter);
210 break;
211 case DataType::DATE:
212 aValue <<= m_xRow->getDate(*aIter);
213 break;
214 case DataType::TIME:
215 aValue <<= m_xRow->getTime(*aIter);
216 break;
217 case DataType::TIMESTAMP:
218 aValue <<= m_xRow->getTimestamp(*aIter);
219 break;
220 case DataType::BIT:
221 case DataType::BOOLEAN:
222 aValue <<= m_xRow->getBoolean(*aIter);
223 break;
224 case DataType::TINYINT:
225 aValue <<= m_xRow->getByte(*aIter);
226 break;
227 case DataType::SMALLINT:
228 aValue <<= m_xRow->getShort(*aIter);
229 break;
230 case DataType::INTEGER:
231 aValue <<= m_xRow->getInt(*aIter);
232 break;
233 case DataType::REAL:
234 aValue <<= m_xRow->getDouble(*aIter);
235 break;
236 case DataType::BINARY:
237 case DataType::VARBINARY:
238 aValue <<= m_xRow->getBytes(*aIter);
239 break;
240 default:
241 OSL_ENSURE(0,"Unknown type");
243 if(m_xRow->wasNull())
244 m_xTargetRowUpdate->updateNull(i);
245 else
246 m_xTargetRowUpdate->updateObject(i,aValue);
248 else if(*aIter == 0)//now we have know that we to set this column to null
249 m_xTargetRowUpdate->updateNull(i);
251 m_xTargetResultSetUpdate->insertRow();
253 catch(const SQLException&)
255 if(!m_bAlreadyAsked)
257 String sAskIfContinue = String(ModuleRes(STR_ERROR_OCCURED_WHILE_COPYING));
258 OSQLWarningBox aDlg( m_pParent, sAskIfContinue, WB_YES_NO | WB_DEF_YES );
259 if(aDlg.Execute() == RET_YES)
260 m_bAlreadyAsked = sal_True;
261 else
262 return sal_False;
265 return sal_True;
267 // -----------------------------------------------------------------------------