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 <mysql/YCatalog.hxx>
21 #include <mysql/YUsers.hxx>
22 #include <mysql/YTables.hxx>
23 #include <mysql/YViews.hxx>
24 #include <com/sun/star/sdbc/XRow.hpp>
25 #include <com/sun/star/sdbc/XResultSet.hpp>
26 #include <comphelper/types.hxx>
27 
28 using namespace connectivity;
29 using namespace connectivity::mysql;
30 using namespace connectivity::sdbcx;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::beans;
33 using namespace ::com::sun::star::sdbcx;
34 using namespace ::com::sun::star::sdbc;
35 using namespace ::com::sun::star::container;
36 using namespace ::com::sun::star::lang;
37 
OMySQLCatalog(const Reference<XConnection> & _xConnection)38 OMySQLCatalog::OMySQLCatalog(const Reference<XConnection>& _xConnection)
39     : OCatalog(_xConnection)
40     , m_xConnection(_xConnection)
41 {
42 }
43 
refreshObjects(const Sequence<OUString> & _sKindOfObject,::std::vector<OUString> & _rNames)44 void OMySQLCatalog::refreshObjects(const Sequence<OUString>& _sKindOfObject,
45                                    ::std::vector<OUString>& _rNames)
46 {
47     Reference<XResultSet> xResult = m_xMetaData->getTables(Any(), "%", "%", _sKindOfObject);
48     fillNames(xResult, _rNames);
49 }
50 
refreshTables()51 void OMySQLCatalog::refreshTables()
52 {
53     ::std::vector<OUString> aVector;
54 
55     Sequence<OUString> sTableTypes{
56         "VIEW", "TABLE", "%"
57     }; // this last one just to be sure to include anything else...
58 
59     refreshObjects(sTableTypes, aVector);
60 
61     if (m_pTables)
62         m_pTables->reFill(aVector);
63     else
64         m_pTables.reset(new OTables(m_xMetaData, *this, m_aMutex, aVector));
65 }
66 
refreshViews()67 void OMySQLCatalog::refreshViews()
68 {
69     Sequence<OUString> aTypes{ "VIEW" };
70 
71     // let's simply assume the server is new enough to support views. Current drivers
72     // as of this writing might not return the proper information in getTableTypes, so
73     // don't rely on it.
74 
75     ::std::vector<OUString> aVector;
76     refreshObjects(aTypes, aVector);
77 
78     if (m_pViews)
79         m_pViews->reFill(aVector);
80     else
81         m_pViews.reset(new OViews(m_xMetaData, *this, m_aMutex, aVector));
82 }
83 
refreshGroups()84 void OMySQLCatalog::refreshGroups() {}
85 
refreshUsers()86 void OMySQLCatalog::refreshUsers()
87 {
88     ::std::vector<OUString> aVector;
89     Reference<XStatement> xStmt = m_xConnection->createStatement();
90     Reference<XResultSet> xResult = xStmt->executeQuery(
91         "SELECT grantee FROM information_schema.user_privileges GROUP BY grantee");
92     if (xResult.is())
93     {
94         Reference<XRow> xRow(xResult, UNO_QUERY);
95         while (xResult->next())
96             aVector.push_back(xRow->getString(1));
97         ::comphelper::disposeComponent(xResult);
98     }
99     ::comphelper::disposeComponent(xStmt);
100 
101     if (m_pUsers)
102         m_pUsers->reFill(aVector);
103     else
104         m_pUsers.reset(new OUsers(*this, m_aMutex, aVector, m_xConnection, this));
105 }
106 
queryInterface(const Type & rType)107 Any SAL_CALL OMySQLCatalog::queryInterface(const Type& rType)
108 {
109     if (rType == cppu::UnoType<XGroupsSupplier>::get())
110         return Any();
111 
112     return OCatalog::queryInterface(rType);
113 }
114 
getTypes()115 Sequence<Type> SAL_CALL OMySQLCatalog::getTypes()
116 {
117     Sequence<Type> aTypes = OCatalog::getTypes();
118     std::vector<Type> aOwnTypes;
119     aOwnTypes.reserve(aTypes.getLength());
120     const Type* pBegin = aTypes.getConstArray();
121     const Type* pEnd = pBegin + aTypes.getLength();
122     for (; pBegin != pEnd; ++pBegin)
123     {
124         if (*pBegin != cppu::UnoType<XGroupsSupplier>::get())
125         {
126             aOwnTypes.push_back(*pBegin);
127         }
128     }
129     return Sequence<Type>(aOwnTypes.data(), aOwnTypes.size());
130 }
131 
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
133