1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <TokenWriter.hxx>
#include <com/sun/star/sdbc/XColumnLocate.hpp>
#include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
#include <core_resource.hxx>
#include <strings.hrc>
#include <strings.hxx>
#include <sqlmessage.hxx>
#include <vcl/msgbox.hxx>
#include <stringconstants.hxx>
#include <com/sun/star/sdbc/XRowUpdate.hpp>
using namespace dbaui;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::lang;
// export data
ORowSetImportExport::ORowSetImportExport( vcl::Window* _pParent,
const Reference< XResultSetUpdate >& _xResultSetUpdate,
const svx::ODataAccessDescriptor& _aDataDescriptor,
const Reference< XComponentContext >& _rM
)
: ODatabaseImportExport(_aDataDescriptor,_rM,nullptr)
,m_xTargetResultSetUpdate(_xResultSetUpdate)
,m_xTargetRowUpdate(_xResultSetUpdate,UNO_QUERY)
,m_pParent(_pParent)
,m_bAlreadyAsked(false)
{
OSL_ENSURE(_pParent,"Window can't be null!");
}
void ORowSetImportExport::initialize()
{
ODatabaseImportExport::initialize();
// do namemapping
Reference<XColumnLocate> xColumnLocate(m_xResultSet,UNO_QUERY);
OSL_ENSURE(xColumnLocate.is(),"The rowset normally should support this");
m_xTargetResultSetMetaData = Reference<XResultSetMetaDataSupplier>(m_xTargetResultSetUpdate,UNO_QUERY)->getMetaData();
if(!m_xTargetResultSetMetaData.is() || !xColumnLocate.is() || !m_xResultSetMetaData.is() )
throw SQLException(DBA_RES(STR_UNEXPECTED_ERROR),*this,"S1000",0,Any());
sal_Int32 nCount = m_xTargetResultSetMetaData->getColumnCount();
m_aColumnMapping.reserve(nCount);
m_aColumnTypes.reserve(nCount);
for (sal_Int32 i = 1;i <= nCount; ++i)
{
sal_Int32 nPos = -1; // -1 means column is autoincrement or doesn't exist
if(!m_xTargetResultSetMetaData->isAutoIncrement(i))
{
try
{
OUString sColumnName = m_xTargetResultSetMetaData->getColumnName(i);
nPos = xColumnLocate->findColumn(sColumnName);
}
catch(const SQLException&)
{
if(m_xTargetResultSetMetaData->isNullable(i))
nPos = 0; // column doesn't exist but we could set it to null
}
}
m_aColumnMapping.push_back(nPos);
if(nPos > 0)
m_aColumnTypes.push_back(m_xResultSetMetaData->getColumnType(nPos));
else
m_aColumnTypes.push_back(DataType::OTHER);
}
}
bool ORowSetImportExport::Write()
{
return true;
}
bool ORowSetImportExport::Read()
{
// check if there is any column to copy
if(std::find_if(m_aColumnMapping.begin(),m_aColumnMapping.end(),
[](sal_Int32 n) { return n > 0; }) == m_aColumnMapping.end())
return false;
bool bContinue = true;
if(m_aSelection.getLength())
{
const Any* pBegin = m_aSelection.getConstArray();
const Any* pEnd = pBegin + m_aSelection.getLength();
for(;pBegin != pEnd && bContinue;++pBegin)
{
sal_Int32 nPos = -1;
*pBegin >>= nPos;
OSL_ENSURE(nPos != -1,"Invalid position!");
bContinue = (m_xResultSet.is() && m_xResultSet->absolute(nPos) && insertNewRow());
}
}
else
{
Reference<XPropertySet> xProp(m_xResultSet,UNO_QUERY);
sal_Int32 nRowCount = 0;
sal_Int32 nCurrentRow = 0;
sal_Int32 nRowFilterIndex = 0;
if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_ISROWCOUNTFINAL) )
{
bool bFinal = false;
xProp->getPropertyValue(PROPERTY_ISROWCOUNTFINAL) >>= bFinal;
if ( !bFinal )
m_xResultSet->afterLast();
xProp->getPropertyValue(PROPERTY_ROWCOUNT) >>= nRowCount;
}
if ( !nRowCount )
{
m_xResultSet->afterLast();
nRowCount = m_xResultSet->getRow();
}
OSL_ENSURE(nRowCount,"RowCount is 0!");
m_xResultSet->beforeFirst();
while(m_xResultSet.is() && m_xResultSet->next() && bContinue && nRowCount )
{
--nRowCount;
++nCurrentRow;
++nRowFilterIndex;
bContinue = insertNewRow();
}
}
return true;
}
bool ORowSetImportExport::insertNewRow()
{
try
{
m_xTargetResultSetUpdate->moveToInsertRow();
sal_Int32 i = 1;
std::vector<sal_Int32>::const_iterator aEnd = m_aColumnMapping.end();
for (std::vector<sal_Int32>::const_iterator aIter = m_aColumnMapping.begin(); aIter != aEnd ;++aIter,++i )
{
if(*aIter > 0)
{
Any aValue;
switch(m_aColumnTypes[i-1])
{
case DataType::CHAR:
case DataType::VARCHAR:
aValue <<= m_xRow->getString(*aIter);
break;
case DataType::DECIMAL:
case DataType::NUMERIC:
aValue <<= m_xRow->getDouble(*aIter);
break;
case DataType::BIGINT:
aValue <<= m_xRow->getLong(*aIter);
break;
case DataType::FLOAT:
aValue <<= m_xRow->getFloat(*aIter);
break;
case DataType::DOUBLE:
aValue <<= m_xRow->getDouble(*aIter);
break;
case DataType::LONGVARCHAR:
aValue <<= m_xRow->getString(*aIter);
break;
case DataType::LONGVARBINARY:
aValue <<= m_xRow->getBytes(*aIter);
break;
case DataType::DATE:
aValue <<= m_xRow->getDate(*aIter);
break;
case DataType::TIME:
aValue <<= m_xRow->getTime(*aIter);
break;
case DataType::TIMESTAMP:
aValue <<= m_xRow->getTimestamp(*aIter);
break;
case DataType::BIT:
case DataType::BOOLEAN:
aValue <<= m_xRow->getBoolean(*aIter);
break;
case DataType::TINYINT:
aValue <<= m_xRow->getByte(*aIter);
break;
case DataType::SMALLINT:
aValue <<= m_xRow->getShort(*aIter);
break;
case DataType::INTEGER:
aValue <<= m_xRow->getInt(*aIter);
break;
case DataType::REAL:
aValue <<= m_xRow->getDouble(*aIter);
break;
case DataType::BINARY:
case DataType::VARBINARY:
aValue <<= m_xRow->getBytes(*aIter);
break;
case DataType::BLOB:
aValue <<= m_xRow->getBlob(*aIter);
break;
case DataType::CLOB:
aValue <<= m_xRow->getClob(*aIter);
break;
default:
SAL_WARN("dbaccess.ui", "Unknown type");
}
if(m_xRow->wasNull())
m_xTargetRowUpdate->updateNull(i);
else
m_xTargetRowUpdate->updateObject(i,aValue);
}
else if(*aIter == 0)//now we have know that we to set this column to null
m_xTargetRowUpdate->updateNull(i);
}
m_xTargetResultSetUpdate->insertRow();
}
catch(const SQLException&)
{
if(!m_bAlreadyAsked)
{
OUString sAskIfContinue = DBA_RES(STR_ERROR_OCCURRED_WHILE_COPYING);
ScopedVclPtrInstance< OSQLWarningBox > aDlg( m_pParent, sAskIfContinue, MessBoxStyle::YesNo | MessBoxStyle::DefaultYes );
if(aDlg->Execute() == RET_YES)
m_bAlreadyAsked = true;
else
return false;
}
}
return true;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|