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 "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>
41 #include <boost/shared_ptr.hpp>
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
;
69 void lcl_fillNameExistsError( const OUString
& _rObjectName
, SQLExceptionInfo
& _out_rErrorToDisplay
)
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
) )
117 catch( const Exception
& )
119 DBG_UNHANDLED_EXCEPTION();
122 lcl_fillNameExistsError( _rObjectName
, _out_rErrorToDisplay
);
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
);
159 catch( const SQLException
& )
161 _out_rErrorToDisplay
= ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() );
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */