1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 
27 #include "connectivity/warningscontainer.hxx"
28 #include "connectivity/dbexception.hxx"
29 
30 #include <osl/diagnose.h>
31 
32 //........................................................................
33 namespace dbtools
34 {
35 //........................................................................
36 
37     using namespace ::com::sun::star::uno;
38     using namespace ::com::sun::star::sdbc;
39     using namespace ::com::sun::star::sdb;
40 
41     //====================================================================
42     //= WarningsContainer
43     //====================================================================
44     //--------------------------------------------------------------------
lcl_concatWarnings(Any & _rChainLeft,const Any & _rChainRight)45     static void lcl_concatWarnings( Any& _rChainLeft, const Any& _rChainRight )
46     {
47 	    if ( !_rChainLeft.hasValue() )
48 		    _rChainLeft = _rChainRight;
49 	    else
50 	    {
51 		    // to travel the chain by reference (and not by value), we need the getValue ...
52 		    // looks like a hack, but the meaning of getValue is documented, and it's the only chance for reference-traveling ....
53 
54 		    OSL_ENSURE( SQLExceptionInfo( _rChainLeft ).isValid(),
55                 "lcl_concatWarnings: invalid warnings chain (this will crash)!" );
56 
57 		    const SQLException* pChainTravel = static_cast< const SQLException* >( _rChainLeft.getValue() );
58 		    SQLExceptionIteratorHelper aReferenceIterHelper( *pChainTravel );
59 		    while ( aReferenceIterHelper.hasMoreElements() )
60 			    pChainTravel = aReferenceIterHelper.next();
61 
62 		    // reached the end of the chain, and pChainTravel points to the last element
63 		    const_cast< SQLException* >( pChainTravel )->NextException = _rChainRight;
64 	    }
65     }
66 
67     //--------------------------------------------------------------------
~WarningsContainer()68     WarningsContainer::~WarningsContainer()
69     {
70     }
71 
72     //--------------------------------------------------------------------
appendWarning(const SQLException & _rWarning)73     void WarningsContainer::appendWarning(const SQLException& _rWarning)
74     {
75 	    lcl_concatWarnings( m_aOwnWarnings, makeAny( _rWarning ) );
76     }
77 
78     //--------------------------------------------------------------------
appendWarning(const SQLContext & _rContext)79     void WarningsContainer::appendWarning( const SQLContext& _rContext )
80     {
81 	    lcl_concatWarnings( m_aOwnWarnings, makeAny( _rContext ));
82     }
83 
84     //--------------------------------------------------------------------
appendWarning(const SQLWarning & _rWarning)85     void WarningsContainer::appendWarning(const SQLWarning& _rWarning)
86     {
87 	    lcl_concatWarnings( m_aOwnWarnings, makeAny( _rWarning ) );
88     }
89 
90     //--------------------------------------------------------------------
getWarnings() const91     Any SAL_CALL WarningsContainer::getWarnings(  ) const
92     {
93 	    Any aAllWarnings;
94         if ( m_xExternalWarnings.is() )
95             aAllWarnings = m_xExternalWarnings->getWarnings();
96 
97         if ( m_aOwnWarnings.hasValue() )
98 		    lcl_concatWarnings( aAllWarnings, m_aOwnWarnings );
99 
100         return aAllWarnings;
101     }
102 
103     //--------------------------------------------------------------------
clearWarnings()104     void SAL_CALL WarningsContainer::clearWarnings(  )
105     {
106         if ( m_xExternalWarnings.is() )
107             m_xExternalWarnings->clearWarnings();
108         m_aOwnWarnings.clear();
109     }
110 
111     //--------------------------------------------------------------------
appendWarning(const::rtl::OUString & _rWarning,const sal_Char * _pAsciiSQLState,const Reference<XInterface> & _rxContext)112     void WarningsContainer::appendWarning( const ::rtl::OUString& _rWarning, const sal_Char* _pAsciiSQLState, const Reference< XInterface >& _rxContext )
113     {
114         appendWarning( SQLWarning( _rWarning, _rxContext, ::rtl::OUString::createFromAscii( _pAsciiSQLState ), 0, Any() ) );
115     }
116 
117 //........................................................................
118 }   // namespace dbtools
119 //........................................................................
120