Branch data Line data Source code
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 : :
21 : : #include "defaultobjectnamecheck.hxx"
22 : :
23 : : #include "dbu_misc.hrc"
24 : :
25 : : #include "moduledbu.hxx"
26 : :
27 : : #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 : : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
29 : : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
30 : : #include <com/sun/star/sdb/CommandType.hpp>
31 : : #include <com/sun/star/sdb/tools/XConnectionTools.hpp>
32 : :
33 : : #include <connectivity/dbexception.hxx>
34 : : #include <connectivity/dbmetadata.hxx>
35 : :
36 : : #include <rtl/ustrbuf.hxx>
37 : :
38 : : #include <tools/diagnose_ex.h>
39 : : #include <tools/string.hxx>
40 : : #include <cppuhelper/exc_hlp.hxx>
41 : :
42 : : #include <vector>
43 : : #include <boost/shared_ptr.hpp>
44 : :
45 : : //........................................................................
46 : : namespace dbaui
47 : : {
48 : : //........................................................................
49 : :
50 : : /** === begin UNO using === **/
51 : : using ::com::sun::star::uno::Reference;
52 : : using ::com::sun::star::container::XNameAccess;
53 : : using ::com::sun::star::lang::IllegalArgumentException;
54 : : using ::com::sun::star::container::XHierarchicalNameAccess;
55 : : using ::com::sun::star::sdbc::SQLException;
56 : : using ::com::sun::star::uno::Exception;
57 : : using ::com::sun::star::sdbc::XConnection;
58 : : using ::com::sun::star::sdbcx::XTablesSupplier;
59 : : using ::com::sun::star::sdb::XQueriesSupplier;
60 : : using ::com::sun::star::uno::UNO_QUERY_THROW;
61 : : using ::com::sun::star::uno::makeAny;
62 : : using ::com::sun::star::uno::Any;
63 : : using ::com::sun::star::sdb::tools::XObjectNames;
64 : : using ::com::sun::star::sdb::tools::XConnectionTools;
65 : : using ::com::sun::star::uno::UNO_QUERY;
66 : : /** === end UNO using === **/
67 : :
68 : : using namespace dbtools;
69 : :
70 : : namespace CommandType = ::com::sun::star::sdb::CommandType;
71 : :
72 : : //====================================================================
73 : : //= helper
74 : : //====================================================================
75 : : namespace
76 : : {
77 : 0 : void lcl_fillNameExistsError( const ::rtl::OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay )
78 : : {
79 [ # # ][ # # ]: 0 : String sErrorMessage = String( ModuleRes( STR_NAMED_OBJECT_ALREADY_EXISTS ) );
80 [ # # ][ # # ]: 0 : sErrorMessage.SearchAndReplaceAllAscii( "$#$", _rObjectName );
[ # # ]
81 [ # # ]: 0 : SQLException aError;
82 [ # # ]: 0 : aError.Message = sErrorMessage;
83 [ # # ][ # # ]: 0 : _out_rErrorToDisplay = aError;
[ # # ]
84 : 0 : }
85 : :
86 : : }
87 : :
88 : : //====================================================================
89 : : //= HierarchicalNameCheck_Impl
90 : : //====================================================================
91 : 0 : struct HierarchicalNameCheck_Impl
92 : : {
93 : : Reference< XHierarchicalNameAccess > xHierarchicalNames;
94 : : ::rtl::OUString sRelativeRoot;
95 : : };
96 : :
97 : : //====================================================================
98 : : //= HierarchicalNameCheck
99 : : //====================================================================
100 : : //--------------------------------------------------------------------
101 : 0 : HierarchicalNameCheck::HierarchicalNameCheck( const Reference< XHierarchicalNameAccess >& _rxNames, const ::rtl::OUString& _rRelativeRoot )
102 [ # # ][ # # ]: 0 : :m_pImpl( new HierarchicalNameCheck_Impl )
103 : : {
104 [ # # ]: 0 : m_pImpl->xHierarchicalNames = _rxNames;
105 : 0 : m_pImpl->sRelativeRoot = _rRelativeRoot;
106 : :
107 [ # # ]: 0 : if ( !m_pImpl->xHierarchicalNames.is() )
108 [ # # ]: 0 : throw IllegalArgumentException();
109 : 0 : }
110 : :
111 : : //--------------------------------------------------------------------
112 : 0 : HierarchicalNameCheck::~HierarchicalNameCheck()
113 : : {
114 [ # # ]: 0 : }
115 : :
116 : : //--------------------------------------------------------------------
117 : 0 : bool HierarchicalNameCheck::isNameValid( const ::rtl::OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) const
118 : : {
119 : : try
120 : : {
121 : 0 : ::rtl::OUStringBuffer aCompleteName;
122 [ # # ]: 0 : if ( !m_pImpl->sRelativeRoot.isEmpty() )
123 : : {
124 [ # # ]: 0 : aCompleteName.append( m_pImpl->sRelativeRoot );
125 [ # # ]: 0 : aCompleteName.appendAscii( "/" );
126 : : }
127 [ # # ]: 0 : aCompleteName.append( _rObjectName );
128 : :
129 [ # # ]: 0 : ::rtl::OUString sCompleteName( aCompleteName.makeStringAndClear() );
130 [ # # ][ # # ]: 0 : if ( !m_pImpl->xHierarchicalNames->hasByHierarchicalName( sCompleteName ) )
[ # # ]
131 [ # # ][ # # ]: 0 : return true;
[ # # ]
132 : : }
133 : 0 : catch( const Exception& )
134 : : {
135 : : DBG_UNHANDLED_EXCEPTION();
136 : : }
137 : :
138 : 0 : lcl_fillNameExistsError( _rObjectName, _out_rErrorToDisplay );
139 : 0 : return false;
140 : : }
141 : :
142 : : //====================================================================
143 : : //= DynamicTableOrQueryNameCheck_Impl
144 : : //====================================================================
145 : 0 : struct DynamicTableOrQueryNameCheck_Impl
146 : : {
147 : : sal_Int32 nCommandType;
148 : : Reference< XObjectNames > xObjectNames;
149 : : };
150 : :
151 : : //====================================================================
152 : : //= DynamicTableOrQueryNameCheck
153 : : //====================================================================
154 : : //--------------------------------------------------------------------
155 : 0 : DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const Reference< XConnection >& _rxSdbLevelConnection, sal_Int32 _nCommandType )
156 [ # # ][ # # ]: 0 : :m_pImpl( new DynamicTableOrQueryNameCheck_Impl )
157 : : {
158 [ # # ]: 0 : Reference< XConnectionTools > xConnTools( _rxSdbLevelConnection, UNO_QUERY );
159 [ # # ]: 0 : if ( xConnTools.is() )
160 [ # # ][ # # ]: 0 : m_pImpl->xObjectNames.set( xConnTools->getObjectNames() );
[ # # ]
161 [ # # ]: 0 : if ( !m_pImpl->xObjectNames.is() )
162 [ # # ]: 0 : throw IllegalArgumentException();
163 : :
164 [ # # ][ # # ]: 0 : if ( ( _nCommandType != CommandType::QUERY ) && ( _nCommandType != CommandType::TABLE ) )
165 [ # # ]: 0 : throw IllegalArgumentException();
166 : 0 : m_pImpl->nCommandType = _nCommandType;
167 : 0 : }
168 : :
169 : : //--------------------------------------------------------------------
170 : 0 : DynamicTableOrQueryNameCheck::~DynamicTableOrQueryNameCheck()
171 : : {
172 [ # # ]: 0 : }
173 : :
174 : : //--------------------------------------------------------------------
175 : 0 : bool DynamicTableOrQueryNameCheck::isNameValid( const ::rtl::OUString& _rObjectName, ::dbtools::SQLExceptionInfo& _out_rErrorToDisplay ) const
176 : : {
177 : : try
178 : : {
179 [ # # ][ # # ]: 0 : m_pImpl->xObjectNames->checkNameForCreate( m_pImpl->nCommandType, _rObjectName );
180 : 0 : return true;
181 : : }
182 [ # # ]: 0 : catch( const SQLException& )
183 : : {
184 [ # # # # : 0 : _out_rErrorToDisplay = ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() );
# # # # ]
185 : : }
186 : 0 : return false;
187 : : }
188 : :
189 : : //........................................................................
190 : : } // namespace dbaui
191 : : //........................................................................
192 : :
193 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|