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/ustrbuf.hxx>
38 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
39 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
40 #include <com/sun/star/sdbc/SQLException.hpp>
41 #include <com/sun/star/sdbc/XRow.hpp>
42 #include <cppuhelper/exc_hlp.hxx>
44 #include "pq_xusers.hxx"
45 #include "pq_xuser.hxx"
46 #include "pq_statics.hxx"
47 #include "pq_tools.hxx"
49 using osl::MutexGuard
;
51 using com::sun::star::beans::XPropertySet
;
53 using com::sun::star::uno::makeAny
;
54 using com::sun::star::uno::UNO_QUERY
;
55 using com::sun::star::uno::Reference
;
57 using com::sun::star::container::NoSuchElementException
;
59 using com::sun::star::sdbc::XRow
;
60 using com::sun::star::sdbc::XStatement
;
61 using com::sun::star::sdbc::XResultSet
;
63 namespace pq_sdbc_driver
66 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
67 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
68 ConnectionSettings
*pSettings
)
69 : Container( refMutex
, origin
, pSettings
, getStatics().USER
)
79 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
80 Statics
& st
= getStatics();
82 Reference
< XStatement
> stmt
= m_origin
->createStatement();
84 Reference
< XResultSet
> rs
= stmt
->executeQuery( "SELECT usename FROM pg_shadow" );
86 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
91 sal_Int32 tableIndex
= 0;
95 new User( m_xMutex
, m_origin
, m_pSettings
);
96 Reference
< css::beans::XPropertySet
> prop
= pUser
;
98 OUString name
= xRow
->getString( 1);
99 pUser
->setPropertyValue_NoBroadcast_public(
100 st
.NAME
, makeAny(xRow
->getString( TABLE_INDEX_CATALOG
+1) ) );
103 m_values
.push_back( makeAny( prop
) );
104 map
[ name
] = tableIndex
;
108 m_name2index
.swap( map
);
110 catch ( css::sdbc::SQLException
& e
)
112 css::uno::Any anyEx
= cppu::getCaughtException();
113 throw css::lang::WrappedTargetRuntimeException( e
.Message
,
117 fire( RefreshedBroadcaster( *this ) );
121 void Users::appendByDescriptor(
122 const css::uno::Reference
< css::beans::XPropertySet
>& descriptor
)
124 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
126 OUStringBuffer
update( 128 );
127 update
.append( "CREATE USER " );
128 bufferQuoteIdentifier( update
, extractStringProperty( descriptor
, getStatics().NAME
), m_pSettings
);
129 update
.append( " PASSWORD " );
130 bufferQuoteConstant( update
, extractStringProperty( descriptor
, getStatics().PASSWORD
), m_pSettings
);
132 Reference
< XStatement
> stmt
= m_origin
->createStatement( );
133 DisposeGuard
disposeGuard( stmt
);
134 stmt
->executeUpdate( update
.makeStringAndClear() );
137 void Users::dropByName( const OUString
& elementName
)
139 String2IntMap::const_iterator ii
= m_name2index
.find( elementName
);
140 if( ii
== m_name2index
.end() )
142 throw css::container::NoSuchElementException(
143 "User " + elementName
+ " is unknown, so it can't be dropped",
146 dropByIndex( ii
->second
);
149 void Users::dropByIndex( sal_Int32 index
)
152 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
153 if( index
< 0 || index
>= static_cast<sal_Int32
>(m_values
.size()) )
155 throw css::lang::IndexOutOfBoundsException(
156 "USERS: Index out of range (allowed 0 to "
157 + OUString::number( m_values
.size() -1 )
158 + ", got " + OUString::number( index
)
163 Reference
< XPropertySet
> set
;
164 m_values
[index
] >>= set
;
166 set
->getPropertyValue( getStatics().NAME
) >>= name
;
168 OUStringBuffer
update( 128 );
169 update
.append( "DROP USER " );
170 bufferQuoteIdentifier( update
, name
, m_pSettings
);
172 Reference
< XStatement
> stmt
= m_origin
->createStatement( );
173 DisposeGuard
disposeGuard( stmt
);
174 stmt
->executeUpdate( update
.makeStringAndClear() );
178 css::uno::Reference
< css::beans::XPropertySet
> Users::createDataDescriptor()
180 return new UserDescriptor( m_xMutex
, m_origin
, m_pSettings
);
183 Reference
< css::container::XNameAccess
> Users::create(
184 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
185 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
186 ConnectionSettings
*pSettings
)
188 Users
*pUsers
= new Users( refMutex
, origin
, pSettings
);
189 Reference
< css::container::XNameAccess
> ret
= pUsers
;
195 void Users::disposing()
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */