1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <core_resource.hxx>
21 #include <defaultobjectnamecheck.hxx>
23 #include <strings.hrc>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 #include <com/sun/star/sdb/CommandType.hpp>
27 #include <com/sun/star/sdbc/SQLException.hpp>
29 #include <connectivity/dbexception.hxx>
30 #include <connectivity/dbmetadata.hxx>
32 #include <rtl/ustrbuf.hxx>
34 #include <comphelper/diagnose_ex.hxx>
35 #include <cppuhelper/exc_hlp.hxx>
38 #include <string_view>
43 using ::com::sun::star::uno::Reference
;
44 using ::com::sun::star::lang::IllegalArgumentException
;
45 using ::com::sun::star::container::XHierarchicalNameAccess
;
46 using ::com::sun::star::sdbc::SQLException
;
47 using ::com::sun::star::uno::Exception
;
48 using ::com::sun::star::sdbc::XConnection
;
49 using ::com::sun::star::sdb::tools::XConnectionTools
;
50 using ::com::sun::star::uno::UNO_QUERY
;
52 using namespace dbtools
;
54 namespace CommandType
= ::com::sun::star::sdb::CommandType
;
59 void lcl_fillNameExistsError( std::u16string_view _rObjectName
, SQLExceptionInfo
& _out_rErrorToDisplay
)
61 OUString sErrorMessage
= DBA_RES(STR_NAMED_OBJECT_ALREADY_EXISTS
);
62 SQLException
aError(sErrorMessage
.replaceAll("$#$", _rObjectName
), {}, {}, 0, {});
63 _out_rErrorToDisplay
= aError
;
68 // HierarchicalNameCheck
69 HierarchicalNameCheck::HierarchicalNameCheck( const Reference
< XHierarchicalNameAccess
>& _rxNames
, const OUString
& _rRelativeRoot
)
71 mxHierarchicalNames
= _rxNames
;
72 msRelativeRoot
= _rRelativeRoot
;
74 if ( !mxHierarchicalNames
.is() )
75 throw IllegalArgumentException();
78 HierarchicalNameCheck::~HierarchicalNameCheck()
82 bool HierarchicalNameCheck::isNameValid( const OUString
& _rObjectName
, SQLExceptionInfo
& _out_rErrorToDisplay
) const
86 OUStringBuffer aCompleteName
;
87 if ( !msRelativeRoot
.isEmpty() )
89 aCompleteName
.append( msRelativeRoot
+ "/" );
91 aCompleteName
.append( _rObjectName
);
93 OUString
sCompleteName( aCompleteName
.makeStringAndClear() );
94 if ( !mxHierarchicalNames
->hasByHierarchicalName( sCompleteName
) )
97 catch( const Exception
& )
99 DBG_UNHANDLED_EXCEPTION("dbaccess");
102 lcl_fillNameExistsError( _rObjectName
, _out_rErrorToDisplay
);
106 // DynamicTableOrQueryNameCheck
107 DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const Reference
< XConnection
>& _rxSdbLevelConnection
, sal_Int32 _nCommandType
)
109 Reference
< XConnectionTools
> xConnTools( _rxSdbLevelConnection
, UNO_QUERY
);
110 if ( xConnTools
.is() )
111 mxObjectNames
.set( xConnTools
->getObjectNames() );
112 if ( !mxObjectNames
.is() )
113 throw IllegalArgumentException();
115 if ( ( _nCommandType
!= CommandType::QUERY
) && ( _nCommandType
!= CommandType::TABLE
) )
116 throw IllegalArgumentException();
117 mnCommandType
= _nCommandType
;
120 DynamicTableOrQueryNameCheck::~DynamicTableOrQueryNameCheck()
124 bool DynamicTableOrQueryNameCheck::isNameValid( const OUString
& _rObjectName
, ::dbtools::SQLExceptionInfo
& _out_rErrorToDisplay
) const
128 mxObjectNames
->checkNameForCreate( mnCommandType
, _rObjectName
);
131 catch( const SQLException
& )
133 _out_rErrorToDisplay
= ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() );
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */