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