1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 #include "mysql/YCatalog.hxx"
31 #include "mysql/YUsers.hxx"
32 #include "mysql/YTables.hxx"
33 #include "mysql/YViews.hxx"
34 #include <com/sun/star/sdbc/XRow.hpp>
35 #include <com/sun/star/sdbc/XResultSet.hpp>
36 #include <comphelper/types.hxx>
37 
38 
39 // -------------------------------------------------------------------------
40 using namespace connectivity;
41 using namespace connectivity::mysql;
42 using namespace connectivity::sdbcx;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star::sdbcx;
46 using namespace ::com::sun::star::sdbc;
47 using namespace ::com::sun::star::container;
48 using namespace ::com::sun::star::lang;
49 // -------------------------------------------------------------------------
50 OMySQLCatalog::OMySQLCatalog(const Reference< XConnection >& _xConnection) : OCatalog(_xConnection)
51 				,m_xConnection(_xConnection)
52 {
53 }
54 // -----------------------------------------------------------------------------
55 void OMySQLCatalog::refreshObjects(const Sequence< ::rtl::OUString >& _sKindOfObject,TStringVector& _rNames)
56 {
57 	Reference< XResultSet > xResult = m_xMetaData->getTables(Any(),
58 															::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%")),
59 															::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%")),
60 															_sKindOfObject);
61 	fillNames(xResult,_rNames);
62 }
63 // -------------------------------------------------------------------------
64 void OMySQLCatalog::refreshTables()
65 {
66 	TStringVector aVector;
67 	static const ::rtl::OUString s_sTableTypeView(RTL_CONSTASCII_USTRINGPARAM("VIEW"));
68 	static const ::rtl::OUString s_sTableTypeTable(RTL_CONSTASCII_USTRINGPARAM("TABLE"));
69 	static const ::rtl::OUString s_sAll(RTL_CONSTASCII_USTRINGPARAM("%"));
70 
71 	Sequence< ::rtl::OUString > sTableTypes(3);
72 	sTableTypes[0] = s_sTableTypeView;
73 	sTableTypes[1] = s_sTableTypeTable;
74 	sTableTypes[2] = s_sAll;	// just to be sure to include anything else ....
75 
76 	refreshObjects(sTableTypes,aVector);
77 
78 	if ( m_pTables )
79 		m_pTables->reFill(aVector);
80 	else
81 		m_pTables = new OTables(m_xMetaData,*this,m_aMutex,aVector);
82 }
83 // -------------------------------------------------------------------------
84 void OMySQLCatalog::refreshViews()
85 {
86 	Sequence< ::rtl::OUString > aTypes(1);
87 	aTypes[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("VIEW"));
88 
89 /*
90 	sal_Bool bSupportsViews = sal_False;
91 	try
92 	{
93 		Reference<XResultSet> xRes = m_xMetaData->getTableTypes();
94 		Reference<XRow> xRow(xRes,UNO_QUERY);
95 		while ( !bSupportsViews && xRow.is() && xRes->next() )
96 		{
97             ::rtl::OUString sTableType( xRow->getString( 1 ) );
98             bSupportsViews = sTableType.equalsIgnoreAsciiCase( aTypes[0] );
99 		}
100 	}
101 	catch(const SQLException&)
102 	{
103 	}
104 */
105     // let's simply assume the server is new enough to support views. Current drivers
106     // as of this writing might not return the proper information in getTableTypes, so
107     // don't rely on it.
108     // during #73245# / 2007-10-26 / frank.schoenheit@sun.com
109     bool bSupportsViews = sal_True;
110 
111 	TStringVector aVector;
112 	if ( bSupportsViews )
113 		refreshObjects(aTypes,aVector);
114 
115 	if ( m_pViews )
116 		m_pViews->reFill(aVector);
117 	else
118 		m_pViews = new OViews(m_xMetaData,*this,m_aMutex,aVector);
119 }
120 // -------------------------------------------------------------------------
121 void OMySQLCatalog::refreshGroups()
122 {
123 }
124 // -------------------------------------------------------------------------
125 void OMySQLCatalog::refreshUsers()
126 {
127 	TStringVector aVector;
128 	Reference< XStatement > xStmt = m_xConnection->createStatement(  );
129 	Reference< XResultSet >  xResult = xStmt->executeQuery(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("select User from mysql.user group by User")));
130 	if ( xResult.is() )
131 	{
132 		Reference< XRow > xRow(xResult,UNO_QUERY);
133 		TString2IntMap aMap;
134 		while( xResult->next() )
135 			aVector.push_back(xRow->getString(1));
136 		::comphelper::disposeComponent(xResult);
137 	}
138 	::comphelper::disposeComponent(xStmt);
139 
140 	if(m_pUsers)
141 		m_pUsers->reFill(aVector);
142 	else
143 		m_pUsers = new OUsers(*this,m_aMutex,aVector,m_xConnection,this);
144 }
145 // -----------------------------------------------------------------------------
146 Any SAL_CALL OMySQLCatalog::queryInterface( const Type & rType ) throw(RuntimeException)
147 {
148 	if ( rType == ::getCppuType((const Reference<XGroupsSupplier>*)0) )
149 		return Any();
150 
151 
152 	return OCatalog::queryInterface(rType);
153 }
154 // -----------------------------------------------------------------------------
155 Sequence< Type > SAL_CALL OMySQLCatalog::getTypes(  ) throw(RuntimeException)
156 {
157 	Sequence< Type > aTypes = OCatalog::getTypes();
158 	::std::vector<Type> aOwnTypes;
159 	aOwnTypes.reserve(aTypes.getLength());
160 	const Type* pBegin = aTypes.getConstArray();
161 	const Type* pEnd = pBegin + aTypes.getLength();
162 	for(;pBegin != pEnd;++pBegin)
163 	{
164 		if ( !(*pBegin == ::getCppuType((const Reference<XGroupsSupplier>*)0)))
165 		{
166 			aOwnTypes.push_back(*pBegin);
167 		}
168 	}
169 	const Type* pTypes = aOwnTypes.empty() ? 0 : &aOwnTypes[0];
170 	return Sequence< Type >(pTypes, aOwnTypes.size());
171 }
172 // -----------------------------------------------------------------------------
173 
174 
175