nss: upgrade to release 3.73
[LibreOffice.git] / dbaccess / source / ui / misc / RowSetDrop.cxx
blob065fee9ce9c0822cf9a2c94c3215303cc1b7b98b
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 <DExport.hxx>
22 #include <TokenWriter.hxx>
23 #include <com/sun/star/sdbc/XColumnLocate.hpp>
24 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
25 #include <sal/log.hxx>
26 #include <osl/diagnose.h>
27 #include <core_resource.hxx>
28 #include <strings.hrc>
29 #include <strings.hxx>
30 #include <sqlmessage.hxx>
31 #include <com/sun/star/sdbc/XRowUpdate.hpp>
33 using namespace dbaui;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::beans;
36 using namespace ::com::sun::star::container;
37 using namespace ::com::sun::star::util;
38 using namespace ::com::sun::star::sdbc;
39 using namespace ::com::sun::star::sdb;
40 using namespace ::com::sun::star::lang;
42 // export data
43 ORowSetImportExport::ORowSetImportExport(weld::Window* pParent,
44 const Reference< XResultSetUpdate >& xResultSetUpdate,
45 const svx::ODataAccessDescriptor& aDataDescriptor,
46 const Reference< XComponentContext >& rM)
47 : ODatabaseImportExport(aDataDescriptor,rM,nullptr)
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_THROW)->getMetaData();
64 if(!m_xTargetResultSetMetaData.is() || !xColumnLocate.is() || !m_xResultSetMetaData.is() )
65 throw SQLException(DBA_RES(STR_UNEXPECTED_ERROR),*this,"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 = COLUMN_POSITION_NOT_FOUND; // 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::none_of(m_aColumnMapping.begin(),m_aColumnMapping.end(),
104 [](sal_Int32 n) { return n > 0; }))
105 return false;
106 bool bContinue = true;
107 if(m_aSelection.hasElements())
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 if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_ISROWCOUNTFINAL) )
125 bool bFinal = false;
126 xProp->getPropertyValue(PROPERTY_ISROWCOUNTFINAL) >>= bFinal;
127 if ( !bFinal )
128 m_xResultSet->afterLast();
129 xProp->getPropertyValue(PROPERTY_ROWCOUNT) >>= nRowCount;
131 if ( !nRowCount )
133 m_xResultSet->afterLast();
134 nRowCount = m_xResultSet->getRow();
136 OSL_ENSURE(nRowCount,"RowCount is 0!");
137 m_xResultSet->beforeFirst();
138 while(m_xResultSet.is() && m_xResultSet->next() && bContinue && nRowCount )
140 --nRowCount;
141 bContinue = insertNewRow();
144 return true;
147 bool ORowSetImportExport::insertNewRow()
151 m_xTargetResultSetUpdate->moveToInsertRow();
152 sal_Int32 i = 1;
153 for (auto const& column : m_aColumnMapping)
155 if(column > 0)
157 Any aValue;
158 switch(m_aColumnTypes[i-1])
160 case DataType::CHAR:
161 case DataType::VARCHAR:
162 aValue <<= m_xRow->getString(column);
163 break;
164 case DataType::DECIMAL:
165 case DataType::NUMERIC:
166 aValue <<= m_xRow->getDouble(column);
167 break;
168 case DataType::BIGINT:
169 aValue <<= m_xRow->getLong(column);
170 break;
171 case DataType::FLOAT:
172 aValue <<= m_xRow->getFloat(column);
173 break;
174 case DataType::DOUBLE:
175 aValue <<= m_xRow->getDouble(column);
176 break;
177 case DataType::LONGVARCHAR:
178 aValue <<= m_xRow->getString(column);
179 break;
180 case DataType::LONGVARBINARY:
181 aValue <<= m_xRow->getBytes(column);
182 break;
183 case DataType::DATE:
184 aValue <<= m_xRow->getDate(column);
185 break;
186 case DataType::TIME:
187 aValue <<= m_xRow->getTime(column);
188 break;
189 case DataType::TIMESTAMP:
190 aValue <<= m_xRow->getTimestamp(column);
191 break;
192 case DataType::BIT:
193 case DataType::BOOLEAN:
194 aValue <<= m_xRow->getBoolean(column);
195 break;
196 case DataType::TINYINT:
197 aValue <<= m_xRow->getByte(column);
198 break;
199 case DataType::SMALLINT:
200 aValue <<= m_xRow->getShort(column);
201 break;
202 case DataType::INTEGER:
203 aValue <<= m_xRow->getInt(column);
204 break;
205 case DataType::REAL:
206 aValue <<= m_xRow->getDouble(column);
207 break;
208 case DataType::BINARY:
209 case DataType::VARBINARY:
210 aValue <<= m_xRow->getBytes(column);
211 break;
212 case DataType::BLOB:
213 aValue <<= m_xRow->getBlob(column);
214 break;
215 case DataType::CLOB:
216 aValue <<= m_xRow->getClob(column);
217 break;
218 default:
219 SAL_WARN("dbaccess.ui", "Unknown type");
221 if(m_xRow->wasNull())
222 m_xTargetRowUpdate->updateNull(i);
223 else
224 m_xTargetRowUpdate->updateObject(i,aValue);
226 else if(column == 0)//now we have know that we to set this column to null
227 m_xTargetRowUpdate->updateNull(i);
228 ++i;
230 m_xTargetResultSetUpdate->insertRow();
232 catch(const SQLException&)
234 if(!m_bAlreadyAsked)
236 OUString sAskIfContinue = DBA_RES(STR_ERROR_OCCURRED_WHILE_COPYING);
237 OSQLWarningBox aDlg(m_pParent, sAskIfContinue, MessBoxStyle::YesNo | MessBoxStyle::DefaultYes);
238 if (aDlg.run() == RET_YES)
239 m_bAlreadyAsked = true;
240 else
241 return false;
244 return true;
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */