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