Use correct object
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_xusers.cxx
blob701adc76600d61e13549d265de8d76b01c0d525c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * Effective License of whole file:
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 * MA 02111-1307 USA
20 * Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
22 * The Contents of this file are made available subject to the terms of
23 * the GNU Lesser General Public License Version 2.1
25 * Copyright: 2000 by Sun Microsystems, Inc.
27 * Contributor(s): Joerg Budischewski
29 * All parts contributed on or after August 2011:
31 * This Source Code Form is subject to the terms of the Mozilla Public
32 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
35 ************************************************************************/
37 #include <rtl/ref.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
40 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
41 #include <com/sun/star/sdbc/SQLException.hpp>
42 #include <com/sun/star/sdbc/XRow.hpp>
43 #include <cppuhelper/exc_hlp.hxx>
44 #include <o3tl/safeint.hxx>
46 #include "pq_xusers.hxx"
47 #include "pq_xuser.hxx"
48 #include "pq_statics.hxx"
49 #include "pq_tools.hxx"
51 using osl::MutexGuard;
53 using com::sun::star::beans::XPropertySet;
55 using com::sun::star::uno::Any;
56 using com::sun::star::uno::UNO_QUERY;
57 using com::sun::star::uno::Reference;
59 using com::sun::star::container::NoSuchElementException;
61 using com::sun::star::sdbc::XRow;
62 using com::sun::star::sdbc::XStatement;
63 using com::sun::star::sdbc::XResultSet;
65 namespace pq_sdbc_driver
67 Users::Users(
68 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
69 const css::uno::Reference< css::sdbc::XConnection > & origin,
70 ConnectionSettings *pSettings )
71 : Container( refMutex, origin, pSettings, getStatics().USER )
74 Users::~Users()
77 void Users::refresh()
79 try
81 osl::MutexGuard guard( m_xMutex->GetMutex() );
82 Statics & st = getStatics();
84 Reference< XStatement > stmt = m_origin->createStatement();
86 Reference< XResultSet > rs = stmt->executeQuery( u"SELECT usename FROM pg_shadow"_ustr );
88 Reference< XRow > xRow( rs , UNO_QUERY );
90 String2IntMap map;
92 m_values.clear();
93 sal_Int32 tableIndex = 0;
94 while( rs->next() )
96 rtl::Reference<User> pUser =
97 new User( m_xMutex, m_origin, m_pSettings );
98 Reference< css::beans::XPropertySet > prop = pUser;
100 OUString name = xRow->getString( 1);
101 pUser->setPropertyValue_NoBroadcast_public(
102 st.NAME , Any(xRow->getString( TABLE_INDEX_CATALOG+1) ) );
105 m_values.push_back( Any( prop ) );
106 map[ name ] = tableIndex;
107 ++tableIndex;
110 m_name2index.swap( map );
112 catch ( css::sdbc::SQLException & e )
114 css::uno::Any anyEx = cppu::getCaughtException();
115 throw css::lang::WrappedTargetRuntimeException( e.Message,
116 e.Context, anyEx );
119 fire( RefreshedBroadcaster( *this ) );
123 void Users::appendByDescriptor(
124 const css::uno::Reference< css::beans::XPropertySet >& descriptor )
126 osl::MutexGuard guard( m_xMutex->GetMutex() );
128 OUStringBuffer update( 128 );
129 update.append( "CREATE USER " );
130 bufferQuoteIdentifier( update, extractStringProperty( descriptor, getStatics().NAME ), m_pSettings );
131 update.append( " PASSWORD " );
132 bufferQuoteConstant( update, extractStringProperty( descriptor, getStatics().PASSWORD ), m_pSettings );
134 Reference< XStatement > stmt = m_origin->createStatement( );
135 DisposeGuard disposeGuard( stmt );
136 stmt->executeUpdate( update.makeStringAndClear() );
139 void Users::dropByName( const OUString& elementName )
141 String2IntMap::const_iterator ii = m_name2index.find( elementName );
142 if( ii == m_name2index.end() )
144 throw css::container::NoSuchElementException(
145 "User " + elementName + " is unknown, so it can't be dropped",
146 *this );
148 dropByIndex( ii->second );
151 void Users::dropByIndex( sal_Int32 index )
154 osl::MutexGuard guard( m_xMutex->GetMutex() );
155 if( index < 0 || o3tl::make_unsigned(index) >= m_values.size() )
157 throw css::lang::IndexOutOfBoundsException(
158 "USERS: Index out of range (allowed 0 to "
159 + OUString::number( m_values.size() -1 )
160 + ", got " + OUString::number( index )
161 + ")",
162 *this );
165 Reference< XPropertySet > set;
166 m_values[index] >>= set;
167 OUString name;
168 set->getPropertyValue( getStatics().NAME ) >>= name;
170 OUStringBuffer update( 128 );
171 update.append( "DROP USER " );
172 bufferQuoteIdentifier( update, name, m_pSettings );
174 Reference< XStatement > stmt = m_origin->createStatement( );
175 DisposeGuard disposeGuard( stmt );
176 stmt->executeUpdate( update.makeStringAndClear() );
180 css::uno::Reference< css::beans::XPropertySet > Users::createDataDescriptor()
182 return new UserDescriptor( m_xMutex, m_origin, m_pSettings );
185 Reference< css::container::XNameAccess > Users::create(
186 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
187 const css::uno::Reference< css::sdbc::XConnection > & origin,
188 ConnectionSettings *pSettings )
190 rtl::Reference<Users> pUsers = new Users( refMutex, origin, pSettings );
191 pUsers->refresh();
193 return pUsers;
196 void Users::disposing()
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */