tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / connectivity / source / commontools / sqlerror.cxx
bloba4855e81d894d52bc0955c122457de9ec91386fb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <memory>
22 #include <connectivity/sqlerror.hxx>
24 #include <com/sun/star/sdbc/SQLException.hpp>
25 #include <com/sun/star/sdb/ErrorCondition.hpp>
27 #include <cppuhelper/exc_hlp.hxx>
28 #include <unotools/resmgr.hxx>
29 #include <osl/diagnose.h>
31 #include <strings.hrc>
32 #include <strings.hxx>
33 #include <string.h>
35 namespace connectivity
39 using ::com::sun::star::uno::Reference;
40 using ::com::sun::star::uno::Any;
41 using ::com::sun::star::uno::XInterface;
42 using ::com::sun::star::sdbc::SQLException;
43 using ::com::sun::star::uno::Type;
45 class SQLError_Impl
47 public:
48 explicit SQLError_Impl();
50 // versions of the public SQLError methods which are just delegated to this impl-class
51 static const OUString& getMessagePrefix();
52 OUString getErrorMessage( const ErrorCondition _eCondition, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 ) const;
53 static ErrorCode getErrorCode( const ErrorCondition _eCondition );
54 void raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 );
55 void raiseException( const ErrorCondition _eCondition, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 );
56 void raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const Type& _rExceptionType, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 );
57 SQLException getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 );
59 private:
60 /// returns the basic error message associated with the given error condition, without any parameter replacements
61 OUString impl_getErrorMessage( ErrorCondition _eCondition ) const;
63 /// returns the SQLState associated with the given error condition
64 static OUString
65 impl_getSQLState( ErrorCondition _eCondition );
67 /// returns an SQLException describing the given error condition
68 SQLException
69 impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
70 const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 );
71 private:
72 std::locale m_aResources;
75 SQLError_Impl::SQLError_Impl()
76 : m_aResources(Translate::Create("cnr"))
80 const OUString& SQLError_Impl::getMessagePrefix()
82 static constexpr OUString s_sMessagePrefix( u"[OOoBase]"_ustr );
83 return s_sMessagePrefix;
86 namespace
89 /** substitutes a given placeholder in the given message with the given value
91 void lcl_substitutePlaceholder(OUString& _rMessage, const char* _pPlaceholder, const std::optional<OUString>& rParamValue)
93 size_t nPlaceholderLen( strlen( _pPlaceholder ) );
94 sal_Int32 nIndex = _rMessage.indexOfAsciiL( _pPlaceholder, nPlaceholderLen );
96 bool bHasPlaceholder = ( nIndex != -1 );
97 bool bWantsPlaceholder = rParamValue.has_value();
98 OSL_ENSURE( bHasPlaceholder == bWantsPlaceholder, "lcl_substitutePlaceholder: placeholder where none is expected, or no placeholder where one is needed!" );
100 if ( bHasPlaceholder && bWantsPlaceholder )
101 _rMessage = _rMessage.replaceAt( nIndex, nPlaceholderLen, *rParamValue );
104 TranslateId lcl_getResourceErrorID(const ErrorCondition _eCondition)
106 switch (_eCondition)
108 case css::sdb::ErrorCondition::ROW_SET_OPERATION_VETOED:
109 return STR_ROW_SET_OPERATION_VETOED;
110 case css::sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES:
111 return STR_PARSER_CYCLIC_SUB_QUERIES;
112 case css::sdb::ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES:
113 return STR_DB_OBJECT_NAME_WITH_SLASHES;
114 case css::sdb::ErrorCondition::DB_INVALID_SQL_NAME:
115 return STR_DB_INVALID_SQL_NAME;
116 case css::sdb::ErrorCondition::DB_QUERY_NAME_WITH_QUOTES:
117 return STR_DB_QUERY_NAME_WITH_QUOTES;
118 case css::sdb::ErrorCondition::DB_OBJECT_NAME_IS_USED:
119 return STR_DB_OBJECT_NAME_IS_USED;
120 case css::sdb::ErrorCondition::DB_NOT_CONNECTED:
121 return STR_DB_NOT_CONNECTED;
122 case css::sdb::ErrorCondition::AB_ADDRESSBOOK_NOT_FOUND:
123 return STR_AB_ADDRESSBOOK_NOT_FOUND;
124 case css::sdb::ErrorCondition::DATA_CANNOT_SELECT_UNFILTERED:
125 return STR_DATA_CANNOT_SELECT_UNFILTERED;
127 return {};
130 const OUString & lcl_getResourceState(const ErrorCondition _eCondition)
132 switch (_eCondition)
134 case css::sdb::ErrorCondition::DB_NOT_CONNECTED:
135 return STR_DB_NOT_CONNECTED_STATE;
136 case css::sdb::ErrorCondition::DATA_CANNOT_SELECT_UNFILTERED:
137 return STR_DATA_CANNOT_SELECT_UNFILTERED_STATE;
139 return EMPTY_OUSTRING;
143 OUString SQLError_Impl::getErrorMessage( const ErrorCondition _eCondition, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 ) const
145 OUString sErrorMessage( impl_getErrorMessage( _eCondition ) );
147 lcl_substitutePlaceholder( sErrorMessage, "$1$", _rParamValue1 );
148 lcl_substitutePlaceholder( sErrorMessage, "$2$", _rParamValue2 );
149 lcl_substitutePlaceholder( sErrorMessage, "$3$", _rParamValue3 );
151 return sErrorMessage;
155 ErrorCode SQLError_Impl::getErrorCode( const ErrorCondition _eCondition )
157 return 0 - ::sal::static_int_cast< ErrorCode, ErrorCondition >( _eCondition );
161 void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 )
163 raiseTypedException(
164 _eCondition,
165 _rxContext,
166 ::cppu::UnoType< SQLException >::get(),
167 _rParamValue1,
168 _rParamValue2,
169 _rParamValue3
174 void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 )
176 raiseTypedException(
177 _eCondition,
178 nullptr,
179 ::cppu::UnoType< SQLException >::get(),
180 _rParamValue1,
181 _rParamValue2,
182 _rParamValue3
186 void SQLError_Impl::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
187 const Type& _rExceptionType, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 )
189 if ( !::cppu::UnoType< SQLException >::get().isAssignableFrom( _rExceptionType ) )
190 throw std::bad_cast();
192 // default-construct an exception of the desired type
193 Any aException( nullptr, _rExceptionType );
195 // fill it
196 SQLException* pException = static_cast< SQLException* >( aException.pData );
197 *pException = impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
199 // throw it
200 ::cppu::throwException( aException );
203 SQLException SQLError_Impl::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
204 const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 )
206 return impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
209 SQLException SQLError_Impl::impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
210 const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 )
212 return SQLException(
213 getErrorMessage( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 ),
214 _rxContext,
215 impl_getSQLState( _eCondition ),
216 getErrorCode( _eCondition ),
217 Any()
221 OUString SQLError_Impl::impl_getErrorMessage( ErrorCondition _eCondition ) const
223 OUString sResMessage(Translate::get(lcl_getResourceErrorID(_eCondition), m_aResources));
224 OSL_ENSURE( !sResMessage.isEmpty(), "SQLError_Impl::impl_getErrorMessage: illegal error condition, or invalid resource!" );
225 return getMessagePrefix() + " " + sResMessage;
228 OUString SQLError_Impl::impl_getSQLState( ErrorCondition _eCondition )
230 static constexpr OUStringLiteral DEFAULT_STATE = u"S1000";
231 OUString sState = lcl_getResourceState(_eCondition);
232 if (sState.isEmpty())
233 sState = DEFAULT_STATE;
234 return sState;
237 SQLError::SQLError()
238 :m_pImpl( std::make_shared<SQLError_Impl>() )
243 SQLError::~SQLError()
248 const OUString& SQLError::getMessagePrefix()
250 return SQLError_Impl::getMessagePrefix();
254 OUString SQLError::getErrorMessage( const ErrorCondition _eCondition ) const
256 return m_pImpl->getErrorMessage( _eCondition, std::optional<OUString>(), std::optional<OUString>(), std::optional<OUString>() );
260 ErrorCode SQLError::getErrorCode( const ErrorCondition _eCondition )
262 return SQLError_Impl::getErrorCode( _eCondition );
266 void SQLError::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 ) const
268 m_pImpl->raiseException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
272 void SQLError::raiseException( const ErrorCondition _eCondition ) const
274 m_pImpl->raiseException( _eCondition, std::optional<OUString>(), std::optional<OUString>(), std::optional<OUString>() );
278 void SQLError::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
279 const Type& _rExceptionType ) const
281 m_pImpl->raiseTypedException( _eCondition, _rxContext, _rExceptionType, std::optional<OUString>(), std::optional<OUString>(), std::optional<OUString>() );
285 SQLException SQLError::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
286 const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 ) const
288 return m_pImpl->getSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
292 } // namespace connectivity
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */