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/WDatabaseMetaData.hxx>
21 #include <writer/WConnection.hxx>
22 #include <com/sun/star/sdbc/SQLException.hpp>
23 #include <com/sun/star/text/XTextDocument.hpp>
24 #include <com/sun/star/text/XTextTablesSupplier.hpp>
25 
26 using namespace ::com::sun::star;
27 
28 namespace connectivity::writer
29 {
OWriterDatabaseMetaData(file::OConnection * pConnection)30 OWriterDatabaseMetaData::OWriterDatabaseMetaData(file::OConnection* pConnection)
31     : OComponentDatabaseMetaData(pConnection)
32 {
33 }
34 
35 OWriterDatabaseMetaData::~OWriterDatabaseMetaData() = default;
36 
getURL()37 OUString SAL_CALL OWriterDatabaseMetaData::getURL()
38 {
39     ::osl::MutexGuard aGuard(m_aMutex);
40 
41     return "sdbc:writer:" + m_pConnection->getURL();
42 }
43 
getTables(const uno::Any &,const OUString &,const OUString & tableNamePattern,const uno::Sequence<OUString> & types)44 uno::Reference<sdbc::XResultSet> SAL_CALL OWriterDatabaseMetaData::getTables(
45     const uno::Any& /*catalog*/, const OUString& /*schemaPattern*/,
46     const OUString& tableNamePattern, const uno::Sequence<OUString>& types)
47 {
48     ::osl::MutexGuard aGuard(m_aMutex);
49 
50     rtl::Reference<ODatabaseMetaDataResultSet> pResult
51         = new ODatabaseMetaDataResultSet(ODatabaseMetaDataResultSet::eTables);
52 
53     // check if ORowSetValue type is given
54     // when no types are given then we have to return all tables e.g. TABLE
55 
56     OUString aTable("TABLE");
57 
58     bool bTableFound = true;
59     sal_Int32 nLength = types.getLength();
60     if (nLength)
61     {
62         bTableFound = false;
63 
64         const OUString* pIter = types.getConstArray();
65         const OUString* pEnd = pIter + nLength;
66         for (; pIter != pEnd; ++pIter)
67         {
68             if (*pIter == aTable)
69             {
70                 bTableFound = true;
71                 break;
72             }
73         }
74     }
75     if (!bTableFound)
76         return pResult;
77 
78     // get the table names from the document
79 
80     OWriterConnection::ODocHolder aDocHolder(static_cast<OWriterConnection*>(m_pConnection));
81     uno::Reference<text::XTextTablesSupplier> xDoc(aDocHolder.getDoc(), uno::UNO_QUERY);
82     if (!xDoc.is())
83         throw sdbc::SQLException();
84     uno::Reference<container::XNameAccess> xTables = xDoc->getTextTables();
85     if (!xTables.is())
86         throw sdbc::SQLException();
87     uno::Sequence<OUString> aTableNames = xTables->getElementNames();
88 
89     ODatabaseMetaDataResultSet::ORows aRows;
90     sal_Int32 nTableCount = aTableNames.getLength();
91     for (sal_Int32 nTable = 0; nTable < nTableCount; nTable++)
92     {
93         OUString aName = aTableNames[nTable];
94         if (match(tableNamePattern, aName, '\0'))
95         {
96             ODatabaseMetaDataResultSet::ORow aRow{ nullptr, nullptr, nullptr };
97             aRow.reserve(6);
98             aRow.push_back(new ORowSetValueDecorator(aName));
99             aRow.push_back(new ORowSetValueDecorator(aTable));
100             aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
101             aRows.push_back(aRow);
102         }
103     }
104 
105     pResult->setRows(aRows);
106 
107     return pResult;
108 }
109 
110 } // namespace
111 
112 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
113