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