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/text/XTextTable.hpp>
24 #include <com/sun/star/text/XTextTablesSupplier.hpp>
25 #include <com/sun/star/table/XCellRange.hpp>
26 #include <com/sun/star/text/XText.hpp>
27 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
28 #include <writer/WConnection.hxx>
29 #include <connectivity/sdbcx/VColumn.hxx>
30 #include <sal/log.hxx>
31 #include <comphelper/servicehelper.hxx>
32 #include <cppuhelper/typeprovider.hxx>
33 
34 namespace com
35 {
36 namespace sun
37 {
38 namespace star
39 {
40 namespace text
41 {
42 class XTextDocument;
43 }
44 }
45 }
46 }
47 
48 using namespace ::com::sun::star;
49 
lcl_GetDataArea(const uno::Reference<text::XTextTable> & xTable,sal_Int32 & rColumnCount,sal_Int32 & rRowCount)50 static void lcl_GetDataArea(const uno::Reference<text::XTextTable>& xTable, sal_Int32& rColumnCount,
51                             sal_Int32& rRowCount)
52 {
53     uno::Reference<container::XIndexAccess> xColumns = xTable->getColumns();
54     if (xColumns.is())
55         rColumnCount = xColumns->getCount();
56 
57     uno::Reference<container::XIndexAccess> xRows = xTable->getRows();
58     if (xRows.is())
59         rRowCount = xRows->getCount() - 1; // first row (headers) is not counted
60 }
61 
lcl_GetColumnInfo(const uno::Reference<text::XTextTable> & xTable,sal_Int32 nDocColumn,bool bHasHeaders,OUString & rName,sal_Int32 & rDataType,bool & rCurrency)62 static void lcl_GetColumnInfo(const uno::Reference<text::XTextTable>& xTable, sal_Int32 nDocColumn,
63                               bool bHasHeaders, OUString& rName, sal_Int32& rDataType,
64                               bool& rCurrency)
65 {
66     uno::Reference<table::XCellRange> xCellRange(xTable, uno::UNO_QUERY);
67     // get column name from first row, if range contains headers
68     if (bHasHeaders)
69     {
70         uno::Reference<text::XText> xHeaderText(
71             xCellRange->getCellByPosition(nDocColumn, /*nStartRow*/ 0), uno::UNO_QUERY);
72         if (xHeaderText.is())
73             rName = xHeaderText->getString();
74     }
75 
76     rCurrency = false;
77     rDataType = sdbc::DataType::VARCHAR;
78 }
79 
lcl_SetValue(connectivity::ORowSetValue & rValue,const uno::Reference<text::XTextTable> & xTable,sal_Int32 nStartCol,bool bHasHeaders,sal_Int32 nDBRow,sal_Int32 nDBColumn)80 static void lcl_SetValue(connectivity::ORowSetValue& rValue,
81                          const uno::Reference<text::XTextTable>& xTable, sal_Int32 nStartCol,
82                          bool bHasHeaders, sal_Int32 nDBRow, sal_Int32 nDBColumn)
83 {
84     sal_Int32 nDocColumn = nStartCol + nDBColumn - 1; // database counts from 1
85     sal_Int32 nDocRow = nDBRow - 1;
86     if (bHasHeaders)
87         ++nDocRow;
88 
89     uno::Reference<table::XCellRange> xCellRange(xTable, uno::UNO_QUERY);
90     uno::Reference<table::XCell> xCell;
91     try
92     {
93         xCell = xCellRange->getCellByPosition(nDocColumn, nDocRow);
94     }
95     catch (const lang::IndexOutOfBoundsException& /*rException*/)
96     {
97         SAL_WARN("connectivity.writer",
98                  "getCellByPosition(" << nDocColumn << ", " << nDocRow << ") failed");
99         rValue = OUString();
100     }
101 
102     if (xCell.is())
103     {
104         const uno::Reference<text::XText> xText(xCell, uno::UNO_QUERY);
105         if (xText.is())
106             rValue = xText->getString();
107     }
108 }
109 
110 namespace connectivity
111 {
112 namespace writer
113 {
fillColumns()114 void OWriterTable::fillColumns()
115 {
116     if (!m_xTable.is())
117         throw sdbc::SQLException();
118 
119     OUString aTypeName;
120     ::comphelper::UStringMixEqual aCase(
121         m_pConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers());
122     const bool bStoresMixedCaseQuotedIdentifiers
123         = getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers();
124 
125     for (sal_Int32 i = 0; i < m_nDataCols; i++)
126     {
127         OUString aColumnName;
128         sal_Int32 eType = sdbc::DataType::OTHER;
129         bool bCurrency = false;
130 
131         lcl_GetColumnInfo(m_xTable, m_nStartCol + i, m_bHasHeaders, aColumnName, eType, bCurrency);
132 
133         sal_Int32 nPrecision = 0; //! ...
134         sal_Int32 nDecimals = 0; //! ...
135 
136         switch (eType)
137         {
138             case sdbc::DataType::VARCHAR:
139                 aTypeName = "VARCHAR";
140                 break;
141             case sdbc::DataType::DECIMAL:
142                 aTypeName = "DECIMAL";
143                 break;
144             case sdbc::DataType::BIT:
145                 aTypeName = "BOOL";
146                 break;
147             case sdbc::DataType::DATE:
148                 aTypeName = "DATE";
149                 break;
150             case sdbc::DataType::TIME:
151                 aTypeName = "TIME";
152                 break;
153             case sdbc::DataType::TIMESTAMP:
154                 aTypeName = "TIMESTAMP";
155                 break;
156             default:
157                 SAL_WARN("connectivity.writer", "missing type name");
158                 aTypeName.clear();
159         }
160 
161         // check if the column name already exists
162         OUString aAlias = aColumnName;
163         auto aFind
164             = connectivity::find(m_aColumns->get().begin(), m_aColumns->get().end(), aAlias, aCase);
165         sal_Int32 nExprCnt = 0;
166         while (aFind != m_aColumns->get().end())
167         {
168             aAlias = aColumnName + OUString::number(++nExprCnt);
169             aFind = connectivity::find(m_aColumns->get().begin(), m_aColumns->get().end(), aAlias,
170                                        aCase);
171         }
172 
173         auto pColumn = new sdbcx::OColumn(
174             aAlias, aTypeName, OUString(), OUString(), sdbc::ColumnValue::NULLABLE, nPrecision,
175             nDecimals, eType, false, false, bCurrency, bStoresMixedCaseQuotedIdentifiers,
176             m_CatalogName, getSchema(), getName());
177         uno::Reference<XPropertySet> xCol = pColumn;
178         m_aColumns->get().push_back(xCol);
179     }
180 }
181 
OWriterTable(sdbcx::OCollection * _pTables,OWriterConnection * _pConnection,const OUString & Name,const OUString & Type)182 OWriterTable::OWriterTable(sdbcx::OCollection* _pTables, OWriterConnection* _pConnection,
183                            const OUString& Name, const OUString& Type)
184     : OWriterTable_BASE(_pTables, _pConnection, Name, Type, OUString() /*Description*/,
185                         OUString() /*SchemaName*/, OUString() /*CatalogName*/)
186     , m_pWriterConnection(_pConnection)
187     , m_nStartCol(0)
188     , m_nDataCols(0)
189     , m_bHasHeaders(false)
190 {
191 }
192 
construct()193 void OWriterTable::construct()
194 {
195     uno::Reference<text::XTextDocument> xDoc = m_pWriterConnection->acquireDoc();
196     if (xDoc.is())
197     {
198         uno::Reference<text::XTextTablesSupplier> xTextTablesSupplier(xDoc, uno::UNO_QUERY);
199         uno::Reference<container::XNameAccess> xTables = xTextTablesSupplier->getTextTables();
200         if (xTables.is() && xTables->hasByName(m_Name))
201         {
202             m_xTable.set(xTables->getByName(m_Name), uno::UNO_QUERY);
203             if (m_xTable.is())
204             {
205                 lcl_GetDataArea(m_xTable, m_nDataCols, m_nDataRows);
206                 m_bHasHeaders = true;
207             }
208         }
209     }
210 
211     fillColumns();
212 
213     refreshColumns();
214 }
215 
disposing()216 void SAL_CALL OWriterTable::disposing()
217 {
218     OFileTable::disposing();
219     ::osl::MutexGuard aGuard(m_aMutex);
220     m_aColumns = nullptr;
221     if (m_pWriterConnection)
222         m_pWriterConnection->releaseDoc();
223     m_pWriterConnection = nullptr;
224 }
225 
getUnoTunnelId()226 uno::Sequence<sal_Int8> OWriterTable::getUnoTunnelId()
227 {
228     static ::cppu::OImplementationId implId;
229 
230     return implId.getImplementationId();
231 }
232 
getSomething(const uno::Sequence<sal_Int8> & rId)233 sal_Int64 OWriterTable::getSomething(const uno::Sequence<sal_Int8>& rId)
234 {
235     return (isUnoTunnelId<OWriterTable>(rId)) ? reinterpret_cast<sal_Int64>(this)
236                                               : OWriterTable_BASE::getSomething(rId);
237 }
238 
fetchRow(OValueRefRow & _rRow,const OSQLColumns & _rCols,bool bRetrieveData)239 bool OWriterTable::fetchRow(OValueRefRow& _rRow, const OSQLColumns& _rCols, bool bRetrieveData)
240 {
241     // read the bookmark
242 
243     _rRow->setDeleted(false);
244     *(_rRow->get())[0] = m_nFilePos;
245 
246     if (!bRetrieveData)
247         return true;
248 
249     // fields
250 
251     const OValueRefVector::Vector::size_type nCount
252         = std::min(_rRow->get().size(), _rCols.get().size() + 1);
253     for (OValueRefVector::Vector::size_type i = 1; i < nCount; i++)
254     {
255         if ((_rRow->get())[i]->isBound())
256         {
257             lcl_SetValue((_rRow->get())[i]->get(), m_xTable, m_nStartCol, m_bHasHeaders, m_nFilePos,
258                          i);
259         }
260     }
261     return true;
262 }
263 
264 } // namespace writer
265 } // namespace connectivity
266 
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
268