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 <ctype.h>
21 : #include "flat/ETable.hxx"
22 : #include <com/sun/star/sdbc/ColumnValue.hpp>
23 : #include <com/sun/star/sdbc/DataType.hpp>
24 : #include <com/sun/star/ucb/XContentAccess.hpp>
25 : #include <svl/converter.hxx>
26 : #include "flat/EConnection.hxx"
27 : #include "flat/EColumns.hxx"
28 : #include <osl/thread.h>
29 : #include <svl/zforlist.hxx>
30 : #include <rtl/math.hxx>
31 : #include <cppuhelper/queryinterface.hxx>
32 : #include <comphelper/extract.hxx>
33 : #include <comphelper/numbers.hxx>
34 : #include <comphelper/processfactory.hxx>
35 : #include <comphelper/sequence.hxx>
36 : #include <comphelper/string.hxx>
37 : #include <comphelper/types.hxx>
38 : #include "flat/EDriver.hxx"
39 : #include <com/sun/star/util/NumberFormat.hpp>
40 : #include <com/sun/star/util/NumberFormatter.hpp>
41 : #include <com/sun/star/util/NumberFormatsSupplier.hpp>
42 : #include <unotools/configmgr.hxx>
43 : #include <i18nlangtag/languagetag.hxx>
44 : #include <connectivity/dbconversion.hxx>
45 : #include "file/quotedstring.hxx"
46 : #include <unotools/syslocale.hxx>
47 :
48 : using namespace ::comphelper;
49 : using namespace connectivity;
50 : using namespace connectivity::flat;
51 : using namespace connectivity::file;
52 : using namespace ::cppu;
53 : using namespace utl;
54 : using namespace ::com::sun::star::uno;
55 : using namespace ::com::sun::star::ucb;
56 : using namespace ::com::sun::star::beans;
57 : using namespace ::com::sun::star::sdbcx;
58 : using namespace ::com::sun::star::sdbc;
59 : using namespace ::com::sun::star::container;
60 : using namespace ::com::sun::star::lang;
61 : using namespace ::com::sun::star::util;
62 : using std::vector;
63 : using std::lower_bound;
64 :
65 :
66 0 : void OFlatTable::fillColumns(const ::com::sun::star::lang::Locale& _aLocale)
67 : {
68 0 : m_bNeedToReadLine = true; // we overwrite m_aCurrentLine, seek the stream, ...
69 0 : m_pFileStream->Seek(0);
70 0 : m_aCurrentLine = QuotedTokenizedString();
71 0 : bool bRead = true;
72 :
73 0 : const OFlatConnection* const pConnection = getFlatConnection();
74 0 : const bool bHasHeaderLine = pConnection->isHeaderLine();
75 :
76 0 : QuotedTokenizedString aHeaderLine;
77 0 : TRowPositionInFile rowPos(0, 0);
78 0 : sal_Int32 rowNum(0);
79 0 : if ( bHasHeaderLine )
80 : {
81 0 : bRead = readLine(&rowPos.second, &rowPos.first, true);
82 0 : if(bRead)
83 0 : aHeaderLine = m_aCurrentLine;
84 : }
85 0 : setRowPos(rowNum++, rowPos);
86 :
87 : // read first row
88 0 : QuotedTokenizedString aFirstLine;
89 0 : if(bRead)
90 : {
91 0 : bRead = readLine(&rowPos.second, &rowPos.first, false);
92 0 : if(bRead)
93 0 : setRowPos(rowNum++, rowPos);
94 : }
95 :
96 0 : if ( !bHasHeaderLine || !aHeaderLine.Len())
97 : {
98 : // use first non-empty row as headerline because we need the number of columns
99 0 : while(bRead && m_aCurrentLine.Len() == 0)
100 : {
101 0 : bRead = readLine(&rowPos.second, &rowPos.first, false);
102 0 : if(bRead)
103 0 : setRowPos(rowNum++, rowPos);
104 : }
105 0 : aHeaderLine = m_aCurrentLine;
106 : }
107 : // column count
108 0 : const sal_Int32 nFieldCount = aHeaderLine.GetTokenCount(m_cFieldDelimiter,m_cStringDelimiter);
109 :
110 0 : if(!m_aColumns.is())
111 0 : m_aColumns = new OSQLColumns();
112 : else
113 0 : m_aColumns->get().clear();
114 :
115 0 : m_aTypes.clear();
116 0 : m_aPrecisions.clear();
117 0 : m_aScales.clear();
118 : // reserve some space
119 0 : m_aColumns->get().reserve(nFieldCount+1);
120 0 : m_aTypes.assign(nFieldCount+1,DataType::SQLNULL);
121 0 : m_aPrecisions.assign(nFieldCount+1,-1);
122 0 : m_aScales.assign(nFieldCount+1,-1);
123 :
124 0 : const bool bCase = m_pConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers();
125 0 : CharClass aCharClass( pConnection->getDriver()->getComponentContext(), LanguageTag( _aLocale));
126 : // read description
127 0 : const sal_Unicode cDecimalDelimiter = pConnection->getDecimalDelimiter();
128 0 : const sal_Unicode cThousandDelimiter = pConnection->getThousandDelimiter();
129 0 : ::comphelper::UStringMixEqual aCase(bCase);
130 0 : vector<OUString> aColumnNames;
131 0 : vector<OUString> m_aTypeNames;
132 0 : m_aTypeNames.resize(nFieldCount);
133 0 : const sal_Int32 nMaxRowsToScan = pConnection->getMaxRowsToScan();
134 0 : sal_Int32 nRowCount = 0;
135 :
136 0 : do
137 : {
138 0 : sal_Int32 nStartPosHeaderLine = 0; // use for efficient way to get the tokens
139 0 : sal_Int32 nStartPosFirstLine = 0; // use for efficient way to get the tokens
140 0 : sal_Int32 nStartPosFirstLine2 = 0;
141 0 : for( sal_Int32 i = 0; i < nFieldCount; i++ )
142 : {
143 0 : if ( nRowCount == 0)
144 : {
145 0 : OUString aColumnName;
146 0 : if ( bHasHeaderLine )
147 : {
148 0 : aColumnName = aHeaderLine.GetTokenSpecial(nStartPosHeaderLine,m_cFieldDelimiter,m_cStringDelimiter);
149 : }
150 0 : if ( aColumnName.isEmpty() )
151 : {
152 0 : aColumnName = "C" + OUString::number(i+1);
153 : }
154 0 : aColumnNames.push_back(aColumnName);
155 : }
156 0 : if(bRead)
157 : {
158 : impl_fillColumnInfo_nothrow(m_aCurrentLine, nStartPosFirstLine, nStartPosFirstLine2,
159 0 : m_aTypes[i], m_aPrecisions[i], m_aScales[i], m_aTypeNames[i],
160 0 : cDecimalDelimiter, cThousandDelimiter, aCharClass);
161 : }
162 : }
163 0 : ++nRowCount;
164 0 : bRead = readLine(&rowPos.second, &rowPos.first, false);
165 0 : if(bRead)
166 0 : setRowPos(rowNum++, rowPos);
167 : }
168 0 : while(nRowCount < nMaxRowsToScan && bRead);
169 :
170 0 : for( sal_Int32 i = 0; i < nFieldCount; i++ )
171 : {
172 : // check if the columname already exists
173 0 : OUString aAlias(aColumnNames[i]);
174 0 : OSQLColumns::Vector::const_iterator aFind = connectivity::find(m_aColumns->get().begin(),m_aColumns->get().end(),aAlias,aCase);
175 0 : sal_Int32 nExprCnt = 0;
176 0 : while(aFind != m_aColumns->get().end())
177 : {
178 0 : aAlias = aColumnNames[i] + OUString::number(++nExprCnt);
179 0 : aFind = connectivity::find(m_aColumns->get().begin(),m_aColumns->get().end(),aAlias,aCase);
180 : }
181 :
182 0 : sdbcx::OColumn* pColumn = new sdbcx::OColumn(aAlias,m_aTypeNames[i],OUString(),OUString(),
183 : ColumnValue::NULLABLE,
184 0 : m_aPrecisions[i],
185 0 : m_aScales[i],
186 0 : m_aTypes[i],
187 : false,
188 : false,
189 : false,
190 : bCase,
191 0 : m_CatalogName, getSchema(), getName());
192 0 : Reference< XPropertySet> xCol = pColumn;
193 0 : m_aColumns->get().push_back(xCol);
194 0 : }
195 :
196 0 : m_pFileStream->Seek(m_aRowPosToFilePos[0].second);
197 0 : }
198 :
199 0 : void OFlatTable::impl_fillColumnInfo_nothrow(QuotedTokenizedString& aFirstLine, sal_Int32& nStartPosFirstLine, sal_Int32& nStartPosFirstLine2,
200 : sal_Int32& io_nType, sal_Int32& io_nPrecisions, sal_Int32& io_nScales, OUString& o_sTypeName,
201 : const sal_Unicode cDecimalDelimiter, const sal_Unicode cThousandDelimiter, const CharClass& aCharClass)
202 : {
203 0 : if ( io_nType != DataType::VARCHAR )
204 : {
205 0 : bool bNumeric = io_nType == DataType::SQLNULL || io_nType == DataType::DOUBLE || io_nType == DataType::DECIMAL || io_nType == DataType::INTEGER;
206 0 : sal_uLong nIndex = 0;
207 :
208 0 : if ( bNumeric )
209 : {
210 : // first without fielddelimiter
211 0 : OUString aField = aFirstLine.GetTokenSpecial(nStartPosFirstLine,m_cFieldDelimiter,'\0');
212 0 : if (aField.isEmpty() ||
213 0 : (m_cStringDelimiter && m_cStringDelimiter == aField[0]))
214 : {
215 0 : bNumeric = false;
216 0 : if ( m_cStringDelimiter != '\0' )
217 0 : aField = aFirstLine.GetTokenSpecial(nStartPosFirstLine2,m_cFieldDelimiter,m_cStringDelimiter);
218 : else
219 0 : nStartPosFirstLine2 = nStartPosFirstLine;
220 : }
221 : else
222 : {
223 0 : OUString aField2;
224 0 : if ( m_cStringDelimiter != '\0' )
225 0 : aField2 = aFirstLine.GetTokenSpecial(nStartPosFirstLine2,m_cFieldDelimiter,m_cStringDelimiter);
226 : else
227 0 : aField2 = aField;
228 :
229 0 : if (aField2.isEmpty())
230 : {
231 0 : bNumeric = false;
232 : }
233 : else
234 : {
235 0 : bNumeric = true;
236 0 : sal_Int32 nDot = 0;
237 0 : sal_Int32 nDecimalDelCount = 0;
238 0 : sal_Int32 nSpaceCount = 0;
239 0 : for( sal_Int32 j = 0; j < aField2.getLength(); j++ )
240 : {
241 0 : const sal_Unicode c = aField2[j];
242 0 : if ( j == nSpaceCount && m_cFieldDelimiter != 32 && c == 32 )
243 : {
244 0 : ++nSpaceCount;
245 0 : continue;
246 : }
247 : // just digits, decimal- and thousands-delimiter?
248 0 : if ( ( !cDecimalDelimiter || c != cDecimalDelimiter ) &&
249 0 : ( !cThousandDelimiter || c != cThousandDelimiter ) &&
250 0 : !aCharClass.isDigit(aField2,j) &&
251 0 : ( j != 0 || (c != '+' && c != '-' ) ) )
252 : {
253 0 : bNumeric = false;
254 0 : break;
255 : }
256 0 : if (cDecimalDelimiter && c == cDecimalDelimiter)
257 : {
258 0 : io_nPrecisions = 15; // we have an decimal value
259 0 : io_nScales = 2;
260 0 : ++nDecimalDelCount;
261 : } // if (cDecimalDelimiter && c == cDecimalDelimiter)
262 0 : if ( c == '.' )
263 0 : ++nDot;
264 : }
265 :
266 0 : if (nDecimalDelCount > 1 || nDot > 1 ) // if there is more than one dot it isn't a number
267 0 : bNumeric = false;
268 0 : if (bNumeric && cThousandDelimiter)
269 : {
270 : // Is the delimiter correct?
271 0 : const OUString aValue = aField2.getToken(0,cDecimalDelimiter);
272 0 : for( sal_Int32 j = aValue.getLength() - 4; j >= 0; j -= 4)
273 : {
274 0 : const sal_Unicode c = aValue[j];
275 : // just digits, decimal- and thousands-delimiter?
276 0 : if (c == cThousandDelimiter && j)
277 0 : continue;
278 : else
279 : {
280 0 : bNumeric = false;
281 0 : break;
282 : }
283 0 : }
284 : }
285 :
286 : // now also check for a date field
287 0 : if (!bNumeric)
288 : {
289 : try
290 : {
291 0 : nIndex = m_xNumberFormatter->detectNumberFormat(::com::sun::star::util::NumberFormat::ALL,aField2);
292 : }
293 0 : catch(Exception&)
294 : {
295 : }
296 : }
297 0 : }
298 0 : }
299 : }
300 0 : else if ( io_nType == DataType::DATE || io_nType == DataType::TIMESTAMP || io_nType == DataType::TIME)
301 : {
302 0 : OUString aField = aFirstLine.GetTokenSpecial(nStartPosFirstLine,m_cFieldDelimiter,'\0');
303 0 : if (aField.isEmpty() ||
304 0 : (m_cStringDelimiter && m_cStringDelimiter == aField[0]))
305 : {
306 : }
307 : else
308 : {
309 0 : OUString aField2;
310 0 : if ( m_cStringDelimiter != '\0' )
311 0 : aField2 = aFirstLine.GetTokenSpecial(nStartPosFirstLine2,m_cFieldDelimiter,m_cStringDelimiter);
312 : else
313 0 : aField2 = aField;
314 0 : if (!aField2.isEmpty() )
315 : {
316 : try
317 : {
318 0 : nIndex = m_xNumberFormatter->detectNumberFormat(::com::sun::star::util::NumberFormat::ALL,aField2);
319 : }
320 0 : catch(Exception&)
321 : {
322 : }
323 0 : }
324 0 : }
325 : }
326 :
327 0 : sal_Int32 nFlags = 0;
328 0 : if (bNumeric)
329 : {
330 0 : if (cDecimalDelimiter)
331 : {
332 0 : if(io_nPrecisions)
333 : {
334 0 : io_nType = DataType::DECIMAL;
335 0 : o_sTypeName = "DECIMAL";
336 : }
337 : else
338 : {
339 0 : io_nType = DataType::DOUBLE;
340 0 : o_sTypeName = "DOUBLE";
341 : }
342 : }
343 : else
344 : {
345 0 : io_nType = DataType::INTEGER;
346 0 : io_nPrecisions = 0;
347 0 : io_nScales = 0;
348 : }
349 0 : nFlags = ColumnSearch::BASIC;
350 : }
351 : else
352 : {
353 0 : switch (comphelper::getNumberFormatType(m_xNumberFormatter,nIndex))
354 : {
355 : case css::util::NumberFormat::DATE:
356 0 : io_nType = DataType::DATE;
357 0 : o_sTypeName = "DATE";
358 0 : break;
359 : case css::util::NumberFormat::DATETIME:
360 0 : io_nType = DataType::TIMESTAMP;
361 0 : o_sTypeName = "TIMESTAMP";
362 0 : break;
363 : case css::util::NumberFormat::TIME:
364 0 : io_nType = DataType::TIME;
365 0 : o_sTypeName = "TIME";
366 0 : break;
367 : default:
368 0 : io_nType = DataType::VARCHAR;
369 0 : io_nPrecisions = 0; // nyi: Data can be longer!
370 0 : io_nScales = 0;
371 0 : o_sTypeName = "VARCHAR";
372 : };
373 0 : nFlags |= ColumnSearch::CHAR;
374 : }
375 : }
376 : else
377 : {
378 0 : OUString aField = aFirstLine.GetTokenSpecial(nStartPosFirstLine,m_cFieldDelimiter,'\0');
379 0 : if (aField.isEmpty() ||
380 0 : (m_cStringDelimiter && m_cStringDelimiter == aField[0]))
381 : {
382 0 : if ( m_cStringDelimiter != '\0' )
383 0 : aField = aFirstLine.GetTokenSpecial(nStartPosFirstLine2, m_cFieldDelimiter, m_cStringDelimiter);
384 : else
385 0 : nStartPosFirstLine2 = nStartPosFirstLine;
386 : }
387 : else
388 : {
389 0 : if ( m_cStringDelimiter != '\0' )
390 0 : aFirstLine.GetTokenSpecial(nStartPosFirstLine2, m_cFieldDelimiter, m_cStringDelimiter);
391 0 : }
392 : }
393 0 : }
394 :
395 0 : OFlatTable::OFlatTable(sdbcx::OCollection* _pTables,OFlatConnection* _pConnection,
396 : const OUString& _Name,
397 : const OUString& _Type,
398 : const OUString& _Description ,
399 : const OUString& _SchemaName,
400 : const OUString& _CatalogName
401 : ) : OFlatTable_BASE(_pTables,_pConnection,_Name,
402 : _Type,
403 : _Description,
404 : _SchemaName,
405 : _CatalogName)
406 : ,m_nRowPos(0)
407 : ,m_nMaxRowCount(0)
408 0 : ,m_cStringDelimiter(_pConnection->getStringDelimiter())
409 0 : ,m_cFieldDelimiter(_pConnection->getFieldDelimiter())
410 0 : ,m_bNeedToReadLine(false)
411 : {
412 :
413 0 : }
414 :
415 0 : void OFlatTable::construct()
416 : {
417 0 : SvtSysLocale aLocale;
418 0 : ::com::sun::star::lang::Locale aAppLocale(aLocale.GetLanguageTag().getLocale());
419 :
420 0 : Reference< XNumberFormatsSupplier > xSupplier = NumberFormatsSupplier::createWithLocale( m_pConnection->getDriver()->getComponentContext(), aAppLocale );
421 0 : m_xNumberFormatter.set( NumberFormatter::create( m_pConnection->getDriver()->getComponentContext()), UNO_QUERY_THROW);
422 0 : m_xNumberFormatter->attachNumberFormatsSupplier(xSupplier);
423 0 : Reference<XPropertySet> xProp(xSupplier->getNumberFormatSettings(),UNO_QUERY);
424 0 : xProp->getPropertyValue("NullDate") >>= m_aNullDate;
425 :
426 0 : INetURLObject aURL;
427 0 : aURL.SetURL(getEntry());
428 :
429 0 : if(aURL.getExtension() != OUString(m_pConnection->getExtension()))
430 0 : aURL.setExtension(m_pConnection->getExtension());
431 :
432 0 : OUString aFileName = aURL.GetMainURL(INetURLObject::NO_DECODE);
433 :
434 0 : m_pFileStream = createStream_simpleError( aFileName, STREAM_READWRITE | StreamMode::NOCREATE | StreamMode::SHARE_DENYWRITE);
435 :
436 0 : if(!m_pFileStream)
437 0 : m_pFileStream = createStream_simpleError( aFileName, StreamMode::READ | StreamMode::NOCREATE | StreamMode::SHARE_DENYNONE);
438 :
439 0 : if(m_pFileStream)
440 : {
441 0 : sal_uInt64 const nSize = m_pFileStream->remainingSize();
442 :
443 : // Buffersize is dependent on the file-size
444 : m_pFileStream->SetBufferSize(nSize > 1000000 ? 32768 :
445 : nSize > 100000 ? 16384 :
446 0 : nSize > 10000 ? 4096 : 1024);
447 :
448 0 : fillColumns(aAppLocale);
449 :
450 0 : refreshColumns();
451 0 : }
452 0 : }
453 :
454 0 : OUString OFlatTable::getEntry()
455 : {
456 0 : OUString sURL;
457 : try
458 : {
459 0 : Reference< XResultSet > xDir = m_pConnection->getDir()->getStaticResultSet();
460 0 : Reference< XRow> xRow(xDir,UNO_QUERY);
461 0 : OUString sName;
462 0 : OUString sExt;
463 :
464 0 : INetURLObject aURL;
465 0 : xDir->beforeFirst();
466 : static const char s_sSeparator[] = "/";
467 0 : while(xDir->next())
468 : {
469 0 : sName = xRow->getString(1);
470 0 : aURL.SetSmartProtocol(INetProtocol::File);
471 0 : OUString sUrl = m_pConnection->getURL() + s_sSeparator + sName;
472 0 : aURL.SetSmartURL( sUrl );
473 :
474 : // cut the extension
475 0 : sExt = aURL.getExtension();
476 :
477 : // name and extension have to coincide
478 0 : if ( m_pConnection->matchesExtension( sExt ) )
479 : {
480 0 : if ( !sExt.isEmpty() )
481 0 : sName = sName.replaceAt(sName.getLength() - (sExt.getLength() + 1), sExt.getLength()+1, OUString());
482 0 : if ( sName == m_Name )
483 : {
484 0 : Reference< XContentAccess > xContentAccess( xDir, UNO_QUERY );
485 0 : sURL = xContentAccess->queryContentIdentifierString();
486 0 : break;
487 : }
488 : }
489 0 : }
490 0 : xDir->beforeFirst(); // move back to before first record
491 : }
492 0 : catch(const Exception&)
493 : {
494 : OSL_ASSERT(false);
495 : }
496 0 : return sURL;
497 : }
498 :
499 0 : void OFlatTable::refreshColumns()
500 : {
501 0 : ::osl::MutexGuard aGuard( m_aMutex );
502 :
503 0 : TStringVector aVector;
504 0 : aVector.reserve(m_aColumns->get().size());
505 :
506 0 : for(OSQLColumns::Vector::const_iterator aIter = m_aColumns->get().begin();aIter != m_aColumns->get().end();++aIter)
507 0 : aVector.push_back(Reference< XNamed>(*aIter,UNO_QUERY)->getName());
508 :
509 0 : if(m_pColumns)
510 0 : m_pColumns->reFill(aVector);
511 : else
512 0 : m_pColumns = new OFlatColumns(this,m_aMutex,aVector);
513 0 : }
514 :
515 :
516 0 : void SAL_CALL OFlatTable::disposing()
517 : {
518 0 : OFileTable::disposing();
519 0 : ::osl::MutexGuard aGuard(m_aMutex);
520 0 : m_aColumns = NULL;
521 0 : }
522 :
523 0 : Sequence< Type > SAL_CALL OFlatTable::getTypes( ) throw(RuntimeException, std::exception)
524 : {
525 0 : Sequence< Type > aTypes = OTable_TYPEDEF::getTypes();
526 0 : vector<Type> aOwnTypes;
527 0 : aOwnTypes.reserve(aTypes.getLength());
528 0 : const Type* pBegin = aTypes.getConstArray();
529 0 : const Type* pEnd = pBegin + aTypes.getLength();
530 0 : for(;pBegin != pEnd;++pBegin)
531 : {
532 0 : if(!(*pBegin == cppu::UnoType<XKeysSupplier>::get()||
533 0 : *pBegin == cppu::UnoType<XRename>::get()||
534 0 : *pBegin == cppu::UnoType<XIndexesSupplier>::get()||
535 0 : *pBegin == cppu::UnoType<XAlterTable>::get()||
536 0 : *pBegin == cppu::UnoType<XDataDescriptorFactory>::get()))
537 : {
538 0 : aOwnTypes.push_back(*pBegin);
539 : }
540 : }
541 0 : return Sequence< Type >(aOwnTypes.data(), aOwnTypes.size());
542 : }
543 :
544 :
545 0 : Any SAL_CALL OFlatTable::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
546 : {
547 0 : if( rType == cppu::UnoType<XKeysSupplier>::get()||
548 0 : rType == cppu::UnoType<XIndexesSupplier>::get()||
549 0 : rType == cppu::UnoType<XRename>::get()||
550 0 : rType == cppu::UnoType<XAlterTable>::get()||
551 0 : rType == cppu::UnoType<XDataDescriptorFactory>::get())
552 0 : return Any();
553 :
554 0 : Any aRet = OTable_TYPEDEF::queryInterface(rType);
555 0 : return aRet.hasValue() ? aRet : ::cppu::queryInterface(rType,static_cast< ::com::sun::star::lang::XUnoTunnel*> (this));
556 : }
557 :
558 :
559 0 : Sequence< sal_Int8 > OFlatTable::getUnoTunnelImplementationId()
560 : {
561 : static ::cppu::OImplementationId * pId = 0;
562 0 : if (! pId)
563 : {
564 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
565 0 : if (! pId)
566 : {
567 0 : static ::cppu::OImplementationId aId;
568 0 : pId = &aId;
569 0 : }
570 : }
571 0 : return pId->getImplementationId();
572 : }
573 :
574 : // com::sun::star::lang::XUnoTunnel
575 :
576 0 : sal_Int64 OFlatTable::getSomething( const Sequence< sal_Int8 > & rId ) throw (RuntimeException, std::exception)
577 : {
578 0 : return (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
579 : ? reinterpret_cast< sal_Int64 >( this )
580 0 : : OFlatTable_BASE::getSomething(rId);
581 : }
582 :
583 0 : bool OFlatTable::fetchRow(OValueRefRow& _rRow, const OSQLColumns & _rCols, bool bIsTable, bool bRetrieveData)
584 : {
585 0 : *(_rRow->get())[0] = m_nFilePos;
586 :
587 0 : if (!bRetrieveData)
588 0 : return true;
589 :
590 0 : bool result = false;
591 0 : if ( m_bNeedToReadLine )
592 : {
593 0 : m_pFileStream->Seek(m_nFilePos);
594 0 : TRowPositionInFile rowPos(0, 0);
595 0 : if(readLine(&rowPos.second, &rowPos.first))
596 : {
597 0 : setRowPos(m_nRowPos, rowPos);
598 0 : m_bNeedToReadLine = false;
599 0 : result = true;
600 : }
601 : // else let run through so that we set _rRow to all NULL
602 : }
603 :
604 0 : const OFlatConnection * const pConnection = getFlatConnection();
605 0 : const sal_Unicode cDecimalDelimiter = pConnection->getDecimalDelimiter();
606 0 : const sal_Unicode cThousandDelimiter = pConnection->getThousandDelimiter();
607 : // Fields:
608 0 : sal_Int32 nStartPos = 0;
609 0 : OSQLColumns::Vector::const_iterator aIter = _rCols.get().begin();
610 0 : OSQLColumns::Vector::const_iterator aEnd = _rCols.get().end();
611 0 : const OValueRefVector::Vector::size_type nCount = _rRow->get().size();
612 0 : for (OValueRefVector::Vector::size_type i = 1;
613 0 : aIter != aEnd && i < nCount;
614 : ++aIter, i++)
615 : {
616 0 : OUString aStr = m_aCurrentLine.GetTokenSpecial(nStartPos,m_cFieldDelimiter,m_cStringDelimiter);
617 :
618 0 : if (aStr.isEmpty())
619 : {
620 0 : (_rRow->get())[i]->setNull();
621 : }
622 : else
623 : {
624 : // lengths depending on data-type:
625 : sal_Int32 nLen;
626 0 : sal_Int32 nType = 0;
627 0 : if(bIsTable)
628 : {
629 0 : nLen = m_aPrecisions[i-1];
630 0 : nType = m_aTypes[i-1];
631 : }
632 : else
633 : {
634 0 : Reference< XPropertySet> xColumn = *aIter;
635 0 : xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION)) >>= nLen;
636 0 : xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
637 : }
638 0 : switch(nType)
639 : {
640 : case DataType::TIMESTAMP:
641 : case DataType::DATE:
642 : case DataType::TIME:
643 : {
644 : try
645 : {
646 0 : double nRes = m_xNumberFormatter->convertStringToNumber(::com::sun::star::util::NumberFormat::ALL,aStr);
647 :
648 0 : switch(nType)
649 : {
650 : case DataType::DATE:
651 0 : *(_rRow->get())[i] = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(nRes,m_aNullDate));
652 0 : break;
653 : case DataType::TIMESTAMP:
654 0 : *(_rRow->get())[i] = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDateTime(nRes,m_aNullDate));
655 0 : break;
656 : default:
657 0 : *(_rRow->get())[i] = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(nRes));
658 : }
659 : }
660 0 : catch(Exception&)
661 : {
662 0 : (_rRow->get())[i]->setNull();
663 : }
664 0 : } break;
665 : case DataType::DOUBLE:
666 : case DataType::INTEGER:
667 : case DataType::DECIMAL:
668 : case DataType::NUMERIC:
669 : {
670 :
671 0 : OUString aStrConverted;
672 0 : if ( DataType::INTEGER != nType )
673 : {
674 : OSL_ENSURE((cDecimalDelimiter && nType != DataType::INTEGER) ||
675 : (!cDecimalDelimiter && nType == DataType::INTEGER),
676 : "FalscherTyp");
677 :
678 0 : OUStringBuffer aBuf(aStr.getLength());
679 : // convert to Standard-Notation (DecimalPOINT without thousands-comma):
680 0 : for (sal_Int32 j = 0; j < aStr.getLength(); ++j)
681 : {
682 0 : const sal_Unicode cChar = aStr[j];
683 0 : if (cDecimalDelimiter && cChar == cDecimalDelimiter)
684 0 : aBuf.append('.');
685 0 : else if ( cChar == '.' ) // special case, if decimal separator isn't '.' we have to put the string after it
686 0 : continue;
687 0 : else if (cThousandDelimiter && cChar == cThousandDelimiter)
688 : {
689 : // leave out
690 : }
691 : else
692 0 : aBuf.append(cChar);
693 : } // for (j = 0; j < aStr.getLength(); ++j)
694 0 : aStrConverted = aBuf.makeStringAndClear();
695 : } // if ( DataType::INTEGER != nType )
696 : else
697 : {
698 0 : if ( cThousandDelimiter )
699 0 : aStrConverted = comphelper::string::remove(aStr, cThousandDelimiter);
700 : else
701 0 : aStrConverted = aStr;
702 : }
703 0 : const double nVal = ::rtl::math::stringToDouble(aStrConverted,'.',',',NULL,NULL);
704 :
705 : // #99178# OJ
706 0 : if ( DataType::DECIMAL == nType || DataType::NUMERIC == nType )
707 0 : *(_rRow->get())[i] = OUString::number(nVal);
708 : else
709 0 : *(_rRow->get())[i] = nVal;
710 0 : } break;
711 :
712 : default:
713 : {
714 : // Copy Value as String in Row-Variable
715 0 : *(_rRow->get())[i] = ORowSetValue(aStr);
716 : }
717 0 : break;
718 : } // switch(nType)
719 0 : (_rRow->get())[i]->setTypeKind(nType);
720 : }
721 0 : }
722 0 : return result;
723 : }
724 :
725 :
726 0 : void OFlatTable::refreshHeader()
727 : {
728 : SAL_INFO( "connectivity.drivers", "flat lionel@mamane.lu OFlatTable::refreshHeader" );
729 0 : }
730 :
731 :
732 : namespace
733 : {
734 : template< typename Tp, typename Te> struct RangeBefore
735 : {
736 0 : bool operator() (const Tp &p, const Te &e)
737 : {
738 : assert(p.first <= p.second);
739 0 : return p.second <= e;
740 : }
741 : };
742 : }
743 :
744 0 : bool OFlatTable::seekRow(IResultSetHelper::Movement eCursorPosition, sal_Int32 nOffset, sal_Int32& nCurPos)
745 : {
746 : OSL_ENSURE(m_pFileStream,"OFlatTable::seekRow: FileStream is NULL!");
747 :
748 :
749 0 : switch(eCursorPosition)
750 : {
751 : case IResultSetHelper::FIRST:
752 0 : m_nRowPos = 0;
753 : // run through
754 : case IResultSetHelper::NEXT:
755 : {
756 : assert(m_nRowPos >= 0);
757 0 : if(m_nMaxRowCount != 0 && m_nRowPos > m_nMaxRowCount)
758 0 : return false;
759 0 : ++m_nRowPos;
760 0 : if(m_aRowPosToFilePos.size() > static_cast< vector< TRowPositionInFile >::size_type >(m_nRowPos))
761 : {
762 0 : m_bNeedToReadLine = true;
763 0 : m_nFilePos = m_aRowPosToFilePos[m_nRowPos].first;
764 0 : nCurPos = m_aRowPosToFilePos[m_nRowPos].second;
765 : }
766 : else
767 : {
768 : assert(m_aRowPosToFilePos.size() == static_cast< vector< TRowPositionInFile >::size_type >(m_nRowPos));
769 0 : const TRowPositionInFile &lastRowPos(m_aRowPosToFilePos.back());
770 : // Our ResultSet is allowed to disagree with us only
771 : // on the position of the first line
772 : // (because of the special case of the header...)
773 : assert(m_nRowPos == 1 || nCurPos == lastRowPos.second);
774 :
775 0 : m_nFilePos = lastRowPos.second;
776 0 : m_pFileStream->Seek(m_nFilePos);
777 :
778 0 : TRowPositionInFile newRowPos;
779 0 : if(!readLine(&newRowPos.second, &newRowPos.first, false))
780 : {
781 0 : m_nMaxRowCount = m_nRowPos - 1;
782 0 : return false;
783 : }
784 :
785 0 : nCurPos = newRowPos.second;
786 0 : setRowPos(m_nRowPos, newRowPos);
787 : }
788 : }
789 :
790 0 : break;
791 : case IResultSetHelper::PRIOR:
792 : assert(m_nRowPos >= 0);
793 :
794 0 : if(m_nRowPos == 0)
795 0 : return false;
796 :
797 0 : --m_nRowPos;
798 : {
799 : assert (m_nRowPos >= 0);
800 : assert(m_aRowPosToFilePos.size() >= static_cast< vector< TRowPositionInFile >::size_type >(m_nRowPos));
801 0 : const TRowPositionInFile &aPositions(m_aRowPosToFilePos[m_nRowPos]);
802 0 : m_nFilePos = aPositions.first;
803 0 : nCurPos = aPositions.second;
804 0 : m_bNeedToReadLine = true;
805 : }
806 :
807 0 : break;
808 : case IResultSetHelper::LAST:
809 0 : if (m_nMaxRowCount == 0)
810 : {
811 0 : while(seekRow(IResultSetHelper::NEXT, 1, nCurPos)) ; // run through after last row
812 : }
813 : // m_nMaxRowCount can still be zero, but now it means there a genuinely zero rows in the table
814 0 : return seekRow(IResultSetHelper::ABSOLUTE1, m_nMaxRowCount, nCurPos);
815 : break;
816 : case IResultSetHelper::RELATIVE1:
817 : {
818 0 : const sal_Int32 nNewRowPos = m_nRowPos + nOffset;
819 0 : if (nNewRowPos < 0)
820 0 : return false;
821 : // ABSOLUTE will take care of case nNewRowPos > nMaxRowCount
822 0 : return seekRow(IResultSetHelper::ABSOLUTE1, nNewRowPos, nCurPos);
823 : }
824 : case IResultSetHelper::ABSOLUTE1:
825 : {
826 0 : if(nOffset < 0)
827 : {
828 0 : if (m_nMaxRowCount == 0)
829 : {
830 0 : if (!seekRow(IResultSetHelper::LAST, 0, nCurPos))
831 0 : return false;
832 : }
833 : // m_nMaxRowCount can still be zero, but now it means there a genuinely zero rows in the table
834 0 : nOffset = m_nMaxRowCount + nOffset;
835 : }
836 0 : if(nOffset < 0)
837 : {
838 0 : seekRow(IResultSetHelper::ABSOLUTE1, 0, nCurPos);
839 0 : return false;
840 : }
841 0 : if(m_nMaxRowCount && nOffset > m_nMaxRowCount)
842 : {
843 0 : m_nRowPos = m_nMaxRowCount + 1;
844 0 : const TRowPositionInFile &lastRowPos(m_aRowPosToFilePos.back());
845 0 : m_nFilePos = lastRowPos.second;
846 0 : nCurPos = lastRowPos.second;
847 0 : return false;
848 : }
849 :
850 : assert(m_nRowPos >=0);
851 : assert(m_aRowPosToFilePos.size() > static_cast< vector< TRowPositionInFile >::size_type >(m_nRowPos));
852 : assert(nOffset >= 0);
853 0 : if(m_aRowPosToFilePos.size() > static_cast< vector< TRowPositionInFile >::size_type >(nOffset))
854 : {
855 0 : m_nFilePos = m_aRowPosToFilePos[nOffset].first;
856 0 : nCurPos = m_aRowPosToFilePos[nOffset].second;
857 0 : m_nRowPos = nOffset;
858 0 : m_bNeedToReadLine = true;
859 : }
860 : else
861 : {
862 : assert(m_nRowPos < nOffset);
863 0 : while(m_nRowPos < nOffset)
864 : {
865 0 : if(!seekRow(IResultSetHelper::NEXT, 1, nCurPos))
866 0 : return false;
867 : }
868 : assert(m_nRowPos == nOffset);
869 : }
870 : }
871 :
872 0 : break;
873 : case IResultSetHelper::BOOKMARK:
874 : {
875 : vector< TRowPositionInFile >::const_iterator aFind = lower_bound(m_aRowPosToFilePos.begin(),
876 : m_aRowPosToFilePos.end(),
877 : nOffset,
878 0 : RangeBefore< TRowPositionInFile, sal_Int32 >());
879 :
880 0 : if(aFind == m_aRowPosToFilePos.end() || aFind->first != nOffset)
881 : //invalid bookmark
882 0 : return false;
883 :
884 0 : m_bNeedToReadLine = true;
885 0 : m_nFilePos = aFind->first;
886 0 : nCurPos = aFind->second;
887 0 : m_nRowPos = aFind - m_aRowPosToFilePos.begin();
888 0 : break;
889 : }
890 : }
891 :
892 0 : return true;
893 : }
894 :
895 :
896 0 : bool OFlatTable::readLine(sal_Int32 * const pEndPos, sal_Int32 * const pStartPos, const bool nonEmpty)
897 : {
898 0 : const rtl_TextEncoding nEncoding = m_pConnection->getTextEncoding();
899 0 : m_aCurrentLine = QuotedTokenizedString();
900 0 : do
901 : {
902 0 : if (pStartPos)
903 0 : *pStartPos = (sal_Int32)m_pFileStream->Tell();
904 0 : m_pFileStream->ReadByteStringLine(m_aCurrentLine, nEncoding);
905 0 : if (m_pFileStream->IsEof())
906 0 : return false;
907 :
908 0 : QuotedTokenizedString sLine = m_aCurrentLine; // check if the string continues on next line
909 0 : while( (comphelper::string::getTokenCount(sLine.GetString(), m_cStringDelimiter) % 2) != 1 )
910 : {
911 0 : m_pFileStream->ReadByteStringLine(sLine,nEncoding);
912 0 : if ( !m_pFileStream->IsEof() )
913 : {
914 0 : OUString aStr = m_aCurrentLine.GetString() + "\n" + sLine.GetString();
915 0 : m_aCurrentLine.SetString(aStr);
916 0 : sLine = m_aCurrentLine;
917 : }
918 : else
919 0 : break;
920 0 : }
921 : }
922 0 : while(nonEmpty && m_aCurrentLine.Len() == 0);
923 :
924 0 : if(pEndPos)
925 0 : *pEndPos = (sal_Int32)m_pFileStream->Tell();
926 0 : return true;
927 : }
928 :
929 :
930 0 : void OFlatTable::setRowPos(const vector<TRowPositionInFile>::size_type rowNum, const TRowPositionInFile &rowPos)
931 : {
932 : assert(m_aRowPosToFilePos.size() >= rowNum);
933 0 : if(m_aRowPosToFilePos.size() == rowNum)
934 0 : m_aRowPosToFilePos.push_back(rowPos);
935 : else
936 : {
937 : SAL_WARN_IF(m_aRowPosToFilePos[rowNum] != rowPos,
938 : "connectivity.flat",
939 : "Setting position for row " << rowNum << " to (" << rowPos.first << ", " << rowPos.second << "), "
940 : "but already had different position (" << m_aRowPosToFilePos[rowNum].first << ", " << m_aRowPosToFilePos[rowNum].second << ")");
941 0 : m_aRowPosToFilePos[rowNum] = rowPos;
942 : }
943 0 : }
944 :
945 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|