1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the LibreOffice project. 4 * 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/. 8 * 9 * This file incorporates work covered by the following license notice: 10 * 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 . 18 */ 19 20 #include <core_resource.hxx> 21 #include <defaultobjectnamecheck.hxx> 22 23 #include <strings.hrc> 24 25 #include <com/sun/star/lang/IllegalArgumentException.hpp> 26 #include <com/sun/star/sdb/CommandType.hpp> 27 #include <com/sun/star/sdb/tools/XConnectionTools.hpp> 28 #include <com/sun/star/sdbc/SQLException.hpp> 29 30 #include <connectivity/dbexception.hxx> 31 #include <connectivity/dbmetadata.hxx> 32 33 #include <rtl/ustrbuf.hxx> 34 35 #include <tools/diagnose_ex.h> 36 #include <cppuhelper/exc_hlp.hxx> 37 38 #include <memory> 39 #include <string_view> 40 41 namespace dbaui 42 { 43 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; 53 54 using namespace dbtools; 55 56 namespace CommandType = ::com::sun::star::sdb::CommandType; 57 58 // helper 59 namespace 60 { lcl_fillNameExistsError(std::u16string_view _rObjectName,SQLExceptionInfo & _out_rErrorToDisplay)61 void lcl_fillNameExistsError( std::u16string_view _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) 62 { 63 SQLException aError; 64 OUString sErrorMessage = DBA_RES(STR_NAMED_OBJECT_ALREADY_EXISTS); 65 aError.Message = sErrorMessage.replaceAll("$#$", _rObjectName); 66 _out_rErrorToDisplay = aError; 67 } 68 69 } 70 71 // HierarchicalNameCheck_Impl 72 struct HierarchicalNameCheck_Impl 73 { 74 Reference< XHierarchicalNameAccess > xHierarchicalNames; 75 OUString sRelativeRoot; 76 }; 77 78 // HierarchicalNameCheck HierarchicalNameCheck(const Reference<XHierarchicalNameAccess> & _rxNames,const OUString & _rRelativeRoot)79 HierarchicalNameCheck::HierarchicalNameCheck( const Reference< XHierarchicalNameAccess >& _rxNames, const OUString& _rRelativeRoot ) 80 :m_pImpl( new HierarchicalNameCheck_Impl ) 81 { 82 m_pImpl->xHierarchicalNames = _rxNames; 83 m_pImpl->sRelativeRoot = _rRelativeRoot; 84 85 if ( !m_pImpl->xHierarchicalNames.is() ) 86 throw IllegalArgumentException(); 87 } 88 ~HierarchicalNameCheck()89 HierarchicalNameCheck::~HierarchicalNameCheck() 90 { 91 } 92 isNameValid(const OUString & _rObjectName,SQLExceptionInfo & _out_rErrorToDisplay) const93 bool HierarchicalNameCheck::isNameValid( const OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) const 94 { 95 try 96 { 97 OUStringBuffer aCompleteName; 98 if ( !m_pImpl->sRelativeRoot.isEmpty() ) 99 { 100 aCompleteName.append( m_pImpl->sRelativeRoot ); 101 aCompleteName.append( "/" ); 102 } 103 aCompleteName.append( _rObjectName ); 104 105 OUString sCompleteName( aCompleteName.makeStringAndClear() ); 106 if ( !m_pImpl->xHierarchicalNames->hasByHierarchicalName( sCompleteName ) ) 107 return true; 108 } 109 catch( const Exception& ) 110 { 111 DBG_UNHANDLED_EXCEPTION("dbaccess"); 112 } 113 114 lcl_fillNameExistsError( _rObjectName, _out_rErrorToDisplay ); 115 return false; 116 } 117 118 // DynamicTableOrQueryNameCheck_Impl 119 struct DynamicTableOrQueryNameCheck_Impl 120 { 121 sal_Int32 nCommandType; 122 Reference< XObjectNames > xObjectNames; 123 }; 124 125 // DynamicTableOrQueryNameCheck DynamicTableOrQueryNameCheck(const Reference<XConnection> & _rxSdbLevelConnection,sal_Int32 _nCommandType)126 DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const Reference< XConnection >& _rxSdbLevelConnection, sal_Int32 _nCommandType ) 127 :m_pImpl( new DynamicTableOrQueryNameCheck_Impl ) 128 { 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(); 134 135 if ( ( _nCommandType != CommandType::QUERY ) && ( _nCommandType != CommandType::TABLE ) ) 136 throw IllegalArgumentException(); 137 m_pImpl->nCommandType = _nCommandType; 138 } 139 ~DynamicTableOrQueryNameCheck()140 DynamicTableOrQueryNameCheck::~DynamicTableOrQueryNameCheck() 141 { 142 } 143 isNameValid(const OUString & _rObjectName,::dbtools::SQLExceptionInfo & _out_rErrorToDisplay) const144 bool DynamicTableOrQueryNameCheck::isNameValid( const OUString& _rObjectName, ::dbtools::SQLExceptionInfo& _out_rErrorToDisplay ) const 145 { 146 try 147 { 148 m_pImpl->xObjectNames->checkNameForCreate( m_pImpl->nCommandType, _rObjectName ); 149 return true; 150 } 151 catch( const SQLException& ) 152 { 153 _out_rErrorToDisplay = ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() ); 154 } 155 return false; 156 } 157 158 } // namespace dbaui 159 160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 161