Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / misc / defaultobjectnamecheck.cxx
blob164ef07a7640fe94f8c4461c9187dba977522725
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "defaultobjectnamecheck.hxx"
22 #include "dbu_misc.hrc"
24 #include "moduledbu.hxx"
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
28 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
29 #include <com/sun/star/sdb/CommandType.hpp>
30 #include <com/sun/star/sdb/tools/XConnectionTools.hpp>
32 #include <connectivity/dbexception.hxx>
33 #include <connectivity/dbmetadata.hxx>
35 #include <rtl/ustrbuf.hxx>
37 #include <tools/diagnose_ex.h>
38 #include <cppuhelper/exc_hlp.hxx>
40 #include <vector>
41 #include <boost/shared_ptr.hpp>
43 namespace dbaui
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::container::XNameAccess;
48 using ::com::sun::star::lang::IllegalArgumentException;
49 using ::com::sun::star::container::XHierarchicalNameAccess;
50 using ::com::sun::star::sdbc::SQLException;
51 using ::com::sun::star::uno::Exception;
52 using ::com::sun::star::sdbc::XConnection;
53 using ::com::sun::star::sdbcx::XTablesSupplier;
54 using ::com::sun::star::sdb::XQueriesSupplier;
55 using ::com::sun::star::uno::UNO_QUERY_THROW;
56 using ::com::sun::star::uno::makeAny;
57 using ::com::sun::star::uno::Any;
58 using ::com::sun::star::sdb::tools::XObjectNames;
59 using ::com::sun::star::sdb::tools::XConnectionTools;
60 using ::com::sun::star::uno::UNO_QUERY;
62 using namespace dbtools;
64 namespace CommandType = ::com::sun::star::sdb::CommandType;
66 // helper
67 namespace
69 void lcl_fillNameExistsError( const OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay )
71 SQLException aError;
72 OUString sErrorMessage = ModuleRes(STR_NAMED_OBJECT_ALREADY_EXISTS).toString();
73 aError.Message = sErrorMessage.replaceAll("$#$", _rObjectName);
74 _out_rErrorToDisplay = aError;
79 // HierarchicalNameCheck_Impl
80 struct HierarchicalNameCheck_Impl
82 Reference< XHierarchicalNameAccess > xHierarchicalNames;
83 OUString sRelativeRoot;
86 // HierarchicalNameCheck
87 HierarchicalNameCheck::HierarchicalNameCheck( const Reference< XHierarchicalNameAccess >& _rxNames, const OUString& _rRelativeRoot )
88 :m_pImpl( new HierarchicalNameCheck_Impl )
90 m_pImpl->xHierarchicalNames = _rxNames;
91 m_pImpl->sRelativeRoot = _rRelativeRoot;
93 if ( !m_pImpl->xHierarchicalNames.is() )
94 throw IllegalArgumentException();
97 HierarchicalNameCheck::~HierarchicalNameCheck()
101 bool HierarchicalNameCheck::isNameValid( const OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) const
105 OUStringBuffer aCompleteName;
106 if ( !m_pImpl->sRelativeRoot.isEmpty() )
108 aCompleteName.append( m_pImpl->sRelativeRoot );
109 aCompleteName.appendAscii( "/" );
111 aCompleteName.append( _rObjectName );
113 OUString sCompleteName( aCompleteName.makeStringAndClear() );
114 if ( !m_pImpl->xHierarchicalNames->hasByHierarchicalName( sCompleteName ) )
115 return true;
117 catch( const Exception& )
119 DBG_UNHANDLED_EXCEPTION();
122 lcl_fillNameExistsError( _rObjectName, _out_rErrorToDisplay );
123 return false;
126 // DynamicTableOrQueryNameCheck_Impl
127 struct DynamicTableOrQueryNameCheck_Impl
129 sal_Int32 nCommandType;
130 Reference< XObjectNames > xObjectNames;
133 // DynamicTableOrQueryNameCheck
134 DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const Reference< XConnection >& _rxSdbLevelConnection, sal_Int32 _nCommandType )
135 :m_pImpl( new DynamicTableOrQueryNameCheck_Impl )
137 Reference< XConnectionTools > xConnTools( _rxSdbLevelConnection, UNO_QUERY );
138 if ( xConnTools.is() )
139 m_pImpl->xObjectNames.set( xConnTools->getObjectNames() );
140 if ( !m_pImpl->xObjectNames.is() )
141 throw IllegalArgumentException();
143 if ( ( _nCommandType != CommandType::QUERY ) && ( _nCommandType != CommandType::TABLE ) )
144 throw IllegalArgumentException();
145 m_pImpl->nCommandType = _nCommandType;
148 DynamicTableOrQueryNameCheck::~DynamicTableOrQueryNameCheck()
152 bool DynamicTableOrQueryNameCheck::isNameValid( const OUString& _rObjectName, ::dbtools::SQLExceptionInfo& _out_rErrorToDisplay ) const
156 m_pImpl->xObjectNames->checkNameForCreate( m_pImpl->nCommandType, _rObjectName );
157 return true;
159 catch( const SQLException& )
161 _out_rErrorToDisplay = ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() );
163 return false;
166 } // namespace dbaui
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */