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 * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
33 * The contents of this file are subject to the Mozilla Public License Version
34 * 1.1 (the "License"); you may not use this file except in compliance with
35 * the License or as specified alternatively below. You may obtain a copy of
36 * the License at http://www.mozilla.org/MPL/
38 * Software distributed under the License is distributed on an "AS IS" basis,
39 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
40 * for the specific language governing rights and limitations under the
43 * Major Contributor(s):
44 * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
46 * All Rights Reserved.
48 * For minor contributions see the git repository.
50 * Alternatively, the contents of this file may be used under the terms of
51 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
52 * the GNU Lesser General Public License Version 2.1 or later (the "LGPLv2.1+"),
53 * in which case the provisions of the GPLv3+ or the LGPLv2.1+ are applicable
54 * instead of those above.
56 ************************************************************************/
58 #include <rtl/ustrbuf.hxx>
60 #include <com/sun/star/sdbc/XRow.hpp>
61 #include <com/sun/star/sdbc/XParameters.hpp>
62 #include <com/sun/star/sdbcx/Privilege.hpp>
64 #include "pq_xusers.hxx"
65 #include "pq_xuser.hxx"
66 #include "pq_statics.hxx"
67 #include "pq_tools.hxx"
69 using osl::MutexGuard
;
72 using rtl::OUStringBuffer
;
73 using rtl::OUStringToOString
;
75 using com::sun::star::beans::XPropertySet
;
77 using com::sun::star::uno::Any
;
78 using com::sun::star::uno::makeAny
;
79 using com::sun::star::uno::UNO_QUERY
;
80 using com::sun::star::uno::Type
;
81 using com::sun::star::uno::XInterface
;
82 using com::sun::star::uno::Reference
;
83 using com::sun::star::uno::Sequence
;
84 using com::sun::star::uno::RuntimeException
;
86 using com::sun::star::container::NoSuchElementException
;
87 using com::sun::star::lang::WrappedTargetException
;
89 using com::sun::star::sdbc::XRow
;
90 using com::sun::star::sdbc::XCloseable
;
91 using com::sun::star::sdbc::XStatement
;
92 using com::sun::star::sdbc::XResultSet
;
93 using com::sun::star::sdbc::XParameters
;
94 using com::sun::star::sdbc::XPreparedStatement
;
95 using com::sun::star::sdbc::XDatabaseMetaData
;
97 namespace pq_sdbc_driver
99 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
101 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
102 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
103 ConnectionSettings
*pSettings
)
104 : Container( refMutex
, origin
, pSettings
, getStatics().USER
)
110 void Users::refresh()
111 throw (::com::sun::star::uno::RuntimeException
)
115 osl::MutexGuard
guard( m_refMutex
->mutex
);
116 Statics
& st
= getStatics();
118 Reference
< XStatement
> stmt
= m_origin
->createStatement();
120 Reference
< XResultSet
> rs
=
121 stmt
->executeQuery( ASCII_STR( "SELECT usename FROM pg_shadow" ) );
123 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
127 m_values
= Sequence
< com::sun::star::uno::Any
> ( );
128 sal_Int32 tableIndex
= 0;
132 new User( m_refMutex
, m_origin
, m_pSettings
);
133 Reference
< com::sun::star::beans::XPropertySet
> prop
= pUser
;
135 OUString name
= xRow
->getString( 1);
136 pUser
->setPropertyValue_NoBroadcast_public(
137 st
.NAME
, makeAny(xRow
->getString( TABLE_INDEX_CATALOG
+1) ) );
140 const int currentTableIndex
= tableIndex
++;
141 assert(currentTableIndex
== m_values
.getLength());
142 m_values
.realloc( tableIndex
);
143 m_values
[currentTableIndex
] = makeAny( prop
);
144 map
[ name
] = currentTableIndex
;
147 m_name2index
.swap( map
);
149 catch ( com::sun::star::sdbc::SQLException
& e
)
151 throw RuntimeException( e
.Message
, e
.Context
);
154 fire( RefreshedBroadcaster( *this ) );
158 void Users::appendByDescriptor(
159 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& descriptor
)
160 throw (::com::sun::star::sdbc::SQLException
,
161 ::com::sun::star::container::ElementExistException
,
162 ::com::sun::star::uno::RuntimeException
)
164 osl::MutexGuard
guard( m_refMutex
->mutex
);
166 OUStringBuffer
update( 128 );
167 update
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "CREATE USER " ) );
168 bufferQuoteIdentifier( update
, extractStringProperty( descriptor
, getStatics().NAME
), m_pSettings
);
169 update
.appendAscii( RTL_CONSTASCII_STRINGPARAM( " PASSWORD " ) );
170 bufferQuoteConstant( update
, extractStringProperty( descriptor
, getStatics().PASSWORD
), m_pSettings
);
172 Reference
< XStatement
> stmt
= m_origin
->createStatement( );
173 DisposeGuard
disposeGuard( stmt
);
174 stmt
->executeUpdate( update
.makeStringAndClear() );
177 void Users::dropByName( const ::rtl::OUString
& elementName
)
178 throw (::com::sun::star::sdbc::SQLException
,
179 ::com::sun::star::container::NoSuchElementException
,
180 ::com::sun::star::uno::RuntimeException
)
182 String2IntMap::const_iterator ii
= m_name2index
.find( elementName
);
183 if( ii
== m_name2index
.end() )
185 OUStringBuffer
buf( 128 );
186 buf
.appendAscii( "User " );
187 buf
.append( elementName
);
188 buf
.appendAscii( " is unknown, so it can't be dropped" );
189 throw com::sun::star::container::NoSuchElementException(
190 buf
.makeStringAndClear(), *this );
192 dropByIndex( ii
->second
);
195 void Users::dropByIndex( sal_Int32 index
)
196 throw (::com::sun::star::sdbc::SQLException
,
197 ::com::sun::star::lang::IndexOutOfBoundsException
,
198 ::com::sun::star::uno::RuntimeException
)
201 osl::MutexGuard
guard( m_refMutex
->mutex
);
202 if( index
< 0 || index
>= m_values
.getLength() )
204 OUStringBuffer
buf( 128 );
205 buf
.appendAscii( "USERS: Index out of range (allowed 0 to " );
206 buf
.append( (sal_Int32
) (m_values
.getLength() -1) );
207 buf
.appendAscii( ", got " );
209 buf
.appendAscii( ")" );
210 throw com::sun::star::lang::IndexOutOfBoundsException(
211 buf
.makeStringAndClear(), *this );
214 Reference
< XPropertySet
> set
;
215 m_values
[index
] >>= set
;
217 set
->getPropertyValue( getStatics().NAME
) >>= name
;
219 OUStringBuffer
update( 128 );
220 update
.appendAscii( "DROP USER " );
221 bufferQuoteIdentifier( update
, name
, m_pSettings
);
223 Reference
< XStatement
> stmt
= m_origin
->createStatement( );
224 DisposeGuard
disposeGuard( stmt
);
225 stmt
->executeUpdate( update
.makeStringAndClear() );
229 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> Users::createDataDescriptor()
230 throw (::com::sun::star::uno::RuntimeException
)
232 return new UserDescriptor( m_refMutex
, m_origin
, m_pSettings
);
235 Reference
< com::sun::star::container::XNameAccess
> Users::create(
236 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
237 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
238 ConnectionSettings
*pSettings
)
240 Users
*pUsers
= new Users( refMutex
, origin
, pSettings
);
241 Reference
< com::sun::star::container::XNameAccess
> ret
= pUsers
;
247 void Users::disposing()