Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_xviews.cxx
blobfbc2ebaebddb05264059cd61263bdcdfdc1a2a63
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,
18 * MA 02111-1307 USA
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/ustrbuf.hxx>
39 #include <com/sun/star/sdbc/XRow.hpp>
40 #include <com/sun/star/sdbc/XParameters.hpp>
41 #include <com/sun/star/sdbcx/Privilege.hpp>
43 #include "pq_xviews.hxx"
44 #include "pq_xview.hxx"
45 #include "pq_xtables.hxx"
46 #include "pq_statics.hxx"
47 #include "pq_tools.hxx"
49 using osl::MutexGuard;
52 using com::sun::star::beans::XPropertySet;
54 using com::sun::star::uno::Any;
55 using com::sun::star::uno::makeAny;
56 using com::sun::star::uno::UNO_QUERY;
57 using com::sun::star::uno::Type;
58 using com::sun::star::uno::XInterface;
59 using com::sun::star::uno::Reference;
60 using com::sun::star::uno::Sequence;
61 using com::sun::star::uno::RuntimeException;
63 using com::sun::star::container::NoSuchElementException;
64 using com::sun::star::lang::WrappedTargetException;
66 using com::sun::star::sdbc::XRow;
67 using com::sun::star::sdbc::XCloseable;
68 using com::sun::star::sdbc::XStatement;
69 using com::sun::star::sdbc::XResultSet;
70 using com::sun::star::sdbc::XParameters;
71 using com::sun::star::sdbc::XPreparedStatement;
72 using com::sun::star::sdbc::XDatabaseMetaData;
74 // using com::sun::star::sdbcx::Privilege;
76 namespace pq_sdbc_driver
78 Views::Views(
79 const ::rtl::Reference< RefCountedMutex > & refMutex,
80 const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
81 ConnectionSettings *pSettings )
82 : Container( refMutex, origin, pSettings, getStatics().VIEW )
85 Views::~Views()
88 void Views::refresh()
89 throw (::com::sun::star::uno::RuntimeException, std::exception)
91 try
93 osl::MutexGuard guard( m_refMutex->mutex );
94 Statics & st = getStatics();
96 Reference< XStatement > stmt = m_origin->createStatement();
98 Reference< XResultSet > rs = stmt->executeQuery("SELECT "
99 "DISTINCT ON( pg_namespace.nspname, relname) " // needed because of duplicates
100 "pg_namespace.nspname," // 1
101 "relname," // 2
102 "pg_get_viewdef(ev_class) " // 3
103 "FROM pg_namespace, pg_class, pg_rewrite "
104 "WHERE pg_namespace.oid = relnamespace "
105 "AND pg_class.oid = ev_class "
106 "AND relkind=\'v\'" );
108 Reference< XRow > xRow( rs , UNO_QUERY );
110 m_values = Sequence< com::sun::star::uno::Any > ();
111 String2IntMap map;
112 sal_Int32 viewIndex = 0;
114 while( rs->next() )
116 OUString table, schema, command;
117 schema = xRow->getString( 1 );
118 table = xRow->getString( 2 );
119 command = xRow->getString( 3 );
121 View *pView = new View (m_refMutex, m_origin, m_pSettings );
122 Reference< com::sun::star::beans::XPropertySet > prop = pView;
124 pView->setPropertyValue_NoBroadcast_public(st.NAME , makeAny(table) );
125 pView->setPropertyValue_NoBroadcast_public(st.SCHEMA_NAME, makeAny(schema) );
126 pView->setPropertyValue_NoBroadcast_public(st.COMMAND, makeAny(command) );
129 const int currentViewIndex = viewIndex++;
130 assert(currentViewIndex == m_values.getLength());
131 m_values.realloc( viewIndex );
132 m_values[currentViewIndex] = makeAny( prop );
133 OUStringBuffer buf( table.getLength() + schema.getLength() + 1);
134 buf.append( schema + "." + table );
135 map[ buf.makeStringAndClear() ] = currentViewIndex;
138 m_name2index.swap( map );
140 catch ( com::sun::star::sdbc::SQLException & e )
142 throw RuntimeException( e.Message , e.Context );
144 fire( RefreshedBroadcaster( *this ) );
148 void Views::appendByDescriptor(
149 const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor )
150 throw (::com::sun::star::sdbc::SQLException,
151 ::com::sun::star::container::ElementExistException,
152 ::com::sun::star::uno::RuntimeException, std::exception)
154 osl::MutexGuard guard( m_refMutex->mutex );
156 Statics &st = getStatics();
157 OUString name,schema,command;
158 descriptor->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
159 descriptor->getPropertyValue( st.NAME ) >>= name;
160 descriptor->getPropertyValue( st.COMMAND ) >>= command;
162 Reference< XStatement > stmt = m_origin->createStatement();
164 OUStringBuffer buf( 128 );
166 buf.append( "CREATE VIEW ");
167 bufferQuoteQualifiedIdentifier( buf, schema, name, m_pSettings );
168 buf.append(" AS " + command );
170 stmt->executeUpdate( buf.makeStringAndClear() );
172 disposeNoThrow( stmt );
173 refresh();
174 if( m_pSettings->tables.is() )
176 m_pSettings->pTablesImpl->refresh();
180 void Views::dropByName( const OUString& elementName )
181 throw (::com::sun::star::sdbc::SQLException,
182 ::com::sun::star::container::NoSuchElementException,
183 ::com::sun::star::uno::RuntimeException, std::exception)
185 String2IntMap::const_iterator ii = m_name2index.find( elementName );
186 if( ii == m_name2index.end() )
188 OUStringBuffer buf( 128 );
189 buf.append( "View " + elementName + " is unknown, so it can't be dropped" );
190 throw com::sun::star::container::NoSuchElementException(
191 buf.makeStringAndClear(), *this );
193 dropByIndex( ii->second );
196 void Views::dropByIndex( sal_Int32 index )
197 throw (::com::sun::star::sdbc::SQLException,
198 ::com::sun::star::lang::IndexOutOfBoundsException,
199 ::com::sun::star::uno::RuntimeException, std::exception)
201 osl::MutexGuard guard( m_refMutex->mutex );
202 if( index < 0 || index >= m_values.getLength() )
204 OUStringBuffer buf( 128 );
205 buf.append( "VIEWS: Index out of range (allowed 0 to " + OUString::number(m_values.getLength() -1) +
206 ", got " + OUString::number( index ) + ")");
207 throw com::sun::star::lang::IndexOutOfBoundsException(
208 buf.makeStringAndClear(), *this );
211 Reference< XPropertySet > set;
212 m_values[index] >>= set;
213 Statics &st = getStatics();
214 OUString name,schema;
215 set->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
216 set->getPropertyValue( st.NAME ) >>= name;
218 OUStringBuffer update( 128 );
219 update.append( "DROP VIEW \"" + schema + "\".\"" + name + "\"" );
221 Reference< XStatement > stmt = m_origin->createStatement( );
223 stmt->executeUpdate( update.makeStringAndClear() );
227 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > Views::createDataDescriptor()
228 throw (::com::sun::star::uno::RuntimeException, std::exception)
230 return new ViewDescriptor( m_refMutex, m_origin, m_pSettings );
233 Reference< com::sun::star::container::XNameAccess > Views::create(
234 const ::rtl::Reference< RefCountedMutex > & refMutex,
235 const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
236 ConnectionSettings *pSettings,
237 Views **ppViews)
239 *ppViews = new Views( refMutex, origin, pSettings );
240 Reference< com::sun::star::container::XNameAccess > ret = *ppViews;
241 (*ppViews)->refresh();
243 return ret;
246 void Views::disposing()
248 Container::disposing();
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */