Line data Source code
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 "calc/CTable.hxx"
21 : #include <com/sun/star/sdbc/ColumnValue.hpp>
22 : #include <com/sun/star/sdbc/DataType.hpp>
23 : #include <com/sun/star/sdbc/XRow.hpp>
24 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
25 : #include <com/sun/star/sheet/XSpreadsheet.hpp>
26 : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
27 : #include <com/sun/star/sheet/XCellRangesQuery.hpp>
28 : #include <com/sun/star/sheet/XDatabaseRanges.hpp>
29 : #include <com/sun/star/sheet/XDatabaseRange.hpp>
30 : #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
31 : #include <com/sun/star/sheet/XUsedAreaCursor.hpp>
32 : #include <com/sun/star/sheet/CellFlags.hpp>
33 : #include <com/sun/star/sheet/FormulaResult.hpp>
34 : #include <com/sun/star/util/NumberFormat.hpp>
35 : #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
36 : #include <com/sun/star/text/XText.hpp>
37 : #include <svl/converter.hxx>
38 : #include "calc/CConnection.hxx"
39 : #include "calc/CColumns.hxx"
40 : #include "connectivity/sdbcx/VColumn.hxx"
41 : #include <rtl/ustrbuf.hxx>
42 : #include <osl/thread.h>
43 : #include <comphelper/sequence.hxx>
44 : #include <svl/zforlist.hxx>
45 : #include <rtl/math.hxx>
46 : #include <comphelper/extract.hxx>
47 : #include <connectivity/dbexception.hxx>
48 : #include <connectivity/dbconversion.hxx>
49 : #include <comphelper/types.hxx>
50 :
51 : using namespace connectivity;
52 : using namespace connectivity::calc;
53 : using namespace connectivity::file;
54 : using namespace ::cppu;
55 : using namespace ::dbtools;
56 : using namespace ::com::sun::star::uno;
57 : using namespace ::com::sun::star::beans;
58 : using namespace ::com::sun::star::sdbcx;
59 : using namespace ::com::sun::star::sdbc;
60 : using namespace ::com::sun::star::container;
61 : using namespace ::com::sun::star::lang;
62 : using namespace ::com::sun::star::sheet;
63 : using namespace ::com::sun::star::table;
64 : using namespace ::com::sun::star::text;
65 : using namespace ::com::sun::star::util;
66 :
67 :
68 0 : static void lcl_UpdateArea( const Reference<XCellRange>& xUsedRange, sal_Int32& rEndCol, sal_Int32& rEndRow )
69 : {
70 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::lcl_UpdateArea" );
71 : // update rEndCol, rEndRow if any non-empty cell in xUsedRange is right/below
72 :
73 0 : const Reference<XCellRangesQuery> xUsedQuery( xUsedRange, UNO_QUERY );
74 0 : if ( xUsedQuery.is() )
75 : {
76 : const sal_Int16 nContentFlags =
77 0 : CellFlags::STRING | CellFlags::VALUE | CellFlags::DATETIME | CellFlags::FORMULA | CellFlags::ANNOTATION;
78 :
79 0 : const Reference<XSheetCellRanges> xUsedRanges = xUsedQuery->queryContentCells( nContentFlags );
80 0 : const Sequence<CellRangeAddress> aAddresses = xUsedRanges->getRangeAddresses();
81 :
82 0 : const sal_Int32 nCount = aAddresses.getLength();
83 0 : const CellRangeAddress* pData = aAddresses.getConstArray();
84 0 : for ( sal_Int32 i=0; i<nCount; i++ )
85 : {
86 0 : rEndCol = pData[i].EndColumn > rEndCol ? pData[i].EndColumn : rEndCol;
87 0 : rEndRow = pData[i].EndRow > rEndRow ? pData[i].EndRow : rEndRow;
88 0 : }
89 0 : }
90 0 : }
91 :
92 0 : static void lcl_GetDataArea( const Reference<XSpreadsheet>& xSheet, sal_Int32& rColumnCount, sal_Int32& rRowCount )
93 : {
94 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::lcl_GetDataArea" );
95 0 : Reference<XSheetCellCursor> xCursor = xSheet->createCursor();
96 0 : Reference<XCellRangeAddressable> xRange( xCursor, UNO_QUERY );
97 0 : if ( !xRange.is() )
98 : {
99 0 : rColumnCount = rRowCount = 0;
100 0 : return;
101 : }
102 :
103 : // first find the contiguous cell area starting at A1
104 :
105 0 : xCursor->collapseToSize( 1, 1 ); // single (first) cell
106 0 : xCursor->collapseToCurrentRegion(); // contiguous data area
107 :
108 0 : CellRangeAddress aRegionAddr = xRange->getRangeAddress();
109 0 : sal_Int32 nEndCol = aRegionAddr.EndColumn;
110 0 : sal_Int32 nEndRow = aRegionAddr.EndRow;
111 :
112 0 : Reference<XUsedAreaCursor> xUsed( xCursor, UNO_QUERY );
113 0 : if ( xUsed.is() )
114 : {
115 : // The used area from XUsedAreaCursor includes visible attributes.
116 : // If the used area is larger than the contiguous cell area, find non-empty
117 : // cells in that area.
118 :
119 0 : xUsed->gotoEndOfUsedArea( sal_False );
120 0 : CellRangeAddress aUsedAddr = xRange->getRangeAddress();
121 :
122 0 : if ( aUsedAddr.EndColumn > aRegionAddr.EndColumn )
123 : {
124 0 : Reference<XCellRange> xUsedRange = xSheet->getCellRangeByPosition(
125 0 : aRegionAddr.EndColumn + 1, 0, aUsedAddr.EndColumn, aUsedAddr.EndRow );
126 0 : lcl_UpdateArea( xUsedRange, nEndCol, nEndRow );
127 : }
128 :
129 0 : if ( aUsedAddr.EndRow > aRegionAddr.EndRow )
130 : {
131 : // only up to the last column of aRegionAddr, the other columns are handled above
132 0 : Reference<XCellRange> xUsedRange = xSheet->getCellRangeByPosition(
133 0 : 0, aRegionAddr.EndRow + 1, aRegionAddr.EndColumn, aUsedAddr.EndRow );
134 0 : lcl_UpdateArea( xUsedRange, nEndCol, nEndRow );
135 : }
136 : }
137 :
138 0 : rColumnCount = nEndCol + 1; // number of columns
139 0 : rRowCount = nEndRow; // first row (headers) is not counted
140 : }
141 :
142 0 : static CellContentType lcl_GetContentOrResultType( const Reference<XCell>& xCell )
143 : {
144 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::lcl_GetContentOrResultType" );
145 0 : CellContentType eCellType = xCell->getType();
146 0 : if ( eCellType == CellContentType_FORMULA )
147 : {
148 0 : static const OUString s_sFormulaResultType("FormulaResultType");
149 0 : Reference<XPropertySet> xProp( xCell, UNO_QUERY );
150 : try
151 : {
152 0 : xProp->getPropertyValue( s_sFormulaResultType ) >>= eCellType; // type of formula result
153 : }
154 0 : catch (UnknownPropertyException&)
155 : {
156 0 : eCellType = CellContentType_VALUE; // if FormulaResultType property not available
157 0 : }
158 : }
159 0 : return eCellType;
160 : }
161 :
162 0 : static Reference<XCell> lcl_GetUsedCell( const Reference<XSpreadsheet>& xSheet, sal_Int32 nDocColumn, sal_Int32 nDocRow )
163 : {
164 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::lcl_GetUsedCell" );
165 0 : Reference<XCell> xCell = xSheet->getCellByPosition( nDocColumn, nDocRow );
166 0 : if ( xCell.is() && xCell->getType() == CellContentType_EMPTY )
167 : {
168 : // get first non-empty cell
169 :
170 0 : Reference<XCellRangeAddressable> xAddr( xSheet, UNO_QUERY );
171 0 : if (xAddr.is())
172 : {
173 0 : CellRangeAddress aTotalRange = xAddr->getRangeAddress();
174 0 : sal_Int32 nLastRow = aTotalRange.EndRow;
175 0 : Reference<XCellRangesQuery> xQuery( xSheet->getCellRangeByPosition( nDocColumn, nDocRow, nDocColumn, nLastRow ), UNO_QUERY );
176 0 : if (xQuery.is())
177 : {
178 : // queryIntersection to get a ranges object
179 0 : Reference<XSheetCellRanges> xRanges = xQuery->queryIntersection( aTotalRange );
180 0 : if (xRanges.is())
181 : {
182 0 : Reference<XEnumerationAccess> xCells = xRanges->getCells();
183 0 : if (xCells.is())
184 : {
185 0 : Reference<XEnumeration> xEnum = xCells->createEnumeration();
186 0 : if ( xEnum.is() && xEnum->hasMoreElements() )
187 : {
188 : // get first non-empty cell from enumeration
189 0 : xCell.set(xEnum->nextElement(),UNO_QUERY);
190 0 : }
191 : // otherwise, keep empty cell
192 0 : }
193 0 : }
194 0 : }
195 0 : }
196 : }
197 0 : return xCell;
198 : }
199 :
200 0 : static bool lcl_HasTextInColumn( const Reference<XSpreadsheet>& xSheet, sal_Int32 nDocColumn, sal_Int32 nDocRow )
201 : {
202 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::lcl_HasTextInColumn" );
203 : // look for any text cell or text result in the column
204 :
205 0 : Reference<XCellRangeAddressable> xAddr( xSheet, UNO_QUERY );
206 0 : if (xAddr.is())
207 : {
208 0 : CellRangeAddress aTotalRange = xAddr->getRangeAddress();
209 0 : sal_Int32 nLastRow = aTotalRange.EndRow;
210 0 : Reference<XCellRangesQuery> xQuery( xSheet->getCellRangeByPosition( nDocColumn, nDocRow, nDocColumn, nLastRow ), UNO_QUERY );
211 0 : if (xQuery.is())
212 : {
213 : // are there text cells in the column?
214 0 : Reference<XSheetCellRanges> xTextContent = xQuery->queryContentCells( CellFlags::STRING );
215 0 : if ( xTextContent.is() && xTextContent->hasElements() )
216 0 : return true;
217 :
218 : // are there formulas with text results in the column?
219 0 : Reference<XSheetCellRanges> xTextFormula = xQuery->queryFormulaCells( FormulaResult::STRING );
220 0 : if ( xTextFormula.is() && xTextFormula->hasElements() )
221 0 : return true;
222 0 : }
223 : }
224 :
225 0 : return false;
226 : }
227 :
228 0 : static void lcl_GetColumnInfo( const Reference<XSpreadsheet>& xSheet, const Reference<XNumberFormats>& xFormats,
229 : sal_Int32 nDocColumn, sal_Int32 nStartRow, sal_Bool bHasHeaders,
230 : OUString& rName, sal_Int32& rDataType, sal_Bool& rCurrency )
231 : {
232 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::lcl_GetColumnInfo" );
233 : //! avoid duplicate field names
234 :
235 : // get column name from first row, if range contains headers
236 :
237 0 : if ( bHasHeaders )
238 : {
239 0 : Reference<XText> xHeaderText( xSheet->getCellByPosition( nDocColumn, nStartRow ), UNO_QUERY );
240 0 : if ( xHeaderText.is() )
241 0 : rName = xHeaderText->getString();
242 : }
243 :
244 : // get column type from first data row
245 :
246 0 : sal_Int32 nDataRow = nStartRow;
247 0 : if ( bHasHeaders )
248 0 : ++nDataRow;
249 0 : Reference<XCell> xDataCell = lcl_GetUsedCell( xSheet, nDocColumn, nDataRow );
250 :
251 0 : Reference<XPropertySet> xProp( xDataCell, UNO_QUERY );
252 0 : if ( xProp.is() )
253 : {
254 0 : rCurrency = sal_False; // set to true for currency below
255 :
256 0 : const CellContentType eCellType = lcl_GetContentOrResultType( xDataCell );
257 : // #i35178# use "text" type if there is any text cell in the column
258 0 : if ( eCellType == CellContentType_TEXT || lcl_HasTextInColumn( xSheet, nDocColumn, nDataRow ) )
259 0 : rDataType = DataType::VARCHAR;
260 0 : else if ( eCellType == CellContentType_VALUE )
261 : {
262 : // get number format to distinguish between different types
263 :
264 0 : sal_Int16 nNumType = NumberFormat::NUMBER;
265 : try
266 : {
267 0 : static OUString s_NumberFormat("NumberFormat");
268 0 : sal_Int32 nKey = 0;
269 :
270 0 : if ( xProp->getPropertyValue( s_NumberFormat ) >>= nKey )
271 : {
272 0 : const Reference<XPropertySet> xFormat = xFormats->getByKey( nKey );
273 0 : if ( xFormat.is() )
274 : {
275 0 : xFormat->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE) ) >>= nNumType;
276 0 : }
277 : }
278 : }
279 0 : catch ( Exception& )
280 : {
281 : }
282 :
283 0 : if ( nNumType & NumberFormat::TEXT )
284 0 : rDataType = DataType::VARCHAR;
285 0 : else if ( nNumType & NumberFormat::NUMBER )
286 0 : rDataType = DataType::DECIMAL;
287 0 : else if ( nNumType & NumberFormat::CURRENCY )
288 : {
289 0 : rCurrency = sal_True;
290 0 : rDataType = DataType::DECIMAL;
291 : }
292 0 : else if ( ( nNumType & NumberFormat::DATETIME ) == NumberFormat::DATETIME )
293 : {
294 : // NumberFormat::DATETIME is DATE | TIME
295 0 : rDataType = DataType::TIMESTAMP;
296 : }
297 0 : else if ( nNumType & NumberFormat::DATE )
298 0 : rDataType = DataType::DATE;
299 0 : else if ( nNumType & NumberFormat::TIME )
300 0 : rDataType = DataType::TIME;
301 0 : else if ( nNumType & NumberFormat::LOGICAL )
302 0 : rDataType = DataType::BIT;
303 : else
304 0 : rDataType = DataType::DECIMAL;
305 : }
306 : else
307 : {
308 : // whole column empty
309 0 : rDataType = DataType::VARCHAR;
310 : }
311 0 : }
312 0 : }
313 :
314 :
315 :
316 0 : static void lcl_SetValue( ORowSetValue& rValue, const Reference<XSpreadsheet>& xSheet,
317 : sal_Int32 nStartCol, sal_Int32 nStartRow, sal_Bool bHasHeaders,
318 : const ::Date& rNullDate,
319 : sal_Int32 nDBRow, sal_Int32 nDBColumn, sal_Int32 nType )
320 : {
321 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::lcl_SetValue" );
322 0 : sal_Int32 nDocColumn = nStartCol + nDBColumn - 1; // database counts from 1
323 0 : sal_Int32 nDocRow = nStartRow + nDBRow - 1;
324 0 : if (bHasHeaders)
325 0 : ++nDocRow;
326 :
327 0 : const Reference<XCell> xCell = xSheet->getCellByPosition( nDocColumn, nDocRow );
328 0 : if ( xCell.is() )
329 : {
330 0 : CellContentType eCellType = lcl_GetContentOrResultType( xCell );
331 0 : switch (nType)
332 : {
333 : case DataType::VARCHAR:
334 0 : if ( eCellType == CellContentType_EMPTY )
335 0 : rValue.setNull();
336 : else
337 : {
338 : // #i25840# still let Calc convert numbers to text
339 0 : const Reference<XText> xText( xCell, UNO_QUERY );
340 0 : if ( xText.is() )
341 0 : rValue = xText->getString();
342 : }
343 0 : break;
344 : case DataType::DECIMAL:
345 0 : if ( eCellType == CellContentType_VALUE )
346 0 : rValue = xCell->getValue(); // double
347 : else
348 0 : rValue.setNull();
349 0 : break;
350 : case DataType::BIT:
351 0 : if ( eCellType == CellContentType_VALUE )
352 0 : rValue = (sal_Bool)( xCell->getValue() != 0.0 );
353 : else
354 0 : rValue.setNull();
355 0 : break;
356 : case DataType::DATE:
357 0 : if ( eCellType == CellContentType_VALUE )
358 : {
359 0 : ::Date aDate( rNullDate );
360 0 : aDate += (long)::rtl::math::approxFloor( xCell->getValue() );
361 0 : ::com::sun::star::util::Date aDateStruct( aDate.GetDay(), aDate.GetMonth(), aDate.GetYear() );
362 0 : rValue = aDateStruct;
363 : }
364 : else
365 0 : rValue.setNull();
366 0 : break;
367 : case DataType::TIME:
368 0 : if ( eCellType == CellContentType_VALUE )
369 : {
370 0 : double fCellVal = xCell->getValue();
371 0 : double fTime = fCellVal - rtl::math::approxFloor( fCellVal );
372 0 : sal_Int64 nIntTime = static_cast<sal_Int64>(rtl::math::round( fTime * static_cast<double>(::Time::nanoSecPerDay) ));
373 0 : if ( nIntTime == ::Time::nanoSecPerDay)
374 0 : nIntTime = 0; // 23:59:59.9999999995 and above is 00:00:00.00
375 0 : ::com::sun::star::util::Time aTime;
376 0 : aTime.NanoSeconds = (sal_uInt32)( nIntTime % ::Time::nanoSecPerSec );
377 0 : nIntTime /= ::Time::nanoSecPerSec;
378 0 : aTime.Seconds = (sal_uInt16)( nIntTime % 60 );
379 0 : nIntTime /= 60;
380 0 : aTime.Minutes = (sal_uInt16)( nIntTime % 60 );
381 0 : nIntTime /= 60;
382 : OSL_ENSURE( nIntTime < 24, "error in time calculation" );
383 0 : aTime.Hours = (sal_uInt16) nIntTime;
384 0 : rValue = aTime;
385 : }
386 : else
387 0 : rValue.setNull();
388 0 : break;
389 : case DataType::TIMESTAMP:
390 0 : if ( eCellType == CellContentType_VALUE )
391 : {
392 0 : double fCellVal = xCell->getValue();
393 0 : double fDays = ::rtl::math::approxFloor( fCellVal );
394 0 : double fTime = fCellVal - fDays;
395 0 : long nIntDays = (long)fDays;
396 0 : sal_Int64 nIntTime = ::rtl::math::round( fTime * static_cast<double>(::Time::nanoSecPerDay) );
397 0 : if ( nIntTime == ::Time::nanoSecPerDay )
398 : {
399 0 : nIntTime = 0; // 23:59:59.9999999995 and above is 00:00:00.00
400 0 : ++nIntDays; // (next day)
401 : }
402 :
403 0 : ::com::sun::star::util::DateTime aDateTime;
404 :
405 0 : aDateTime.NanoSeconds = (sal_uInt16)( nIntTime % ::Time::nanoSecPerSec );
406 0 : nIntTime /= ::Time::nanoSecPerSec;
407 0 : aDateTime.Seconds = (sal_uInt16)( nIntTime % 60 );
408 0 : nIntTime /= 60;
409 0 : aDateTime.Minutes = (sal_uInt16)( nIntTime % 60 );
410 0 : nIntTime /= 60;
411 : OSL_ENSURE( nIntTime < 24, "error in time calculation" );
412 0 : aDateTime.Hours = (sal_uInt16) nIntTime;
413 :
414 0 : ::Date aDate( rNullDate );
415 0 : aDate += nIntDays;
416 0 : aDateTime.Day = aDate.GetDay();
417 0 : aDateTime.Month = aDate.GetMonth();
418 0 : aDateTime.Year = aDate.GetYear();
419 :
420 0 : rValue = aDateTime;
421 : }
422 : else
423 0 : rValue.setNull();
424 0 : break;
425 : } // switch (nType)
426 0 : }
427 :
428 : // rValue.setTypeKind(nType);
429 0 : }
430 :
431 :
432 :
433 0 : static OUString lcl_GetColumnStr( sal_Int32 nColumn )
434 : {
435 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::lcl_GetColumnStr" );
436 0 : if ( nColumn < 26 )
437 0 : return OUString( (sal_Unicode) ( 'A' + nColumn ) );
438 : else
439 : {
440 0 : OUStringBuffer aBuffer(2);
441 0 : aBuffer.setLength( 2 );
442 0 : aBuffer[0] = (sal_Unicode) ( 'A' + ( nColumn / 26 ) - 1 );
443 0 : aBuffer[1] = (sal_Unicode) ( 'A' + ( nColumn % 26 ) );
444 0 : return aBuffer.makeStringAndClear();
445 : }
446 : }
447 :
448 0 : void OCalcTable::fillColumns()
449 : {
450 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::fillColumns" );
451 0 : if ( !m_xSheet.is() )
452 0 : throw SQLException();
453 :
454 0 : OUString aTypeName;
455 0 : ::comphelper::UStringMixEqual aCase(m_pConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers());
456 0 : const sal_Bool bStoresMixedCaseQuotedIdentifiers = getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers();
457 :
458 0 : for (sal_Int32 i = 0; i < m_nDataCols; i++)
459 : {
460 0 : OUString aColumnName;
461 0 : sal_Int32 eType = DataType::OTHER;
462 0 : sal_Bool bCurrency = sal_False;
463 :
464 : lcl_GetColumnInfo( m_xSheet, m_xFormats, m_nStartCol + i, m_nStartRow, m_bHasHeaders,
465 0 : aColumnName, eType, bCurrency );
466 :
467 0 : if ( aColumnName.isEmpty() )
468 0 : aColumnName = lcl_GetColumnStr( i );
469 :
470 0 : sal_Int32 nPrecision = 0; //! ...
471 0 : sal_Int32 nDecimals = 0; //! ...
472 :
473 0 : switch ( eType )
474 : {
475 : case DataType::VARCHAR:
476 : {
477 0 : static const OUString s_sType("VARCHAR");
478 0 : aTypeName = s_sType;
479 : }
480 0 : break;
481 : case DataType::DECIMAL:
482 0 : aTypeName = "DECIMAL";
483 0 : break;
484 : case DataType::BIT:
485 0 : aTypeName = "BOOL";
486 0 : break;
487 : case DataType::DATE:
488 0 : aTypeName = "DATE";
489 0 : break;
490 : case DataType::TIME:
491 0 : aTypeName = "TIME";
492 0 : break;
493 : case DataType::TIMESTAMP:
494 0 : aTypeName = "TIMESTAMP";
495 0 : break;
496 : default:
497 : SAL_WARN( "connectivity.drivers","missing type name");
498 0 : aTypeName = "";
499 : }
500 :
501 : // check if the column name already exists
502 0 : OUString aAlias = aColumnName;
503 0 : OSQLColumns::Vector::const_iterator aFind = connectivity::find(m_aColumns->get().begin(),m_aColumns->get().end(),aAlias,aCase);
504 0 : sal_Int32 nExprCnt = 0;
505 0 : while(aFind != m_aColumns->get().end())
506 : {
507 0 : (aAlias = aColumnName) += OUString::number(++nExprCnt);
508 0 : aFind = connectivity::find(m_aColumns->get().begin(),m_aColumns->get().end(),aAlias,aCase);
509 : }
510 :
511 : sdbcx::OColumn* pColumn = new sdbcx::OColumn( aAlias, aTypeName, OUString(),OUString(),
512 : ColumnValue::NULLABLE, nPrecision, nDecimals,
513 : eType, false, false, bCurrency,
514 : bStoresMixedCaseQuotedIdentifiers,
515 0 : m_CatalogName, getSchema(), getName());
516 0 : Reference< XPropertySet> xCol = pColumn;
517 0 : m_aColumns->get().push_back(xCol);
518 0 : m_aTypes.push_back(eType);
519 0 : m_aPrecisions.push_back(nPrecision);
520 0 : m_aScales.push_back(nDecimals);
521 0 : }
522 0 : }
523 :
524 :
525 0 : OCalcTable::OCalcTable(sdbcx::OCollection* _pTables,OCalcConnection* _pConnection,
526 : const OUString& _Name,
527 : const OUString& _Type,
528 : const OUString& _Description ,
529 : const OUString& _SchemaName,
530 : const OUString& _CatalogName
531 : ) : OCalcTable_BASE(_pTables,_pConnection,_Name,
532 : _Type,
533 : _Description,
534 : _SchemaName,
535 : _CatalogName)
536 : ,m_pConnection(_pConnection)
537 : ,m_nStartCol(0)
538 : ,m_nStartRow(0)
539 : ,m_nDataCols(0)
540 : ,m_nDataRows(0)
541 : ,m_bHasHeaders(sal_False)
542 0 : ,m_aNullDate(::Date::EMPTY)
543 : {
544 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::OCalcTable" );
545 0 : }
546 :
547 0 : void OCalcTable::construct()
548 : {
549 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::construct" );
550 : // get sheet object
551 0 : Reference< XSpreadsheetDocument> xDoc = m_pConnection->acquireDoc();
552 0 : if (xDoc.is())
553 : {
554 0 : Reference<XSpreadsheets> xSheets = xDoc->getSheets();
555 0 : if ( xSheets.is() && xSheets->hasByName( m_Name ) )
556 : {
557 0 : m_xSheet.set(xSheets->getByName( m_Name ),UNO_QUERY);
558 0 : if ( m_xSheet.is() )
559 : {
560 0 : lcl_GetDataArea( m_xSheet, m_nDataCols, m_nDataRows );
561 0 : m_bHasHeaders = sal_True;
562 : // whole sheet is always assumed to include a header row
563 : }
564 : }
565 : else // no sheet -> try database range
566 : {
567 0 : Reference<XPropertySet> xDocProp( xDoc, UNO_QUERY );
568 0 : if ( xDocProp.is() )
569 : {
570 0 : Reference<XDatabaseRanges> xRanges(xDocProp->getPropertyValue("DatabaseRanges"),UNO_QUERY);
571 :
572 0 : if ( xRanges.is() && xRanges->hasByName( m_Name ) )
573 : {
574 0 : Reference<XDatabaseRange> xDBRange(xRanges->getByName( m_Name ),UNO_QUERY);
575 0 : Reference<XCellRangeReferrer> xRefer( xDBRange, UNO_QUERY );
576 0 : if ( xRefer.is() )
577 : {
578 : // Header flag is always stored with database range
579 : // Get flag from FilterDescriptor
580 :
581 0 : sal_Bool bRangeHeader = sal_True;
582 0 : Reference<XPropertySet> xFiltProp( xDBRange->getFilterDescriptor(), UNO_QUERY );
583 0 : if ( xFiltProp.is() )
584 0 : xFiltProp->getPropertyValue("ContainsHeader") >>= bRangeHeader;
585 :
586 0 : Reference<XSheetCellRange> xSheetRange( xRefer->getReferredCells(), UNO_QUERY );
587 0 : Reference<XCellRangeAddressable> xAddr( xSheetRange, UNO_QUERY );
588 0 : if ( xSheetRange.is() && xAddr.is() )
589 : {
590 0 : m_xSheet = xSheetRange->getSpreadsheet();
591 0 : CellRangeAddress aRangeAddr = xAddr->getRangeAddress();
592 0 : m_nStartCol = aRangeAddr.StartColumn;
593 0 : m_nStartRow = aRangeAddr.StartRow;
594 0 : m_nDataCols = aRangeAddr.EndColumn - m_nStartCol + 1;
595 : // m_nDataRows is excluding header row
596 0 : m_nDataRows = aRangeAddr.EndRow - m_nStartRow;
597 0 : if ( !bRangeHeader )
598 : {
599 : // m_nDataRows counts the whole range
600 0 : m_nDataRows += 1;
601 : }
602 :
603 0 : m_bHasHeaders = bRangeHeader;
604 0 : }
605 0 : }
606 0 : }
607 0 : }
608 : }
609 :
610 0 : Reference<XNumberFormatsSupplier> xSupp( xDoc, UNO_QUERY );
611 0 : if (xSupp.is())
612 0 : m_xFormats = xSupp->getNumberFormats();
613 :
614 0 : Reference<XPropertySet> xProp( xDoc, UNO_QUERY );
615 0 : if (xProp.is())
616 : {
617 0 : ::com::sun::star::util::Date aDateStruct;
618 0 : if ( xProp->getPropertyValue("NullDate") >>= aDateStruct )
619 0 : m_aNullDate = ::Date( aDateStruct.Day, aDateStruct.Month, aDateStruct.Year );
620 0 : }
621 : }
622 :
623 : //! default if no null date available?
624 :
625 0 : fillColumns();
626 :
627 0 : refreshColumns();
628 0 : }
629 :
630 0 : void OCalcTable::refreshColumns()
631 : {
632 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::refreshColumns" );
633 0 : ::osl::MutexGuard aGuard( m_aMutex );
634 :
635 0 : TStringVector aVector;
636 :
637 0 : OSQLColumns::Vector::const_iterator aEnd = m_aColumns->get().end();
638 0 : for(OSQLColumns::Vector::const_iterator aIter = m_aColumns->get().begin();aIter != aEnd;++aIter)
639 0 : aVector.push_back(Reference< XNamed>(*aIter,UNO_QUERY)->getName());
640 :
641 0 : if(m_pColumns)
642 0 : m_pColumns->reFill(aVector);
643 : else
644 0 : m_pColumns = new OCalcColumns(this,m_aMutex,aVector);
645 0 : }
646 :
647 0 : void OCalcTable::refreshIndexes()
648 : {
649 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::refreshIndexes" );
650 : // Calc table has no index
651 0 : }
652 :
653 :
654 0 : void SAL_CALL OCalcTable::disposing(void)
655 : {
656 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::disposing" );
657 0 : OFileTable::disposing();
658 0 : ::osl::MutexGuard aGuard(m_aMutex);
659 0 : m_aColumns = NULL;
660 0 : if ( m_pConnection )
661 0 : m_pConnection->releaseDoc();
662 0 : m_pConnection = NULL;
663 :
664 0 : }
665 :
666 0 : Sequence< Type > SAL_CALL OCalcTable::getTypes( ) throw(RuntimeException, std::exception)
667 : {
668 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::getTypes" );
669 0 : Sequence< Type > aTypes = OTable_TYPEDEF::getTypes();
670 0 : ::std::vector<Type> aOwnTypes;
671 0 : aOwnTypes.reserve(aTypes.getLength());
672 :
673 0 : const Type* pBegin = aTypes.getConstArray();
674 0 : const Type* pEnd = pBegin + aTypes.getLength();
675 0 : for(;pBegin != pEnd;++pBegin)
676 : {
677 0 : if(!( *pBegin == ::getCppuType((const Reference<XKeysSupplier>*)0) ||
678 0 : *pBegin == ::getCppuType((const Reference<XIndexesSupplier>*)0) ||
679 0 : *pBegin == ::getCppuType((const Reference<XRename>*)0) ||
680 0 : *pBegin == ::getCppuType((const Reference<XAlterTable>*)0) ||
681 0 : *pBegin == ::getCppuType((const Reference<XDataDescriptorFactory>*)0)))
682 0 : aOwnTypes.push_back(*pBegin);
683 : }
684 0 : aOwnTypes.push_back(::getCppuType( (const Reference< ::com::sun::star::lang::XUnoTunnel > *)0 ));
685 :
686 0 : const Type* pAttrs = aOwnTypes.empty() ? 0 : &aOwnTypes[0];
687 0 : return Sequence< Type >(pAttrs, aOwnTypes.size());
688 : }
689 :
690 :
691 0 : Any SAL_CALL OCalcTable::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
692 : {
693 0 : if( rType == ::getCppuType((const Reference<XKeysSupplier>*)0) ||
694 0 : rType == ::getCppuType((const Reference<XIndexesSupplier>*)0) ||
695 0 : rType == ::getCppuType((const Reference<XRename>*)0) ||
696 0 : rType == ::getCppuType((const Reference<XAlterTable>*)0) ||
697 0 : rType == ::getCppuType((const Reference<XDataDescriptorFactory>*)0))
698 0 : return Any();
699 :
700 0 : const Any aRet = ::cppu::queryInterface(rType,static_cast< ::com::sun::star::lang::XUnoTunnel*> (this));
701 0 : return aRet.hasValue() ? aRet : OTable_TYPEDEF::queryInterface(rType);
702 : }
703 :
704 :
705 0 : Sequence< sal_Int8 > OCalcTable::getUnoTunnelImplementationId()
706 : {
707 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::getUnoTunnelImplementationId" );
708 : static ::cppu::OImplementationId * pId = 0;
709 0 : if (! pId)
710 : {
711 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
712 0 : if (! pId)
713 : {
714 0 : static ::cppu::OImplementationId aId;
715 0 : pId = &aId;
716 0 : }
717 : }
718 0 : return pId->getImplementationId();
719 : }
720 :
721 : // com::sun::star::lang::XUnoTunnel
722 :
723 0 : sal_Int64 OCalcTable::getSomething( const Sequence< sal_Int8 > & rId ) throw (RuntimeException, std::exception)
724 : {
725 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::getSomething" );
726 0 : return (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
727 : ? reinterpret_cast< sal_Int64 >( this )
728 0 : : OCalcTable_BASE::getSomething(rId);
729 : }
730 :
731 0 : sal_Int32 OCalcTable::getCurrentLastPos() const
732 : {
733 : //SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::getCurrentLastPos" );
734 0 : return m_nDataRows;
735 : }
736 :
737 0 : sal_Bool OCalcTable::seekRow(IResultSetHelper::Movement eCursorPosition, sal_Int32 nOffset, sal_Int32& nCurPos)
738 : {
739 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::seekRow" );
740 :
741 : // prepare positioning:
742 :
743 0 : sal_uInt32 nNumberOfRecords = m_nDataRows;
744 0 : sal_uInt32 nTempPos = m_nFilePos;
745 0 : m_nFilePos = nCurPos;
746 :
747 0 : switch(eCursorPosition)
748 : {
749 : case IResultSetHelper::NEXT:
750 0 : m_nFilePos++;
751 0 : break;
752 : case IResultSetHelper::PRIOR:
753 0 : if (m_nFilePos > 0)
754 0 : m_nFilePos--;
755 0 : break;
756 : case IResultSetHelper::FIRST:
757 0 : m_nFilePos = 1;
758 0 : break;
759 : case IResultSetHelper::LAST:
760 0 : m_nFilePos = nNumberOfRecords;
761 0 : break;
762 : case IResultSetHelper::RELATIVE:
763 0 : m_nFilePos = (((sal_Int32)m_nFilePos) + nOffset < 0) ? 0L
764 0 : : (sal_uInt32)(((sal_Int32)m_nFilePos) + nOffset);
765 0 : break;
766 : case IResultSetHelper::ABSOLUTE:
767 : case IResultSetHelper::BOOKMARK:
768 0 : m_nFilePos = (sal_uInt32)nOffset;
769 0 : break;
770 : }
771 :
772 0 : if (m_nFilePos > (sal_Int32)nNumberOfRecords)
773 0 : m_nFilePos = (sal_Int32)nNumberOfRecords + 1;
774 :
775 0 : if (m_nFilePos == 0 || m_nFilePos == (sal_Int32)nNumberOfRecords + 1)
776 : goto Error;
777 : else
778 : {
779 : //! read buffer / setup row object etc?
780 : }
781 0 : goto End;
782 :
783 : Error:
784 0 : switch(eCursorPosition)
785 : {
786 : case IResultSetHelper::PRIOR:
787 : case IResultSetHelper::FIRST:
788 0 : m_nFilePos = 0;
789 0 : break;
790 : case IResultSetHelper::LAST:
791 : case IResultSetHelper::NEXT:
792 : case IResultSetHelper::ABSOLUTE:
793 : case IResultSetHelper::RELATIVE:
794 0 : if (nOffset > 0)
795 0 : m_nFilePos = nNumberOfRecords + 1;
796 0 : else if (nOffset < 0)
797 0 : m_nFilePos = 0;
798 0 : break;
799 : case IResultSetHelper::BOOKMARK:
800 0 : m_nFilePos = nTempPos; // previous position
801 : }
802 : // aStatus.Set(SDB_STAT_NO_DATA_FOUND);
803 0 : return sal_False;
804 :
805 : End:
806 0 : nCurPos = m_nFilePos;
807 0 : return sal_True;
808 : }
809 :
810 0 : sal_Bool OCalcTable::fetchRow( OValueRefRow& _rRow, const OSQLColumns & _rCols,
811 : sal_Bool _bUseTableDefs, sal_Bool bRetrieveData )
812 : {
813 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::fetchRow" );
814 : // read the bookmark
815 :
816 0 : sal_Bool bIsCurRecordDeleted = sal_False;
817 0 : _rRow->setDeleted(bIsCurRecordDeleted);
818 0 : *(_rRow->get())[0] = m_nFilePos;
819 :
820 0 : if (!bRetrieveData)
821 0 : return sal_True;
822 :
823 : // fields
824 :
825 0 : OSQLColumns::Vector::const_iterator aIter = _rCols.get().begin();
826 0 : OSQLColumns::Vector::const_iterator aEnd = _rCols.get().end();
827 0 : const OValueRefVector::Vector::size_type nCount = _rRow->get().size();
828 0 : for (OValueRefVector::Vector::size_type i = 1; aIter != aEnd && i < nCount;
829 : ++aIter, i++)
830 : {
831 0 : if ( (_rRow->get())[i]->isBound() )
832 : {
833 0 : sal_Int32 nType = 0;
834 0 : if ( _bUseTableDefs )
835 0 : nType = m_aTypes[i-1];
836 : else
837 0 : (*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
838 :
839 :
840 0 : lcl_SetValue( (_rRow->get())[i]->get(), m_xSheet, m_nStartCol, m_nStartRow, m_bHasHeaders,
841 0 : m_aNullDate, m_nFilePos, i, nType );
842 : }
843 : }
844 0 : return sal_True;
845 : }
846 :
847 0 : void OCalcTable::FileClose()
848 : {
849 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcTable::FileClose" );
850 0 : ::osl::MutexGuard aGuard(m_aMutex);
851 :
852 0 : OCalcTable_BASE::FileClose();
853 0 : }
854 :
855 :
856 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|