1 /*************************************************************************
3 * $RCSfile: pq_xviews.cxx,v $
7 * last change: $Author: jbu $ $Date: 2006/05/01 19:19:09 $
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_xviews.hxx"
71 #include "pq_xview.hxx"
72 #include "pq_xtables.hxx"
73 #include "pq_statics.hxx"
74 #include "pq_tools.hxx"
76 using osl::MutexGuard
;
79 using rtl::OUStringBuffer
;
80 using rtl::OUStringToOString
;
82 using com::sun::star::beans::XPropertySet
;
84 using com::sun::star::uno::Any
;
85 using com::sun::star::uno::makeAny
;
86 using com::sun::star::uno::UNO_QUERY
;
87 using com::sun::star::uno::Type
;
88 using com::sun::star::uno::XInterface
;
89 using com::sun::star::uno::Reference
;
90 using com::sun::star::uno::Sequence
;
91 using com::sun::star::uno::RuntimeException
;
93 using com::sun::star::container::NoSuchElementException
;
94 using com::sun::star::lang::WrappedTargetException
;
96 using com::sun::star::sdbc::XRow
;
97 using com::sun::star::sdbc::XCloseable
;
98 using com::sun::star::sdbc::XStatement
;
99 using com::sun::star::sdbc::XResultSet
;
100 using com::sun::star::sdbc::XParameters
;
101 using com::sun::star::sdbc::XPreparedStatement
;
102 using com::sun::star::sdbc::XDatabaseMetaData
;
104 // using com::sun::star::sdbcx::Privilege;
106 namespace pq_sdbc_driver
108 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
110 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
111 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
112 ConnectionSettings
*pSettings
)
113 : Container( refMutex
, origin
, pSettings
, getStatics().VIEW
)
119 void Views::refresh()
120 throw (::com::sun::star::uno::RuntimeException
)
124 osl::MutexGuard
guard( m_refMutex
->mutex
);
125 Statics
& st
= getStatics();
127 Reference
< XStatement
> stmt
= m_origin
->createStatement();
129 Reference
< XResultSet
> rs
= stmt
->executeQuery(
131 "DISTINCT ON( pg_namespace.nspname, relname) " // needed because of duplicates
132 "pg_namespace.nspname," // 1
134 "pg_get_viewdef(ev_class) " // 3
135 "FROM pg_namespace, pg_class, pg_rewrite "
136 "WHERE pg_namespace.oid = relnamespace "
137 "AND pg_class.oid = ev_class "
138 "AND relkind='v'" ) );
140 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
142 std::vector
< Any
, Allocator
< Any
> > vec
;
144 sal_Int32 viewIndex
= 0;
148 rtl::OUString table
, schema
, command
;
149 schema
= xRow
->getString( 1 );
150 table
= xRow
->getString( 2 );
151 command
= xRow
->getString( 3 );
153 View
*pView
= new View (m_refMutex
, m_origin
, m_pSettings
);
154 Reference
< com::sun::star::beans::XPropertySet
> prop
= pView
;
156 pView
->setPropertyValue_NoBroadcast_public(st
.NAME
, makeAny(table
) );
157 pView
->setPropertyValue_NoBroadcast_public(st
.SCHEMA_NAME
, makeAny(schema
) );
158 pView
->setPropertyValue_NoBroadcast_public(st
.COMMAND
, makeAny(command
) );
159 vec
.push_back( makeAny( prop
) );
161 OUStringBuffer
buf( table
.getLength() + schema
.getLength() + 1);
162 buf
.append( schema
).appendAscii( "." ).append( table
);
163 map
[ buf
.makeStringAndClear() ] = viewIndex
;
167 m_values
= Sequence
< com::sun::star::uno::Any
> ( &vec
[0], vec
.size() );
168 m_name2index
.swap( map
);
170 catch ( com::sun::star::sdbc::SQLException
& e
)
172 throw RuntimeException( e
.Message
, e
.Context
);
174 fire( RefreshedBroadcaster( *this ) );
178 void Views::appendByDescriptor(
179 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& descriptor
)
180 throw (::com::sun::star::sdbc::SQLException
,
181 ::com::sun::star::container::ElementExistException
,
182 ::com::sun::star::uno::RuntimeException
)
184 osl::MutexGuard
guard( m_refMutex
->mutex
);
186 Statics
&st
= getStatics();
187 OUString name
,schema
,command
;
188 descriptor
->getPropertyValue( st
.SCHEMA_NAME
) >>= schema
;
189 descriptor
->getPropertyValue( st
.NAME
) >>= name
;
190 descriptor
->getPropertyValue( st
.COMMAND
) >>= command
;
192 Reference
< XStatement
> stmt
= m_origin
->createStatement();
194 OUStringBuffer
buf( 128 );
196 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "CREATE VIEW " ) );
197 bufferQuoteQualifiedIdentifier( buf
, schema
, name
);
198 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( " AS " ) );
199 buf
.append( command
);
201 stmt
->executeUpdate( buf
.makeStringAndClear() );
203 disposeNoThrow( stmt
);
205 if( m_pSettings
->tables
.is() )
207 m_pSettings
->pTablesImpl
->refresh();
209 // increase the vector
210 // sal_Int32 index = m_values.getLength();
211 // m_values.realloc( index + 1 );
214 // new View( m_refMutex, m_origin, m_pSettings, false /*modifiable*/ );
215 // Reference< com::sun::star::beans::XPropertySet > prop = pTable;
216 // copyProperties( pTable, descriptor );
217 // m_values[index] = makeAny( prop );
218 // OUStringBuffer buf( name.getLength() + 1 + schema.getLength() );
219 // buf.append( schema ).appendAscii( "." ).append( name );
220 // m_name2index[ buf.makeStringAndClear() ] = index;
223 void Views::dropByName( const ::rtl::OUString
& elementName
)
224 throw (::com::sun::star::sdbc::SQLException
,
225 ::com::sun::star::container::NoSuchElementException
,
226 ::com::sun::star::uno::RuntimeException
)
228 String2IntMap::const_iterator ii
= m_name2index
.find( elementName
);
229 if( ii
== m_name2index
.end() )
231 OUStringBuffer
buf( 128 );
232 buf
.appendAscii( "View " );
233 buf
.append( elementName
);
234 buf
.appendAscii( " is unknown, so it can't be dropped" );
235 throw com::sun::star::container::NoSuchElementException(
236 buf
.makeStringAndClear(), *this );
238 dropByIndex( ii
->second
);
241 void Views::dropByIndex( sal_Int32 index
)
242 throw (::com::sun::star::sdbc::SQLException
,
243 ::com::sun::star::lang::IndexOutOfBoundsException
,
244 ::com::sun::star::uno::RuntimeException
)
246 // throw SQLException(
247 // ASCII_STR( "view deletion not supported" ), *this, OUString(), 1, Any() );
248 osl::MutexGuard
guard( m_refMutex
->mutex
);
249 if( index
< 0 || index
>= m_values
.getLength() )
251 OUStringBuffer
buf( 128 );
252 buf
.appendAscii( "VIEWS: Index out of range (allowed 0 to " );
253 buf
.append( (sal_Int32
) (m_values
.getLength() -1) );
254 buf
.appendAscii( ", got " );
256 buf
.appendAscii( ")" );
257 throw com::sun::star::lang::IndexOutOfBoundsException(
258 buf
.makeStringAndClear(), *this );
261 Reference
< XPropertySet
> set
;
262 m_values
[index
] >>= set
;
263 Statics
&st
= getStatics();
264 OUString name
,schema
;
265 set
->getPropertyValue( st
.SCHEMA_NAME
) >>= schema
;
266 set
->getPropertyValue( st
.NAME
) >>= name
;
268 OUStringBuffer
update( 128 );
269 update
.appendAscii( "DROP VIEW \"" ).append( schema
).appendAscii( "\".\"" );
270 update
.append( name
).appendAscii( "\"" );
272 Reference
< XStatement
> stmt
= m_origin
->createStatement( );
274 stmt
->executeUpdate( update
.makeStringAndClear() );
278 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> Views::createDataDescriptor()
279 throw (::com::sun::star::uno::RuntimeException
)
281 return new ViewDescriptor( m_refMutex
, m_origin
, m_pSettings
);
284 Reference
< com::sun::star::container::XNameAccess
> Views::create(
285 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
286 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
287 ConnectionSettings
*pSettings
,
290 *ppViews
= new Views( refMutex
, origin
, pSettings
);
291 Reference
< com::sun::star::container::XNameAccess
> ret
= *ppViews
;
292 (*ppViews
)->refresh();
297 void Views::disposing()
299 Container::disposing();