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 <connectivity/warningscontainer.hxx>
22 #include <connectivity/dbexception.hxx>
23 
24 #include <com/sun/star/sdb/SQLContext.hpp>
25 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
26 
27 #include <o3tl/any.hxx>
28 #include <osl/diagnose.h>
29 
30 
31 namespace dbtools
32 {
33 
34 
35     using namespace ::com::sun::star::uno;
36     using namespace ::com::sun::star::sdbc;
37     using namespace ::com::sun::star::sdb;
38 
lcl_concatWarnings(Any & _rChainLeft,const Any & _rChainRight)39     static void lcl_concatWarnings( Any& _rChainLeft, const Any& _rChainRight )
40     {
41         if ( !_rChainLeft.hasValue() )
42             _rChainLeft = _rChainRight;
43         else
44         {
45             // to travel the chain by reference (and not by value), we need the getValue...
46             // looks like a hack, but the meaning of getValue is documented, and it's the only chance for reference-traveling...
47 
48             OSL_ENSURE( SQLExceptionInfo( _rChainLeft ).isValid(),
49                 "lcl_concatWarnings: invalid warnings chain (this will crash)!" );
50 
51             const SQLException* pChainTravel = o3tl::doAccess<SQLException>( _rChainLeft );
52             SQLExceptionIteratorHelper aReferenceIterHelper( *pChainTravel );
53             while ( aReferenceIterHelper.hasMoreElements() )
54                 pChainTravel = aReferenceIterHelper.next();
55 
56             // reached the end of the chain, and pChainTravel points to the last element
57             const_cast< SQLException* >( pChainTravel )->NextException = _rChainRight;
58         }
59     }
60 
61 
appendWarning(const SQLException & _rWarning)62     void WarningsContainer::appendWarning(const SQLException& _rWarning)
63     {
64         lcl_concatWarnings( m_aOwnWarnings, makeAny( _rWarning ) );
65     }
66 
67 
appendWarning(const SQLContext & _rContext)68     void WarningsContainer::appendWarning( const SQLContext& _rContext )
69     {
70         lcl_concatWarnings( m_aOwnWarnings, makeAny( _rContext ));
71     }
72 
73 
appendWarning(const SQLWarning & _rWarning)74     void WarningsContainer::appendWarning(const SQLWarning& _rWarning)
75     {
76         lcl_concatWarnings( m_aOwnWarnings, makeAny( _rWarning ) );
77     }
78 
79 
getWarnings() const80     Any WarningsContainer::getWarnings(  ) const
81     {
82         Any aAllWarnings;
83         if ( m_xExternalWarnings.is() )
84             aAllWarnings = m_xExternalWarnings->getWarnings();
85 
86         if ( m_aOwnWarnings.hasValue() )
87             lcl_concatWarnings( aAllWarnings, m_aOwnWarnings );
88 
89         return aAllWarnings;
90     }
91 
92 
clearWarnings()93     void WarningsContainer::clearWarnings(  )
94     {
95         if ( m_xExternalWarnings.is() )
96             m_xExternalWarnings->clearWarnings();
97         m_aOwnWarnings.clear();
98     }
99 
100 
appendWarning(const OUString & _rWarning,const char * _pAsciiSQLState,const Reference<XInterface> & _rxContext)101     void WarningsContainer::appendWarning( const OUString& _rWarning, const char* _pAsciiSQLState, const Reference< XInterface >& _rxContext )
102     {
103         appendWarning( SQLWarning( _rWarning, _rxContext, OUString::createFromAscii( _pAsciiSQLState ), 0, Any() ) );
104     }
105 
106 
107 }   // namespace dbtools
108 
109 
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
111