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
|
/* -*- 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 <writer/WTable.hxx>
#include <com/sun/star/sdbc/ColumnValue.hpp>
#include <com/sun/star/sdbc/DataType.hpp>
#include <com/sun/star/sdbc/SQLException.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/text/XTextTable.hpp>
#include <com/sun/star/text/XTextTablesSupplier.hpp>
#include <com/sun/star/table/XCellRange.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
#include <writer/WConnection.hxx>
#include <connectivity/sdbcx/VColumn.hxx>
#include <sal/log.hxx>
#include <comphelper/servicehelper.hxx>
namespace com::sun::star::text
{
class XTextDocument;
}
using namespace ::com::sun::star;
static void lcl_GetDataArea(const uno::Reference<text::XTextTable>& xTable, sal_Int32& rColumnCount,
sal_Int32& rRowCount)
{
uno::Reference<container::XIndexAccess> xColumns = xTable->getColumns();
if (xColumns.is())
rColumnCount = xColumns->getCount();
uno::Reference<container::XIndexAccess> xRows = xTable->getRows();
if (xRows.is())
rRowCount = xRows->getCount() - 1; // first row (headers) is not counted
}
static void lcl_GetColumnInfo(const uno::Reference<text::XTextTable>& xTable, sal_Int32 nDocColumn,
bool bHasHeaders, OUString& rName, sal_Int32& rDataType,
bool& rCurrency)
{
uno::Reference<table::XCellRange> xCellRange(xTable, uno::UNO_QUERY);
// get column name from first row, if range contains headers
if (bHasHeaders)
{
uno::Reference<text::XText> xHeaderText(
xCellRange->getCellByPosition(nDocColumn, /*nStartRow*/ 0), uno::UNO_QUERY);
if (xHeaderText.is())
rName = xHeaderText->getString();
}
rCurrency = false;
rDataType = sdbc::DataType::VARCHAR;
}
static void lcl_SetValue(connectivity::ORowSetValue& rValue,
const uno::Reference<text::XTextTable>& xTable, sal_Int32 nStartCol,
bool bHasHeaders, sal_Int32 nDBRow, sal_Int32 nDBColumn)
{
sal_Int32 nDocColumn = nStartCol + nDBColumn - 1; // database counts from 1
sal_Int32 nDocRow = nDBRow - 1;
if (bHasHeaders)
++nDocRow;
uno::Reference<table::XCellRange> xCellRange(xTable, uno::UNO_QUERY);
uno::Reference<table::XCell> xCell;
try
{
xCell = xCellRange->getCellByPosition(nDocColumn, nDocRow);
}
catch (const lang::IndexOutOfBoundsException& /*rException*/)
{
SAL_WARN("connectivity.writer",
"getCellByPosition(" << nDocColumn << ", " << nDocRow << ") failed");
rValue = OUString();
}
if (xCell.is())
{
const uno::Reference<text::XText> xText(xCell, uno::UNO_QUERY);
if (xText.is())
rValue = xText->getString();
}
}
namespace connectivity::writer
{
void OWriterTable::fillColumns()
{
if (!m_xTable.is())
throw sdbc::SQLException();
OUString aTypeName;
::comphelper::UStringMixEqual aCase(
m_pConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers());
const bool bStoresMixedCaseQuotedIdentifiers
= getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers();
for (sal_Int32 i = 0; i < m_nDataCols; i++)
{
OUString aColumnName;
sal_Int32 eType = sdbc::DataType::OTHER;
bool bCurrency = false;
lcl_GetColumnInfo(m_xTable, m_nStartCol + i, m_bHasHeaders, aColumnName, eType, bCurrency);
sal_Int32 nPrecision = 0; //! ...
sal_Int32 nDecimals = 0; //! ...
switch (eType)
{
case sdbc::DataType::VARCHAR:
aTypeName = "VARCHAR";
break;
case sdbc::DataType::DECIMAL:
aTypeName = "DECIMAL";
break;
case sdbc::DataType::BIT:
aTypeName = "BOOL";
break;
case sdbc::DataType::DATE:
aTypeName = "DATE";
break;
case sdbc::DataType::TIME:
aTypeName = "TIME";
break;
case sdbc::DataType::TIMESTAMP:
aTypeName = "TIMESTAMP";
break;
default:
SAL_WARN("connectivity.writer", "missing type name");
aTypeName.clear();
}
// check if the column name already exists
OUString aAlias = aColumnName;
auto aFind = connectivity::find(m_aColumns->begin(), m_aColumns->end(), aAlias, aCase);
sal_Int32 nExprCnt = 0;
while (aFind != m_aColumns->end())
{
aAlias = aColumnName + OUString::number(++nExprCnt);
aFind = connectivity::find(m_aColumns->begin(), m_aColumns->end(), aAlias, aCase);
}
rtl::Reference<sdbcx::OColumn> pColumn = new sdbcx::OColumn(
aAlias, aTypeName, OUString(), OUString(), sdbc::ColumnValue::NULLABLE, nPrecision,
nDecimals, eType, false, false, bCurrency, bStoresMixedCaseQuotedIdentifiers,
m_CatalogName, getSchema(), getName());
m_aColumns->push_back(pColumn);
}
}
OWriterTable::OWriterTable(sdbcx::OCollection* _pTables, OWriterConnection* _pConnection,
const OUString& Name, const OUString& Type)
: OWriterTable_BASE(_pTables, _pConnection, Name, Type, OUString() /*Description*/,
OUString() /*SchemaName*/, OUString() /*CatalogName*/)
, m_pWriterConnection(_pConnection)
, m_nStartCol(0)
, m_nDataCols(0)
, m_bHasHeaders(false)
{
}
void OWriterTable::construct()
{
uno::Reference<text::XTextDocument> xDoc = m_pWriterConnection->acquireDoc();
if (xDoc.is())
{
uno::Reference<text::XTextTablesSupplier> xTextTablesSupplier(xDoc, uno::UNO_QUERY);
uno::Reference<container::XNameAccess> xTables = xTextTablesSupplier->getTextTables();
if (xTables.is() && xTables->hasByName(m_Name))
{
m_xTable.set(xTables->getByName(m_Name), uno::UNO_QUERY);
if (m_xTable.is())
{
lcl_GetDataArea(m_xTable, m_nDataCols, m_nDataRows);
m_bHasHeaders = true;
}
}
}
fillColumns();
refreshColumns();
}
void SAL_CALL OWriterTable::disposing()
{
OFileTable::disposing();
::osl::MutexGuard aGuard(m_aMutex);
m_aColumns = nullptr;
if (m_pWriterConnection)
m_pWriterConnection->releaseDoc();
m_pWriterConnection = nullptr;
}
const uno::Sequence<sal_Int8>& OWriterTable::getUnoTunnelId()
{
static const comphelper::UnoIdInit implId;
return implId.getSeq();
}
sal_Int64 OWriterTable::getSomething(const uno::Sequence<sal_Int8>& rId)
{
return comphelper::getSomethingImpl(rId, this,
comphelper::FallbackToGetSomethingOf<OWriterTable_BASE>{});
}
bool OWriterTable::fetchRow(OValueRefRow& _rRow, const OSQLColumns& _rCols, bool bRetrieveData)
{
// read the bookmark
_rRow->setDeleted(false);
*(*_rRow)[0] = m_nFilePos;
if (!bRetrieveData)
return true;
// fields
const OValueRefVector::size_type nCount = std::min(_rRow->size(), _rCols.size() + 1);
for (OValueRefVector::size_type i = 1; i < nCount; i++)
{
if ((*_rRow)[i]->isBound())
{
lcl_SetValue((*_rRow)[i]->get(), m_xTable, m_nStartCol, m_bHasHeaders, m_nFilePos, i);
}
}
return true;
}
} // namespace
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|