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 * 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/.
35 ************************************************************************/
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>
43 #include <cppuhelper/exc_hlp.hxx>
44 #include <o3tl/safeint.hxx>
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"
52 using osl::MutexGuard
;
54 using com::sun::star::beans::XPropertySet
;
56 using com::sun::star::uno::Any
;
57 using com::sun::star::uno::UNO_QUERY
;
58 using com::sun::star::uno::Reference
;
60 using com::sun::star::container::NoSuchElementException
;
62 using com::sun::star::sdbc::XRow
;
63 using com::sun::star::sdbc::XStatement
;
64 using com::sun::star::sdbc::XResultSet
;
67 namespace pq_sdbc_driver
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
)
83 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
84 Statics
& st
= getStatics();
86 Reference
< XStatement
> stmt
= m_origin
->createStatement();
88 Reference
< XResultSet
> rs
= stmt
->executeQuery(u
"SELECT "
89 "DISTINCT ON( pg_namespace.nspname, relname) " // needed because of duplicates
90 "pg_namespace.nspname," // 1
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\'"_ustr
);
98 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
102 sal_Int32 viewIndex
= 0;
106 OUString table
, schema
, command
;
107 schema
= xRow
->getString( 1 );
108 table
= xRow
->getString( 2 );
109 command
= xRow
->getString( 3 );
111 rtl::Reference
<View
> pView
= new View (m_xMutex
, m_origin
, m_pSettings
);
112 Reference
< css::beans::XPropertySet
> prop
= pView
;
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
) );
119 m_values
.push_back( Any( prop
) );
120 map
[ schema
+ "." + table
] = viewIndex
;
124 m_name2index
.swap( map
);
126 catch ( css::sdbc::SQLException
& e
)
128 css::uno::Any anyEx
= cppu::getCaughtException();
129 throw css::lang::WrappedTargetRuntimeException( e
.Message
,
132 fire( RefreshedBroadcaster( *this ) );
136 void Views::appendByDescriptor(
137 const css::uno::Reference
< css::beans::XPropertySet
>& descriptor
)
139 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
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
;
147 Reference
< XStatement
> stmt
= m_origin
->createStatement();
149 OUStringBuffer
buf( 128 );
151 buf
.append( "CREATE VIEW ");
152 bufferQuoteQualifiedIdentifier( buf
, schema
, name
, m_pSettings
);
153 buf
.append(" AS " + command
);
155 stmt
->executeUpdate( buf
.makeStringAndClear() );
157 disposeNoThrow( stmt
);
159 if( m_pSettings
->tables
.is() )
161 m_pSettings
->pTablesImpl
->refresh();
165 void Views::dropByName( const OUString
& elementName
)
167 String2IntMap::const_iterator ii
= m_name2index
.find( elementName
);
168 if( ii
== m_name2index
.end() )
170 throw css::container::NoSuchElementException(
171 "View " + elementName
+ " is unknown, so it can't be dropped", *this );
173 dropByIndex( ii
->second
);
176 void Views::dropByIndex( sal_Int32 index
)
178 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
179 if( index
< 0 || o3tl::make_unsigned(index
) >= m_values
.size() )
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
) + ")",
187 Reference
< XPropertySet
> set
;
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
;
194 Reference
< XStatement
> stmt
= m_origin
->createStatement( );
196 stmt
->executeUpdate( "DROP VIEW \"" + schema
+ "\".\"" + name
+ "\"" );
200 css::uno::Reference
< css::beans::XPropertySet
> Views::createDataDescriptor()
202 return new ViewDescriptor( m_xMutex
, m_origin
, m_pSettings
);
205 Reference
< css::container::XNameAccess
> Views::create(
206 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
207 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
208 ConnectionSettings
*pSettings
,
209 rtl::Reference
<Views
> *ppViews
)
211 *ppViews
= new Views( refMutex
, origin
, pSettings
);
212 (*ppViews
)->refresh();
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */