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 : #include "defaultobjectnamecheck.hxx"
21 :
22 : #include "dbu_misc.hrc"
23 :
24 : #include "moduledbu.hxx"
25 :
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>
31 :
32 : #include <connectivity/dbexception.hxx>
33 : #include <connectivity/dbmetadata.hxx>
34 :
35 : #include <rtl/ustrbuf.hxx>
36 :
37 : #include <tools/diagnose_ex.h>
38 : #include <cppuhelper/exc_hlp.hxx>
39 :
40 : #include <vector>
41 : #include <boost/shared_ptr.hpp>
42 :
43 : namespace dbaui
44 : {
45 :
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;
61 :
62 : using namespace dbtools;
63 :
64 : namespace CommandType = ::com::sun::star::sdb::CommandType;
65 :
66 : // helper
67 : namespace
68 : {
69 0 : void lcl_fillNameExistsError( const OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay )
70 : {
71 0 : SQLException aError;
72 0 : OUString sErrorMessage = ModuleRes(STR_NAMED_OBJECT_ALREADY_EXISTS).toString();
73 0 : aError.Message = sErrorMessage.replaceAll("$#$", _rObjectName);
74 0 : _out_rErrorToDisplay = aError;
75 0 : }
76 :
77 : }
78 :
79 : // HierarchicalNameCheck_Impl
80 0 : struct HierarchicalNameCheck_Impl
81 : {
82 : Reference< XHierarchicalNameAccess > xHierarchicalNames;
83 : OUString sRelativeRoot;
84 : };
85 :
86 : // HierarchicalNameCheck
87 0 : HierarchicalNameCheck::HierarchicalNameCheck( const Reference< XHierarchicalNameAccess >& _rxNames, const OUString& _rRelativeRoot )
88 0 : :m_pImpl( new HierarchicalNameCheck_Impl )
89 : {
90 0 : m_pImpl->xHierarchicalNames = _rxNames;
91 0 : m_pImpl->sRelativeRoot = _rRelativeRoot;
92 :
93 0 : if ( !m_pImpl->xHierarchicalNames.is() )
94 0 : throw IllegalArgumentException();
95 0 : }
96 :
97 0 : HierarchicalNameCheck::~HierarchicalNameCheck()
98 : {
99 0 : }
100 :
101 0 : bool HierarchicalNameCheck::isNameValid( const OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) const
102 : {
103 : try
104 : {
105 0 : OUStringBuffer aCompleteName;
106 0 : if ( !m_pImpl->sRelativeRoot.isEmpty() )
107 : {
108 0 : aCompleteName.append( m_pImpl->sRelativeRoot );
109 0 : aCompleteName.appendAscii( "/" );
110 : }
111 0 : aCompleteName.append( _rObjectName );
112 :
113 0 : OUString sCompleteName( aCompleteName.makeStringAndClear() );
114 0 : if ( !m_pImpl->xHierarchicalNames->hasByHierarchicalName( sCompleteName ) )
115 0 : return true;
116 : }
117 0 : catch( const Exception& )
118 : {
119 : DBG_UNHANDLED_EXCEPTION();
120 : }
121 :
122 0 : lcl_fillNameExistsError( _rObjectName, _out_rErrorToDisplay );
123 0 : return false;
124 : }
125 :
126 : // DynamicTableOrQueryNameCheck_Impl
127 0 : struct DynamicTableOrQueryNameCheck_Impl
128 : {
129 : sal_Int32 nCommandType;
130 : Reference< XObjectNames > xObjectNames;
131 : };
132 :
133 : // DynamicTableOrQueryNameCheck
134 0 : DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const Reference< XConnection >& _rxSdbLevelConnection, sal_Int32 _nCommandType )
135 0 : :m_pImpl( new DynamicTableOrQueryNameCheck_Impl )
136 : {
137 0 : Reference< XConnectionTools > xConnTools( _rxSdbLevelConnection, UNO_QUERY );
138 0 : if ( xConnTools.is() )
139 0 : m_pImpl->xObjectNames.set( xConnTools->getObjectNames() );
140 0 : if ( !m_pImpl->xObjectNames.is() )
141 0 : throw IllegalArgumentException();
142 :
143 0 : if ( ( _nCommandType != CommandType::QUERY ) && ( _nCommandType != CommandType::TABLE ) )
144 0 : throw IllegalArgumentException();
145 0 : m_pImpl->nCommandType = _nCommandType;
146 0 : }
147 :
148 0 : DynamicTableOrQueryNameCheck::~DynamicTableOrQueryNameCheck()
149 : {
150 0 : }
151 :
152 0 : bool DynamicTableOrQueryNameCheck::isNameValid( const OUString& _rObjectName, ::dbtools::SQLExceptionInfo& _out_rErrorToDisplay ) const
153 : {
154 : try
155 : {
156 0 : m_pImpl->xObjectNames->checkNameForCreate( m_pImpl->nCommandType, _rObjectName );
157 0 : return true;
158 : }
159 0 : catch( const SQLException& )
160 : {
161 0 : _out_rErrorToDisplay = ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() );
162 : }
163 0 : return false;
164 : }
165 :
166 : } // namespace dbaui
167 :
168 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|