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_xviews.hxx"
65 #include "pq_xview.hxx"
66 #include "pq_xtables.hxx"
67 #include "pq_statics.hxx"
68 #include "pq_tools.hxx"
70 using osl::MutexGuard
;
73 using rtl::OUStringBuffer
;
74 using rtl::OUStringToOString
;
76 using com::sun::star::beans::XPropertySet
;
78 using com::sun::star::uno::Any
;
79 using com::sun::star::uno::makeAny
;
80 using com::sun::star::uno::UNO_QUERY
;
81 using com::sun::star::uno::Type
;
82 using com::sun::star::uno::XInterface
;
83 using com::sun::star::uno::Reference
;
84 using com::sun::star::uno::Sequence
;
85 using com::sun::star::uno::RuntimeException
;
87 using com::sun::star::container::NoSuchElementException
;
88 using com::sun::star::lang::WrappedTargetException
;
90 using com::sun::star::sdbc::XRow
;
91 using com::sun::star::sdbc::XCloseable
;
92 using com::sun::star::sdbc::XStatement
;
93 using com::sun::star::sdbc::XResultSet
;
94 using com::sun::star::sdbc::XParameters
;
95 using com::sun::star::sdbc::XPreparedStatement
;
96 using com::sun::star::sdbc::XDatabaseMetaData
;
98 // using com::sun::star::sdbcx::Privilege;
100 namespace pq_sdbc_driver
102 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
104 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
105 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
106 ConnectionSettings
*pSettings
)
107 : Container( refMutex
, origin
, pSettings
, getStatics().VIEW
)
113 void Views::refresh()
114 throw (::com::sun::star::uno::RuntimeException
)
118 osl::MutexGuard
guard( m_refMutex
->mutex
);
119 Statics
& st
= getStatics();
121 Reference
< XStatement
> stmt
= m_origin
->createStatement();
123 Reference
< XResultSet
> rs
= stmt
->executeQuery(
125 "DISTINCT ON( pg_namespace.nspname, relname) " // needed because of duplicates
126 "pg_namespace.nspname," // 1
128 "pg_get_viewdef(ev_class) " // 3
129 "FROM pg_namespace, pg_class, pg_rewrite "
130 "WHERE pg_namespace.oid = relnamespace "
131 "AND pg_class.oid = ev_class "
132 "AND relkind='v'" ) );
134 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
136 m_values
= Sequence
< com::sun::star::uno::Any
> ();
138 sal_Int32 viewIndex
= 0;
142 rtl::OUString table
, schema
, command
;
143 schema
= xRow
->getString( 1 );
144 table
= xRow
->getString( 2 );
145 command
= xRow
->getString( 3 );
147 View
*pView
= new View (m_refMutex
, m_origin
, m_pSettings
);
148 Reference
< com::sun::star::beans::XPropertySet
> prop
= pView
;
150 pView
->setPropertyValue_NoBroadcast_public(st
.NAME
, makeAny(table
) );
151 pView
->setPropertyValue_NoBroadcast_public(st
.SCHEMA_NAME
, makeAny(schema
) );
152 pView
->setPropertyValue_NoBroadcast_public(st
.COMMAND
, makeAny(command
) );
155 const int currentViewIndex
= viewIndex
++;
156 assert(currentViewIndex
== m_values
.getLength());
157 m_values
.realloc( viewIndex
);
158 m_values
[currentViewIndex
] = makeAny( prop
);
159 OUStringBuffer
buf( table
.getLength() + schema
.getLength() + 1);
160 buf
.append( schema
).appendAscii( "." ).append( table
);
161 map
[ buf
.makeStringAndClear() ] = currentViewIndex
;
164 m_name2index
.swap( map
);
166 catch ( com::sun::star::sdbc::SQLException
& e
)
168 throw RuntimeException( e
.Message
, e
.Context
);
170 fire( RefreshedBroadcaster( *this ) );
174 void Views::appendByDescriptor(
175 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& descriptor
)
176 throw (::com::sun::star::sdbc::SQLException
,
177 ::com::sun::star::container::ElementExistException
,
178 ::com::sun::star::uno::RuntimeException
)
180 osl::MutexGuard
guard( m_refMutex
->mutex
);
182 Statics
&st
= getStatics();
183 OUString name
,schema
,command
;
184 descriptor
->getPropertyValue( st
.SCHEMA_NAME
) >>= schema
;
185 descriptor
->getPropertyValue( st
.NAME
) >>= name
;
186 descriptor
->getPropertyValue( st
.COMMAND
) >>= command
;
188 Reference
< XStatement
> stmt
= m_origin
->createStatement();
190 OUStringBuffer
buf( 128 );
192 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "CREATE VIEW " ) );
193 bufferQuoteQualifiedIdentifier( buf
, schema
, name
, m_pSettings
);
194 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( " AS " ) );
195 buf
.append( command
);
197 stmt
->executeUpdate( buf
.makeStringAndClear() );
199 disposeNoThrow( stmt
);
201 if( m_pSettings
->tables
.is() )
203 m_pSettings
->pTablesImpl
->refresh();
205 // increase the vector
206 // sal_Int32 index = m_values.getLength();
207 // m_values.realloc( index + 1 );
210 // new View( m_refMutex, m_origin, m_pSettings, false /*modifiable*/ );
211 // Reference< com::sun::star::beans::XPropertySet > prop = pTable;
212 // copyProperties( pTable, descriptor );
213 // m_values[index] = makeAny( prop );
214 // OUStringBuffer buf( name.getLength() + 1 + schema.getLength() );
215 // buf.append( schema ).appendAscii( "." ).append( name );
216 // m_name2index[ buf.makeStringAndClear() ] = index;
219 void Views::dropByName( const ::rtl::OUString
& elementName
)
220 throw (::com::sun::star::sdbc::SQLException
,
221 ::com::sun::star::container::NoSuchElementException
,
222 ::com::sun::star::uno::RuntimeException
)
224 String2IntMap::const_iterator ii
= m_name2index
.find( elementName
);
225 if( ii
== m_name2index
.end() )
227 OUStringBuffer
buf( 128 );
228 buf
.appendAscii( "View " );
229 buf
.append( elementName
);
230 buf
.appendAscii( " is unknown, so it can't be dropped" );
231 throw com::sun::star::container::NoSuchElementException(
232 buf
.makeStringAndClear(), *this );
234 dropByIndex( ii
->second
);
237 void Views::dropByIndex( sal_Int32 index
)
238 throw (::com::sun::star::sdbc::SQLException
,
239 ::com::sun::star::lang::IndexOutOfBoundsException
,
240 ::com::sun::star::uno::RuntimeException
)
242 // throw SQLException(
243 // ASCII_STR( "view deletion not supported" ), *this, OUString(), 1, Any() );
244 osl::MutexGuard
guard( m_refMutex
->mutex
);
245 if( index
< 0 || index
>= m_values
.getLength() )
247 OUStringBuffer
buf( 128 );
248 buf
.appendAscii( "VIEWS: Index out of range (allowed 0 to " );
249 buf
.append( (sal_Int32
) (m_values
.getLength() -1) );
250 buf
.appendAscii( ", got " );
252 buf
.appendAscii( ")" );
253 throw com::sun::star::lang::IndexOutOfBoundsException(
254 buf
.makeStringAndClear(), *this );
257 Reference
< XPropertySet
> set
;
258 m_values
[index
] >>= set
;
259 Statics
&st
= getStatics();
260 OUString name
,schema
;
261 set
->getPropertyValue( st
.SCHEMA_NAME
) >>= schema
;
262 set
->getPropertyValue( st
.NAME
) >>= name
;
264 OUStringBuffer
update( 128 );
265 update
.appendAscii( "DROP VIEW \"" ).append( schema
).appendAscii( "\".\"" );
266 update
.append( name
).appendAscii( "\"" );
268 Reference
< XStatement
> stmt
= m_origin
->createStatement( );
270 stmt
->executeUpdate( update
.makeStringAndClear() );
274 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> Views::createDataDescriptor()
275 throw (::com::sun::star::uno::RuntimeException
)
277 return new ViewDescriptor( m_refMutex
, m_origin
, m_pSettings
);
280 Reference
< com::sun::star::container::XNameAccess
> Views::create(
281 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
282 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
283 ConnectionSettings
*pSettings
,
286 *ppViews
= new Views( refMutex
, origin
, pSettings
);
287 Reference
< com::sun::star::container::XNameAccess
> ret
= *ppViews
;
288 (*ppViews
)->refresh();
293 void Views::disposing()
295 Container::disposing();