1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include "resultcolumn.hxx"
21 #include <com/sun/star/lang/DisposedException.hpp>
22 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
23 #include <com/sun/star/sdbc/DataType.hpp>
24 #include <com/sun/star/sdbc/ColumnValue.hpp>
25 #include <cppuhelper/typeprovider.hxx>
26 #include <tools/debug.hxx>
27 #include <tools/diagnose_ex.h>
28 #include "dbastrings.hrc"
29 #include "apitools.hxx"
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #include <cppuhelper/exc_hlp.hxx>
32 #include <osl/thread.h>
34 using namespace ::com::sun::star::sdbc
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::lang
;
38 using namespace ::com::sun::star::container
;
39 using namespace ::osl
;
40 using namespace ::comphelper
;
41 using namespace ::cppu
;
42 using namespace dbaccess
;
44 DBG_NAME(OResultColumn
)
46 OResultColumn::OResultColumn( const Reference
< XResultSetMetaData
>& _xMetaData
, sal_Int32 _nPos
,
47 const Reference
< XDatabaseMetaData
>& _rxDBMeta
)
49 ,m_xMetaData( _xMetaData
)
50 ,m_xDBMetaData( _rxDBMeta
)
53 DBG_CTOR(OResultColumn
,NULL
);
56 void OResultColumn::impl_determineIsRowVersion_nothrow()
58 if ( m_aIsRowVersion
.hasValue() )
60 m_aIsRowVersion
<<= (sal_Bool
)(sal_False
);
62 OSL_ENSURE( m_xDBMetaData
.is(), "OResultColumn::impl_determineIsRowVersion_nothrow: no DBMetaData!" );
63 if ( !m_xDBMetaData
.is() )
68 ::rtl::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
;
76 Reference
< XResultSet
> xVersionColumns
= m_xDBMetaData
->getVersionColumns(
77 makeAny( sCatalog
), sSchema
, sTable
);
78 if ( xVersionColumns
.is() ) // allowed to be NULL
80 Reference
< XRow
> xResultRow( xVersionColumns
, UNO_QUERY_THROW
);
81 while ( xVersionColumns
->next() )
83 if ( xResultRow
->getString( 2 ) == sColumnName
)
85 m_aIsRowVersion
<<= (sal_Bool
)(sal_True
);
91 catch(const SQLException
&)
95 catch( const Exception
& )
97 DBG_UNHANDLED_EXCEPTION();
101 OResultColumn::~OResultColumn()
103 DBG_DTOR(OResultColumn
,NULL
);
106 // com::sun::star::lang::XTypeProvider
107 Sequence
< sal_Int8
> OResultColumn::getImplementationId() throw (RuntimeException
)
109 static OImplementationId
* pId
= 0;
112 MutexGuard
aGuard( Mutex::getGlobalMutex() );
115 static OImplementationId aId
;
119 return pId
->getImplementationId();
123 rtl::OUString
OResultColumn::getImplementationName( ) throw(RuntimeException
)
125 return rtl::OUString("com.sun.star.sdb.OResultColumn");
128 Sequence
< ::rtl::OUString
> OResultColumn::getSupportedServiceNames( ) throw (RuntimeException
)
130 Sequence
< ::rtl::OUString
> aSNS( 2 );
131 aSNS
[0] = SERVICE_SDBCX_COLUMN
;
132 aSNS
[1] = SERVICE_SDB_RESULTCOLUMN
;
137 void OResultColumn::disposing()
139 OColumn::disposing();
141 MutexGuard
aGuard(m_aMutex
);
145 // comphelper::OPropertyArrayUsageHelper
146 ::cppu::IPropertyArrayHelper
* OResultColumn::createArrayHelper( ) const
148 BEGIN_PROPERTY_HELPER(21)
149 DECL_PROP1(CATALOGNAME
, ::rtl::OUString
, READONLY
);
150 DECL_PROP1(DISPLAYSIZE
, sal_Int32
, READONLY
);
151 DECL_PROP1_BOOL(ISAUTOINCREMENT
, READONLY
);
152 DECL_PROP1_BOOL(ISCASESENSITIVE
, READONLY
);
153 DECL_PROP1_BOOL(ISCURRENCY
, READONLY
);
154 DECL_PROP1_BOOL(ISDEFINITELYWRITABLE
, READONLY
);
155 DECL_PROP1(ISNULLABLE
, sal_Int32
, READONLY
);
156 DECL_PROP1_BOOL(ISREADONLY
, READONLY
);
157 DECL_PROP1_BOOL(ISROWVERSION
, READONLY
);
158 DECL_PROP1_BOOL(ISSEARCHABLE
, READONLY
);
159 DECL_PROP1_BOOL(ISSIGNED
, READONLY
);
160 DECL_PROP1_BOOL(ISWRITABLE
, READONLY
);
161 DECL_PROP1(LABEL
, ::rtl::OUString
, READONLY
);
162 DECL_PROP1(NAME
, ::rtl::OUString
, READONLY
);
163 DECL_PROP1(PRECISION
, sal_Int32
, READONLY
);
164 DECL_PROP1(SCALE
, sal_Int32
, READONLY
);
165 DECL_PROP1(SCHEMANAME
, ::rtl::OUString
, READONLY
);
166 DECL_PROP1(SERVICENAME
, ::rtl::OUString
, READONLY
);
167 DECL_PROP1(TABLENAME
, ::rtl::OUString
, READONLY
);
168 DECL_PROP1(TYPE
, sal_Int32
, READONLY
);
169 DECL_PROP1(TYPENAME
, ::rtl::OUString
, READONLY
);
170 END_PROPERTY_HELPER();
173 // cppu::OPropertySetHelper
174 ::cppu::IPropertyArrayHelper
& OResultColumn::getInfoHelper()
176 return *static_cast< ::comphelper::OPropertyArrayUsageHelper
< OResultColumn
>* >(this)->getArrayHelper();
181 template< typename TYPE
>
182 void obtain( Any
& _out_rValue
, ::boost::optional
< TYPE
> _rCache
, const sal_Int32 _nPos
, const Reference
< XResultSetMetaData
>& _rxResultMeta
, TYPE (SAL_CALL
XResultSetMetaData::*Getter
)( sal_Int32
) )
185 _rCache
.reset( (_rxResultMeta
.get()->*Getter
)( _nPos
) );
186 _out_rValue
<<= *_rCache
;
190 void OResultColumn::getFastPropertyValue( Any
& rValue
, sal_Int32 nHandle
) const
194 if ( OColumn::isRegisteredProperty( nHandle
) )
196 OColumn::getFastPropertyValue( rValue
, nHandle
);
202 case PROPERTY_ID_ISROWVERSION
:
203 const_cast< OResultColumn
* >( this )->impl_determineIsRowVersion_nothrow();
204 rValue
= m_aIsRowVersion
;
206 case PROPERTY_ID_TABLENAME
:
207 rValue
<<= m_xMetaData
->getTableName(m_nPos
);
209 case PROPERTY_ID_SCHEMANAME
:
210 rValue
<<= m_xMetaData
->getSchemaName(m_nPos
);
212 case PROPERTY_ID_CATALOGNAME
:
213 rValue
<<= m_xMetaData
->getCatalogName(m_nPos
);
215 case PROPERTY_ID_ISSIGNED
:
216 obtain( rValue
, m_isSigned
, m_nPos
, m_xMetaData
, &XResultSetMetaData::isSigned
);
218 case PROPERTY_ID_ISCURRENCY
:
219 obtain( rValue
, m_isCurrency
, m_nPos
, m_xMetaData
, &XResultSetMetaData::isCurrency
);
221 case PROPERTY_ID_ISSEARCHABLE
:
222 obtain( rValue
, m_bSearchable
, m_nPos
, m_xMetaData
, &XResultSetMetaData::isSearchable
);
224 case PROPERTY_ID_ISCASESENSITIVE
:
225 obtain( rValue
, m_isCaseSensitive
, m_nPos
, m_xMetaData
, &XResultSetMetaData::isCaseSensitive
);
227 case PROPERTY_ID_ISREADONLY
:
228 obtain( rValue
, m_isReadOnly
, m_nPos
, m_xMetaData
, &XResultSetMetaData::isReadOnly
);
230 case PROPERTY_ID_ISWRITABLE
:
231 obtain( rValue
, m_isWritable
, m_nPos
, m_xMetaData
, &XResultSetMetaData::isWritable
);
233 case PROPERTY_ID_ISDEFINITELYWRITABLE
:
234 obtain( rValue
, m_isDefinitelyWritable
, m_nPos
, m_xMetaData
, &XResultSetMetaData::isDefinitelyWritable
);
236 case PROPERTY_ID_ISAUTOINCREMENT
:
237 obtain( rValue
, m_isAutoIncrement
, m_nPos
, m_xMetaData
, &XResultSetMetaData::isAutoIncrement
);
239 case PROPERTY_ID_SERVICENAME
:
240 rValue
<<= m_xMetaData
->getColumnServiceName(m_nPos
);
242 case PROPERTY_ID_LABEL
:
243 obtain( rValue
, m_sColumnLabel
, m_nPos
, m_xMetaData
, &XResultSetMetaData::getColumnLabel
);
245 case PROPERTY_ID_DISPLAYSIZE
:
246 obtain( rValue
, m_nColumnDisplaySize
, m_nPos
, m_xMetaData
, &XResultSetMetaData::getColumnDisplaySize
);
248 case PROPERTY_ID_TYPE
:
249 obtain( rValue
, m_nColumnType
, m_nPos
, m_xMetaData
, &XResultSetMetaData::getColumnType
);
251 case PROPERTY_ID_PRECISION
:
252 obtain( rValue
, m_nPrecision
, m_nPos
, m_xMetaData
, &XResultSetMetaData::getPrecision
);
254 case PROPERTY_ID_SCALE
:
255 obtain( rValue
, m_nScale
, m_nPos
, m_xMetaData
, &XResultSetMetaData::getScale
);
257 case PROPERTY_ID_ISNULLABLE
:
258 obtain( rValue
, m_isNullable
, m_nPos
, m_xMetaData
, &XResultSetMetaData::isNullable
);
260 case PROPERTY_ID_TYPENAME
:
261 rValue
<<= m_xMetaData
->getColumnTypeName(m_nPos
);
264 OSL_FAIL( "OResultColumn::getFastPropertyValue: unknown property handle!" );
269 catch (SQLException
& )
271 // default handling if we caught an exception
274 case PROPERTY_ID_LABEL
:
275 case PROPERTY_ID_TYPENAME
:
276 case PROPERTY_ID_SERVICENAME
:
277 case PROPERTY_ID_TABLENAME
:
278 case PROPERTY_ID_SCHEMANAME
:
279 case PROPERTY_ID_CATALOGNAME
:
281 rValue
<<= rtl::OUString();
283 case PROPERTY_ID_ISROWVERSION
:
284 case PROPERTY_ID_ISAUTOINCREMENT
:
285 case PROPERTY_ID_ISWRITABLE
:
286 case PROPERTY_ID_ISDEFINITELYWRITABLE
:
287 case PROPERTY_ID_ISCASESENSITIVE
:
288 case PROPERTY_ID_ISSEARCHABLE
:
289 case PROPERTY_ID_ISCURRENCY
:
290 case PROPERTY_ID_ISSIGNED
:
292 sal_Bool bVal
= sal_False
;
293 rValue
.setValue(&bVal
, getBooleanCppuType());
295 case PROPERTY_ID_ISREADONLY
:
297 sal_Bool bVal
= sal_True
;
298 rValue
.setValue(&bVal
, getBooleanCppuType());
300 case PROPERTY_ID_SCALE
:
301 case PROPERTY_ID_PRECISION
:
302 case PROPERTY_ID_DISPLAYSIZE
:
303 rValue
<<= sal_Int32(0);
305 case PROPERTY_ID_TYPE
:
306 rValue
<<= sal_Int32(DataType::SQLNULL
);
308 case PROPERTY_ID_ISNULLABLE
:
309 rValue
<<= ColumnValue::NULLABLE_UNKNOWN
;
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */