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