1 /*************************************************************************
3 * $RCSfile: pq_xusers.cxx,v $
7 * last change: $Author: jbu $ $Date: 2006/01/22 15:14:41 $
9 * The Contents of this file are made available subject to the terms of
10 * either of the following licenses
12 * - GNU Lesser General Public License Version 2.1
13 * - Sun Industry Standards Source License Version 1.1
15 * Sun Microsystems Inc., October, 2000
17 * GNU Lesser General Public License Version 2.1
18 * =============================================
19 * Copyright 2000 by Sun Microsystems, Inc.
20 * 901 San Antonio Road, Palo Alto, CA 94303, USA
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License version 2.1, as published by the Free Software Foundation.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 * Sun Industry Standards Source License Version 1.1
38 * =================================================
39 * The contents of this file are subject to the Sun Industry Standards
40 * Source License Version 1.1 (the "License"); You may not use this file
41 * except in compliance with the License. You may obtain a copy of the
42 * License at http://www.openoffice.org/license.html.
44 * Software provided under this License is provided on an "AS IS" basis,
45 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
46 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
47 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
48 * See the License for the specific provisions governing your rights and
49 * obligations concerning the Software.
51 * The Initial Developer of the Original Code is: Joerg Budischewski
53 * Copyright: 2000 by Sun Microsystems, Inc.
55 * All Rights Reserved.
57 * Contributor(s): Joerg Budischewski
60 ************************************************************************/
64 #include <rtl/ustrbuf.hxx>
66 #include <com/sun/star/sdbc/XRow.hpp>
67 #include <com/sun/star/sdbc/XParameters.hpp>
68 #include <com/sun/star/sdbcx/Privilege.hpp>
70 #include "pq_xusers.hxx"
71 #include "pq_xuser.hxx"
72 #include "pq_statics.hxx"
73 #include "pq_tools.hxx"
75 using osl::MutexGuard
;
78 using rtl::OUStringBuffer
;
79 using rtl::OUStringToOString
;
81 using com::sun::star::beans::XPropertySet
;
83 using com::sun::star::uno::Any
;
84 using com::sun::star::uno::makeAny
;
85 using com::sun::star::uno::UNO_QUERY
;
86 using com::sun::star::uno::Type
;
87 using com::sun::star::uno::XInterface
;
88 using com::sun::star::uno::Reference
;
89 using com::sun::star::uno::Sequence
;
90 using com::sun::star::uno::RuntimeException
;
92 using com::sun::star::container::NoSuchElementException
;
93 using com::sun::star::lang::WrappedTargetException
;
95 using com::sun::star::sdbc::XRow
;
96 using com::sun::star::sdbc::XCloseable
;
97 using com::sun::star::sdbc::XStatement
;
98 using com::sun::star::sdbc::XResultSet
;
99 using com::sun::star::sdbc::XParameters
;
100 using com::sun::star::sdbc::XPreparedStatement
;
101 using com::sun::star::sdbc::XDatabaseMetaData
;
103 namespace pq_sdbc_driver
105 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
107 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
108 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
109 ConnectionSettings
*pSettings
)
110 : Container( refMutex
, origin
, pSettings
, getStatics().USER
)
116 void Users::refresh()
117 throw (::com::sun::star::uno::RuntimeException
)
121 osl::MutexGuard
guard( m_refMutex
->mutex
);
122 Statics
& st
= getStatics();
124 Reference
< XStatement
> stmt
= m_origin
->createStatement();
126 Reference
< XResultSet
> rs
=
127 stmt
->executeQuery( ASCII_STR( "SELECT usename FROM pg_shadow" ) );
129 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
133 std::vector
< Any
, Allocator
< Any
> > vec
;
134 sal_Int32 tableIndex
= 0;
138 new User( m_refMutex
, m_origin
, m_pSettings
);
139 Reference
< com::sun::star::beans::XPropertySet
> prop
= pUser
;
141 OUString name
= xRow
->getString( 1);
142 pUser
->setPropertyValue_NoBroadcast_public(
143 st
.NAME
, makeAny(xRow
->getString( TABLE_INDEX_CATALOG
+1) ) );
145 vec
.push_back( makeAny(prop
) );
146 map
[ name
] = tableIndex
;
149 m_values
= Sequence
< com::sun::star::uno::Any
> ( & vec
[0] , vec
.size() );
150 m_name2index
.swap( map
);
152 catch ( com::sun::star::sdbc::SQLException
& e
)
154 throw RuntimeException( e
.Message
, e
.Context
);
157 fire( RefreshedBroadcaster( *this ) );
161 void Users::appendByDescriptor(
162 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& descriptor
)
163 throw (::com::sun::star::sdbc::SQLException
,
164 ::com::sun::star::container::ElementExistException
,
165 ::com::sun::star::uno::RuntimeException
)
167 osl::MutexGuard
guard( m_refMutex
->mutex
);
168 Statics
&st
= getStatics();
170 OUStringBuffer
update( 128 );
171 update
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "CREATE USER " ) );
172 bufferQuoteIdentifier( update
, extractStringProperty( descriptor
, getStatics().NAME
) );
173 update
.appendAscii( RTL_CONSTASCII_STRINGPARAM( " PASSWORD " ) );
174 bufferQuoteConstant( update
, extractStringProperty( descriptor
, getStatics().PASSWORD
), m_pSettings
->encoding
);
176 Reference
< XStatement
> stmt
= m_origin
->createStatement( );
177 DisposeGuard
disposeGuard( stmt
);
178 stmt
->executeUpdate( update
.makeStringAndClear() );
181 void Users::dropByName( const ::rtl::OUString
& elementName
)
182 throw (::com::sun::star::sdbc::SQLException
,
183 ::com::sun::star::container::NoSuchElementException
,
184 ::com::sun::star::uno::RuntimeException
)
186 String2IntMap::const_iterator ii
= m_name2index
.find( elementName
);
187 if( ii
== m_name2index
.end() )
189 OUStringBuffer
buf( 128 );
190 buf
.appendAscii( "User " );
191 buf
.append( elementName
);
192 buf
.appendAscii( " is unknown, so it can't be dropped" );
193 throw com::sun::star::container::NoSuchElementException(
194 buf
.makeStringAndClear(), *this );
196 dropByIndex( ii
->second
);
199 void Users::dropByIndex( sal_Int32 index
)
200 throw (::com::sun::star::sdbc::SQLException
,
201 ::com::sun::star::lang::IndexOutOfBoundsException
,
202 ::com::sun::star::uno::RuntimeException
)
205 osl::MutexGuard
guard( m_refMutex
->mutex
);
206 if( index
< 0 || index
>= m_values
.getLength() )
208 OUStringBuffer
buf( 128 );
209 buf
.appendAscii( "USERS: Index out of range (allowed 0 to " );
210 buf
.append( (sal_Int32
) (m_values
.getLength() -1) );
211 buf
.appendAscii( ", got " );
213 buf
.appendAscii( ")" );
214 throw com::sun::star::lang::IndexOutOfBoundsException(
215 buf
.makeStringAndClear(), *this );
218 Reference
< XPropertySet
> set
;
219 m_values
[index
] >>= set
;
220 Statics
&st
= getStatics();
222 set
->getPropertyValue( getStatics().NAME
) >>= name
;
224 OUStringBuffer
update( 128 );
225 update
.appendAscii( "DROP USER " );
226 bufferQuoteIdentifier( update
, name
);
228 Reference
< XStatement
> stmt
= m_origin
->createStatement( );
229 DisposeGuard
disposeGuard( stmt
);
230 stmt
->executeUpdate( update
.makeStringAndClear() );
234 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> Users::createDataDescriptor()
235 throw (::com::sun::star::uno::RuntimeException
)
237 return new UserDescriptor( m_refMutex
, m_origin
, m_pSettings
);
240 Reference
< com::sun::star::container::XNameAccess
> Users::create(
241 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
242 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
243 ConnectionSettings
*pSettings
)
245 Users
*pUsers
= new Users( refMutex
, origin
, pSettings
);
246 Reference
< com::sun::star::container::XNameAccess
> ret
= pUsers
;
252 void Users::disposing()