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,
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
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
)
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
);
93 sal_Int32 tableIndex
= 0;
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
;
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
,
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",
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
)
165 Reference
< XPropertySet
> set
;
166 m_values
[index
] >>= set
;
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
);
196 void Users::disposing()
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */