Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / misc / defaultobjectnamecheck.cxx
blob4ea7d2024b82179d08e61cd234a1629b4cfd864b
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/sdb/CommandType.hpp>
28 #include <com/sun/star/sdb/tools/XConnectionTools.hpp>
30 #include <connectivity/dbexception.hxx>
31 #include <connectivity/dbmetadata.hxx>
33 #include <rtl/ustrbuf.hxx>
35 #include <tools/diagnose_ex.h>
36 #include <cppuhelper/exc_hlp.hxx>
38 #include <memory>
39 #include <vector>
41 namespace dbaui
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::lang::IllegalArgumentException;
46 using ::com::sun::star::container::XHierarchicalNameAccess;
47 using ::com::sun::star::sdbc::SQLException;
48 using ::com::sun::star::uno::Exception;
49 using ::com::sun::star::sdbc::XConnection;
50 using ::com::sun::star::sdb::tools::XObjectNames;
51 using ::com::sun::star::sdb::tools::XConnectionTools;
52 using ::com::sun::star::uno::UNO_QUERY;
54 using namespace dbtools;
56 namespace CommandType = ::com::sun::star::sdb::CommandType;
58 // helper
59 namespace
61 void lcl_fillNameExistsError( const OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay )
63 SQLException aError;
64 OUString sErrorMessage = ModuleRes(STR_NAMED_OBJECT_ALREADY_EXISTS).toString();
65 aError.Message = sErrorMessage.replaceAll("$#$", _rObjectName);
66 _out_rErrorToDisplay = aError;
71 // HierarchicalNameCheck_Impl
72 struct HierarchicalNameCheck_Impl
74 Reference< XHierarchicalNameAccess > xHierarchicalNames;
75 OUString sRelativeRoot;
78 // HierarchicalNameCheck
79 HierarchicalNameCheck::HierarchicalNameCheck( const Reference< XHierarchicalNameAccess >& _rxNames, const OUString& _rRelativeRoot )
80 :m_pImpl( new HierarchicalNameCheck_Impl )
82 m_pImpl->xHierarchicalNames = _rxNames;
83 m_pImpl->sRelativeRoot = _rRelativeRoot;
85 if ( !m_pImpl->xHierarchicalNames.is() )
86 throw IllegalArgumentException();
89 HierarchicalNameCheck::~HierarchicalNameCheck()
93 bool HierarchicalNameCheck::isNameValid( const OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) const
95 try
97 OUStringBuffer aCompleteName;
98 if ( !m_pImpl->sRelativeRoot.isEmpty() )
100 aCompleteName.append( m_pImpl->sRelativeRoot );
101 aCompleteName.append( "/" );
103 aCompleteName.append( _rObjectName );
105 OUString sCompleteName( aCompleteName.makeStringAndClear() );
106 if ( !m_pImpl->xHierarchicalNames->hasByHierarchicalName( sCompleteName ) )
107 return true;
109 catch( const Exception& )
111 DBG_UNHANDLED_EXCEPTION();
114 lcl_fillNameExistsError( _rObjectName, _out_rErrorToDisplay );
115 return false;
118 // DynamicTableOrQueryNameCheck_Impl
119 struct DynamicTableOrQueryNameCheck_Impl
121 sal_Int32 nCommandType;
122 Reference< XObjectNames > xObjectNames;
125 // DynamicTableOrQueryNameCheck
126 DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const Reference< XConnection >& _rxSdbLevelConnection, sal_Int32 _nCommandType )
127 :m_pImpl( new DynamicTableOrQueryNameCheck_Impl )
129 Reference< XConnectionTools > xConnTools( _rxSdbLevelConnection, UNO_QUERY );
130 if ( xConnTools.is() )
131 m_pImpl->xObjectNames.set( xConnTools->getObjectNames() );
132 if ( !m_pImpl->xObjectNames.is() )
133 throw IllegalArgumentException();
135 if ( ( _nCommandType != CommandType::QUERY ) && ( _nCommandType != CommandType::TABLE ) )
136 throw IllegalArgumentException();
137 m_pImpl->nCommandType = _nCommandType;
140 DynamicTableOrQueryNameCheck::~DynamicTableOrQueryNameCheck()
144 bool DynamicTableOrQueryNameCheck::isNameValid( const OUString& _rObjectName, ::dbtools::SQLExceptionInfo& _out_rErrorToDisplay ) const
148 m_pImpl->xObjectNames->checkNameForCreate( m_pImpl->nCommandType, _rObjectName );
149 return true;
151 catch( const SQLException& )
153 _out_rErrorToDisplay = ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() );
155 return false;
158 } // namespace dbaui
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */