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