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