LibreOffice Module connectivity (master) 1
pq_xviews.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*************************************************************************
3 *
4 * Effective License of whole file:
5 *
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.
9 *
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.
14 *
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,
18 * MA 02111-1307 USA
19 *
20 * Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
21 *
22 * The Contents of this file are made available subject to the terms of
23 * the GNU Lesser General Public License Version 2.1
24 *
25 * Copyright: 2000 by Sun Microsystems, Inc.
26 *
27 * Contributor(s): Joerg Budischewski
28 *
29 * All parts contributed on or after August 2011:
30 *
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/.
34 *
35 ************************************************************************/
36
37#include <rtl/ref.hxx>
38#include <rtl/ustrbuf.hxx>
39#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
40#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
41#include <com/sun/star/sdbc/SQLException.hpp>
42#include <com/sun/star/sdbc/XRow.hpp>
44#include <o3tl/safeint.hxx>
45
46#include "pq_xviews.hxx"
47#include "pq_xview.hxx"
48#include "pq_xtables.hxx"
49#include "pq_statics.hxx"
50#include "pq_tools.hxx"
51
52using osl::MutexGuard;
53
55
56using com::sun::star::uno::Any;
57using com::sun::star::uno::UNO_QUERY;
59
60using com::sun::star::container::NoSuchElementException;
61
62using com::sun::star::sdbc::XRow;
63using com::sun::star::sdbc::XStatement;
64using com::sun::star::sdbc::XResultSet;
65
66
67namespace pq_sdbc_driver
68{
70 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
71 const css::uno::Reference< css::sdbc::XConnection > & origin,
72 ConnectionSettings *pSettings )
73 : Container( refMutex, origin, pSettings, getStatics().VIEW )
74{}
75
77{}
78
80{
81 try
82 {
83 osl::MutexGuard guard( m_xMutex->GetMutex() );
84 Statics & st = getStatics();
85
86 Reference< XStatement > stmt = m_origin->createStatement();
87
88 Reference< XResultSet > rs = stmt->executeQuery("SELECT "
89 "DISTINCT ON( pg_namespace.nspname, relname) " // needed because of duplicates
90 "pg_namespace.nspname," // 1
91 "relname," // 2
92 "pg_get_viewdef(ev_class) " // 3
93 "FROM pg_namespace, pg_class, pg_rewrite "
94 "WHERE pg_namespace.oid = relnamespace "
95 "AND pg_class.oid = ev_class "
96 "AND relkind=\'v\'" );
97
98 Reference< XRow > xRow( rs , UNO_QUERY );
99
100 m_values.clear();
102 sal_Int32 viewIndex = 0;
103
104 while( rs->next() )
105 {
106 OUString table, schema, command;
107 schema = xRow->getString( 1 );
108 table = xRow->getString( 2 );
109 command = xRow->getString( 3 );
110
113
114 pView->setPropertyValue_NoBroadcast_public(st.NAME , Any(table) );
115 pView->setPropertyValue_NoBroadcast_public(st.SCHEMA_NAME, Any(schema) );
116 pView->setPropertyValue_NoBroadcast_public(st.COMMAND, Any(command) );
117
118 {
119 m_values.push_back( Any( prop ) );
120 map[ schema + "." + table ] = viewIndex;
121 ++viewIndex;
122 }
123 }
124 m_name2index.swap( map );
125 }
126 catch ( css::sdbc::SQLException & e )
127 {
128 css::uno::Any anyEx = cppu::getCaughtException();
129 throw css::lang::WrappedTargetRuntimeException( e.Message,
130 e.Context, anyEx );
131 }
132 fire( RefreshedBroadcaster( *this ) );
133}
134
135
137 const css::uno::Reference< css::beans::XPropertySet >& descriptor )
138{
139 osl::MutexGuard guard( m_xMutex->GetMutex() );
140
141 Statics &st = getStatics();
142 OUString name,schema,command;
143 descriptor->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
144 descriptor->getPropertyValue( st.NAME ) >>= name;
145 descriptor->getPropertyValue( st.COMMAND ) >>= command;
146
147 Reference< XStatement > stmt = m_origin->createStatement();
148
149 OUStringBuffer buf( 128 );
150
151 buf.append( "CREATE VIEW ");
153 buf.append(" AS " + command );
154
155 stmt->executeUpdate( buf.makeStringAndClear() );
156
157 disposeNoThrow( stmt );
158 refresh();
159 if( m_pSettings->tables.is() )
160 {
161 m_pSettings->pTablesImpl->refresh();
162 }
163}
164
165void Views::dropByName( const OUString& elementName )
166{
167 String2IntMap::const_iterator ii = m_name2index.find( elementName );
168 if( ii == m_name2index.end() )
169 {
170 throw css::container::NoSuchElementException(
171 "View " + elementName + " is unknown, so it can't be dropped", *this );
172 }
173 dropByIndex( ii->second );
174}
175
176void Views::dropByIndex( sal_Int32 index )
177{
178 osl::MutexGuard guard( m_xMutex->GetMutex() );
179 if( index < 0 || o3tl::make_unsigned(index) >= m_values.size() )
180 {
181 throw css::lang::IndexOutOfBoundsException(
182 "VIEWS: Index out of range (allowed 0 to " + OUString::number(m_values.size() -1)
183 + ", got " + OUString::number( index ) + ")",
184 *this );
185 }
186
188 m_values[index] >>= set;
189 Statics &st = getStatics();
190 OUString name,schema;
191 set->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
192 set->getPropertyValue( st.NAME ) >>= name;
193
194 Reference< XStatement > stmt = m_origin->createStatement( );
195
196 stmt->executeUpdate( "DROP VIEW \"" + schema + "\".\"" + name + "\"" );
197}
198
199
200css::uno::Reference< css::beans::XPropertySet > Views::createDataDescriptor()
201{
203}
204
206 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
207 const css::uno::Reference< css::sdbc::XConnection > & origin,
208 ConnectionSettings *pSettings,
209 rtl::Reference<Views> *ppViews)
210{
211 *ppViews = new Views( refMutex, origin, pSettings );
212 (*ppViews)->refresh();
213
214 return *ppViews;
215}
216
217};
218
219/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ConnectionSettings * m_pSettings
void fire(const EventBroadcastHelper &helper)
css::uno::Reference< css::sdbc::XConnection > m_origin
::rtl::Reference< comphelper::RefCountedMutex > m_xMutex
std::vector< css::uno::Any > m_values
virtual ~Views() override
Definition: pq_xviews.cxx:76
virtual void SAL_CALL appendByDescriptor(const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
Definition: pq_xviews.cxx:136
virtual void SAL_CALL dropByName(const OUString &elementName) override
Definition: pq_xviews.cxx:165
virtual css::uno::Reference< css::beans::XPropertySet > SAL_CALL createDataDescriptor() override
Definition: pq_xviews.cxx:200
virtual void SAL_CALL dropByIndex(sal_Int32 index) override
Definition: pq_xviews.cxx:176
Views(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings)
Definition: pq_xviews.cxx:69
virtual void SAL_CALL refresh() override
Definition: pq_xviews.cxx:79
static css::uno::Reference< css::container::XNameAccess > create(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings, rtl::Reference< Views > *ppViews)
Definition: pq_xviews.cxx:205
@ table
void set(css::uno::UnoInterfaceReference const &value)
class SAL_NO_VTABLE XPropertySet
Any SAL_CALL getCaughtException()
index
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
Statics & getStatics()
Definition: pq_statics.cxx:112
void bufferQuoteQualifiedIdentifier(OUStringBuffer &buf, std::u16string_view schema, std::u16string_view table, ConnectionSettings *settings)
Definition: pq_tools.cxx:181
std::unordered_map< OUString, sal_Int32 > String2IntMap
void disposeNoThrow(const css::uno::Reference< css::uno::XInterface > &r)
Definition: pq_tools.cxx:235
OUString name
Definition: pq_statics.cxx:74
std::map< OUString, rtl::Reference< Entity > > map
css::uno::Reference< css::container::XNameAccess > tables
rtl::Reference< Tables > pTablesImpl