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 "resultcolumn.hxx"
21 #include <com/sun/star/lang/DisposedException.hpp>
22 #include <com/sun/star/sdbc/SQLException.hpp>
23 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
24 #include <com/sun/star/sdbc/DataType.hpp>
25 #include <com/sun/star/sdbc/ColumnValue.hpp>
26 #include <com/sun/star/sdbc/XRow.hpp>
27 #include <cppuhelper/typeprovider.hxx>
28 #include <tools/debug.hxx>
29 #include <tools/diagnose_ex.h>
30 #include <stringconstants.hxx>
31 #include <apitools.hxx>
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 #include <cppuhelper/exc_hlp.hxx>
34 #include <osl/thread.h>
35 
36 using namespace ::com::sun::star::sdbc;
37 using namespace ::com::sun::star::beans;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::container;
41 using namespace ::osl;
42 using namespace ::comphelper;
43 using namespace ::cppu;
44 using namespace dbaccess;
45 
46 
OResultColumn(const Reference<XResultSetMetaData> & _xMetaData,sal_Int32 _nPos,const Reference<XDatabaseMetaData> & _rxDBMeta)47 OResultColumn::OResultColumn( const Reference < XResultSetMetaData >& _xMetaData, sal_Int32 _nPos,
48         const Reference< XDatabaseMetaData >& _rxDBMeta )
49     :OColumn( true )
50     ,m_xMetaData( _xMetaData )
51     ,m_xDBMetaData( _rxDBMeta )
52     ,m_nPos( _nPos )
53 {
54 }
55 
impl_determineIsRowVersion_nothrow()56 void OResultColumn::impl_determineIsRowVersion_nothrow()
57 {
58     if ( m_aIsRowVersion.hasValue() )
59         return;
60     m_aIsRowVersion <<= false;
61 
62     OSL_ENSURE( m_xDBMetaData.is(), "OResultColumn::impl_determineIsRowVersion_nothrow: no DBMetaData!" );
63     if ( !m_xDBMetaData.is() )
64         return;
65 
66     try
67     {
68         OUString sCatalog, sSchema, sTable, sColumnName;
69         getPropertyValue( PROPERTY_CATALOGNAME ) >>= sCatalog;
70         getPropertyValue( PROPERTY_SCHEMANAME ) >>= sSchema;
71         getPropertyValue( PROPERTY_TABLENAME ) >>= sTable;
72         getPropertyValue( PROPERTY_NAME ) >>= sColumnName;
73 
74         try
75         {
76             Reference< XResultSet > xVersionColumns = m_xDBMetaData->getVersionColumns(
77                 makeAny( sCatalog ), sSchema, sTable );
78             if ( xVersionColumns.is() ) // allowed to be NULL
79             {
80                 Reference< XRow > xResultRow( xVersionColumns, UNO_QUERY_THROW );
81                 while ( xVersionColumns->next() )
82                 {
83                     if ( xResultRow->getString( 2 ) == sColumnName )
84                     {
85                         m_aIsRowVersion <<= true;
86                         break;
87                     }
88                 }
89             }
90         }
91         catch(const SQLException&)
92         {
93         }
94     }
95     catch( const Exception& )
96     {
97         DBG_UNHANDLED_EXCEPTION("dbaccess");
98     }
99 }
100 
~OResultColumn()101 OResultColumn::~OResultColumn()
102 {
103 }
104 
105 // css::lang::XTypeProvider
getImplementationId()106 Sequence< sal_Int8 > OResultColumn::getImplementationId()
107 {
108     return css::uno::Sequence<sal_Int8>();
109 }
110 
111 // XServiceInfo
getImplementationName()112 OUString OResultColumn::getImplementationName(  )
113 {
114     return "com.sun.star.sdb.OResultColumn";
115 }
116 
getSupportedServiceNames()117 Sequence< OUString > OResultColumn::getSupportedServiceNames(  )
118 {
119     return { SERVICE_SDBCX_COLUMN, SERVICE_SDB_RESULTCOLUMN };
120 }
121 
122 // OComponentHelper
disposing()123 void OResultColumn::disposing()
124 {
125     OColumn::disposing();
126 
127     MutexGuard aGuard(m_aMutex);
128     m_xMetaData = nullptr;
129 }
130 
131 // comphelper::OPropertyArrayUsageHelper
createArrayHelper() const132 ::cppu::IPropertyArrayHelper* OResultColumn::createArrayHelper( ) const
133 {
134     BEGIN_PROPERTY_HELPER(21)
135         DECL_PROP1(CATALOGNAME,             OUString,    READONLY);
136         DECL_PROP1(DISPLAYSIZE,             sal_Int32,          READONLY);
137         DECL_PROP1_BOOL(ISAUTOINCREMENT,                        READONLY);
138         DECL_PROP1_BOOL(ISCASESENSITIVE,                        READONLY);
139         DECL_PROP1_BOOL(ISCURRENCY,                             READONLY);
140         DECL_PROP1_BOOL(ISDEFINITELYWRITABLE,                   READONLY);
141         DECL_PROP1(ISNULLABLE,              sal_Int32,          READONLY);
142         DECL_PROP1_BOOL(ISREADONLY,                             READONLY);
143         DECL_PROP1_BOOL(ISROWVERSION,                           READONLY);
144         DECL_PROP1_BOOL(ISSEARCHABLE,                           READONLY);
145         DECL_PROP1_BOOL(ISSIGNED,                               READONLY);
146         DECL_PROP1_BOOL(ISWRITABLE,                             READONLY);
147         DECL_PROP1(LABEL,                   OUString,    READONLY);
148         DECL_PROP1(NAME,                    OUString,    READONLY);
149         DECL_PROP1(PRECISION,               sal_Int32,          READONLY);
150         DECL_PROP1(SCALE,                   sal_Int32,          READONLY);
151         DECL_PROP1(SCHEMANAME,              OUString,    READONLY);
152         DECL_PROP1(SERVICENAME,             OUString,    READONLY);
153         DECL_PROP1(TABLENAME,               OUString,    READONLY);
154         DECL_PROP1(TYPE,                    sal_Int32,          READONLY);
155         DECL_PROP1(TYPENAME,                OUString,    READONLY);
156     END_PROPERTY_HELPER();
157 }
158 
159 // cppu::OPropertySetHelper
getInfoHelper()160 ::cppu::IPropertyArrayHelper& OResultColumn::getInfoHelper()
161 {
162     return *static_cast< ::comphelper::OPropertyArrayUsageHelper< OResultColumn >* >(this)->getArrayHelper();
163 }
164 
165 namespace
166 {
167     template< typename T >
obtain(Any & _out_rValue,::boost::optional<T> & _rCache,const sal_Int32 _nPos,const Reference<XResultSetMetaData> & _rxResultMeta,T (SAL_CALL XResultSetMetaData::* Getter)(sal_Int32))168     void obtain( Any& _out_rValue, ::boost::optional< T > & _rCache, const sal_Int32 _nPos, const Reference < XResultSetMetaData >& _rxResultMeta, T (SAL_CALL XResultSetMetaData::*Getter)( sal_Int32 ) )
169     {
170         if ( !_rCache )
171             _rCache = (_rxResultMeta.get()->*Getter)(_nPos);
172         _out_rValue <<= *_rCache;
173     }
174 }
175 
getFastPropertyValue(Any & rValue,sal_Int32 nHandle) const176 void OResultColumn::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
177 {
178     try
179     {
180         if ( OColumn::isRegisteredProperty( nHandle ) )
181         {
182             OColumn::getFastPropertyValue( rValue, nHandle );
183         }
184         else
185         {
186             switch (nHandle)
187             {
188                 case PROPERTY_ID_ISROWVERSION:
189                     const_cast< OResultColumn* >( this )->impl_determineIsRowVersion_nothrow();
190                     rValue = m_aIsRowVersion;
191                     break;
192                 case PROPERTY_ID_TABLENAME:
193                     rValue <<= m_xMetaData->getTableName(m_nPos);
194                     break;
195                 case PROPERTY_ID_SCHEMANAME:
196                     rValue <<= m_xMetaData->getSchemaName(m_nPos);
197                     break;
198                 case PROPERTY_ID_CATALOGNAME:
199                     rValue <<= m_xMetaData->getCatalogName(m_nPos);
200                     break;
201                 case PROPERTY_ID_ISSIGNED:
202                     obtain( rValue, m_isSigned, m_nPos, m_xMetaData, &XResultSetMetaData::isSigned );
203                     break;
204                 case PROPERTY_ID_ISCURRENCY:
205                     obtain( rValue, m_isCurrency, m_nPos, m_xMetaData, &XResultSetMetaData::isCurrency );
206                     break;
207                 case PROPERTY_ID_ISSEARCHABLE:
208                     obtain( rValue, m_bSearchable, m_nPos, m_xMetaData, &XResultSetMetaData::isSearchable );
209                     break;
210                 case PROPERTY_ID_ISCASESENSITIVE:
211                     obtain( rValue, m_isCaseSensitive, m_nPos, m_xMetaData, &XResultSetMetaData::isCaseSensitive );
212                     break;
213                 case PROPERTY_ID_ISREADONLY:
214                     obtain( rValue, m_isReadOnly, m_nPos, m_xMetaData, &XResultSetMetaData::isReadOnly );
215                     break;
216                 case PROPERTY_ID_ISWRITABLE:
217                     obtain( rValue, m_isWritable, m_nPos, m_xMetaData, &XResultSetMetaData::isWritable );
218                     break;
219                 case PROPERTY_ID_ISDEFINITELYWRITABLE:
220                     obtain( rValue, m_isDefinitelyWritable, m_nPos, m_xMetaData, &XResultSetMetaData::isDefinitelyWritable );
221                     break;
222                 case PROPERTY_ID_ISAUTOINCREMENT:
223                     obtain( rValue, m_isAutoIncrement, m_nPos, m_xMetaData, &XResultSetMetaData::isAutoIncrement );
224                     break;
225                 case PROPERTY_ID_SERVICENAME:
226                     rValue <<= m_xMetaData->getColumnServiceName(m_nPos);
227                     break;
228                 case PROPERTY_ID_LABEL:
229                     obtain( rValue, m_sColumnLabel, m_nPos, m_xMetaData, &XResultSetMetaData::getColumnLabel );
230                     break;
231                 case PROPERTY_ID_DISPLAYSIZE:
232                     obtain( rValue, m_nColumnDisplaySize, m_nPos, m_xMetaData, &XResultSetMetaData::getColumnDisplaySize );
233                     break;
234                 case PROPERTY_ID_TYPE:
235                     obtain( rValue, m_nColumnType, m_nPos, m_xMetaData, &XResultSetMetaData::getColumnType );
236                     break;
237                 case PROPERTY_ID_PRECISION:
238                     obtain( rValue, m_nPrecision, m_nPos, m_xMetaData, &XResultSetMetaData::getPrecision );
239                     break;
240                 case PROPERTY_ID_SCALE:
241                     obtain( rValue, m_nScale, m_nPos, m_xMetaData, &XResultSetMetaData::getScale );
242                     break;
243                 case PROPERTY_ID_ISNULLABLE:
244                     obtain( rValue, m_isNullable, m_nPos, m_xMetaData, &XResultSetMetaData::isNullable );
245                     break;
246                 case PROPERTY_ID_TYPENAME:
247                     rValue <<= m_xMetaData->getColumnTypeName(m_nPos);
248                     break;
249                 default:
250                     OSL_FAIL( "OResultColumn::getFastPropertyValue: unknown property handle!" );
251                     break;
252             }
253         }
254     }
255     catch (SQLException& )
256     {
257         // default handling if we caught an exception
258         switch (nHandle)
259         {
260             case PROPERTY_ID_LABEL:
261             case PROPERTY_ID_TYPENAME:
262             case PROPERTY_ID_SERVICENAME:
263             case PROPERTY_ID_TABLENAME:
264             case PROPERTY_ID_SCHEMANAME:
265             case PROPERTY_ID_CATALOGNAME:
266                 // empty string'S
267                 rValue <<= OUString();
268                 break;
269             case PROPERTY_ID_ISROWVERSION:
270             case PROPERTY_ID_ISAUTOINCREMENT:
271             case PROPERTY_ID_ISWRITABLE:
272             case PROPERTY_ID_ISDEFINITELYWRITABLE:
273             case PROPERTY_ID_ISCASESENSITIVE:
274             case PROPERTY_ID_ISSEARCHABLE:
275             case PROPERTY_ID_ISCURRENCY:
276             case PROPERTY_ID_ISSIGNED:
277             {
278                 rValue <<= false;
279             }   break;
280             case PROPERTY_ID_ISREADONLY:
281             {
282                 rValue <<= true;
283             }   break;
284             case PROPERTY_ID_SCALE:
285             case PROPERTY_ID_PRECISION:
286             case PROPERTY_ID_DISPLAYSIZE:
287                 rValue <<= sal_Int32(0);
288                 break;
289             case PROPERTY_ID_TYPE:
290                 rValue <<= sal_Int32(DataType::SQLNULL);
291                 break;
292             case PROPERTY_ID_ISNULLABLE:
293                 rValue <<= ColumnValue::NULLABLE_UNKNOWN;
294                 break;
295         }
296     }
297 }
298 
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
300