1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "hsqldb/HCatalog.hxx"
27 #include "hsqldb/HUsers.hxx"
28 #include "hsqldb/HTables.hxx"
29 #include "hsqldb/HViews.hxx"
30 #include <com/sun/star/sdbc/XRow.hpp>
31 #include <com/sun/star/sdbc/XResultSet.hpp>
32 #include <comphelper/types.hxx>
33 
34 
35 // -------------------------------------------------------------------------
36 using namespace connectivity;
37 using namespace connectivity::hsqldb;
38 //using namespace connectivity::sdbcx;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::sdbcx;
42 using namespace ::com::sun::star::sdbc;
43 using namespace ::com::sun::star::container;
44 using namespace ::com::sun::star::lang;
45 // -------------------------------------------------------------------------
OHCatalog(const Reference<XConnection> & _xConnection)46 OHCatalog::OHCatalog(const Reference< XConnection >& _xConnection) : sdbcx::OCatalog(_xConnection)
47 				,m_xConnection(_xConnection)
48 {
49 }
50 // -----------------------------------------------------------------------------
refreshObjects(const Sequence<::rtl::OUString> & _sKindOfObject,TStringVector & _rNames)51 void OHCatalog::refreshObjects(const Sequence< ::rtl::OUString >& _sKindOfObject,TStringVector& _rNames)
52 {
53 	Reference< XResultSet > xResult = m_xMetaData->getTables(Any(),
54 															::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%")),
55 															::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%")),
56 															_sKindOfObject);
57 	fillNames(xResult,_rNames);
58 }
59 // -------------------------------------------------------------------------
refreshTables()60 void OHCatalog::refreshTables()
61 {
62 	TStringVector aVector;
63 	static const ::rtl::OUString s_sTableTypeView(RTL_CONSTASCII_USTRINGPARAM("VIEW"));
64 	static const ::rtl::OUString s_sTableTypeTable(RTL_CONSTASCII_USTRINGPARAM("TABLE"));
65 
66 	Sequence< ::rtl::OUString > sTableTypes(2);
67 	sTableTypes[0] = s_sTableTypeView;
68 	sTableTypes[1] = s_sTableTypeTable;
69 
70 	refreshObjects(sTableTypes,aVector);
71 
72 	if ( m_pTables )
73 		m_pTables->reFill(aVector);
74 	else
75 		m_pTables = new OTables(m_xMetaData,*this,m_aMutex,aVector);
76 }
77 // -------------------------------------------------------------------------
refreshViews()78 void OHCatalog::refreshViews()
79 {
80 	Sequence< ::rtl::OUString > aTypes(1);
81 	aTypes[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("VIEW"));
82 
83 	sal_Bool bSupportsViews = sal_False;
84 	try
85 	{
86 		Reference<XResultSet> xRes = m_xMetaData->getTableTypes();
87 		Reference<XRow> xRow(xRes,UNO_QUERY);
88 		while ( xRow.is() && xRes->next() )
89 		{
90 			if ( (bSupportsViews = xRow->getString(1).equalsIgnoreAsciiCase(aTypes[0])) )
91 			{
92 				break;
93 			}
94 		}
95 	}
96 	catch(const SQLException&)
97 	{
98 	}
99 
100 	TStringVector aVector;
101 	if ( bSupportsViews )
102 		refreshObjects(aTypes,aVector);
103 
104 	if ( m_pViews )
105 		m_pViews->reFill(aVector);
106 	else
107 		m_pViews = new HViews( m_xConnection, *this, m_aMutex, aVector );
108 }
109 // -------------------------------------------------------------------------
refreshGroups()110 void OHCatalog::refreshGroups()
111 {
112 }
113 // -------------------------------------------------------------------------
refreshUsers()114 void OHCatalog::refreshUsers()
115 {
116 	TStringVector aVector;
117 	Reference< XStatement > xStmt = m_xConnection->createStatement(  );
118 	Reference< XResultSet >  xResult = xStmt->executeQuery(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("select User from hsqldb.user group by User")));
119 	if ( xResult.is() )
120 	{
121 		Reference< XRow > xRow(xResult,UNO_QUERY);
122 		TString2IntMap aMap;
123 		while( xResult->next() )
124 			aVector.push_back(xRow->getString(1));
125 		::comphelper::disposeComponent(xResult);
126 	}
127 	::comphelper::disposeComponent(xStmt);
128 
129 	if(m_pUsers)
130 		m_pUsers->reFill(aVector);
131 	else
132 		m_pUsers = new OUsers(*this,m_aMutex,aVector,m_xConnection,this);
133 }
134 // -----------------------------------------------------------------------------
queryInterface(const Type & rType)135 Any SAL_CALL OHCatalog::queryInterface( const Type & rType ) throw(RuntimeException)
136 {
137 	if ( rType == ::getCppuType((const Reference<XGroupsSupplier>*)0) )
138 		return Any();
139 
140 	return OCatalog::queryInterface(rType);
141 }
142 // -----------------------------------------------------------------------------
getTypes()143 Sequence< Type > SAL_CALL OHCatalog::getTypes(  ) throw(RuntimeException)
144 {
145 	Sequence< Type > aTypes = OCatalog::getTypes();
146 	::std::vector<Type> aOwnTypes;
147 	aOwnTypes.reserve(aTypes.getLength());
148 	const Type* pBegin = aTypes.getConstArray();
149 	const Type* pEnd = pBegin + aTypes.getLength();
150 	for(;pBegin != pEnd;++pBegin)
151 	{
152 		if ( !(*pBegin == ::getCppuType((const Reference<XGroupsSupplier>*)0)))
153 		{
154 			aOwnTypes.push_back(*pBegin);
155 		}
156 	}
157 	const Type* pTypes = aOwnTypes.empty() ? 0 : &aOwnTypes[0];
158 	return Sequence< Type >(pTypes, aOwnTypes.size());
159 }
160 // -----------------------------------------------------------------------------
161 
162 
163