Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / misc / RowSetDrop.cxx
blobf0f3d47a9541b54d458daf62d62c792b55e0bfe2
2 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*************************************************************************
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * OpenOffice.org - a multi-platform office productivity suite
11 * This file is part of OpenOffice.org.
13 * OpenOffice.org is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License version 3
15 * only, as published by the Free Software Foundation.
17 * OpenOffice.org is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License version 3 for more details
21 * (a copy is included in the LICENSE file that accompanied this code).
23 * You should have received a copy of the GNU Lesser General Public License
24 * version 3 along with OpenOffice.org. If not, see
25 * <http://www.openoffice.org/license.html>
26 * for a copy of the LGPLv3 License.
28 ************************************************************************/
31 #include "TokenWriter.hxx"
32 #include <com/sun/star/sdbc/XColumnLocate.hpp>
33 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
34 #include "dbu_misc.hrc"
35 #include "sqlmessage.hxx"
36 #include <vcl/msgbox.hxx>
37 #include "dbustrings.hrc"
38 #include <com/sun/star/sdbc/XRowUpdate.hpp>
39 #include <functional>
40 #include <rtl/logfile.hxx>
42 using namespace dbaui;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star::container;
46 using namespace ::com::sun::star::util;
47 using namespace ::com::sun::star::sdbc;
48 using namespace ::com::sun::star::sdb;
49 using namespace ::com::sun::star::lang;
51 // export data
52 ORowSetImportExport::ORowSetImportExport( Window* _pParent,
53 const Reference< XResultSetUpdate >& _xResultSetUpdate,
54 const ::svx::ODataAccessDescriptor& _aDataDescriptor,
55 const Reference< XMultiServiceFactory >& _rM,
56 const String& rExchange
58 : ODatabaseImportExport(_aDataDescriptor,_rM,NULL,rExchange)
59 ,m_xTargetResultSetUpdate(_xResultSetUpdate)
60 ,m_xTargetRowUpdate(_xResultSetUpdate,UNO_QUERY)
61 ,m_pParent(_pParent)
62 ,m_bAlreadyAsked(sal_False)
64 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::ORowSetImportExport" );
65 OSL_ENSURE(_pParent,"Window can't be null!");
67 // -----------------------------------------------------------------------------
68 void ORowSetImportExport::initialize()
70 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::initialize" );
71 ODatabaseImportExport::initialize();
72 // do namemapping
73 Reference<XColumnLocate> xColumnLocate(m_xResultSet,UNO_QUERY);
74 OSL_ENSURE(xColumnLocate.is(),"The rowset normally should support this");
76 m_xTargetResultSetMetaData = Reference<XResultSetMetaDataSupplier>(m_xTargetResultSetUpdate,UNO_QUERY)->getMetaData();
77 if(!m_xTargetResultSetMetaData.is() || !xColumnLocate.is() || !m_xResultSetMetaData.is() )
78 throw SQLException(String(ModuleRes(STR_UNEXPECTED_ERROR)),*this,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")) ,0,Any());
80 sal_Int32 nCount = m_xTargetResultSetMetaData->getColumnCount();
81 m_aColumnMapping.reserve(nCount);
82 m_aColumnTypes.reserve(nCount);
83 for (sal_Int32 i = 1;i <= nCount; ++i)
85 sal_Int32 nPos = -1; // -1 means column is autoincrement or doesn't exist
86 if(!m_xTargetResultSetMetaData->isAutoIncrement(i))
88 try
90 ::rtl::OUString sColumnName = m_xTargetResultSetMetaData->getColumnName(i);
91 nPos = xColumnLocate->findColumn(sColumnName);
93 catch(const SQLException&)
95 if(m_xTargetResultSetMetaData->isNullable(i))
96 nPos = 0; // column doesn't exist but we could set it to null
100 m_aColumnMapping.push_back(nPos);
101 if(nPos > 0)
102 m_aColumnTypes.push_back(m_xResultSetMetaData->getColumnType(nPos));
103 else
104 m_aColumnTypes.push_back(DataType::OTHER);
107 // -----------------------------------------------------------------------------
108 sal_Bool ORowSetImportExport::Write()
110 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::Write" );
111 return sal_True;
113 // -----------------------------------------------------------------------------
114 sal_Bool ORowSetImportExport::Read()
116 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::Read" );
117 // check if there is any column to copy
118 if(::std::find_if(m_aColumnMapping.begin(),m_aColumnMapping.end(),
119 ::std::bind2nd(::std::greater<sal_Int32>(),0)) == m_aColumnMapping.end())
120 return sal_False;
121 sal_Bool bContinue = sal_True;
122 if(m_aSelection.getLength())
124 const Any* pBegin = m_aSelection.getConstArray();
125 const Any* pEnd = pBegin + m_aSelection.getLength();
126 for(;pBegin != pEnd && bContinue;++pBegin)
128 sal_Int32 nPos = -1;
129 *pBegin >>= nPos;
130 OSL_ENSURE(nPos != -1,"Invalid position!");
131 bContinue = (m_xResultSet.is() && m_xResultSet->absolute(nPos) && insertNewRow());
134 else
136 Reference<XPropertySet> xProp(m_xResultSet,UNO_QUERY);
137 sal_Int32 nRowCount = 0;
138 sal_Int32 nCurrentRow = 0;
139 sal_Int32 nRowFilterIndex = 0;
140 if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_ISROWCOUNTFINAL) )
142 sal_Bool bFinal = sal_False;
143 xProp->getPropertyValue(PROPERTY_ISROWCOUNTFINAL) >>= bFinal;
144 if ( !bFinal )
145 m_xResultSet->afterLast();
146 xProp->getPropertyValue(PROPERTY_ROWCOUNT) >>= nRowCount;
148 if ( !nRowCount )
150 m_xResultSet->afterLast();
151 nRowCount = m_xResultSet->getRow();
153 OSL_ENSURE(nRowCount,"RowCount is 0!");
154 m_xResultSet->beforeFirst();
155 while(m_xResultSet.is() && m_xResultSet->next() && bContinue && nRowCount )
157 --nRowCount;
158 ++nCurrentRow;
159 if(!m_pRowMarker || m_pRowMarker[nRowFilterIndex] == nCurrentRow)
161 ++nRowFilterIndex;
162 bContinue = insertNewRow();
166 return sal_True;
168 // -----------------------------------------------------------------------------
169 sal_Bool ORowSetImportExport::insertNewRow()
171 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::insertNewRow" );
174 m_xTargetResultSetUpdate->moveToInsertRow();
175 sal_Int32 i = 1;
176 ::std::vector<sal_Int32>::iterator aEnd = m_aColumnMapping.end();
177 for (::std::vector<sal_Int32>::iterator aIter = m_aColumnMapping.begin(); aIter != aEnd ;++aIter,++i )
179 if(*aIter > 0)
181 Any aValue;
182 switch(m_aColumnTypes[i-1])
184 case DataType::CHAR:
185 case DataType::VARCHAR:
186 aValue <<= m_xRow->getString(*aIter);
187 break;
188 case DataType::DECIMAL:
189 case DataType::NUMERIC:
190 aValue <<= m_xRow->getDouble(*aIter);
191 break;
192 case DataType::BIGINT:
193 aValue <<= m_xRow->getLong(*aIter);
194 break;
195 case DataType::FLOAT:
196 aValue <<= m_xRow->getFloat(*aIter);
197 break;
198 case DataType::DOUBLE:
199 aValue <<= m_xRow->getDouble(*aIter);
200 break;
201 case DataType::LONGVARCHAR:
202 aValue <<= m_xRow->getString(*aIter);
203 break;
204 case DataType::LONGVARBINARY:
205 aValue <<= m_xRow->getBytes(*aIter);
206 break;
207 case DataType::DATE:
208 aValue <<= m_xRow->getDate(*aIter);
209 break;
210 case DataType::TIME:
211 aValue <<= m_xRow->getTime(*aIter);
212 break;
213 case DataType::TIMESTAMP:
214 aValue <<= m_xRow->getTimestamp(*aIter);
215 break;
216 case DataType::BIT:
217 case DataType::BOOLEAN:
218 aValue <<= m_xRow->getBoolean(*aIter);
219 break;
220 case DataType::TINYINT:
221 aValue <<= m_xRow->getByte(*aIter);
222 break;
223 case DataType::SMALLINT:
224 aValue <<= m_xRow->getShort(*aIter);
225 break;
226 case DataType::INTEGER:
227 aValue <<= m_xRow->getInt(*aIter);
228 break;
229 case DataType::REAL:
230 aValue <<= m_xRow->getDouble(*aIter);
231 break;
232 case DataType::BINARY:
233 case DataType::VARBINARY:
234 aValue <<= m_xRow->getBytes(*aIter);
235 break;
236 case DataType::BLOB:
237 aValue <<= m_xRow->getBlob(*aIter);
238 break;
239 case DataType::CLOB:
240 aValue <<= m_xRow->getClob(*aIter);
241 break;
242 default:
243 OSL_FAIL("Unknown type");
245 if(m_xRow->wasNull())
246 m_xTargetRowUpdate->updateNull(i);
247 else
248 m_xTargetRowUpdate->updateObject(i,aValue);
250 else if(*aIter == 0)//now we have know that we to set this column to null
251 m_xTargetRowUpdate->updateNull(i);
253 m_xTargetResultSetUpdate->insertRow();
255 catch(const SQLException&)
257 if(!m_bAlreadyAsked)
259 String sAskIfContinue = String(ModuleRes(STR_ERROR_OCCURRED_WHILE_COPYING));
260 OSQLWarningBox aDlg( m_pParent, sAskIfContinue, WB_YES_NO | WB_DEF_YES );
261 if(aDlg.Execute() == RET_YES)
262 m_bAlreadyAsked = sal_True;
263 else
264 return sal_False;
267 return sal_True;
269 // -----------------------------------------------------------------------------
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */