LibreOffice Module connectivity (master) 1
WTable.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <writer/WTable.hxx>
21#include <com/sun/star/sdbc/ColumnValue.hpp>
22#include <com/sun/star/sdbc/DataType.hpp>
23#include <com/sun/star/sdbc/SQLException.hpp>
24#include <com/sun/star/text/XTextDocument.hpp>
25#include <com/sun/star/text/XTextTable.hpp>
26#include <com/sun/star/text/XTextTablesSupplier.hpp>
27#include <com/sun/star/table/XCellRange.hpp>
28#include <com/sun/star/text/XText.hpp>
29#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
32#include <sal/log.hxx>
33
35{
36class XTextDocument;
37}
38
39using namespace ::com::sun::star;
40
41static void lcl_GetDataArea(const uno::Reference<text::XTextTable>& xTable, sal_Int32& rColumnCount,
42 sal_Int32& rRowCount)
43{
44 uno::Reference<container::XIndexAccess> xColumns = xTable->getColumns();
45 if (xColumns.is())
46 rColumnCount = xColumns->getCount();
47
48 uno::Reference<container::XIndexAccess> xRows = xTable->getRows();
49 if (xRows.is())
50 rRowCount = xRows->getCount() - 1; // first row (headers) is not counted
51}
52
53static void lcl_GetColumnInfo(const uno::Reference<text::XTextTable>& xTable, sal_Int32 nDocColumn,
54 bool bHasHeaders, OUString& rName, sal_Int32& rDataType,
55 bool& rCurrency)
56{
57 uno::Reference<table::XCellRange> xCellRange(xTable, uno::UNO_QUERY);
58 // get column name from first row, if range contains headers
59 if (bHasHeaders)
60 {
61 uno::Reference<text::XText> xHeaderText(
62 xCellRange->getCellByPosition(nDocColumn, /*nStartRow*/ 0), uno::UNO_QUERY);
63 if (xHeaderText.is())
64 rName = xHeaderText->getString();
65 }
66
67 rCurrency = false;
68 rDataType = sdbc::DataType::VARCHAR;
69}
70
72 const uno::Reference<text::XTextTable>& xTable, sal_Int32 nStartCol,
73 bool bHasHeaders, sal_Int32 nDBRow, sal_Int32 nDBColumn)
74{
75 sal_Int32 nDocColumn = nStartCol + nDBColumn - 1; // database counts from 1
76 sal_Int32 nDocRow = nDBRow - 1;
77 if (bHasHeaders)
78 ++nDocRow;
79
80 uno::Reference<table::XCellRange> xCellRange(xTable, uno::UNO_QUERY);
81 uno::Reference<table::XCell> xCell;
82 try
83 {
84 xCell = xCellRange->getCellByPosition(nDocColumn, nDocRow);
85 }
86 catch (const lang::IndexOutOfBoundsException& /*rException*/)
87 {
88 SAL_WARN("connectivity.writer",
89 "getCellByPosition(" << nDocColumn << ", " << nDocRow << ") failed");
90 rValue = OUString();
91 }
92
93 if (xCell.is())
94 {
95 const uno::Reference<text::XText> xText(xCell, uno::UNO_QUERY);
96 if (xText.is())
97 rValue = xText->getString();
98 }
99}
100
101namespace connectivity::writer
102{
104{
105 if (!m_xTable.is())
106 throw sdbc::SQLException();
107
108 OUString aTypeName;
110 m_pConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers());
111 const bool bStoresMixedCaseQuotedIdentifiers
112 = getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers();
113
114 for (sal_Int32 i = 0; i < m_nDataCols; i++)
115 {
116 OUString aColumnName;
117 sal_Int32 eType = sdbc::DataType::OTHER;
118 bool bCurrency = false;
119
120 lcl_GetColumnInfo(m_xTable, m_nStartCol + i, m_bHasHeaders, aColumnName, eType, bCurrency);
121
122 sal_Int32 nPrecision = 0;
123 sal_Int32 nDecimals = 0;
124
125 switch (eType)
126 {
127 case sdbc::DataType::VARCHAR:
128 aTypeName = "VARCHAR";
129 break;
130 case sdbc::DataType::DECIMAL:
131 aTypeName = "DECIMAL";
132 break;
133 case sdbc::DataType::BIT:
134 aTypeName = "BOOL";
135 break;
136 case sdbc::DataType::DATE:
137 aTypeName = "DATE";
138 break;
139 case sdbc::DataType::TIME:
140 aTypeName = "TIME";
141 break;
142 case sdbc::DataType::TIMESTAMP:
143 aTypeName = "TIMESTAMP";
144 break;
145 default:
146 SAL_WARN("connectivity.writer", "missing type name");
147 aTypeName.clear();
148 }
149
150 // check if the column name already exists
151 OUString aAlias = aColumnName;
152 auto aFind = connectivity::find(m_aColumns->begin(), m_aColumns->end(), aAlias, aCase);
153 sal_Int32 nExprCnt = 0;
154 while (aFind != m_aColumns->end())
155 {
156 aAlias = aColumnName + OUString::number(++nExprCnt);
157 aFind = connectivity::find(m_aColumns->begin(), m_aColumns->end(), aAlias, aCase);
158 }
159
161 aAlias, aTypeName, OUString(), OUString(), sdbc::ColumnValue::NULLABLE, nPrecision,
162 nDecimals, eType, false, false, bCurrency, bStoresMixedCaseQuotedIdentifiers,
164 m_aColumns->push_back(pColumn);
165 }
166}
167
169 const OUString& Name, const OUString& Type)
170 : OWriterTable_BASE(_pTables, _pConnection, Name, Type, OUString() /*Description*/,
171 OUString() /*SchemaName*/, OUString() /*CatalogName*/)
172 , m_pWriterConnection(_pConnection)
173{
174}
175
177{
178 uno::Reference<text::XTextDocument> xDoc = m_pWriterConnection->acquireDoc();
179 if (xDoc.is())
180 {
181 uno::Reference<text::XTextTablesSupplier> xTextTablesSupplier(xDoc, uno::UNO_QUERY);
182 uno::Reference<container::XNameAccess> xTables = xTextTablesSupplier->getTextTables();
183 if (xTables.is() && xTables->hasByName(m_Name))
184 {
185 m_xTable.set(xTables->getByName(m_Name), uno::UNO_QUERY);
186 if (m_xTable.is())
187 {
189 m_bHasHeaders = true;
190 }
191 }
192 }
193
194 fillColumns();
195
197}
198
200{
201 OFileTable::disposing();
202 ::osl::MutexGuard aGuard(m_aMutex);
203 m_aColumns = nullptr;
206 m_pWriterConnection = nullptr;
207}
208
209bool OWriterTable::fetchRow(OValueRefRow& _rRow, const OSQLColumns& _rCols, bool bRetrieveData)
210{
211 // read the bookmark
212
213 _rRow->setDeleted(false);
214 *(*_rRow)[0] = m_nFilePos;
215
216 if (!bRetrieveData)
217 return true;
218
219 // fields
220
221 const OValueRefVector::size_type nCount = std::min(_rRow->size(), _rCols.size() + 1);
222 for (OValueRefVector::size_type i = 1; i < nCount; i++)
223 {
224 if ((*_rRow)[i]->isBound())
225 {
227 }
228 }
229 return true;
230}
231
232} // namespace
233
234/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void lcl_GetDataArea(const uno::Reference< text::XTextTable > &xTable, sal_Int32 &rColumnCount, sal_Int32 &rRowCount)
Definition: WTable.cxx:41
static void lcl_SetValue(connectivity::ORowSetValue &rValue, const uno::Reference< text::XTextTable > &xTable, sal_Int32 nStartCol, bool bHasHeaders, sal_Int32 nDBRow, sal_Int32 nDBColumn)
Definition: WTable.cxx:71
static void lcl_GetColumnInfo(const uno::Reference< text::XTextTable > &xTable, sal_Int32 nDocColumn, bool bHasHeaders, OUString &rName, sal_Int32 &rDataType, bool &rCurrency)
Definition: WTable.cxx:53
OUString getString() const
Definition: FValue.cxx:933
Shared Table base class for Writer tables and Calc sheets.
virtual css::uno::Reference< css::sdbc::XDatabaseMetaData > SAL_CALL getMetaData() override
OConnection * m_pConnection
Definition: FTable.hxx:36
const OUString & getSchema() const
Definition: FTable.hxx:83
OUString SAL_CALL getName() override
Definition: FTable.hxx:81
::rtl::Reference< OSQLColumns > m_aColumns
Definition: FTable.hxx:38
OConnection * getConnection() const
Definition: FTable.hxx:66
css::uno::Reference< css::text::XTextDocument > const & acquireDoc()
Definition: WConnection.cxx:91
bool fetchRow(OValueRefRow &_rRow, const OSQLColumns &_rCols, bool bRetrieveData) override
Definition: WTable.cxx:209
css::uno::Reference< css::text::XTextTable > m_xTable
Definition: WTable.hxx:41
OWriterConnection * m_pWriterConnection
Definition: WTable.hxx:42
OWriterTable(sdbcx::OCollection *_pTables, OWriterConnection *_pConnection, const OUString &Name, const OUString &Type)
Definition: WTable.cxx:168
void SAL_CALL disposing() override
Definition: WTable.cxx:199
mutable::osl::Mutex m_aMutex
int nCount
DocumentType eType
#define SAL_WARN(area, stream)
Type
OSQLColumns::const_iterator find(const OSQLColumns::const_iterator &first, const OSQLColumns::const_iterator &last, std::u16string_view _rVal, const ::comphelper::UStringMixEqual &_rCase)
Definition: dbtools.cxx:1958
int i
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
OUString Name