Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / dbaccess / source / core / api / resultcolumn.cxx
blob8d679ec60a92d51838a359a0b65dfdd360ed362e
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 .
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 )
48 :OColumn( true )
49 ,m_xMetaData( _xMetaData )
50 ,m_xDBMetaData( _rxDBMeta )
51 ,m_nPos( _nPos )
53 DBG_CTOR(OResultColumn,NULL);
56 void OResultColumn::impl_determineIsRowVersion_nothrow()
58 if ( m_aIsRowVersion.hasValue() )
59 return;
60 m_aIsRowVersion <<= (sal_Bool)(sal_False);
62 OSL_ENSURE( m_xDBMetaData.is(), "OResultColumn::impl_determineIsRowVersion_nothrow: no DBMetaData!" );
63 if ( !m_xDBMetaData.is() )
64 return;
66 try
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;
74 try
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);
86 break;
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;
110 if (! pId)
112 MutexGuard aGuard( Mutex::getGlobalMutex() );
113 if (! pId)
115 static OImplementationId aId;
116 pId = &aId;
119 return pId->getImplementationId();
122 // XServiceInfo
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;
133 return aSNS;
136 // OComponentHelper
137 void OResultColumn::disposing()
139 OColumn::disposing();
141 MutexGuard aGuard(m_aMutex);
142 m_xMetaData = NULL;
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();
179 namespace
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 ) )
184 if ( !_rCache )
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 );
198 else
200 switch (nHandle)
202 case PROPERTY_ID_ISROWVERSION:
203 const_cast< OResultColumn* >( this )->impl_determineIsRowVersion_nothrow();
204 rValue = m_aIsRowVersion;
205 break;
206 case PROPERTY_ID_TABLENAME:
207 rValue <<= m_xMetaData->getTableName(m_nPos);
208 break;
209 case PROPERTY_ID_SCHEMANAME:
210 rValue <<= m_xMetaData->getSchemaName(m_nPos);
211 break;
212 case PROPERTY_ID_CATALOGNAME:
213 rValue <<= m_xMetaData->getCatalogName(m_nPos);
214 break;
215 case PROPERTY_ID_ISSIGNED:
216 obtain( rValue, m_isSigned, m_nPos, m_xMetaData, &XResultSetMetaData::isSigned );
217 break;
218 case PROPERTY_ID_ISCURRENCY:
219 obtain( rValue, m_isCurrency, m_nPos, m_xMetaData, &XResultSetMetaData::isCurrency );
220 break;
221 case PROPERTY_ID_ISSEARCHABLE:
222 obtain( rValue, m_bSearchable, m_nPos, m_xMetaData, &XResultSetMetaData::isSearchable );
223 break;
224 case PROPERTY_ID_ISCASESENSITIVE:
225 obtain( rValue, m_isCaseSensitive, m_nPos, m_xMetaData, &XResultSetMetaData::isCaseSensitive );
226 break;
227 case PROPERTY_ID_ISREADONLY:
228 obtain( rValue, m_isReadOnly, m_nPos, m_xMetaData, &XResultSetMetaData::isReadOnly );
229 break;
230 case PROPERTY_ID_ISWRITABLE:
231 obtain( rValue, m_isWritable, m_nPos, m_xMetaData, &XResultSetMetaData::isWritable );
232 break;
233 case PROPERTY_ID_ISDEFINITELYWRITABLE:
234 obtain( rValue, m_isDefinitelyWritable, m_nPos, m_xMetaData, &XResultSetMetaData::isDefinitelyWritable );
235 break;
236 case PROPERTY_ID_ISAUTOINCREMENT:
237 obtain( rValue, m_isAutoIncrement, m_nPos, m_xMetaData, &XResultSetMetaData::isAutoIncrement );
238 break;
239 case PROPERTY_ID_SERVICENAME:
240 rValue <<= m_xMetaData->getColumnServiceName(m_nPos);
241 break;
242 case PROPERTY_ID_LABEL:
243 obtain( rValue, m_sColumnLabel, m_nPos, m_xMetaData, &XResultSetMetaData::getColumnLabel );
244 break;
245 case PROPERTY_ID_DISPLAYSIZE:
246 obtain( rValue, m_nColumnDisplaySize, m_nPos, m_xMetaData, &XResultSetMetaData::getColumnDisplaySize );
247 break;
248 case PROPERTY_ID_TYPE:
249 obtain( rValue, m_nColumnType, m_nPos, m_xMetaData, &XResultSetMetaData::getColumnType );
250 break;
251 case PROPERTY_ID_PRECISION:
252 obtain( rValue, m_nPrecision, m_nPos, m_xMetaData, &XResultSetMetaData::getPrecision );
253 break;
254 case PROPERTY_ID_SCALE:
255 obtain( rValue, m_nScale, m_nPos, m_xMetaData, &XResultSetMetaData::getScale );
256 break;
257 case PROPERTY_ID_ISNULLABLE:
258 obtain( rValue, m_isNullable, m_nPos, m_xMetaData, &XResultSetMetaData::isNullable );
259 break;
260 case PROPERTY_ID_TYPENAME:
261 rValue <<= m_xMetaData->getColumnTypeName(m_nPos);
262 break;
263 default:
264 OSL_FAIL( "OResultColumn::getFastPropertyValue: unknown property handle!" );
265 break;
269 catch (SQLException& )
271 // default handling if we caught an exception
272 switch (nHandle)
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:
280 // empty string'S
281 rValue <<= rtl::OUString();
282 break;
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());
294 } break;
295 case PROPERTY_ID_ISREADONLY:
297 sal_Bool bVal = sal_True;
298 rValue.setValue(&bVal, getBooleanCppuType());
299 } break;
300 case PROPERTY_ID_SCALE:
301 case PROPERTY_ID_PRECISION:
302 case PROPERTY_ID_DISPLAYSIZE:
303 rValue <<= sal_Int32(0);
304 break;
305 case PROPERTY_ID_TYPE:
306 rValue <<= sal_Int32(DataType::SQLNULL);
307 break;
308 case PROPERTY_ID_ISNULLABLE:
309 rValue <<= ColumnValue::NULLABLE_UNKNOWN;
310 break;
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */