LibreOffice Module ucbhelper (master)  1
resultsetmetadata.cxx
Go to the documentation of this file.
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 /**************************************************************************
22  TODO
23  **************************************************************************
24 
25  *************************************************************************/
26 
27 #include <osl/diagnose.h>
28 #include <com/sun/star/beans/Property.hpp>
29 #include <com/sun/star/beans/XPropertySetInfo.hpp>
30 #include <com/sun/star/io/XInputStream.hpp>
31 #include <com/sun/star/sdbc/ColumnValue.hpp>
32 #include <com/sun/star/sdbc/DataType.hpp>
33 #include <com/sun/star/sdbc/XArray.hpp>
34 #include <com/sun/star/sdbc/XBlob.hpp>
35 #include <com/sun/star/sdbc/XClob.hpp>
36 #include <com/sun/star/sdbc/XRef.hpp>
37 #include <com/sun/star/util/Date.hpp>
38 #include <com/sun/star/util/Time.hpp>
39 #include <com/sun/star/util/DateTime.hpp>
40 #include <com/sun/star/ucb/PropertiesManager.hpp>
41 #include <ucbhelper/macros.hxx>
44 
45 using namespace com::sun::star::beans;
46 using namespace com::sun::star::io;
47 using namespace com::sun::star::lang;
48 using namespace com::sun::star::sdbc;
49 using namespace com::sun::star::ucb;
50 using namespace com::sun::star::uno;
51 using namespace com::sun::star::util;
52 
53 
54 namespace ucbhelper_impl {
55 
57 {
58  osl::Mutex m_aMutex;
59  std::vector< ::ucbhelper::ResultSetColumnData > m_aColumnData;
61 
62  explicit ResultSetMetaData_Impl( sal_Int32 nSize )
63  : m_aColumnData( nSize ), m_bObtainedTypes( false ) {}
64 
66  const std::vector< ::ucbhelper::ResultSetColumnData >& rColumnData )
67  : m_aColumnData( rColumnData ), m_bObtainedTypes( false ) {}
68 };
69 
70 }
71 
72 using namespace ucbhelper_impl;
73 
74 namespace ucbhelper {
75 
76 
77 // ResultSetMetaData Implementation.
78 
79 
80 ResultSetMetaData::ResultSetMetaData(
81  const Reference< XComponentContext >& rxContext,
82  const Sequence< Property >& rProps )
83 : m_pImpl( new ResultSetMetaData_Impl( rProps.getLength() ) ),
84  m_xContext( rxContext ),
85  m_aProps( rProps )
86 {
87 }
88 
89 
90 ResultSetMetaData::ResultSetMetaData(
91  const Reference< XComponentContext >& rxContext,
92  const Sequence< Property >& rProps,
93  const std::vector< ResultSetColumnData >& rColumnData )
94 : m_pImpl( new ResultSetMetaData_Impl( rColumnData ) ),
95  m_xContext( rxContext ),
96  m_aProps( rProps )
97 {
98  OSL_ENSURE( rColumnData.size() == sal_uInt32( rProps.getLength() ),
99  "ResultSetMetaData ctor - different array sizes!" );
100 }
101 
102 
103 // virtual
104 ResultSetMetaData::~ResultSetMetaData()
105 {
106 }
107 
108 
109 // XResultSetMetaData methods.
110 
111 
112 // virtual
113 sal_Int32 SAL_CALL ResultSetMetaData::getColumnCount()
114 {
115  return m_aProps.getLength();
116 }
117 
118 
119 // virtual
120 sal_Bool SAL_CALL ResultSetMetaData::isAutoIncrement( sal_Int32 /*column*/ )
121 {
122  /*
123  Checks whether column is automatically numbered, which makes it
124  read-only.
125  */
126  return false;
127 }
128 
129 
130 // virtual
131 sal_Bool SAL_CALL ResultSetMetaData::isCaseSensitive( sal_Int32 column )
132 {
133  if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
134  return false;
135 
136  return m_pImpl->m_aColumnData[ column - 1 ].isCaseSensitive;
137 }
138 
139 
140 // virtual
141 sal_Bool SAL_CALL ResultSetMetaData::isSearchable( sal_Int32 /*column*/ )
142 {
143  return false;
144 }
145 
146 
147 // virtual
148 sal_Bool SAL_CALL ResultSetMetaData::isCurrency( sal_Int32 /*column*/ )
149 {
150  return false;
151 }
152 
153 
154 // virtual
155 sal_Int32 SAL_CALL ResultSetMetaData::isNullable( sal_Int32 /*column*/ )
156 {
157  return ColumnValue::NULLABLE;
158 }
159 
160 
161 // virtual
162 sal_Bool SAL_CALL ResultSetMetaData::isSigned( sal_Int32 /*column*/ )
163 {
164  return false;
165 }
166 
167 
168 // virtual
169 sal_Int32 SAL_CALL ResultSetMetaData::getColumnDisplaySize( sal_Int32 /*column*/ )
170 {
171  /*
172  Gets the normal maximum width in characters for column.
173  */
174  return 16;
175 }
176 
177 
178 // virtual
179 OUString SAL_CALL ResultSetMetaData::getColumnLabel( sal_Int32 column )
180 {
181  /*
182  Gets the suggested column title for column, to be used in print-
183  outs and displays.
184  */
185 
186  if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
187  return OUString();
188 
189  return m_aProps.getConstArray()[ column - 1 ].Name;
190 }
191 
192 
193 // virtual
194 OUString SAL_CALL ResultSetMetaData::getColumnName( sal_Int32 column )
195 {
196  /*
197  Gets the name of column.
198  */
199 
200  if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
201  return OUString();
202 
203  return m_aProps.getConstArray()[ column - 1 ].Name;
204 }
205 
206 
207 // virtual
208 OUString SAL_CALL ResultSetMetaData::getSchemaName( sal_Int32 /*column*/ )
209 {
210  /*
211  Gets the schema name for the table from which column of this
212  result set was derived.
213  Because this feature is not widely supported, the return value
214  for many DBMSs will be an empty string.
215  */
216  return OUString();
217 }
218 
219 
220 // virtual
221 sal_Int32 SAL_CALL ResultSetMetaData::getPrecision( sal_Int32 /*column*/ )
222 {
223  return -1;
224 }
225 
226 
227 // virtual
228 sal_Int32 SAL_CALL ResultSetMetaData::getScale( sal_Int32 /*column*/ )
229 {
230  return 0;
231 }
232 
233 
234 // virtual
235 OUString SAL_CALL ResultSetMetaData::getTableName( sal_Int32 /*column*/ )
236 {
237  /*
238  Gets the name of the table from which column of this result set
239  was derived or "" if there is none (for example, for a join).
240  Because this feature is not widely supported, the return value
241  for many DBMSs will be an empty string.
242  */
243  return OUString();
244 }
245 
246 
247 // virtual
248 OUString SAL_CALL ResultSetMetaData::getCatalogName( sal_Int32 /*column*/ )
249 {
250  /*
251  Gets the catalog name for the table from which column of this
252  result set was derived.
253  Because this feature is not widely supported, the return value
254  for many DBMSs will be an empty string.
255  */
256  return OUString();
257 }
258 
259 
260 // virtual
261 sal_Int32 SAL_CALL ResultSetMetaData::getColumnType( sal_Int32 column )
262 {
263  /*
264  Gets the JDBC type for the value stored in column. ... The STRUCT
265  and DISTINCT type codes are always returned for structured and
266  distinct types, regardless of whether the value will be mapped
267  according to the standard mapping or be a custom mapping.
268  */
269 
270  if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
271  return DataType::SQLNULL;
272 
273  if ( m_aProps.getConstArray()[ column - 1 ].Type
275  {
276  // No type given. Try UCB's Properties Manager...
277 
278  osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
279 
280  if ( !m_pImpl->m_bObtainedTypes )
281  {
282  try
283  {
284  Reference< XPropertySetInfo > xInfo = PropertiesManager::create( m_xContext );
285  // Less (remote) calls...
286 
287  Sequence< Property > aProps = xInfo->getProperties();
288 
289  for ( Property& rProp : m_aProps )
290  {
291  auto pProp = std::find_if(aProps.begin(), aProps.end(),
292  [&rProp](const Property& rProp1) { return rProp.Name == rProp1.Name; });
293  if (pProp != aProps.end())
294  {
295  // Found...
296  rProp.Type = pProp->Type;
297  }
298  }
299  }
300  catch ( RuntimeException& )
301  {
302  throw;
303  }
304  catch ( Exception& )
305  {
306  // createInstance
307  }
308 
309  m_pImpl->m_bObtainedTypes = true;
310  }
311  }
312 
313  const Type& rType = m_aProps.getConstArray()[ column - 1 ].Type;
314  sal_Int32 nType = DataType::OTHER;
315 
316  if ( rType == cppu::UnoType<OUString>::get() )
317  nType = DataType::VARCHAR; // XRow::getString
318  else if ( rType == cppu::UnoType<bool>::get() )
319  nType = DataType::BIT; // XRow::getBoolean
320  else if ( rType == cppu::UnoType<sal_Int32>::get() )
321  nType = DataType::INTEGER; // XRow::getInt
322  else if ( rType == cppu::UnoType<sal_Int64>::get() )
323  nType = DataType::BIGINT; // XRow::getLong
324  else if ( rType == cppu::UnoType<sal_Int16>::get() )
325  nType = DataType::SMALLINT; // XRow::getShort
326  else if ( rType == cppu::UnoType<sal_Int8>::get() )
327  nType = DataType::TINYINT; // XRow::getByte
328  else if ( rType == cppu::UnoType<float>::get() )
329  nType = DataType::REAL; // XRow::getFloat
330  else if ( rType == cppu::UnoType<double>::get() )
331  nType = DataType::DOUBLE; // XRow::getDouble
332  else if ( rType == cppu::UnoType<Sequence<sal_Int8>>::get() )
333  nType = DataType::VARBINARY;// XRow::getBytes
334  else if ( rType == cppu::UnoType<Date>::get() )
335  nType = DataType::DATE; // XRow::getDate
336  else if ( rType == cppu::UnoType<Time>::get() )
337  nType = DataType::TIME; // XRow::getTime
338  else if ( rType == cppu::UnoType<DateTime>::get() )
339  nType = DataType::TIMESTAMP;// XRow::getTimestamp
340  else if ( rType == cppu::UnoType<XInputStream>::get() )
341  nType = DataType::LONGVARBINARY; // XRow::getBinaryStream
342 // nType = DataType::LONGVARCHAR; // XRow::getCharacterStream
343  else if ( rType == cppu::UnoType<XClob>::get() )
344  nType = DataType::CLOB; // XRow::getClob
345  else if ( rType == cppu::UnoType<XBlob>::get() )
346  nType = DataType::BLOB; // XRow::getBlob
347  else if ( rType == cppu::UnoType<XArray>::get() )
348  nType = DataType::ARRAY;// XRow::getArray
349  else if ( rType == cppu::UnoType<XRef>::get() )
350  nType = DataType::REF;// XRow::getRef
351  else
352  nType = DataType::OBJECT;// XRow::getObject
353 
354  return nType;
355 }
356 
357 
358 // virtual
359 OUString SAL_CALL ResultSetMetaData::getColumnTypeName( sal_Int32 /*column*/ )
360 {
361  /*
362  Gets the type name used by this particular data source for the
363  values stored in column. If the type code for the type of value
364  stored in column is STRUCT, DISTINCT or JAVA_OBJECT, this method
365  returns a fully-qualified SQL type name.
366  */
367  return OUString();
368 }
369 
370 
371 // virtual
372 sal_Bool SAL_CALL ResultSetMetaData::isReadOnly( sal_Int32 /*column*/ )
373 {
374  return true;
375 }
376 
377 
378 // virtual
379 sal_Bool SAL_CALL ResultSetMetaData::isWritable( sal_Int32 /*column*/ )
380 {
381  return false;
382 }
383 
384 
385 // virtual
386 sal_Bool SAL_CALL ResultSetMetaData::isDefinitelyWritable( sal_Int32 /*column*/ )
387 {
388  return false;
389 }
390 
391 
392 // virtual
393 OUString SAL_CALL ResultSetMetaData::getColumnServiceName( sal_Int32 /*column*/ )
394 {
395  /*
396  Returns the fully-qualified name of the service whose instances
397  are manufactured if XResultSet::getObject is called to retrieve
398  a value from the column.
399  */
400  return OUString();
401 }
402 
403 } // namespace ucbhelper
404 
405 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
std::vector< ::ucbhelper::ResultSetColumnData > m_aColumnData
ResultSetMetaData_Impl(const std::vector< ::ucbhelper::ResultSetColumnData > &rColumnData)
unsigned char sal_Bool
css::uno::Type const & get()
QPRO_FUNC_TYPE nType
double getLength(const B2DPolygon &rCandidate)
Reference< XComponentContext > m_xContext
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl