LibreOffice Module connectivity (master) 1
pq_xusers.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*************************************************************************
3 *
4 * Effective License of whole file:
5 *
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.
9 *
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.
14 *
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
19 *
20 * Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
21 *
22 * The Contents of this file are made available subject to the terms of
23 * the GNU Lesser General Public License Version 2.1
24 *
25 * Copyright: 2000 by Sun Microsystems, Inc.
26 *
27 * Contributor(s): Joerg Budischewski
28 *
29 * All parts contributed on or after August 2011:
30 *
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/.
34 *
35 ************************************************************************/
36
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>
44#include <o3tl/safeint.hxx>
45
46#include "pq_xusers.hxx"
47#include "pq_xuser.hxx"
48#include "pq_statics.hxx"
49#include "pq_tools.hxx"
50
51using osl::MutexGuard;
52
54
55using com::sun::star::uno::Any;
56using com::sun::star::uno::UNO_QUERY;
58
59using com::sun::star::container::NoSuchElementException;
60
61using com::sun::star::sdbc::XRow;
62using com::sun::star::sdbc::XStatement;
63using com::sun::star::sdbc::XResultSet;
64
65namespace pq_sdbc_driver
66{
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 )
72{}
73
75{}
76
78{
79 try
80 {
81 osl::MutexGuard guard( m_xMutex->GetMutex() );
82 Statics & st = getStatics();
83
84 Reference< XStatement > stmt = m_origin->createStatement();
85
86 Reference< XResultSet > rs = stmt->executeQuery( "SELECT usename FROM pg_shadow" );
87
88 Reference< XRow > xRow( rs , UNO_QUERY );
89
91
92 m_values.clear();
93 sal_Int32 tableIndex = 0;
94 while( rs->next() )
95 {
99
100 OUString name = xRow->getString( 1);
101 pUser->setPropertyValue_NoBroadcast_public(
102 st.NAME , Any(xRow->getString( TABLE_INDEX_CATALOG+1) ) );
103
104 {
105 m_values.push_back( Any( prop ) );
106 map[ name ] = tableIndex;
107 ++tableIndex;
108 }
109 }
110 m_name2index.swap( map );
111 }
112 catch ( css::sdbc::SQLException & e )
113 {
114 css::uno::Any anyEx = cppu::getCaughtException();
115 throw css::lang::WrappedTargetRuntimeException( e.Message,
116 e.Context, anyEx );
117 }
118
119 fire( RefreshedBroadcaster( *this ) );
120}
121
122
124 const css::uno::Reference< css::beans::XPropertySet >& descriptor )
125{
126 osl::MutexGuard guard( m_xMutex->GetMutex() );
127
128 OUStringBuffer update( 128 );
129 update.append( "CREATE USER " );
131 update.append( " PASSWORD " );
133
134 Reference< XStatement > stmt = m_origin->createStatement( );
135 DisposeGuard disposeGuard( stmt );
136 stmt->executeUpdate( update.makeStringAndClear() );
137}
138
139void Users::dropByName( const OUString& elementName )
140{
141 String2IntMap::const_iterator ii = m_name2index.find( elementName );
142 if( ii == m_name2index.end() )
143 {
144 throw css::container::NoSuchElementException(
145 "User " + elementName + " is unknown, so it can't be dropped",
146 *this );
147 }
148 dropByIndex( ii->second );
149}
150
151void Users::dropByIndex( sal_Int32 index )
152{
153
154 osl::MutexGuard guard( m_xMutex->GetMutex() );
155 if( index < 0 || o3tl::make_unsigned(index) >= m_values.size() )
156 {
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 );
163 }
164
166 m_values[index] >>= set;
167 OUString name;
168 set->getPropertyValue( getStatics().NAME ) >>= name;
169
170 OUStringBuffer update( 128 );
171 update.append( "DROP USER " );
173
174 Reference< XStatement > stmt = m_origin->createStatement( );
175 DisposeGuard disposeGuard( stmt );
176 stmt->executeUpdate( update.makeStringAndClear() );
177}
178
179
180css::uno::Reference< css::beans::XPropertySet > Users::createDataDescriptor()
181{
183}
184
186 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
187 const css::uno::Reference< css::sdbc::XConnection > & origin,
188 ConnectionSettings *pSettings )
189{
190 rtl::Reference<Users> pUsers = new Users( refMutex, origin, pSettings );
191 pUsers->refresh();
192
193 return pUsers;
194}
195
197{
198}
199
200};
201
202/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
NAME
ConnectionSettings * m_pSettings
void fire(const EventBroadcastHelper &helper)
css::uno::Reference< css::sdbc::XConnection > m_origin
::rtl::Reference< comphelper::RefCountedMutex > m_xMutex
std::vector< css::uno::Any > m_values
virtual ~Users() override
Definition: pq_xusers.cxx:74
virtual void SAL_CALL refresh() override
Definition: pq_xusers.cxx:77
virtual void SAL_CALL appendByDescriptor(const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
Definition: pq_xusers.cxx:123
virtual void SAL_CALL dropByIndex(sal_Int32 index) override
Definition: pq_xusers.cxx:151
virtual css::uno::Reference< css::beans::XPropertySet > SAL_CALL createDataDescriptor() override
Definition: pq_xusers.cxx:180
virtual void SAL_CALL disposing() override
Definition: pq_xusers.cxx:196
virtual void SAL_CALL dropByName(const OUString &elementName) override
Definition: pq_xusers.cxx:139
Users(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings)
Definition: pq_xusers.cxx:67
static css::uno::Reference< css::container::XNameAccess > create(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings)
Definition: pq_xusers.cxx:185
void set(css::uno::UnoInterfaceReference const &value)
class SAL_NO_VTABLE XPropertySet
Any SAL_CALL getCaughtException()
constexpr OUStringLiteral USER
index
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
void bufferQuoteConstant(OUStringBuffer &buf, std::u16string_view value, ConnectionSettings *settings)
Definition: pq_tools.cxx:138
Statics & getStatics()
Definition: pq_statics.cxx:112
std::unordered_map< OUString, sal_Int32 > String2IntMap
OUString extractStringProperty(const Reference< XPropertySet > &descriptor, const OUString &name)
Definition: pq_tools.cxx:204
void bufferQuoteIdentifier(OUStringBuffer &buf, std::u16string_view toQuote, ConnectionSettings *settings)
Definition: pq_tools.cxx:175
const sal_Int32 TABLE_INDEX_CATALOG
Definition: pq_statics.hxx:128
OUString name
Definition: pq_statics.cxx:74
std::map< OUString, rtl::Reference< Entity > > map
bool update()