Branch data 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 : 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 : 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 : 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 : 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 : 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 : 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 : 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 : ::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 : 0 : bStoresMixedCaseQuotedIdentifiers);
518 : 0 : Reference< XPropertySet> xCol = pColumn;
519 : 0 : m_aColumns->get().push_back(xCol);
520 : 0 : m_aTypes.push_back(eType);
521 : 0 : m_aPrecisions.push_back(nPrecision);
522 : 0 : m_aScales.push_back(nDecimals);
523 : 0 : }
524 : 0 : }
525 : :
526 : : // -------------------------------------------------------------------------
527 : 0 : OCalcTable::OCalcTable(sdbcx::OCollection* _pTables,OCalcConnection* _pConnection,
528 : : const ::rtl::OUString& _Name,
529 : : const ::rtl::OUString& _Type,
530 : : const ::rtl::OUString& _Description ,
531 : : const ::rtl::OUString& _SchemaName,
532 : : const ::rtl::OUString& _CatalogName
533 : : ) : OCalcTable_BASE(_pTables,_pConnection,_Name,
534 : : _Type,
535 : : _Description,
536 : : _SchemaName,
537 : : _CatalogName)
538 : : ,m_pConnection(_pConnection)
539 : : ,m_nStartCol(0)
540 : : ,m_nStartRow(0)
541 : : ,m_nDataCols(0)
542 : : ,m_nDataRows(0)
543 : : ,m_bHasHeaders(sal_False)
544 : 0 : ,m_aNullDate(::Date::EMPTY)
545 : : {
546 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::OCalcTable" );
547 : 0 : }
548 : : // -----------------------------------------------------------------------------
549 : 0 : void OCalcTable::construct()
550 : : {
551 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::construct" );
552 : : // get sheet object
553 : 0 : Reference< XSpreadsheetDocument> xDoc = m_pConnection->acquireDoc();
554 : 0 : if (xDoc.is())
555 : : {
556 : 0 : Reference<XSpreadsheets> xSheets = xDoc->getSheets();
557 : 0 : if ( xSheets.is() && xSheets->hasByName( m_Name ) )
558 : : {
559 : 0 : m_xSheet.set(xSheets->getByName( m_Name ),UNO_QUERY);
560 : 0 : if ( m_xSheet.is() )
561 : : {
562 : 0 : lcl_GetDataArea( m_xSheet, m_nDataCols, m_nDataRows );
563 : 0 : m_bHasHeaders = sal_True;
564 : : // whole sheet is always assumed to include a header row
565 : : }
566 : : }
567 : : else // no sheet -> try database range
568 : : {
569 : 0 : Reference<XPropertySet> xDocProp( xDoc, UNO_QUERY );
570 : 0 : if ( xDocProp.is() )
571 : : {
572 : 0 : Reference<XDatabaseRanges> xRanges(xDocProp->getPropertyValue( ::rtl::OUString("DatabaseRanges") ),UNO_QUERY);
573 : :
574 : 0 : if ( xRanges.is() && xRanges->hasByName( m_Name ) )
575 : : {
576 : 0 : Reference<XDatabaseRange> xDBRange(xRanges->getByName( m_Name ),UNO_QUERY);
577 : 0 : Reference<XCellRangeReferrer> xRefer( xDBRange, UNO_QUERY );
578 : 0 : if ( xRefer.is() )
579 : : {
580 : : // Header flag is always stored with database range
581 : : // Get flag from FilterDescriptor
582 : :
583 : 0 : sal_Bool bRangeHeader = sal_True;
584 : 0 : Reference<XPropertySet> xFiltProp( xDBRange->getFilterDescriptor(), UNO_QUERY );
585 : 0 : if ( xFiltProp.is() )
586 : 0 : xFiltProp->getPropertyValue(::rtl::OUString("ContainsHeader")) >>= bRangeHeader;
587 : :
588 : 0 : Reference<XSheetCellRange> xSheetRange( xRefer->getReferredCells(), UNO_QUERY );
589 : 0 : Reference<XCellRangeAddressable> xAddr( xSheetRange, UNO_QUERY );
590 : 0 : if ( xSheetRange.is() && xAddr.is() )
591 : : {
592 : 0 : m_xSheet = xSheetRange->getSpreadsheet();
593 : 0 : CellRangeAddress aRangeAddr = xAddr->getRangeAddress();
594 : 0 : m_nStartCol = aRangeAddr.StartColumn;
595 : 0 : m_nStartRow = aRangeAddr.StartRow;
596 : 0 : m_nDataCols = aRangeAddr.EndColumn - m_nStartCol + 1;
597 : : // m_nDataRows is excluding header row
598 : 0 : m_nDataRows = aRangeAddr.EndRow - m_nStartRow;
599 : 0 : if ( !bRangeHeader )
600 : : {
601 : : // m_nDataRows counts the whole range
602 : 0 : m_nDataRows += 1;
603 : : }
604 : :
605 : 0 : m_bHasHeaders = bRangeHeader;
606 : 0 : }
607 : 0 : }
608 : 0 : }
609 : 0 : }
610 : : }
611 : :
612 : 0 : Reference<XNumberFormatsSupplier> xSupp( xDoc, UNO_QUERY );
613 : 0 : if (xSupp.is())
614 : 0 : m_xFormats = xSupp->getNumberFormats();
615 : :
616 : 0 : Reference<XPropertySet> xProp( xDoc, UNO_QUERY );
617 : 0 : if (xProp.is())
618 : : {
619 : 0 : ::com::sun::star::util::Date aDateStruct;
620 : 0 : if ( xProp->getPropertyValue( ::rtl::OUString("NullDate") ) >>= aDateStruct )
621 : 0 : m_aNullDate = ::Date( aDateStruct.Day, aDateStruct.Month, aDateStruct.Year );
622 : 0 : }
623 : : }
624 : :
625 : : //! default if no null date available?
626 : :
627 : 0 : fillColumns();
628 : :
629 : 0 : refreshColumns();
630 : 0 : }
631 : : // -------------------------------------------------------------------------
632 : 0 : void OCalcTable::refreshColumns()
633 : : {
634 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::refreshColumns" );
635 : 0 : ::osl::MutexGuard aGuard( m_aMutex );
636 : :
637 : 0 : TStringVector aVector;
638 : :
639 : 0 : OSQLColumns::Vector::const_iterator aEnd = m_aColumns->get().end();
640 : 0 : for(OSQLColumns::Vector::const_iterator aIter = m_aColumns->get().begin();aIter != aEnd;++aIter)
641 : 0 : aVector.push_back(Reference< XNamed>(*aIter,UNO_QUERY)->getName());
642 : :
643 : 0 : if(m_pColumns)
644 : 0 : m_pColumns->reFill(aVector);
645 : : else
646 : 0 : m_pColumns = new OCalcColumns(this,m_aMutex,aVector);
647 : 0 : }
648 : : // -------------------------------------------------------------------------
649 : 0 : void OCalcTable::refreshIndexes()
650 : : {
651 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::refreshIndexes" );
652 : : // Calc table has no index
653 : 0 : }
654 : :
655 : : // -------------------------------------------------------------------------
656 : 0 : void SAL_CALL OCalcTable::disposing(void)
657 : : {
658 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::disposing" );
659 : 0 : OFileTable::disposing();
660 : 0 : ::osl::MutexGuard aGuard(m_aMutex);
661 : 0 : m_aColumns = NULL;
662 : 0 : if ( m_pConnection )
663 : 0 : m_pConnection->releaseDoc();
664 : 0 : m_pConnection = NULL;
665 : :
666 : 0 : }
667 : : // -------------------------------------------------------------------------
668 : 0 : Sequence< Type > SAL_CALL OCalcTable::getTypes( ) throw(RuntimeException)
669 : : {
670 : : //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::getTypes" );
671 : 0 : Sequence< Type > aTypes = OTable_TYPEDEF::getTypes();
672 : 0 : ::std::vector<Type> aOwnTypes;
673 : 0 : aOwnTypes.reserve(aTypes.getLength());
674 : :
675 : 0 : const Type* pBegin = aTypes.getConstArray();
676 : 0 : const Type* pEnd = pBegin + aTypes.getLength();
677 : 0 : for(;pBegin != pEnd;++pBegin)
678 : : {
679 : 0 : if(!( *pBegin == ::getCppuType((const Reference<XKeysSupplier>*)0) ||
680 : 0 : *pBegin == ::getCppuType((const Reference<XIndexesSupplier>*)0) ||
681 : 0 : *pBegin == ::getCppuType((const Reference<XRename>*)0) ||
682 : 0 : *pBegin == ::getCppuType((const Reference<XAlterTable>*)0) ||
683 : 0 : *pBegin == ::getCppuType((const Reference<XDataDescriptorFactory>*)0)))
684 : 0 : aOwnTypes.push_back(*pBegin);
685 : : }
686 : 0 : aOwnTypes.push_back(::getCppuType( (const Reference< ::com::sun::star::lang::XUnoTunnel > *)0 ));
687 : :
688 : 0 : const Type* pAttrs = aOwnTypes.empty() ? 0 : &aOwnTypes[0];
689 : 0 : return Sequence< Type >(pAttrs, aOwnTypes.size());
690 : : }
691 : :
692 : : // -------------------------------------------------------------------------
693 : 0 : Any SAL_CALL OCalcTable::queryInterface( const Type & rType ) throw(RuntimeException)
694 : : {
695 : 0 : if( rType == ::getCppuType((const Reference<XKeysSupplier>*)0) ||
696 : 0 : rType == ::getCppuType((const Reference<XIndexesSupplier>*)0) ||
697 : 0 : rType == ::getCppuType((const Reference<XRename>*)0) ||
698 : 0 : rType == ::getCppuType((const Reference<XAlterTable>*)0) ||
699 : 0 : rType == ::getCppuType((const Reference<XDataDescriptorFactory>*)0))
700 : 0 : return Any();
701 : :
702 : 0 : const Any aRet = ::cppu::queryInterface(rType,static_cast< ::com::sun::star::lang::XUnoTunnel*> (this));
703 : 0 : return aRet.hasValue() ? aRet : OTable_TYPEDEF::queryInterface(rType);
704 : : }
705 : :
706 : : //--------------------------------------------------------------------------
707 : 0 : Sequence< sal_Int8 > OCalcTable::getUnoTunnelImplementationId()
708 : : {
709 : : //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::getUnoTunnelImplementationId" );
710 : : static ::cppu::OImplementationId * pId = 0;
711 : 0 : if (! pId)
712 : : {
713 : 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
714 : 0 : if (! pId)
715 : : {
716 : 0 : static ::cppu::OImplementationId aId;
717 : 0 : pId = &aId;
718 : 0 : }
719 : : }
720 : 0 : return pId->getImplementationId();
721 : : }
722 : :
723 : : // com::sun::star::lang::XUnoTunnel
724 : : //------------------------------------------------------------------
725 : 0 : sal_Int64 OCalcTable::getSomething( const Sequence< sal_Int8 > & rId ) throw (RuntimeException)
726 : : {
727 : : //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::getSomething" );
728 : 0 : return (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
729 : : ? reinterpret_cast< sal_Int64 >( this )
730 : 0 : : OCalcTable_BASE::getSomething(rId);
731 : : }
732 : : //------------------------------------------------------------------
733 : 0 : sal_Int32 OCalcTable::getCurrentLastPos() const
734 : : {
735 : : //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::getCurrentLastPos" );
736 : 0 : return m_nDataRows;
737 : : }
738 : : //------------------------------------------------------------------
739 : 0 : sal_Bool OCalcTable::seekRow(IResultSetHelper::Movement eCursorPosition, sal_Int32 nOffset, sal_Int32& nCurPos)
740 : : {
741 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::seekRow" );
742 : : // ----------------------------------------------------------
743 : : // prepare positioning:
744 : :
745 : 0 : sal_uInt32 nNumberOfRecords = m_nDataRows;
746 : 0 : sal_uInt32 nTempPos = m_nFilePos;
747 : 0 : m_nFilePos = nCurPos;
748 : :
749 : 0 : switch(eCursorPosition)
750 : : {
751 : : case IResultSetHelper::NEXT:
752 : 0 : m_nFilePos++;
753 : 0 : break;
754 : : case IResultSetHelper::PRIOR:
755 : 0 : if (m_nFilePos > 0)
756 : 0 : m_nFilePos--;
757 : 0 : break;
758 : : case IResultSetHelper::FIRST:
759 : 0 : m_nFilePos = 1;
760 : 0 : break;
761 : : case IResultSetHelper::LAST:
762 : 0 : m_nFilePos = nNumberOfRecords;
763 : 0 : break;
764 : : case IResultSetHelper::RELATIVE:
765 : : m_nFilePos = (((sal_Int32)m_nFilePos) + nOffset < 0) ? 0L
766 : 0 : : (sal_uInt32)(((sal_Int32)m_nFilePos) + nOffset);
767 : 0 : break;
768 : : case IResultSetHelper::ABSOLUTE:
769 : : case IResultSetHelper::BOOKMARK:
770 : 0 : m_nFilePos = (sal_uInt32)nOffset;
771 : 0 : break;
772 : : }
773 : :
774 : 0 : if (m_nFilePos > (sal_Int32)nNumberOfRecords)
775 : 0 : m_nFilePos = (sal_Int32)nNumberOfRecords + 1;
776 : :
777 : 0 : if (m_nFilePos == 0 || m_nFilePos == (sal_Int32)nNumberOfRecords + 1)
778 : : goto Error;
779 : : else
780 : : {
781 : : //! read buffer / setup row object etc?
782 : : }
783 : 0 : goto End;
784 : :
785 : : Error:
786 : 0 : switch(eCursorPosition)
787 : : {
788 : : case IResultSetHelper::PRIOR:
789 : : case IResultSetHelper::FIRST:
790 : 0 : m_nFilePos = 0;
791 : 0 : break;
792 : : case IResultSetHelper::LAST:
793 : : case IResultSetHelper::NEXT:
794 : : case IResultSetHelper::ABSOLUTE:
795 : : case IResultSetHelper::RELATIVE:
796 : 0 : if (nOffset > 0)
797 : 0 : m_nFilePos = nNumberOfRecords + 1;
798 : 0 : else if (nOffset < 0)
799 : 0 : m_nFilePos = 0;
800 : 0 : break;
801 : : case IResultSetHelper::BOOKMARK:
802 : 0 : m_nFilePos = nTempPos; // previous position
803 : : }
804 : : // aStatus.Set(SDB_STAT_NO_DATA_FOUND);
805 : 0 : return sal_False;
806 : :
807 : : End:
808 : 0 : nCurPos = m_nFilePos;
809 : 0 : return sal_True;
810 : : }
811 : : //------------------------------------------------------------------
812 : 0 : sal_Bool OCalcTable::fetchRow( OValueRefRow& _rRow, const OSQLColumns & _rCols,
813 : : sal_Bool _bUseTableDefs, sal_Bool bRetrieveData )
814 : : {
815 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::fetchRow" );
816 : : // read the bookmark
817 : :
818 : 0 : sal_Bool bIsCurRecordDeleted = sal_False;
819 : 0 : _rRow->setDeleted(bIsCurRecordDeleted);
820 : 0 : *(_rRow->get())[0] = m_nFilePos;
821 : :
822 : 0 : if (!bRetrieveData)
823 : 0 : return sal_True;
824 : :
825 : : // fields
826 : :
827 : 0 : OSQLColumns::Vector::const_iterator aIter = _rCols.get().begin();
828 : 0 : OSQLColumns::Vector::const_iterator aEnd = _rCols.get().end();
829 : 0 : const OValueRefVector::Vector::size_type nCount = _rRow->get().size();
830 : 0 : for (OValueRefVector::Vector::size_type i = 1; aIter != aEnd && i < nCount;
831 : : ++aIter, i++)
832 : : {
833 : 0 : if ( (_rRow->get())[i]->isBound() )
834 : : {
835 : 0 : sal_Int32 nType = 0;
836 : 0 : if ( _bUseTableDefs )
837 : 0 : nType = m_aTypes[i-1];
838 : : else
839 : 0 : (*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
840 : :
841 : :
842 : 0 : lcl_SetValue( (_rRow->get())[i]->get(), m_xSheet, m_nStartCol, m_nStartRow, m_bHasHeaders,
843 : 0 : m_aNullDate, m_nFilePos, i, nType );
844 : : }
845 : : }
846 : 0 : return sal_True;
847 : : }
848 : : // -------------------------------------------------------------------------
849 : 0 : void OCalcTable::FileClose()
850 : : {
851 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcTable::FileClose" );
852 : 0 : ::osl::MutexGuard aGuard(m_aMutex);
853 : :
854 : 0 : OCalcTable_BASE::FileClose();
855 : 0 : }
856 : : // -------------------------------------------------------------------------
857 : :
858 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|