Line data Source code
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 :
22 : #include "TokenWriter.hxx"
23 : #include <com/sun/star/sdbc/XColumnLocate.hpp>
24 : #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
25 : #include "dbu_misc.hrc"
26 : #include "sqlmessage.hxx"
27 : #include <vcl/msgbox.hxx>
28 : #include "dbustrings.hrc"
29 : #include <com/sun/star/sdbc/XRowUpdate.hpp>
30 : #include <functional>
31 : #include <rtl/logfile.hxx>
32 :
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;
41 :
42 : // export data
43 0 : ORowSetImportExport::ORowSetImportExport( Window* _pParent,
44 : const Reference< XResultSetUpdate >& _xResultSetUpdate,
45 : const ::svx::ODataAccessDescriptor& _aDataDescriptor,
46 : const Reference< XMultiServiceFactory >& _rM,
47 : const String& rExchange
48 : )
49 : : ODatabaseImportExport(_aDataDescriptor,_rM,NULL,rExchange)
50 : ,m_xTargetResultSetUpdate(_xResultSetUpdate)
51 : ,m_xTargetRowUpdate(_xResultSetUpdate,UNO_QUERY)
52 : ,m_pParent(_pParent)
53 0 : ,m_bAlreadyAsked(sal_False)
54 : {
55 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::ORowSetImportExport" );
56 : OSL_ENSURE(_pParent,"Window can't be null!");
57 0 : }
58 : // -----------------------------------------------------------------------------
59 0 : void ORowSetImportExport::initialize()
60 : {
61 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::initialize" );
62 0 : ODatabaseImportExport::initialize();
63 : // do namemapping
64 0 : Reference<XColumnLocate> xColumnLocate(m_xResultSet,UNO_QUERY);
65 : OSL_ENSURE(xColumnLocate.is(),"The rowset normally should support this");
66 :
67 0 : m_xTargetResultSetMetaData = Reference<XResultSetMetaDataSupplier>(m_xTargetResultSetUpdate,UNO_QUERY)->getMetaData();
68 0 : if(!m_xTargetResultSetMetaData.is() || !xColumnLocate.is() || !m_xResultSetMetaData.is() )
69 0 : throw SQLException(String(ModuleRes(STR_UNEXPECTED_ERROR)),*this,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")) ,0,Any());
70 :
71 0 : sal_Int32 nCount = m_xTargetResultSetMetaData->getColumnCount();
72 0 : m_aColumnMapping.reserve(nCount);
73 0 : m_aColumnTypes.reserve(nCount);
74 0 : for (sal_Int32 i = 1;i <= nCount; ++i)
75 : {
76 0 : sal_Int32 nPos = -1; // -1 means column is autoincrement or doesn't exist
77 0 : if(!m_xTargetResultSetMetaData->isAutoIncrement(i))
78 : {
79 : try
80 : {
81 0 : ::rtl::OUString sColumnName = m_xTargetResultSetMetaData->getColumnName(i);
82 0 : nPos = xColumnLocate->findColumn(sColumnName);
83 : }
84 0 : catch(const SQLException&)
85 : {
86 0 : if(m_xTargetResultSetMetaData->isNullable(i))
87 0 : nPos = 0; // column doesn't exist but we could set it to null
88 : }
89 : }
90 :
91 0 : m_aColumnMapping.push_back(nPos);
92 0 : if(nPos > 0)
93 0 : m_aColumnTypes.push_back(m_xResultSetMetaData->getColumnType(nPos));
94 : else
95 0 : m_aColumnTypes.push_back(DataType::OTHER);
96 0 : }
97 0 : }
98 : // -----------------------------------------------------------------------------
99 0 : sal_Bool ORowSetImportExport::Write()
100 : {
101 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::Write" );
102 0 : return sal_True;
103 : }
104 : // -----------------------------------------------------------------------------
105 0 : sal_Bool ORowSetImportExport::Read()
106 : {
107 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::Read" );
108 : // check if there is any column to copy
109 0 : if(::std::find_if(m_aColumnMapping.begin(),m_aColumnMapping.end(),
110 0 : ::std::bind2nd(::std::greater<sal_Int32>(),0)) == m_aColumnMapping.end())
111 0 : return sal_False;
112 0 : sal_Bool bContinue = sal_True;
113 0 : if(m_aSelection.getLength())
114 : {
115 0 : const Any* pBegin = m_aSelection.getConstArray();
116 0 : const Any* pEnd = pBegin + m_aSelection.getLength();
117 0 : for(;pBegin != pEnd && bContinue;++pBegin)
118 : {
119 0 : sal_Int32 nPos = -1;
120 0 : *pBegin >>= nPos;
121 : OSL_ENSURE(nPos != -1,"Invalid position!");
122 0 : bContinue = (m_xResultSet.is() && m_xResultSet->absolute(nPos) && insertNewRow());
123 : }
124 : }
125 : else
126 : {
127 0 : Reference<XPropertySet> xProp(m_xResultSet,UNO_QUERY);
128 0 : sal_Int32 nRowCount = 0;
129 0 : sal_Int32 nCurrentRow = 0;
130 0 : sal_Int32 nRowFilterIndex = 0;
131 0 : if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_ISROWCOUNTFINAL) )
132 : {
133 0 : sal_Bool bFinal = sal_False;
134 0 : xProp->getPropertyValue(PROPERTY_ISROWCOUNTFINAL) >>= bFinal;
135 0 : if ( !bFinal )
136 0 : m_xResultSet->afterLast();
137 0 : xProp->getPropertyValue(PROPERTY_ROWCOUNT) >>= nRowCount;
138 : }
139 0 : if ( !nRowCount )
140 : {
141 0 : m_xResultSet->afterLast();
142 0 : nRowCount = m_xResultSet->getRow();
143 : }
144 : OSL_ENSURE(nRowCount,"RowCount is 0!");
145 0 : m_xResultSet->beforeFirst();
146 0 : while(m_xResultSet.is() && m_xResultSet->next() && bContinue && nRowCount )
147 : {
148 0 : --nRowCount;
149 0 : ++nCurrentRow;
150 0 : if(!m_pRowMarker || m_pRowMarker[nRowFilterIndex] == nCurrentRow)
151 : {
152 0 : ++nRowFilterIndex;
153 0 : bContinue = insertNewRow();
154 : }
155 0 : }
156 : }
157 0 : return sal_True;
158 : }
159 : // -----------------------------------------------------------------------------
160 0 : sal_Bool ORowSetImportExport::insertNewRow()
161 : {
162 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORowSetImportExport::insertNewRow" );
163 : try
164 : {
165 0 : m_xTargetResultSetUpdate->moveToInsertRow();
166 0 : sal_Int32 i = 1;
167 0 : ::std::vector<sal_Int32>::iterator aEnd = m_aColumnMapping.end();
168 0 : for (::std::vector<sal_Int32>::iterator aIter = m_aColumnMapping.begin(); aIter != aEnd ;++aIter,++i )
169 : {
170 0 : if(*aIter > 0)
171 : {
172 0 : Any aValue;
173 0 : switch(m_aColumnTypes[i-1])
174 : {
175 : case DataType::CHAR:
176 : case DataType::VARCHAR:
177 0 : aValue <<= m_xRow->getString(*aIter);
178 0 : break;
179 : case DataType::DECIMAL:
180 : case DataType::NUMERIC:
181 0 : aValue <<= m_xRow->getDouble(*aIter);
182 0 : break;
183 : case DataType::BIGINT:
184 0 : aValue <<= m_xRow->getLong(*aIter);
185 0 : break;
186 : case DataType::FLOAT:
187 0 : aValue <<= m_xRow->getFloat(*aIter);
188 0 : break;
189 : case DataType::DOUBLE:
190 0 : aValue <<= m_xRow->getDouble(*aIter);
191 0 : break;
192 : case DataType::LONGVARCHAR:
193 0 : aValue <<= m_xRow->getString(*aIter);
194 0 : break;
195 : case DataType::LONGVARBINARY:
196 0 : aValue <<= m_xRow->getBytes(*aIter);
197 0 : break;
198 : case DataType::DATE:
199 0 : aValue <<= m_xRow->getDate(*aIter);
200 0 : break;
201 : case DataType::TIME:
202 0 : aValue <<= m_xRow->getTime(*aIter);
203 0 : break;
204 : case DataType::TIMESTAMP:
205 0 : aValue <<= m_xRow->getTimestamp(*aIter);
206 0 : break;
207 : case DataType::BIT:
208 : case DataType::BOOLEAN:
209 0 : aValue <<= m_xRow->getBoolean(*aIter);
210 0 : break;
211 : case DataType::TINYINT:
212 0 : aValue <<= m_xRow->getByte(*aIter);
213 0 : break;
214 : case DataType::SMALLINT:
215 0 : aValue <<= m_xRow->getShort(*aIter);
216 0 : break;
217 : case DataType::INTEGER:
218 0 : aValue <<= m_xRow->getInt(*aIter);
219 0 : break;
220 : case DataType::REAL:
221 0 : aValue <<= m_xRow->getDouble(*aIter);
222 0 : break;
223 : case DataType::BINARY:
224 : case DataType::VARBINARY:
225 0 : aValue <<= m_xRow->getBytes(*aIter);
226 0 : break;
227 : case DataType::BLOB:
228 0 : aValue <<= m_xRow->getBlob(*aIter);
229 0 : break;
230 : case DataType::CLOB:
231 0 : aValue <<= m_xRow->getClob(*aIter);
232 0 : break;
233 : default:
234 : OSL_FAIL("Unknown type");
235 : }
236 0 : if(m_xRow->wasNull())
237 0 : m_xTargetRowUpdate->updateNull(i);
238 : else
239 0 : m_xTargetRowUpdate->updateObject(i,aValue);
240 : }
241 0 : else if(*aIter == 0)//now we have know that we to set this column to null
242 0 : m_xTargetRowUpdate->updateNull(i);
243 : }
244 0 : m_xTargetResultSetUpdate->insertRow();
245 : }
246 0 : catch(const SQLException&)
247 : {
248 0 : if(!m_bAlreadyAsked)
249 : {
250 0 : String sAskIfContinue = String(ModuleRes(STR_ERROR_OCCURRED_WHILE_COPYING));
251 0 : OSQLWarningBox aDlg( m_pParent, sAskIfContinue, WB_YES_NO | WB_DEF_YES );
252 0 : if(aDlg.Execute() == RET_YES)
253 0 : m_bAlreadyAsked = sal_True;
254 : else
255 0 : return sal_False;
256 : }
257 : }
258 0 : return sal_True;
259 : }
260 : // -----------------------------------------------------------------------------
261 :
262 :
263 :
264 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|