2 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
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
)
51 ,m_bAlreadyAsked(false)
53 OSL_ENSURE(pParent
,"Window can't be null!");
56 void ORowSetImportExport::initialize()
58 ODatabaseImportExport::initialize();
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
))
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
);
89 m_aColumnTypes
.push_back(m_xResultSetMetaData
->getColumnType(nPos
));
91 m_aColumnTypes
.push_back(DataType::OTHER
);
95 bool ORowSetImportExport::Write()
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; }))
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
)
115 OSL_ENSURE(nPos
!= -1,"Invalid position!");
116 bContinue
= (m_xResultSet
.is() && m_xResultSet
->absolute(nPos
) && insertNewRow());
121 Reference
<XPropertySet
> xProp(m_xResultSet
,UNO_QUERY
);
122 sal_Int32 nRowCount
= 0;
123 if ( xProp
.is() && xProp
->getPropertySetInfo()->hasPropertyByName(PROPERTY_ISROWCOUNTFINAL
) )
126 xProp
->getPropertyValue(PROPERTY_ISROWCOUNTFINAL
) >>= bFinal
;
128 m_xResultSet
->afterLast();
129 xProp
->getPropertyValue(PROPERTY_ROWCOUNT
) >>= 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
)
141 bContinue
= insertNewRow();
147 bool ORowSetImportExport::insertNewRow()
151 m_xTargetResultSetUpdate
->moveToInsertRow();
153 for (auto const& column
: m_aColumnMapping
)
158 switch(m_aColumnTypes
[i
-1])
161 case DataType::VARCHAR
:
162 aValue
<<= m_xRow
->getString(column
);
164 case DataType::DECIMAL
:
165 case DataType::NUMERIC
:
166 aValue
<<= m_xRow
->getDouble(column
);
168 case DataType::BIGINT
:
169 aValue
<<= m_xRow
->getLong(column
);
171 case DataType::FLOAT
:
172 aValue
<<= m_xRow
->getFloat(column
);
174 case DataType::DOUBLE
:
175 aValue
<<= m_xRow
->getDouble(column
);
177 case DataType::LONGVARCHAR
:
178 aValue
<<= m_xRow
->getString(column
);
180 case DataType::LONGVARBINARY
:
181 aValue
<<= m_xRow
->getBytes(column
);
184 aValue
<<= m_xRow
->getDate(column
);
187 aValue
<<= m_xRow
->getTime(column
);
189 case DataType::TIMESTAMP
:
190 aValue
<<= m_xRow
->getTimestamp(column
);
193 case DataType::BOOLEAN
:
194 aValue
<<= m_xRow
->getBoolean(column
);
196 case DataType::TINYINT
:
197 aValue
<<= m_xRow
->getByte(column
);
199 case DataType::SMALLINT
:
200 aValue
<<= m_xRow
->getShort(column
);
202 case DataType::INTEGER
:
203 aValue
<<= m_xRow
->getInt(column
);
206 aValue
<<= m_xRow
->getDouble(column
);
208 case DataType::BINARY
:
209 case DataType::VARBINARY
:
210 aValue
<<= m_xRow
->getBytes(column
);
213 aValue
<<= m_xRow
->getBlob(column
);
216 aValue
<<= m_xRow
->getClob(column
);
219 SAL_WARN("dbaccess.ui", "Unknown type");
221 if(m_xRow
->wasNull())
222 m_xTargetRowUpdate
->updateNull(i
);
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
);
230 m_xTargetResultSetUpdate
->insertRow();
232 catch(const SQLException
&)
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;
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */