2 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*************************************************************************
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * OpenOffice.org - a multi-platform office productivity suite
11 * This file is part of OpenOffice.org.
13 * OpenOffice.org is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License version 3
15 * only, as published by the Free Software Foundation.
17 * OpenOffice.org is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License version 3 for more details
21 * (a copy is included in the LICENSE file that accompanied this code).
23 * You should have received a copy of the GNU Lesser General Public License
24 * version 3 along with OpenOffice.org. If not, see
25 * <http://www.openoffice.org/license.html>
26 * for a copy of the LGPLv3 License.
28 ************************************************************************/
31 #include "TokenWriter.hxx"
32 #include <com/sun/star/sdbc/XColumnLocate.hpp>
33 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
34 #include "dbu_misc.hrc"
35 #include "sqlmessage.hxx"
36 #include <vcl/msgbox.hxx>
37 #include "dbustrings.hrc"
38 #include <com/sun/star/sdbc/XRowUpdate.hpp>
40 #include <rtl/logfile.hxx>
42 using namespace dbaui
;
43 using namespace ::com::sun::star::uno
;
44 using namespace ::com::sun::star::beans
;
45 using namespace ::com::sun::star::container
;
46 using namespace ::com::sun::star::util
;
47 using namespace ::com::sun::star::sdbc
;
48 using namespace ::com::sun::star::sdb
;
49 using namespace ::com::sun::star::lang
;
52 ORowSetImportExport::ORowSetImportExport( Window
* _pParent
,
53 const Reference
< XResultSetUpdate
>& _xResultSetUpdate
,
54 const ::svx::ODataAccessDescriptor
& _aDataDescriptor
,
55 const Reference
< XMultiServiceFactory
>& _rM
,
56 const String
& rExchange
58 : ODatabaseImportExport(_aDataDescriptor
,_rM
,NULL
,rExchange
)
59 ,m_xTargetResultSetUpdate(_xResultSetUpdate
)
60 ,m_xTargetRowUpdate(_xResultSetUpdate
,UNO_QUERY
)
62 ,m_bAlreadyAsked(sal_False
)
64 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::ORowSetImportExport" );
65 OSL_ENSURE(_pParent
,"Window can't be null!");
67 // -----------------------------------------------------------------------------
68 void ORowSetImportExport::initialize()
70 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::initialize" );
71 ODatabaseImportExport::initialize();
73 Reference
<XColumnLocate
> xColumnLocate(m_xResultSet
,UNO_QUERY
);
74 OSL_ENSURE(xColumnLocate
.is(),"The rowset normally should support this");
76 m_xTargetResultSetMetaData
= Reference
<XResultSetMetaDataSupplier
>(m_xTargetResultSetUpdate
,UNO_QUERY
)->getMetaData();
77 if(!m_xTargetResultSetMetaData
.is() || !xColumnLocate
.is() || !m_xResultSetMetaData
.is() )
78 throw SQLException(String(ModuleRes(STR_UNEXPECTED_ERROR
)),*this,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")) ,0,Any());
80 sal_Int32 nCount
= m_xTargetResultSetMetaData
->getColumnCount();
81 m_aColumnMapping
.reserve(nCount
);
82 m_aColumnTypes
.reserve(nCount
);
83 for (sal_Int32 i
= 1;i
<= nCount
; ++i
)
85 sal_Int32 nPos
= -1; // -1 means column is autoincrement or doesn't exist
86 if(!m_xTargetResultSetMetaData
->isAutoIncrement(i
))
90 ::rtl::OUString sColumnName
= m_xTargetResultSetMetaData
->getColumnName(i
);
91 nPos
= xColumnLocate
->findColumn(sColumnName
);
93 catch(const SQLException
&)
95 if(m_xTargetResultSetMetaData
->isNullable(i
))
96 nPos
= 0; // column doesn't exist but we could set it to null
100 m_aColumnMapping
.push_back(nPos
);
102 m_aColumnTypes
.push_back(m_xResultSetMetaData
->getColumnType(nPos
));
104 m_aColumnTypes
.push_back(DataType::OTHER
);
107 // -----------------------------------------------------------------------------
108 sal_Bool
ORowSetImportExport::Write()
110 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::Write" );
113 // -----------------------------------------------------------------------------
114 sal_Bool
ORowSetImportExport::Read()
116 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::Read" );
117 // check if there is any column to copy
118 if(::std::find_if(m_aColumnMapping
.begin(),m_aColumnMapping
.end(),
119 ::std::bind2nd(::std::greater
<sal_Int32
>(),0)) == m_aColumnMapping
.end())
121 sal_Bool bContinue
= sal_True
;
122 if(m_aSelection
.getLength())
124 const Any
* pBegin
= m_aSelection
.getConstArray();
125 const Any
* pEnd
= pBegin
+ m_aSelection
.getLength();
126 for(;pBegin
!= pEnd
&& bContinue
;++pBegin
)
130 OSL_ENSURE(nPos
!= -1,"Invalid position!");
131 bContinue
= (m_xResultSet
.is() && m_xResultSet
->absolute(nPos
) && insertNewRow());
136 Reference
<XPropertySet
> xProp(m_xResultSet
,UNO_QUERY
);
137 sal_Int32 nRowCount
= 0;
138 sal_Int32 nCurrentRow
= 0;
139 sal_Int32 nRowFilterIndex
= 0;
140 if ( xProp
.is() && xProp
->getPropertySetInfo()->hasPropertyByName(PROPERTY_ISROWCOUNTFINAL
) )
142 sal_Bool bFinal
= sal_False
;
143 xProp
->getPropertyValue(PROPERTY_ISROWCOUNTFINAL
) >>= bFinal
;
145 m_xResultSet
->afterLast();
146 xProp
->getPropertyValue(PROPERTY_ROWCOUNT
) >>= nRowCount
;
150 m_xResultSet
->afterLast();
151 nRowCount
= m_xResultSet
->getRow();
153 OSL_ENSURE(nRowCount
,"RowCount is 0!");
154 m_xResultSet
->beforeFirst();
155 while(m_xResultSet
.is() && m_xResultSet
->next() && bContinue
&& nRowCount
)
159 if(!m_pRowMarker
|| m_pRowMarker
[nRowFilterIndex
] == nCurrentRow
)
162 bContinue
= insertNewRow();
168 // -----------------------------------------------------------------------------
169 sal_Bool
ORowSetImportExport::insertNewRow()
171 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::insertNewRow" );
174 m_xTargetResultSetUpdate
->moveToInsertRow();
176 ::std::vector
<sal_Int32
>::iterator aEnd
= m_aColumnMapping
.end();
177 for (::std::vector
<sal_Int32
>::iterator aIter
= m_aColumnMapping
.begin(); aIter
!= aEnd
;++aIter
,++i
)
182 switch(m_aColumnTypes
[i
-1])
185 case DataType::VARCHAR
:
186 aValue
<<= m_xRow
->getString(*aIter
);
188 case DataType::DECIMAL
:
189 case DataType::NUMERIC
:
190 aValue
<<= m_xRow
->getDouble(*aIter
);
192 case DataType::BIGINT
:
193 aValue
<<= m_xRow
->getLong(*aIter
);
195 case DataType::FLOAT
:
196 aValue
<<= m_xRow
->getFloat(*aIter
);
198 case DataType::DOUBLE
:
199 aValue
<<= m_xRow
->getDouble(*aIter
);
201 case DataType::LONGVARCHAR
:
202 aValue
<<= m_xRow
->getString(*aIter
);
204 case DataType::LONGVARBINARY
:
205 aValue
<<= m_xRow
->getBytes(*aIter
);
208 aValue
<<= m_xRow
->getDate(*aIter
);
211 aValue
<<= m_xRow
->getTime(*aIter
);
213 case DataType::TIMESTAMP
:
214 aValue
<<= m_xRow
->getTimestamp(*aIter
);
217 case DataType::BOOLEAN
:
218 aValue
<<= m_xRow
->getBoolean(*aIter
);
220 case DataType::TINYINT
:
221 aValue
<<= m_xRow
->getByte(*aIter
);
223 case DataType::SMALLINT
:
224 aValue
<<= m_xRow
->getShort(*aIter
);
226 case DataType::INTEGER
:
227 aValue
<<= m_xRow
->getInt(*aIter
);
230 aValue
<<= m_xRow
->getDouble(*aIter
);
232 case DataType::BINARY
:
233 case DataType::VARBINARY
:
234 aValue
<<= m_xRow
->getBytes(*aIter
);
237 aValue
<<= m_xRow
->getBlob(*aIter
);
240 aValue
<<= m_xRow
->getClob(*aIter
);
243 OSL_FAIL("Unknown type");
245 if(m_xRow
->wasNull())
246 m_xTargetRowUpdate
->updateNull(i
);
248 m_xTargetRowUpdate
->updateObject(i
,aValue
);
250 else if(*aIter
== 0)//now we have know that we to set this column to null
251 m_xTargetRowUpdate
->updateNull(i
);
253 m_xTargetResultSetUpdate
->insertRow();
255 catch(const SQLException
&)
259 String sAskIfContinue
= String(ModuleRes(STR_ERROR_OCCURRED_WHILE_COPYING
));
260 OSQLWarningBox
aDlg( m_pParent
, sAskIfContinue
, WB_YES_NO
| WB_DEF_YES
);
261 if(aDlg
.Execute() == RET_YES
)
262 m_bAlreadyAsked
= sal_True
;
269 // -----------------------------------------------------------------------------
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */