LibreOffice Module dbaccess (master) 1
RowSetDrop.cxx
Go to the documentation of this file.
1
2/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3/*
4 * This file is part of the LibreOffice project.
5 *
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/.
9 *
10 * This file incorporates work covered by the following license notice:
11 *
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 .
19 */
20
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>
32
33using namespace dbaui;
34using namespace ::com::sun::star::uno;
35using namespace ::com::sun::star::beans;
36using namespace ::com::sun::star::container;
37using namespace ::com::sun::star::util;
38using namespace ::com::sun::star::sdbc;
39using namespace ::com::sun::star::sdb;
40using namespace ::com::sun::star::lang;
41
42// export data
43ORowSetImportExport::ORowSetImportExport(weld::Window* pParent,
44 const Reference< XResultSetUpdate >& xResultSetUpdate,
45 const svx::ODataAccessDescriptor& aDataDescriptor,
47 : ODatabaseImportExport(aDataDescriptor,rM,nullptr)
48 ,m_xTargetResultSetUpdate(xResultSetUpdate)
49 ,m_xTargetRowUpdate(xResultSetUpdate,UNO_QUERY)
50 ,m_pParent(pParent)
51 ,m_bAlreadyAsked(false)
52{
53 OSL_ENSURE(pParent,"Window can't be null!");
54}
55
57{
59 // do namemapping
60 Reference<XColumnLocate> xColumnLocate(m_xResultSet,UNO_QUERY);
61 OSL_ENSURE(xColumnLocate.is(),"The rowset normally should support this");
62
64 if(!m_xTargetResultSetMetaData.is() || !xColumnLocate.is() || !m_xResultSetMetaData.is() )
65 throw SQLException(DBA_RES(STR_UNEXPECTED_ERROR),*this,"S1000",0,Any());
66
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)
71 {
72 sal_Int32 nPos = COLUMN_POSITION_NOT_FOUND; // means column is autoincrement or doesn't exist
73 if(!m_xTargetResultSetMetaData->isAutoIncrement(i))
74 {
75 try
76 {
77 OUString sColumnName = m_xTargetResultSetMetaData->getColumnName(i);
78 nPos = xColumnLocate->findColumn(sColumnName);
79 }
80 catch(const SQLException&)
81 {
82 if(m_xTargetResultSetMetaData->isNullable(i))
83 nPos = 0; // column doesn't exist but we could set it to null
84 }
85 }
86
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);
92 }
93}
94
96{
97 return true;
98}
99
101{
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())
108 {
109 const Any* pBegin = m_aSelection.getConstArray();
110 const Any* pEnd = pBegin + m_aSelection.getLength();
111 for(;pBegin != pEnd && bContinue;++pBegin)
112 {
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());
117 }
118 }
119 else
120 {
121 Reference<XPropertySet> xProp(m_xResultSet,UNO_QUERY);
122 sal_Int32 nRowCount = 0;
123 if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_ISROWCOUNTFINAL) )
124 {
125 bool bFinal = false;
126 xProp->getPropertyValue(PROPERTY_ISROWCOUNTFINAL) >>= bFinal;
127 if ( !bFinal )
128 m_xResultSet->afterLast();
129 xProp->getPropertyValue(PROPERTY_ROWCOUNT) >>= nRowCount;
130 }
131 if ( !nRowCount )
132 {
133 m_xResultSet->afterLast();
134 nRowCount = m_xResultSet->getRow();
135 }
136 OSL_ENSURE(nRowCount,"RowCount is 0!");
137 m_xResultSet->beforeFirst();
138 while(m_xResultSet.is() && m_xResultSet->next() && bContinue && nRowCount )
139 {
140 --nRowCount;
141 bContinue = insertNewRow();
142 }
143 }
144 return true;
145}
146
148{
149 try
150 {
151 m_xTargetResultSetUpdate->moveToInsertRow();
152 sal_Int32 i = 1;
153 for (auto const& column : m_aColumnMapping)
154 {
155 if(column > 0)
156 {
157 Any aValue;
158 switch(m_aColumnTypes[i-1])
159 {
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");
220 }
221 if(m_xRow->wasNull())
222 m_xTargetRowUpdate->updateNull(i);
223 else
224 m_xTargetRowUpdate->updateObject(i,aValue);
225 }
226 else if(column == 0)//now we have know that we to set this column to null
227 m_xTargetRowUpdate->updateNull(i);
228 ++i;
229 }
230 m_xTargetResultSetUpdate->insertRow();
231 }
232 catch(const SQLException&)
233 {
234 if(!m_bAlreadyAsked)
235 {
236 OUString sAskIfContinue = DBA_RES(STR_ERROR_OCCURRED_WHILE_COPYING);
238 if (aDlg.run() == RET_YES)
239 m_bAlreadyAsked = true;
240 else
241 return false;
242 }
243 }
244 return true;
245}
246
247/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define COLUMN_POSITION_NOT_FOUND
Definition: DExport.hxx:48
css::uno::Sequence< css::uno::Any > m_aSelection
Definition: TokenWriter.hxx:53
css::uno::Reference< css::sdbc::XResultSet > m_xResultSet
Definition: TokenWriter.hxx:59
css::uno::Reference< css::sdbc::XResultSetMetaData > m_xResultSetMetaData
Definition: TokenWriter.hxx:62
css::uno::Reference< css::sdbc::XRow > m_xRow
Definition: TokenWriter.hxx:60
virtual void initialize() override
Definition: RowSetDrop.cxx:56
css::uno::Reference< css::sdbc::XResultSetUpdate > m_xTargetResultSetUpdate
css::uno::Reference< css::sdbc::XRowUpdate > m_xTargetRowUpdate
std::vector< sal_Int32 > m_aColumnTypes
virtual bool Write() override
Definition: RowSetDrop.cxx:95
virtual bool Read() override
Definition: RowSetDrop.cxx:100
std::vector< sal_Int32 > m_aColumnMapping
css::uno::Reference< css::sdbc::XResultSetMetaData > m_xTargetResultSetMetaData
virtual short run()
#define DBA_RES(id)
int nCount
sal_Int64 n
sal_uInt16 nPos
#define SAL_WARN(area, stream)
int i
constexpr OUStringLiteral PROPERTY_ISROWCOUNTFINAL(u"IsRowCountFinal")
constexpr OUStringLiteral PROPERTY_ROWCOUNT(u"RowCount")
RET_YES