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