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 "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>
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
;
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
,nullptr,rExchange
)
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
)->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
))
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::find_if(m_aColumnMapping
.begin(),m_aColumnMapping
.end(),
104 ::std::bind2nd(::std::greater
<sal_Int32
>(),0)) == m_aColumnMapping
.end())
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
)
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 sal_Int32 nCurrentRow
= 0;
124 sal_Int32 nRowFilterIndex
= 0;
125 if ( xProp
.is() && xProp
->getPropertySetInfo()->hasPropertyByName(PROPERTY_ISROWCOUNTFINAL
) )
128 xProp
->getPropertyValue(PROPERTY_ISROWCOUNTFINAL
) >>= bFinal
;
130 m_xResultSet
->afterLast();
131 xProp
->getPropertyValue(PROPERTY_ROWCOUNT
) >>= 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
)
144 if(!m_pRowMarker
|| m_pRowMarker
[nRowFilterIndex
] == nCurrentRow
)
147 bContinue
= insertNewRow();
154 bool ORowSetImportExport::insertNewRow()
158 m_xTargetResultSetUpdate
->moveToInsertRow();
160 ::std::vector
<sal_Int32
>::const_iterator aEnd
= m_aColumnMapping
.end();
161 for (::std::vector
<sal_Int32
>::const_iterator aIter
= m_aColumnMapping
.begin(); aIter
!= aEnd
;++aIter
,++i
)
166 switch(m_aColumnTypes
[i
-1])
169 case DataType::VARCHAR
:
170 aValue
<<= m_xRow
->getString(*aIter
);
172 case DataType::DECIMAL
:
173 case DataType::NUMERIC
:
174 aValue
<<= m_xRow
->getDouble(*aIter
);
176 case DataType::BIGINT
:
177 aValue
<<= m_xRow
->getLong(*aIter
);
179 case DataType::FLOAT
:
180 aValue
<<= m_xRow
->getFloat(*aIter
);
182 case DataType::DOUBLE
:
183 aValue
<<= m_xRow
->getDouble(*aIter
);
185 case DataType::LONGVARCHAR
:
186 aValue
<<= m_xRow
->getString(*aIter
);
188 case DataType::LONGVARBINARY
:
189 aValue
<<= m_xRow
->getBytes(*aIter
);
192 aValue
<<= m_xRow
->getDate(*aIter
);
195 aValue
<<= m_xRow
->getTime(*aIter
);
197 case DataType::TIMESTAMP
:
198 aValue
<<= m_xRow
->getTimestamp(*aIter
);
201 case DataType::BOOLEAN
:
202 aValue
<<= m_xRow
->getBoolean(*aIter
);
204 case DataType::TINYINT
:
205 aValue
<<= m_xRow
->getByte(*aIter
);
207 case DataType::SMALLINT
:
208 aValue
<<= m_xRow
->getShort(*aIter
);
210 case DataType::INTEGER
:
211 aValue
<<= m_xRow
->getInt(*aIter
);
214 aValue
<<= m_xRow
->getDouble(*aIter
);
216 case DataType::BINARY
:
217 case DataType::VARBINARY
:
218 aValue
<<= m_xRow
->getBytes(*aIter
);
221 aValue
<<= m_xRow
->getBlob(*aIter
);
224 aValue
<<= m_xRow
->getClob(*aIter
);
227 SAL_WARN("dbaccess.ui", "Unknown type");
229 if(m_xRow
->wasNull())
230 m_xTargetRowUpdate
->updateNull(i
);
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
&)
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;
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */