Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / misc / RowSetDrop.cxx
bloba1f5f4b936ace89277072a1993f2acee4fc48f90
2 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4 * This file is part of the LibreOffice project.
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 * This file incorporates work covered by the following license notice:
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed
14 * with this work for additional information regarding copyright
15 * ownership. The ASF licenses this file to you under the Apache
16 * License, Version 2.0 (the "License"); you may not use this file
17 * except in compliance with the License. You may obtain a copy of
18 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "TokenWriter.hxx"
22 #include <com/sun/star/sdbc/XColumnLocate.hpp>
23 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
24 #include "dbu_misc.hrc"
25 #include "sqlmessage.hxx"
26 #include <vcl/msgbox.hxx>
27 #include "dbustrings.hrc"
28 #include <com/sun/star/sdbc/XRowUpdate.hpp>
29 #include <functional>
31 using namespace dbaui;
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::beans;
34 using namespace ::com::sun::star::container;
35 using namespace ::com::sun::star::util;
36 using namespace ::com::sun::star::sdbc;
37 using namespace ::com::sun::star::sdb;
38 using namespace ::com::sun::star::lang;
40 // export data
41 ORowSetImportExport::ORowSetImportExport( vcl::Window* _pParent,
42 const Reference< XResultSetUpdate >& _xResultSetUpdate,
43 const svx::ODataAccessDescriptor& _aDataDescriptor,
44 const Reference< XComponentContext >& _rM,
45 const OUString& rExchange
47 : ODatabaseImportExport(_aDataDescriptor,_rM,NULL,rExchange)
48 ,m_xTargetResultSetUpdate(_xResultSetUpdate)
49 ,m_xTargetRowUpdate(_xResultSetUpdate,UNO_QUERY)
50 ,m_pParent(_pParent)
51 ,m_bAlreadyAsked(false)
53 OSL_ENSURE(_pParent,"Window can't be null!");
56 void ORowSetImportExport::initialize()
58 ODatabaseImportExport::initialize();
59 // do namemapping
60 Reference<XColumnLocate> xColumnLocate(m_xResultSet,UNO_QUERY);
61 OSL_ENSURE(xColumnLocate.is(),"The rowset normally should support this");
63 m_xTargetResultSetMetaData = Reference<XResultSetMetaDataSupplier>(m_xTargetResultSetUpdate,UNO_QUERY)->getMetaData();
64 if(!m_xTargetResultSetMetaData.is() || !xColumnLocate.is() || !m_xResultSetMetaData.is() )
65 throw SQLException(ModuleRes(STR_UNEXPECTED_ERROR),*this,OUString("S1000") ,0,Any());
67 sal_Int32 nCount = m_xTargetResultSetMetaData->getColumnCount();
68 m_aColumnMapping.reserve(nCount);
69 m_aColumnTypes.reserve(nCount);
70 for (sal_Int32 i = 1;i <= nCount; ++i)
72 sal_Int32 nPos = -1; // -1 means column is autoincrement or doesn't exist
73 if(!m_xTargetResultSetMetaData->isAutoIncrement(i))
75 try
77 OUString sColumnName = m_xTargetResultSetMetaData->getColumnName(i);
78 nPos = xColumnLocate->findColumn(sColumnName);
80 catch(const SQLException&)
82 if(m_xTargetResultSetMetaData->isNullable(i))
83 nPos = 0; // column doesn't exist but we could set it to null
87 m_aColumnMapping.push_back(nPos);
88 if(nPos > 0)
89 m_aColumnTypes.push_back(m_xResultSetMetaData->getColumnType(nPos));
90 else
91 m_aColumnTypes.push_back(DataType::OTHER);
95 bool ORowSetImportExport::Write()
97 return true;
100 bool ORowSetImportExport::Read()
102 // check if there is any column to copy
103 if(::std::find_if(m_aColumnMapping.begin(),m_aColumnMapping.end(),
104 ::std::bind2nd(::std::greater<sal_Int32>(),0)) == m_aColumnMapping.end())
105 return false;
106 bool bContinue = true;
107 if(m_aSelection.getLength())
109 const Any* pBegin = m_aSelection.getConstArray();
110 const Any* pEnd = pBegin + m_aSelection.getLength();
111 for(;pBegin != pEnd && bContinue;++pBegin)
113 sal_Int32 nPos = -1;
114 *pBegin >>= nPos;
115 OSL_ENSURE(nPos != -1,"Invalid position!");
116 bContinue = (m_xResultSet.is() && m_xResultSet->absolute(nPos) && insertNewRow());
119 else
121 Reference<XPropertySet> xProp(m_xResultSet,UNO_QUERY);
122 sal_Int32 nRowCount = 0;
123 sal_Int32 nCurrentRow = 0;
124 sal_Int32 nRowFilterIndex = 0;
125 if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_ISROWCOUNTFINAL) )
127 bool bFinal = false;
128 xProp->getPropertyValue(PROPERTY_ISROWCOUNTFINAL) >>= bFinal;
129 if ( !bFinal )
130 m_xResultSet->afterLast();
131 xProp->getPropertyValue(PROPERTY_ROWCOUNT) >>= nRowCount;
133 if ( !nRowCount )
135 m_xResultSet->afterLast();
136 nRowCount = m_xResultSet->getRow();
138 OSL_ENSURE(nRowCount,"RowCount is 0!");
139 m_xResultSet->beforeFirst();
140 while(m_xResultSet.is() && m_xResultSet->next() && bContinue && nRowCount )
142 --nRowCount;
143 ++nCurrentRow;
144 if(!m_pRowMarker || m_pRowMarker[nRowFilterIndex] == nCurrentRow)
146 ++nRowFilterIndex;
147 bContinue = insertNewRow();
151 return true;
154 bool ORowSetImportExport::insertNewRow()
158 m_xTargetResultSetUpdate->moveToInsertRow();
159 sal_Int32 i = 1;
160 ::std::vector<sal_Int32>::iterator aEnd = m_aColumnMapping.end();
161 for (::std::vector<sal_Int32>::iterator aIter = m_aColumnMapping.begin(); aIter != aEnd ;++aIter,++i )
163 if(*aIter > 0)
165 Any aValue;
166 switch(m_aColumnTypes[i-1])
168 case DataType::CHAR:
169 case DataType::VARCHAR:
170 aValue <<= m_xRow->getString(*aIter);
171 break;
172 case DataType::DECIMAL:
173 case DataType::NUMERIC:
174 aValue <<= m_xRow->getDouble(*aIter);
175 break;
176 case DataType::BIGINT:
177 aValue <<= m_xRow->getLong(*aIter);
178 break;
179 case DataType::FLOAT:
180 aValue <<= m_xRow->getFloat(*aIter);
181 break;
182 case DataType::DOUBLE:
183 aValue <<= m_xRow->getDouble(*aIter);
184 break;
185 case DataType::LONGVARCHAR:
186 aValue <<= m_xRow->getString(*aIter);
187 break;
188 case DataType::LONGVARBINARY:
189 aValue <<= m_xRow->getBytes(*aIter);
190 break;
191 case DataType::DATE:
192 aValue <<= m_xRow->getDate(*aIter);
193 break;
194 case DataType::TIME:
195 aValue <<= m_xRow->getTime(*aIter);
196 break;
197 case DataType::TIMESTAMP:
198 aValue <<= m_xRow->getTimestamp(*aIter);
199 break;
200 case DataType::BIT:
201 case DataType::BOOLEAN:
202 aValue <<= m_xRow->getBoolean(*aIter);
203 break;
204 case DataType::TINYINT:
205 aValue <<= m_xRow->getByte(*aIter);
206 break;
207 case DataType::SMALLINT:
208 aValue <<= m_xRow->getShort(*aIter);
209 break;
210 case DataType::INTEGER:
211 aValue <<= m_xRow->getInt(*aIter);
212 break;
213 case DataType::REAL:
214 aValue <<= m_xRow->getDouble(*aIter);
215 break;
216 case DataType::BINARY:
217 case DataType::VARBINARY:
218 aValue <<= m_xRow->getBytes(*aIter);
219 break;
220 case DataType::BLOB:
221 aValue <<= m_xRow->getBlob(*aIter);
222 break;
223 case DataType::CLOB:
224 aValue <<= m_xRow->getClob(*aIter);
225 break;
226 default:
227 SAL_WARN("dbaccess.ui", "Unknown type");
229 if(m_xRow->wasNull())
230 m_xTargetRowUpdate->updateNull(i);
231 else
232 m_xTargetRowUpdate->updateObject(i,aValue);
234 else if(*aIter == 0)//now we have know that we to set this column to null
235 m_xTargetRowUpdate->updateNull(i);
237 m_xTargetResultSetUpdate->insertRow();
239 catch(const SQLException&)
241 if(!m_bAlreadyAsked)
243 OUString sAskIfContinue = ModuleRes(STR_ERROR_OCCURRED_WHILE_COPYING);
244 ScopedVclPtrInstance< OSQLWarningBox > aDlg( m_pParent, sAskIfContinue, WB_YES_NO | WB_DEF_YES );
245 if(aDlg->Execute() == RET_YES)
246 m_bAlreadyAsked = true;
247 else
248 return false;
251 return true;
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */