nss: upgrade to release 3.73
[LibreOffice.git] / connectivity / source / drivers / writer / WTable.cxx
blob7236df2476dd084025702e4eb4643e1c12dcdb9e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
30 #include <writer/WConnection.hxx>
31 #include <connectivity/sdbcx/VColumn.hxx>
32 #include <sal/log.hxx>
33 #include <comphelper/servicehelper.hxx>
34 #include <cppuhelper/typeprovider.hxx>
36 namespace com::sun::star::text
38 class XTextDocument;
41 using namespace ::com::sun::star;
43 static void lcl_GetDataArea(const uno::Reference<text::XTextTable>& xTable, sal_Int32& rColumnCount,
44 sal_Int32& rRowCount)
46 uno::Reference<container::XIndexAccess> xColumns = xTable->getColumns();
47 if (xColumns.is())
48 rColumnCount = xColumns->getCount();
50 uno::Reference<container::XIndexAccess> xRows = xTable->getRows();
51 if (xRows.is())
52 rRowCount = xRows->getCount() - 1; // first row (headers) is not counted
55 static void lcl_GetColumnInfo(const uno::Reference<text::XTextTable>& xTable, sal_Int32 nDocColumn,
56 bool bHasHeaders, OUString& rName, sal_Int32& rDataType,
57 bool& rCurrency)
59 uno::Reference<table::XCellRange> xCellRange(xTable, uno::UNO_QUERY);
60 // get column name from first row, if range contains headers
61 if (bHasHeaders)
63 uno::Reference<text::XText> xHeaderText(
64 xCellRange->getCellByPosition(nDocColumn, /*nStartRow*/ 0), uno::UNO_QUERY);
65 if (xHeaderText.is())
66 rName = xHeaderText->getString();
69 rCurrency = false;
70 rDataType = sdbc::DataType::VARCHAR;
73 static void lcl_SetValue(connectivity::ORowSetValue& rValue,
74 const uno::Reference<text::XTextTable>& xTable, sal_Int32 nStartCol,
75 bool bHasHeaders, sal_Int32 nDBRow, sal_Int32 nDBColumn)
77 sal_Int32 nDocColumn = nStartCol + nDBColumn - 1; // database counts from 1
78 sal_Int32 nDocRow = nDBRow - 1;
79 if (bHasHeaders)
80 ++nDocRow;
82 uno::Reference<table::XCellRange> xCellRange(xTable, uno::UNO_QUERY);
83 uno::Reference<table::XCell> xCell;
84 try
86 xCell = xCellRange->getCellByPosition(nDocColumn, nDocRow);
88 catch (const lang::IndexOutOfBoundsException& /*rException*/)
90 SAL_WARN("connectivity.writer",
91 "getCellByPosition(" << nDocColumn << ", " << nDocRow << ") failed");
92 rValue = OUString();
95 if (xCell.is())
97 const uno::Reference<text::XText> xText(xCell, uno::UNO_QUERY);
98 if (xText.is())
99 rValue = xText->getString();
103 namespace connectivity::writer
105 void OWriterTable::fillColumns()
107 if (!m_xTable.is())
108 throw sdbc::SQLException();
110 OUString aTypeName;
111 ::comphelper::UStringMixEqual aCase(
112 m_pConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers());
113 const bool bStoresMixedCaseQuotedIdentifiers
114 = getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers();
116 for (sal_Int32 i = 0; i < m_nDataCols; i++)
118 OUString aColumnName;
119 sal_Int32 eType = sdbc::DataType::OTHER;
120 bool bCurrency = false;
122 lcl_GetColumnInfo(m_xTable, m_nStartCol + i, m_bHasHeaders, aColumnName, eType, bCurrency);
124 sal_Int32 nPrecision = 0; //! ...
125 sal_Int32 nDecimals = 0; //! ...
127 switch (eType)
129 case sdbc::DataType::VARCHAR:
130 aTypeName = "VARCHAR";
131 break;
132 case sdbc::DataType::DECIMAL:
133 aTypeName = "DECIMAL";
134 break;
135 case sdbc::DataType::BIT:
136 aTypeName = "BOOL";
137 break;
138 case sdbc::DataType::DATE:
139 aTypeName = "DATE";
140 break;
141 case sdbc::DataType::TIME:
142 aTypeName = "TIME";
143 break;
144 case sdbc::DataType::TIMESTAMP:
145 aTypeName = "TIMESTAMP";
146 break;
147 default:
148 SAL_WARN("connectivity.writer", "missing type name");
149 aTypeName.clear();
152 // check if the column name already exists
153 OUString aAlias = aColumnName;
154 auto aFind = connectivity::find(m_aColumns->begin(), m_aColumns->end(), aAlias, aCase);
155 sal_Int32 nExprCnt = 0;
156 while (aFind != m_aColumns->end())
158 aAlias = aColumnName + OUString::number(++nExprCnt);
159 aFind = connectivity::find(m_aColumns->begin(), m_aColumns->end(), aAlias, aCase);
162 auto pColumn = new sdbcx::OColumn(
163 aAlias, aTypeName, OUString(), OUString(), sdbc::ColumnValue::NULLABLE, nPrecision,
164 nDecimals, eType, false, false, bCurrency, bStoresMixedCaseQuotedIdentifiers,
165 m_CatalogName, getSchema(), getName());
166 uno::Reference<XPropertySet> xCol = pColumn;
167 m_aColumns->push_back(xCol);
171 OWriterTable::OWriterTable(sdbcx::OCollection* _pTables, OWriterConnection* _pConnection,
172 const OUString& Name, const OUString& Type)
173 : OWriterTable_BASE(_pTables, _pConnection, Name, Type, OUString() /*Description*/,
174 OUString() /*SchemaName*/, OUString() /*CatalogName*/)
175 , m_pWriterConnection(_pConnection)
176 , m_nStartCol(0)
177 , m_nDataCols(0)
178 , m_bHasHeaders(false)
182 void OWriterTable::construct()
184 uno::Reference<text::XTextDocument> xDoc = m_pWriterConnection->acquireDoc();
185 if (xDoc.is())
187 uno::Reference<text::XTextTablesSupplier> xTextTablesSupplier(xDoc, uno::UNO_QUERY);
188 uno::Reference<container::XNameAccess> xTables = xTextTablesSupplier->getTextTables();
189 if (xTables.is() && xTables->hasByName(m_Name))
191 m_xTable.set(xTables->getByName(m_Name), uno::UNO_QUERY);
192 if (m_xTable.is())
194 lcl_GetDataArea(m_xTable, m_nDataCols, m_nDataRows);
195 m_bHasHeaders = true;
200 fillColumns();
202 refreshColumns();
205 void SAL_CALL OWriterTable::disposing()
207 OFileTable::disposing();
208 ::osl::MutexGuard aGuard(m_aMutex);
209 m_aColumns = nullptr;
210 if (m_pWriterConnection)
211 m_pWriterConnection->releaseDoc();
212 m_pWriterConnection = nullptr;
215 uno::Sequence<sal_Int8> OWriterTable::getUnoTunnelId()
217 static ::cppu::OImplementationId implId;
219 return implId.getImplementationId();
222 sal_Int64 OWriterTable::getSomething(const uno::Sequence<sal_Int8>& rId)
224 return (isUnoTunnelId<OWriterTable>(rId)) ? reinterpret_cast<sal_Int64>(this)
225 : OWriterTable_BASE::getSomething(rId);
228 bool OWriterTable::fetchRow(OValueRefRow& _rRow, const OSQLColumns& _rCols, bool bRetrieveData)
230 // read the bookmark
232 _rRow->setDeleted(false);
233 *(*_rRow)[0] = m_nFilePos;
235 if (!bRetrieveData)
236 return true;
238 // fields
240 const OValueRefVector::size_type nCount = std::min(_rRow->size(), _rCols.size() + 1);
241 for (OValueRefVector::size_type i = 1; i < nCount; i++)
243 if ((*_rRow)[i]->isBound())
245 lcl_SetValue((*_rRow)[i]->get(), m_xTable, m_nStartCol, m_bHasHeaders, m_nFilePos, i);
248 return true;
251 } // namespace
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */