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 "dbase/DTable.hxx"
21 : #include <com/sun/star/sdbc/ColumnValue.hpp>
22 : #include <com/sun/star/sdbc/DataType.hpp>
23 : #include <com/sun/star/ucb/XContentAccess.hpp>
24 : #include <com/sun/star/sdbc/XRow.hpp>
25 : #include <svl/converter.hxx>
26 : #include "dbase/DConnection.hxx"
27 : #include "dbase/DColumns.hxx"
28 : #include <osl/thread.h>
29 : #include <tools/config.hxx>
30 : #include "dbase/DIndex.hxx"
31 : #include "dbase/DIndexes.hxx"
32 : #include <comphelper/processfactory.hxx>
33 : #include <comphelper/sequence.hxx>
34 : #include <svl/zforlist.hxx>
35 : #include <unotools/syslocale.hxx>
36 : #include <rtl/math.hxx>
37 : #include <stdio.h>
38 : #include <ucbhelper/content.hxx>
39 : #include <connectivity/dbexception.hxx>
40 : #include <connectivity/dbconversion.hxx>
41 : #include <com/sun/star/lang/DisposedException.hpp>
42 : #include <comphelper/property.hxx>
43 : #include <comphelper/string.hxx>
44 : #include <unotools/tempfile.hxx>
45 : #include <unotools/ucbhelper.hxx>
46 : #include <comphelper/types.hxx>
47 : #include <cppuhelper/exc_hlp.hxx>
48 : #include "connectivity/PColumn.hxx"
49 : #include "connectivity/dbtools.hxx"
50 : #include "connectivity/FValue.hxx"
51 : #include "connectivity/dbconversion.hxx"
52 : #include "resource/dbase_res.hrc"
53 : #include <rtl/strbuf.hxx>
54 :
55 : #include <algorithm>
56 :
57 : using namespace ::comphelper;
58 : using namespace connectivity;
59 : using namespace connectivity::sdbcx;
60 : using namespace connectivity::dbase;
61 : using namespace connectivity::file;
62 : using namespace ::ucbhelper;
63 : using namespace ::utl;
64 : using namespace ::cppu;
65 : using namespace ::dbtools;
66 : using namespace ::com::sun::star::uno;
67 : using namespace ::com::sun::star::ucb;
68 : using namespace ::com::sun::star::beans;
69 : using namespace ::com::sun::star::sdbcx;
70 : using namespace ::com::sun::star::sdbc;
71 : using namespace ::com::sun::star::container;
72 : using namespace ::com::sun::star::lang;
73 : using namespace ::com::sun::star::i18n;
74 :
75 : // stored as the Field Descriptor terminator
76 : #define FIELD_DESCRIPTOR_TERMINATOR 0x0D
77 : #define DBF_EOL 0x1A
78 :
79 : namespace
80 : {
81 0 : sal_Size lcl_getFileSize(SvStream& _rStream)
82 : {
83 0 : sal_Size nFileSize = 0;
84 0 : _rStream.Seek(STREAM_SEEK_TO_END);
85 0 : _rStream.SeekRel(-1);
86 : char cEOL;
87 0 : _rStream.ReadChar( cEOL );
88 0 : nFileSize = _rStream.Tell();
89 0 : if ( cEOL == DBF_EOL )
90 0 : nFileSize -= 1;
91 0 : return nFileSize;
92 : }
93 : /**
94 : calculates the Julian date
95 : */
96 0 : void lcl_CalcJulDate(sal_Int32& _nJulianDate,sal_Int32& _nJulianTime,const com::sun::star::util::DateTime _aDateTime)
97 : {
98 0 : com::sun::star::util::DateTime aDateTime = _aDateTime;
99 : // weird: months fix
100 0 : if (aDateTime.Month > 12)
101 : {
102 0 : aDateTime.Month--;
103 0 : sal_uInt16 delta = _aDateTime.Month / 12;
104 0 : aDateTime.Year += delta;
105 0 : aDateTime.Month -= delta * 12;
106 0 : aDateTime.Month++;
107 : }
108 :
109 0 : _nJulianTime = ((aDateTime.Hours*3600000)+(aDateTime.Minutes*60000)+(aDateTime.Seconds*1000)+(aDateTime.NanoSeconds/1000000));
110 : /* conversion factors */
111 : sal_uInt16 iy0;
112 : sal_uInt16 im0;
113 0 : if ( aDateTime.Month <= 2 )
114 : {
115 0 : iy0 = aDateTime.Year - 1;
116 0 : im0 = aDateTime.Month + 12;
117 : }
118 : else
119 : {
120 0 : iy0 = aDateTime.Year;
121 0 : im0 = aDateTime.Month;
122 : }
123 0 : sal_Int32 ia = iy0 / 100;
124 0 : sal_Int32 ib = 2 - ia + (ia >> 2);
125 : /* calculate julian date */
126 0 : if ( aDateTime.Year <= 0 )
127 : {
128 0 : _nJulianDate = (sal_Int32) ((365.25 * iy0) - 0.75)
129 0 : + (sal_Int32) (30.6001 * (im0 + 1) )
130 0 : + aDateTime.Day + 1720994;
131 : } // if ( _aDateTime.Year <= 0 )
132 : else
133 : {
134 0 : _nJulianDate = static_cast<sal_Int32>( ((365.25 * iy0)
135 0 : + (sal_Int32) (30.6001 * (im0 + 1))
136 0 : + aDateTime.Day + 1720994));
137 : }
138 0 : double JD = _nJulianDate + 0.5;
139 0 : _nJulianDate = (sal_Int32)( JD + 0.5);
140 0 : const double gyr = aDateTime.Year + (0.01 * aDateTime.Month) + (0.0001 * aDateTime.Day);
141 0 : if ( gyr >= 1582.1015 ) /* on or after 15 October 1582 */
142 0 : _nJulianDate += ib;
143 0 : }
144 :
145 : /**
146 : calculates date time from the Julian Date
147 : */
148 0 : void lcl_CalDate(sal_Int32 _nJulianDate,sal_Int32 _nJulianTime,com::sun::star::util::DateTime& _rDateTime)
149 : {
150 0 : if ( _nJulianDate )
151 : {
152 : sal_Int32 ialp;
153 0 : sal_Int32 ka = _nJulianDate;
154 0 : if ( _nJulianDate >= 2299161 )
155 : {
156 0 : ialp = (sal_Int32)( ((double) _nJulianDate - 1867216.25 ) / ( 36524.25 ));
157 0 : ka = _nJulianDate + 1 + ialp - ( ialp >> 2 );
158 : }
159 0 : sal_Int32 kb = ka + 1524;
160 0 : sal_Int32 kc = (sal_Int32) ( ((double) kb - 122.1 ) / 365.25 );
161 0 : sal_Int32 kd = (sal_Int32) ((double) kc * 365.25);
162 0 : sal_Int32 ke = (sal_Int32) ((double) ( kb - kd ) / 30.6001 );
163 0 : _rDateTime.Day = static_cast<sal_uInt16>(kb - kd - ((sal_Int32) ( (double) ke * 30.6001 )));
164 0 : if ( ke > 13 )
165 0 : _rDateTime.Month = static_cast<sal_uInt16>(ke - 13);
166 : else
167 0 : _rDateTime.Month = static_cast<sal_uInt16>(ke - 1);
168 0 : if ( (_rDateTime.Month == 2) && (_rDateTime.Day > 28) )
169 0 : _rDateTime.Day = 29;
170 0 : if ( (_rDateTime.Month == 2) && (_rDateTime.Day == 29) && (ke == 3) )
171 0 : _rDateTime.Year = static_cast<sal_uInt16>(kc - 4716);
172 0 : else if ( _rDateTime.Month > 2 )
173 0 : _rDateTime.Year = static_cast<sal_uInt16>(kc - 4716);
174 : else
175 0 : _rDateTime.Year = static_cast<sal_uInt16>(kc - 4715);
176 : }
177 :
178 0 : if ( _nJulianTime )
179 : {
180 0 : double d_s = _nJulianTime / 1000;
181 0 : double d_m = d_s / 60;
182 0 : double d_h = d_m / 60;
183 0 : _rDateTime.Hours = (sal_uInt16) (d_h);
184 0 : _rDateTime.Minutes = (sal_uInt16) d_m;
185 0 : _rDateTime.Seconds = static_cast<sal_uInt16>(( d_m - (double) _rDateTime.Minutes ) * 60.0);
186 : }
187 0 : }
188 :
189 : }
190 :
191 :
192 0 : void ODbaseTable::readHeader()
193 : {
194 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::readHeader" );
195 : OSL_ENSURE(m_pFileStream,"No Stream available!");
196 0 : if(!m_pFileStream)
197 0 : return;
198 0 : m_pFileStream->RefreshBuffer(); // Make sure, that the header information actually is read again
199 0 : m_pFileStream->Seek(STREAM_SEEK_TO_BEGIN);
200 :
201 0 : sal_uInt8 nType=0;
202 0 : (*m_pFileStream).ReadUChar( nType );
203 0 : if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
204 0 : throwInvalidDbaseFormat();
205 :
206 0 : m_pFileStream->Read((char*)(&m_aHeader.db_aedat), 3*sizeof(sal_uInt8));
207 0 : if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
208 0 : throwInvalidDbaseFormat();
209 0 : (*m_pFileStream).ReadUInt32( m_aHeader.db_anz );
210 0 : if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
211 0 : throwInvalidDbaseFormat();
212 0 : (*m_pFileStream).ReadUInt16( m_aHeader.db_kopf );
213 0 : if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
214 0 : throwInvalidDbaseFormat();
215 0 : (*m_pFileStream).ReadUInt16( m_aHeader.db_slng );
216 0 : if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
217 0 : throwInvalidDbaseFormat();
218 0 : m_pFileStream->Read((char*)(&m_aHeader.db_frei), 20*sizeof(sal_uInt8));
219 0 : if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
220 0 : throwInvalidDbaseFormat();
221 :
222 0 : if ( ( ( m_aHeader.db_kopf - 1 ) / 32 - 1 ) <= 0 ) // number of fields
223 : {
224 : // no dbase file
225 0 : throwInvalidDbaseFormat();
226 : }
227 : else
228 : {
229 : // Consistency check of the header:
230 0 : m_aHeader.db_typ = (DBFType)nType;
231 0 : switch (m_aHeader.db_typ)
232 : {
233 : case dBaseIII:
234 : case dBaseIV:
235 : case dBaseV:
236 : case VisualFoxPro:
237 : case VisualFoxProAuto:
238 : case dBaseFS:
239 : case dBaseFSMemo:
240 : case dBaseIVMemoSQL:
241 : case dBaseIIIMemo:
242 : case FoxProMemo:
243 0 : m_pFileStream->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
244 0 : if ( m_aHeader.db_frei[17] != 0x00
245 0 : && !m_aHeader.db_frei[18] && !m_aHeader.db_frei[19] && getConnection()->isTextEncodingDefaulted() )
246 : {
247 0 : switch(m_aHeader.db_frei[17])
248 : {
249 0 : case 0x01: m_eEncoding = RTL_TEXTENCODING_IBM_437; break; // DOS USA code page 437
250 0 : case 0x02: m_eEncoding = RTL_TEXTENCODING_IBM_850; break; // DOS Multilingual code page 850
251 0 : case 0x03: m_eEncoding = RTL_TEXTENCODING_MS_1252; break; // Windows ANSI code page 1252
252 0 : case 0x04: m_eEncoding = RTL_TEXTENCODING_APPLE_ROMAN; break; // Standard Macintosh
253 0 : case 0x64: m_eEncoding = RTL_TEXTENCODING_IBM_852; break; // EE MS-DOS code page 852
254 0 : case 0x65: m_eEncoding = RTL_TEXTENCODING_IBM_865; break; // Nordic MS-DOS code page 865
255 0 : case 0x66: m_eEncoding = RTL_TEXTENCODING_IBM_866; break; // Russian MS-DOS code page 866
256 0 : case 0x67: m_eEncoding = RTL_TEXTENCODING_IBM_861; break; // Icelandic MS-DOS
257 : //case 0x68: m_eEncoding = ; break; // Kamenicky (Czech) MS-DOS
258 : //case 0x69: m_eEncoding = ; break; // Mazovia (Polish) MS-DOS
259 0 : case 0x6A: m_eEncoding = RTL_TEXTENCODING_IBM_737; break; // Greek MS-DOS (437G)
260 0 : case 0x6B: m_eEncoding = RTL_TEXTENCODING_IBM_857; break; // Turkish MS-DOS
261 0 : case 0x6C: m_eEncoding = RTL_TEXTENCODING_IBM_863; break; // MS-DOS, Canada
262 0 : case 0x78: m_eEncoding = RTL_TEXTENCODING_MS_950; break; // Windows, Traditional Chinese
263 0 : case 0x79: m_eEncoding = RTL_TEXTENCODING_MS_949; break; // Windows, Korean (Hangul)
264 0 : case 0x7A: m_eEncoding = RTL_TEXTENCODING_MS_936; break; // Windows, Simplified Chinese
265 0 : case 0x7B: m_eEncoding = RTL_TEXTENCODING_MS_932; break; // Windows, Japanese (Shift-jis)
266 0 : case 0x7C: m_eEncoding = RTL_TEXTENCODING_MS_874; break; // Windows, Thai
267 0 : case 0x7D: m_eEncoding = RTL_TEXTENCODING_MS_1255; break; // Windows, Hebrew
268 0 : case 0x7E: m_eEncoding = RTL_TEXTENCODING_MS_1256; break; // Windows, Arabic
269 0 : case 0x96: m_eEncoding = RTL_TEXTENCODING_APPLE_CYRILLIC; break; // Russian Macintosh
270 0 : case 0x97: m_eEncoding = RTL_TEXTENCODING_APPLE_CENTEURO; break; // Eastern European Macintosh
271 0 : case 0x98: m_eEncoding = RTL_TEXTENCODING_APPLE_GREEK; break; // Greek Macintosh
272 0 : case 0xC8: m_eEncoding = RTL_TEXTENCODING_MS_1250; break; // Windows EE code page 1250
273 0 : case 0xC9: m_eEncoding = RTL_TEXTENCODING_MS_1251; break; // Russian Windows
274 0 : case 0xCA: m_eEncoding = RTL_TEXTENCODING_MS_1254; break; // Turkish Windows
275 0 : case 0xCB: m_eEncoding = RTL_TEXTENCODING_MS_1253; break; // Greek Windows
276 0 : case 0xCC: m_eEncoding = RTL_TEXTENCODING_MS_1257; break; // Windows, Baltic
277 : default:
278 : // Default Encoding
279 0 : m_eEncoding = RTL_TEXTENCODING_IBM_850;
280 0 : break;
281 : }
282 : }
283 0 : break;
284 : case dBaseIVMemo:
285 0 : m_pFileStream->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
286 0 : break;
287 : default:
288 : {
289 0 : throwInvalidDbaseFormat();
290 : }
291 : }
292 : }
293 : }
294 :
295 0 : void ODbaseTable::fillColumns()
296 : {
297 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::fillColumns" );
298 0 : m_pFileStream->Seek(STREAM_SEEK_TO_BEGIN);
299 0 : m_pFileStream->Seek(32L);
300 :
301 0 : if(!m_aColumns.is())
302 0 : m_aColumns = new OSQLColumns();
303 : else
304 0 : m_aColumns->get().clear();
305 :
306 0 : m_aTypes.clear();
307 0 : m_aPrecisions.clear();
308 0 : m_aScales.clear();
309 :
310 : // Number of fields:
311 0 : const sal_Int32 nFieldCount = (m_aHeader.db_kopf - 1) / 32 - 1;
312 : OSL_ENSURE(nFieldCount,"No columns in table!");
313 :
314 0 : m_aColumns->get().reserve(nFieldCount);
315 0 : m_aTypes.reserve(nFieldCount);
316 0 : m_aPrecisions.reserve(nFieldCount);
317 0 : m_aScales.reserve(nFieldCount);
318 :
319 0 : OUString aTypeName;
320 0 : const sal_Bool bCase = getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers();
321 0 : const bool bFoxPro = m_aHeader.db_typ == VisualFoxPro || m_aHeader.db_typ == VisualFoxProAuto || m_aHeader.db_typ == FoxProMemo;
322 :
323 0 : sal_Int32 i = 0;
324 0 : for (; i < nFieldCount; i++)
325 : {
326 : DBFColumn aDBFColumn;
327 0 : m_pFileStream->Read((char*)&aDBFColumn, sizeof(aDBFColumn));
328 0 : if ( FIELD_DESCRIPTOR_TERMINATOR == aDBFColumn.db_fnm[0] ) // 0x0D stored as the Field Descriptor terminator.
329 0 : break;
330 :
331 0 : sal_Bool bIsRowVersion = bFoxPro && ( aDBFColumn.db_frei2[0] & 0x01 ) == 0x01;
332 0 : const OUString aColumnName((const char *)aDBFColumn.db_fnm, strlen((const char *)aDBFColumn.db_fnm), m_eEncoding);
333 :
334 0 : m_aRealFieldLengths.push_back(aDBFColumn.db_flng);
335 0 : sal_Int32 nPrecision = aDBFColumn.db_flng;
336 : sal_Int32 eType;
337 0 : sal_Bool bIsCurrency = sal_False;
338 :
339 : char cType[2];
340 0 : cType[0] = aDBFColumn.db_typ;
341 0 : cType[1] = 0;
342 0 : aTypeName = OUString::createFromAscii(cType);
343 : SAL_INFO( "connectivity.drivers","column type: " << aDBFColumn.db_typ);
344 :
345 0 : switch (aDBFColumn.db_typ)
346 : {
347 : case 'C':
348 0 : eType = DataType::VARCHAR;
349 0 : aTypeName = "VARCHAR";
350 0 : break;
351 : case 'F':
352 : case 'N':
353 0 : aTypeName = "DECIMAL";
354 0 : if ( aDBFColumn.db_typ == 'N' )
355 0 : aTypeName = "NUMERIC";
356 0 : eType = DataType::DECIMAL;
357 :
358 : // for numeric fields two characters more are written, than the precision of the column description predescribes,
359 : // to keep room for the possible sign and the comma. This has to be considered...
360 0 : nPrecision = SvDbaseConverter::ConvertPrecisionToOdbc(nPrecision,aDBFColumn.db_dez);
361 : // This is not true for older versions ....
362 0 : break;
363 : case 'L':
364 0 : eType = DataType::BIT;
365 0 : aTypeName = "BOOLEAN";
366 0 : break;
367 : case 'Y':
368 0 : bIsCurrency = sal_True;
369 0 : eType = DataType::DOUBLE;
370 0 : aTypeName = "DOUBLE";
371 0 : break;
372 : case 'D':
373 0 : eType = DataType::DATE;
374 0 : aTypeName = "DATE";
375 0 : break;
376 : case 'T':
377 0 : eType = DataType::TIMESTAMP;
378 0 : aTypeName = "TIMESTAMP";
379 0 : break;
380 : case 'I':
381 0 : eType = DataType::INTEGER;
382 0 : aTypeName = "INTEGER";
383 0 : break;
384 : case 'M':
385 0 : if ( bFoxPro && ( aDBFColumn.db_frei2[0] & 0x04 ) == 0x04 )
386 : {
387 0 : eType = DataType::LONGVARBINARY;
388 0 : aTypeName = "LONGVARBINARY";
389 : }
390 : else
391 : {
392 0 : aTypeName = "LONGVARCHAR";
393 0 : eType = DataType::LONGVARCHAR;
394 : }
395 0 : nPrecision = 2147483647;
396 0 : break;
397 : case 'P':
398 0 : aTypeName = "LONGVARBINARY";
399 0 : eType = DataType::LONGVARBINARY;
400 0 : nPrecision = 2147483647;
401 0 : break;
402 : case '0':
403 : case 'B':
404 0 : if ( m_aHeader.db_typ == VisualFoxPro || m_aHeader.db_typ == VisualFoxProAuto )
405 : {
406 0 : aTypeName = "DOUBLE";
407 0 : eType = DataType::DOUBLE;
408 : }
409 : else
410 : {
411 0 : aTypeName = "LONGVARBINARY";
412 0 : eType = DataType::LONGVARBINARY;
413 0 : nPrecision = 2147483647;
414 : }
415 0 : break;
416 : default:
417 0 : eType = DataType::OTHER;
418 : }
419 :
420 0 : m_aTypes.push_back(eType);
421 0 : m_aPrecisions.push_back(nPrecision);
422 0 : m_aScales.push_back(aDBFColumn.db_dez);
423 :
424 : Reference< XPropertySet> xCol = new sdbcx::OColumn(aColumnName,
425 : aTypeName,
426 : OUString(),
427 : OUString(),
428 : ColumnValue::NULLABLE,
429 : nPrecision,
430 : aDBFColumn.db_dez,
431 : eType,
432 : false,
433 : bIsRowVersion,
434 : bIsCurrency,
435 : bCase,
436 0 : m_CatalogName, getSchema(), getName());
437 0 : m_aColumns->get().push_back(xCol);
438 0 : } // for (; i < nFieldCount; i++)
439 0 : OSL_ENSURE(i,"No columns in table!");
440 0 : }
441 :
442 0 : ODbaseTable::ODbaseTable(sdbcx::OCollection* _pTables, ODbaseConnection* _pConnection)
443 : : ODbaseTable_BASE(_pTables,_pConnection)
444 : , m_pMemoStream(NULL)
445 0 : , m_bWriteableMemo(sal_False)
446 : {
447 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::ODbaseTable" );
448 : // initialize the header
449 0 : memset(&m_aHeader, 0, sizeof(m_aHeader));
450 0 : m_aHeader.db_typ = dBaseIII;
451 0 : m_eEncoding = getConnection()->getTextEncoding();
452 0 : }
453 :
454 0 : ODbaseTable::ODbaseTable(sdbcx::OCollection* _pTables, ODbaseConnection* _pConnection,
455 : const OUString& _Name,
456 : const OUString& _Type,
457 : const OUString& _Description ,
458 : const OUString& _SchemaName,
459 : const OUString& _CatalogName )
460 : : ODbaseTable_BASE(_pTables,_pConnection,_Name,
461 : _Type,
462 : _Description,
463 : _SchemaName,
464 : _CatalogName)
465 : , m_pMemoStream(NULL)
466 0 : , m_bWriteableMemo(sal_False)
467 : {
468 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::ODbaseTable" );
469 0 : memset(&m_aHeader, 0, sizeof(m_aHeader));
470 0 : m_eEncoding = getConnection()->getTextEncoding();
471 0 : }
472 :
473 :
474 0 : void ODbaseTable::construct()
475 : {
476 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::construct" );
477 : // initialize the header
478 0 : m_aHeader.db_typ = dBaseIII;
479 0 : m_aHeader.db_anz = 0;
480 0 : m_aHeader.db_kopf = 0;
481 0 : m_aHeader.db_slng = 0;
482 0 : m_aMemoHeader.db_size = 0;
483 :
484 0 : OUString sFileName(getEntry(m_pConnection, m_Name));
485 :
486 0 : INetURLObject aURL;
487 0 : aURL.SetURL(sFileName);
488 :
489 : OSL_ENSURE( m_pConnection->matchesExtension( aURL.getExtension() ),
490 : "ODbaseTable::ODbaseTable: invalid extension!");
491 : // getEntry is expected to ensure the correct file name
492 :
493 0 : m_pFileStream = createStream_simpleError( sFileName, STREAM_READWRITE | STREAM_NOCREATE | STREAM_SHARE_DENYWRITE);
494 0 : m_bWriteable = ( m_pFileStream != NULL );
495 :
496 0 : if ( !m_pFileStream )
497 : {
498 0 : m_bWriteable = sal_False;
499 0 : m_pFileStream = createStream_simpleError( sFileName, STREAM_READ | STREAM_NOCREATE | STREAM_SHARE_DENYNONE);
500 : }
501 :
502 0 : if(m_pFileStream)
503 : {
504 0 : readHeader();
505 0 : if (HasMemoFields())
506 : {
507 : // Create Memo-Filename (.DBT):
508 : // nyi: Ugly for Unix and Mac!
509 :
510 0 : if ( m_aHeader.db_typ == FoxProMemo || VisualFoxPro == m_aHeader.db_typ || VisualFoxProAuto == m_aHeader.db_typ ) // foxpro uses another extension
511 0 : aURL.SetExtension("fpt");
512 : else
513 0 : aURL.SetExtension("dbt");
514 :
515 : // If the memo file isn't found, the data will be displayed anyhow.
516 : // However, updates can't be done
517 : // but the operation is executed
518 0 : m_pMemoStream = createStream_simpleError( aURL.GetMainURL(INetURLObject::NO_DECODE), STREAM_READWRITE | STREAM_NOCREATE | STREAM_SHARE_DENYWRITE);
519 0 : if ( !m_pMemoStream )
520 : {
521 0 : m_bWriteableMemo = sal_False;
522 0 : m_pMemoStream = createStream_simpleError( aURL.GetMainURL(INetURLObject::NO_DECODE), STREAM_READ | STREAM_NOCREATE | STREAM_SHARE_DENYNONE);
523 : }
524 0 : if (m_pMemoStream)
525 0 : ReadMemoHeader();
526 : }
527 0 : fillColumns();
528 :
529 0 : sal_Size nFileSize = lcl_getFileSize(*m_pFileStream);
530 0 : m_pFileStream->Seek(STREAM_SEEK_TO_BEGIN);
531 0 : if ( m_aHeader.db_anz == 0 && ((nFileSize-m_aHeader.db_kopf)/m_aHeader.db_slng) > 0) // seems to be empty or someone wrote bullshit into the dbase file
532 0 : m_aHeader.db_anz = ((nFileSize-m_aHeader.db_kopf)/m_aHeader.db_slng);
533 :
534 : // Buffersize dependent on the file size
535 : m_pFileStream->SetBufferSize(nFileSize > 1000000 ? 32768 :
536 : nFileSize > 100000 ? 16384 :
537 0 : nFileSize > 10000 ? 4096 : 1024);
538 :
539 0 : if (m_pMemoStream)
540 : {
541 : // set the buffer extactly to the length of a record
542 0 : m_pMemoStream->Seek(STREAM_SEEK_TO_END);
543 0 : nFileSize = m_pMemoStream->Tell();
544 0 : m_pMemoStream->Seek(STREAM_SEEK_TO_BEGIN);
545 :
546 : // Buffersize dependent on the file size
547 : m_pMemoStream->SetBufferSize(nFileSize > 1000000 ? 32768 :
548 : nFileSize > 100000 ? 16384 :
549 : nFileSize > 10000 ? 4096 :
550 0 : m_aMemoHeader.db_size);
551 : }
552 :
553 0 : AllocBuffer();
554 0 : }
555 0 : }
556 :
557 0 : sal_Bool ODbaseTable::ReadMemoHeader()
558 : {
559 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::ReadMemoHeader" );
560 0 : m_pMemoStream->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
561 0 : m_pMemoStream->RefreshBuffer(); // make sure that the header information is actually read again
562 0 : m_pMemoStream->Seek(0L);
563 :
564 0 : (*m_pMemoStream).ReadUInt32( m_aMemoHeader.db_next );
565 0 : switch (m_aHeader.db_typ)
566 : {
567 : case dBaseIIIMemo: // dBase III: fixed block size
568 : case dBaseIVMemo:
569 : // sometimes dBase3 is attached to dBase4 memo
570 0 : m_pMemoStream->Seek(20L);
571 0 : (*m_pMemoStream).ReadUInt16( m_aMemoHeader.db_size );
572 0 : if (m_aMemoHeader.db_size > 1 && m_aMemoHeader.db_size != 512) // 1 is also for dBase 3
573 0 : m_aMemoHeader.db_typ = MemodBaseIV;
574 0 : else if (m_aMemoHeader.db_size > 1 && m_aMemoHeader.db_size == 512)
575 : {
576 : // There are files using size specification, though they are dBase-files
577 : char sHeader[4];
578 0 : m_pMemoStream->Seek(m_aMemoHeader.db_size);
579 0 : m_pMemoStream->Read(sHeader,4);
580 :
581 0 : if ((m_pMemoStream->GetErrorCode() != ERRCODE_NONE) || ((sal_uInt8)sHeader[0]) != 0xFF || ((sal_uInt8)sHeader[1]) != 0xFF || ((sal_uInt8)sHeader[2]) != 0x08)
582 0 : m_aMemoHeader.db_typ = MemodBaseIII;
583 : else
584 0 : m_aMemoHeader.db_typ = MemodBaseIV;
585 : }
586 : else
587 : {
588 0 : m_aMemoHeader.db_typ = MemodBaseIII;
589 0 : m_aMemoHeader.db_size = 512;
590 : }
591 0 : break;
592 : case VisualFoxPro:
593 : case VisualFoxProAuto:
594 : case FoxProMemo:
595 0 : m_aMemoHeader.db_typ = MemoFoxPro;
596 0 : m_pMemoStream->Seek(6L);
597 0 : m_pMemoStream->SetNumberFormatInt(NUMBERFORMAT_INT_BIGENDIAN);
598 0 : (*m_pMemoStream).ReadUInt16( m_aMemoHeader.db_size );
599 0 : break;
600 : default:
601 : SAL_WARN( "connectivity.drivers", "ODbaseTable::ReadMemoHeader: unsupported memo type!" );
602 0 : break;
603 : }
604 0 : return sal_True;
605 : }
606 :
607 0 : OUString ODbaseTable::getEntry(OConnection* _pConnection,const OUString& _sName )
608 : {
609 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::getEntry" );
610 0 : OUString sURL;
611 : try
612 : {
613 0 : Reference< XResultSet > xDir = _pConnection->getDir()->getStaticResultSet();
614 0 : Reference< XRow> xRow(xDir,UNO_QUERY);
615 0 : OUString sName;
616 0 : OUString sExt;
617 0 : INetURLObject aURL;
618 0 : static const OUString s_sSeparator("/");
619 0 : xDir->beforeFirst();
620 0 : while(xDir->next())
621 : {
622 0 : sName = xRow->getString(1);
623 0 : aURL.SetSmartProtocol(INET_PROT_FILE);
624 0 : OUString sUrl = _pConnection->getURL() + s_sSeparator + sName;
625 0 : aURL.SetSmartURL( sUrl );
626 :
627 : // cut the extension
628 0 : sExt = aURL.getExtension();
629 :
630 : // name and extension have to coincide
631 0 : if ( _pConnection->matchesExtension( sExt ) )
632 : {
633 0 : sName = sName.replaceAt(sName.getLength() - (sExt.getLength() + 1), sExt.getLength() + 1, OUString());
634 0 : if ( sName == _sName )
635 : {
636 0 : Reference< XContentAccess > xContentAccess( xDir, UNO_QUERY );
637 0 : sURL = xContentAccess->queryContentIdentifierString();
638 0 : break;
639 : }
640 : }
641 0 : }
642 0 : xDir->beforeFirst(); // move back to before first record
643 : }
644 0 : catch(const Exception&)
645 : {
646 : OSL_ASSERT(false);
647 : }
648 0 : return sURL;
649 : }
650 :
651 0 : void ODbaseTable::refreshColumns()
652 : {
653 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::refreshColumns" );
654 0 : ::osl::MutexGuard aGuard( m_aMutex );
655 :
656 0 : TStringVector aVector;
657 0 : aVector.reserve(m_aColumns->get().size());
658 :
659 0 : for(OSQLColumns::Vector::const_iterator aIter = m_aColumns->get().begin();aIter != m_aColumns->get().end();++aIter)
660 0 : aVector.push_back(Reference< XNamed>(*aIter,UNO_QUERY)->getName());
661 :
662 0 : if(m_pColumns)
663 0 : m_pColumns->reFill(aVector);
664 : else
665 0 : m_pColumns = new ODbaseColumns(this,m_aMutex,aVector);
666 0 : }
667 :
668 0 : void ODbaseTable::refreshIndexes()
669 : {
670 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::refreshIndexes" );
671 0 : TStringVector aVector;
672 0 : if(m_pFileStream && (!m_pIndexes || m_pIndexes->getCount() == 0))
673 : {
674 0 : INetURLObject aURL;
675 0 : aURL.SetURL(getEntry(m_pConnection,m_Name));
676 :
677 0 : aURL.setExtension("inf");
678 0 : Config aInfFile(aURL.getFSysPath(INetURLObject::FSYS_DETECT));
679 0 : aInfFile.SetGroup(dBASE_III_GROUP);
680 0 : sal_uInt16 nKeyCnt = aInfFile.GetKeyCount();
681 0 : OString aKeyName;
682 :
683 0 : for (sal_uInt16 nKey = 0; nKey < nKeyCnt; nKey++)
684 : {
685 : // Refences the key an index-file?
686 0 : aKeyName = aInfFile.GetKeyName( nKey );
687 : //...if yes, add the index list of the table
688 0 : if (aKeyName.copy(0,3).equals("NDX"))
689 : {
690 0 : OString aIndexName = aInfFile.ReadKey(aKeyName);
691 0 : aURL.setName(OStringToOUString(aIndexName, m_eEncoding));
692 : try
693 : {
694 0 : Content aCnt(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
695 0 : if (aCnt.isDocument())
696 : {
697 0 : aVector.push_back(aURL.getBase());
698 0 : }
699 : }
700 0 : catch(const Exception&) // an exception is thrown when no file exists
701 : {
702 0 : }
703 : }
704 0 : }
705 : }
706 0 : if(m_pIndexes)
707 0 : m_pIndexes->reFill(aVector);
708 : else
709 0 : m_pIndexes = new ODbaseIndexes(this,m_aMutex,aVector);
710 0 : }
711 :
712 :
713 0 : void SAL_CALL ODbaseTable::disposing(void)
714 : {
715 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::disposing" );
716 0 : OFileTable::disposing();
717 0 : ::osl::MutexGuard aGuard(m_aMutex);
718 0 : m_aColumns = NULL;
719 0 : }
720 :
721 0 : Sequence< Type > SAL_CALL ODbaseTable::getTypes( ) throw(RuntimeException, std::exception)
722 : {
723 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::getTypes" );
724 0 : Sequence< Type > aTypes = OTable_TYPEDEF::getTypes();
725 0 : ::std::vector<Type> aOwnTypes;
726 0 : aOwnTypes.reserve(aTypes.getLength());
727 :
728 0 : const Type* pBegin = aTypes.getConstArray();
729 0 : const Type* pEnd = pBegin + aTypes.getLength();
730 0 : for(;pBegin != pEnd;++pBegin)
731 : {
732 0 : if(!(*pBegin == ::getCppuType((const Reference<XKeysSupplier>*)0) ||
733 0 : *pBegin == ::getCppuType((const Reference<XDataDescriptorFactory>*)0)))
734 : {
735 0 : aOwnTypes.push_back(*pBegin);
736 : }
737 : }
738 0 : aOwnTypes.push_back(::getCppuType( (const Reference< ::com::sun::star::lang::XUnoTunnel > *)0 ));
739 0 : Type *pTypes = aOwnTypes.empty() ? 0 : &aOwnTypes[0];
740 0 : return Sequence< Type >(pTypes, aOwnTypes.size());
741 : }
742 :
743 :
744 0 : Any SAL_CALL ODbaseTable::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
745 : {
746 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::queryInterface" );
747 0 : if( rType == ::getCppuType((const Reference<XKeysSupplier>*)0) ||
748 0 : rType == ::getCppuType((const Reference<XDataDescriptorFactory>*)0))
749 0 : return Any();
750 :
751 0 : Any aRet = OTable_TYPEDEF::queryInterface(rType);
752 0 : return aRet.hasValue() ? aRet : ::cppu::queryInterface(rType,static_cast< ::com::sun::star::lang::XUnoTunnel*> (this));
753 : }
754 :
755 :
756 0 : Sequence< sal_Int8 > ODbaseTable::getUnoTunnelImplementationId()
757 : {
758 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::getUnoTunnelImplementationId" );
759 : static ::cppu::OImplementationId * pId = 0;
760 0 : if (! pId)
761 : {
762 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
763 0 : if (! pId)
764 : {
765 0 : static ::cppu::OImplementationId aId;
766 0 : pId = &aId;
767 0 : }
768 : }
769 0 : return pId->getImplementationId();
770 : }
771 :
772 : // com::sun::star::lang::XUnoTunnel
773 :
774 0 : sal_Int64 ODbaseTable::getSomething( const Sequence< sal_Int8 > & rId ) throw (RuntimeException, std::exception)
775 : {
776 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::getSomething" );
777 0 : return (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
778 : ? reinterpret_cast< sal_Int64 >( this )
779 0 : : ODbaseTable_BASE::getSomething(rId);
780 : }
781 :
782 0 : sal_Bool ODbaseTable::fetchRow(OValueRefRow& _rRow,const OSQLColumns & _rCols, sal_Bool _bUseTableDefs,sal_Bool bRetrieveData)
783 : {
784 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::fetchRow" );
785 : // Read the data
786 0 : bool bIsCurRecordDeleted = (char)m_pBuffer[0] == '*';
787 :
788 : // only read the bookmark
789 :
790 : // Mark record as deleted
791 0 : _rRow->setDeleted(bIsCurRecordDeleted);
792 0 : *(_rRow->get())[0] = m_nFilePos;
793 :
794 0 : if (!bRetrieveData)
795 0 : return sal_True;
796 :
797 0 : sal_Size nByteOffset = 1;
798 : // Fields:
799 0 : OSQLColumns::Vector::const_iterator aIter = _rCols.get().begin();
800 0 : OSQLColumns::Vector::const_iterator aEnd = _rCols.get().end();
801 0 : const sal_Size nCount = _rRow->get().size();
802 0 : for (sal_Size i = 1; aIter != aEnd && nByteOffset <= m_nBufferSize && i < nCount;++aIter, i++)
803 : {
804 : // Lengths depending on data type:
805 0 : sal_Int32 nLen = 0;
806 0 : sal_Int32 nType = 0;
807 0 : if(_bUseTableDefs)
808 : {
809 0 : nLen = m_aPrecisions[i-1];
810 0 : nType = m_aTypes[i-1];
811 : }
812 : else
813 : {
814 0 : (*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION)) >>= nLen;
815 0 : (*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
816 : }
817 0 : switch(nType)
818 : {
819 : case DataType::INTEGER:
820 : case DataType::DOUBLE:
821 : case DataType::TIMESTAMP:
822 : case DataType::DATE:
823 : case DataType::BIT:
824 : case DataType::LONGVARCHAR:
825 : case DataType::LONGVARBINARY:
826 0 : nLen = m_aRealFieldLengths[i-1];
827 0 : break;
828 : case DataType::DECIMAL:
829 0 : if(_bUseTableDefs)
830 0 : nLen = SvDbaseConverter::ConvertPrecisionToDbase(nLen,m_aScales[i-1]);
831 : else
832 0 : nLen = SvDbaseConverter::ConvertPrecisionToDbase(nLen,getINT32((*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))));
833 0 : break; // the sign and the comma
834 :
835 : case DataType::BINARY:
836 : case DataType::OTHER:
837 0 : nByteOffset += nLen;
838 0 : continue;
839 : }
840 :
841 : // Is the variable bound?
842 0 : if ( !(_rRow->get())[i]->isBound() )
843 : {
844 : // No - next field.
845 0 : nByteOffset += nLen;
846 : OSL_ENSURE( nByteOffset <= m_nBufferSize ,"ByteOffset > m_nBufferSize!");
847 0 : continue;
848 : } // if ( !(_rRow->get())[i]->isBound() )
849 0 : if ( ( nByteOffset + nLen) > m_nBufferSize )
850 0 : break; // length doesn't match buffer size.
851 :
852 0 : char *pData = (char *) (m_pBuffer + nByteOffset);
853 :
854 0 : if (nType == DataType::CHAR || nType == DataType::VARCHAR)
855 : {
856 0 : sal_Int32 nLastPos = -1;
857 0 : for (sal_Int32 k = 0; k < nLen; ++k)
858 : {
859 0 : if (pData[k] != ' ')
860 : // Record last non-empty position.
861 0 : nLastPos = k;
862 : }
863 0 : if (nLastPos < 0)
864 : {
865 : // Empty string. Skip it.
866 0 : (_rRow->get())[i]->setNull();
867 : }
868 : else
869 : {
870 : // Commit the string. Use intern() to ref-count it.
871 0 : *(_rRow->get())[i] = OUString::intern(pData, static_cast<sal_Int32>(nLastPos+1), m_eEncoding);
872 0 : }
873 : } // if (nType == DataType::CHAR || nType == DataType::VARCHAR)
874 0 : else if ( DataType::TIMESTAMP == nType )
875 : {
876 0 : sal_Int32 nDate = 0,nTime = 0;
877 0 : memcpy(&nDate, pData, 4);
878 0 : memcpy(&nTime, pData+ 4, 4);
879 0 : if ( !nDate && !nTime )
880 : {
881 0 : (_rRow->get())[i]->setNull();
882 : }
883 : else
884 : {
885 0 : ::com::sun::star::util::DateTime aDateTime;
886 0 : lcl_CalDate(nDate,nTime,aDateTime);
887 0 : *(_rRow->get())[i] = aDateTime;
888 : }
889 : }
890 0 : else if ( DataType::INTEGER == nType )
891 : {
892 0 : sal_Int32 nValue = 0;
893 0 : memcpy(&nValue, pData, nLen);
894 0 : *(_rRow->get())[i] = nValue;
895 : }
896 0 : else if ( DataType::DOUBLE == nType )
897 : {
898 0 : double d = 0.0;
899 0 : if (getBOOL((*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY)))) // Currency is treated separately
900 : {
901 0 : sal_Int64 nValue = 0;
902 0 : memcpy(&nValue, pData, nLen);
903 :
904 0 : if ( m_aScales[i-1] )
905 0 : d = (double)(nValue / pow(10.0,(int)m_aScales[i-1]));
906 : else
907 0 : d = (double)(nValue);
908 : }
909 : else
910 : {
911 0 : memcpy(&d, pData, nLen);
912 : }
913 :
914 0 : *(_rRow->get())[i] = d;
915 : }
916 : else
917 : {
918 0 : sal_Int32 nPos1 = -1, nPos2 = -1;
919 : // If the string contains Nul-characters, then convert them to blanks!
920 0 : for (sal_Int32 k = 0; k < nLen; k++)
921 : {
922 0 : if (pData[k] == '\0')
923 0 : pData[k] = ' ';
924 :
925 0 : if (pData[k] != ' ')
926 : {
927 0 : if (nPos1 < 0)
928 : // first non-empty char position.
929 0 : nPos1 = k;
930 :
931 : // last non-empty char position.
932 0 : nPos2 = k;
933 : }
934 : }
935 :
936 0 : if (nPos1 < 0)
937 : {
938 : // Empty string. Skip it.
939 0 : nByteOffset += nLen;
940 0 : (_rRow->get())[i]->setNull(); // no values -> done
941 0 : continue;
942 : }
943 :
944 0 : OUString aStr = OUString::intern(pData+nPos1, nPos2-nPos1+1, m_eEncoding);
945 :
946 0 : switch (nType)
947 : {
948 : case DataType::DATE:
949 : {
950 0 : if (aStr.getLength() != nLen)
951 : {
952 0 : (_rRow->get())[i]->setNull();
953 0 : break;
954 : }
955 0 : const sal_uInt16 nYear = (sal_uInt16)aStr.copy( 0, 4 ).toInt32();
956 0 : const sal_uInt16 nMonth = (sal_uInt16)aStr.copy( 4, 2 ).toInt32();
957 0 : const sal_uInt16 nDay = (sal_uInt16)aStr.copy( 6, 2 ).toInt32();
958 :
959 0 : const ::com::sun::star::util::Date aDate(nDay,nMonth,nYear);
960 0 : *(_rRow->get())[i] = aDate;
961 : }
962 0 : break;
963 : case DataType::DECIMAL:
964 0 : *(_rRow->get())[i] = ORowSetValue(aStr);
965 0 : break;
966 : case DataType::BIT:
967 : {
968 : sal_Bool b;
969 0 : switch (* ((const char *)pData))
970 : {
971 : case 'T':
972 : case 'Y':
973 0 : case 'J': b = sal_True; break;
974 0 : default: b = sal_False; break;
975 : }
976 0 : *(_rRow->get())[i] = b;
977 : }
978 0 : break;
979 : case DataType::LONGVARBINARY:
980 : case DataType::BINARY:
981 : case DataType::LONGVARCHAR:
982 : {
983 0 : const long nBlockNo = aStr.toInt32(); // read blocknumber
984 0 : if (nBlockNo > 0 && m_pMemoStream) // Read data from memo-file, only if
985 : {
986 0 : if ( !ReadMemo(nBlockNo, (_rRow->get())[i]->get()) )
987 0 : break;
988 : }
989 : else
990 0 : (_rRow->get())[i]->setNull();
991 0 : } break;
992 : default:
993 : SAL_WARN( "connectivity.drivers","Falscher Type");
994 : }
995 0 : (_rRow->get())[i]->setTypeKind(nType);
996 : }
997 :
998 0 : nByteOffset += nLen;
999 : OSL_ENSURE( nByteOffset <= m_nBufferSize ,"ByteOffset > m_nBufferSize!");
1000 : }
1001 0 : return sal_True;
1002 : }
1003 :
1004 :
1005 0 : void ODbaseTable::FileClose()
1006 : {
1007 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::FileClose" );
1008 0 : ::osl::MutexGuard aGuard(m_aMutex);
1009 : // if not everything has been written yet
1010 0 : if (m_pMemoStream && m_pMemoStream->IsWritable())
1011 0 : m_pMemoStream->Flush();
1012 :
1013 0 : delete m_pMemoStream;
1014 0 : m_pMemoStream = NULL;
1015 :
1016 0 : ODbaseTable_BASE::FileClose();
1017 0 : }
1018 :
1019 0 : sal_Bool ODbaseTable::CreateImpl()
1020 : {
1021 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::CreateImpl" );
1022 : OSL_ENSURE(!m_pFileStream, "SequenceError");
1023 :
1024 0 : if ( m_pConnection->isCheckEnabled() && ::dbtools::convertName2SQLName(m_Name,OUString()) != m_Name )
1025 : {
1026 0 : const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
1027 : STR_SQL_NAME_ERROR,
1028 : "$name$", m_Name
1029 0 : ) );
1030 0 : ::dbtools::throwGenericSQLException( sError, *this );
1031 : }
1032 :
1033 0 : INetURLObject aURL;
1034 0 : aURL.SetSmartProtocol(INET_PROT_FILE);
1035 0 : OUString aName = getEntry(m_pConnection, m_Name);
1036 0 : if(aName.isEmpty())
1037 : {
1038 0 : OUString aIdent = m_pConnection->getContent()->getIdentifier()->getContentIdentifier();
1039 0 : if ( aIdent.lastIndexOf('/') != (aIdent.getLength()-1) )
1040 0 : aIdent += "/";
1041 0 : aIdent += m_Name;
1042 0 : aName = aIdent.getStr();
1043 : }
1044 0 : aURL.SetURL(aName);
1045 :
1046 0 : if ( !m_pConnection->matchesExtension( aURL.getExtension() ) )
1047 0 : aURL.setExtension(m_pConnection->getExtension());
1048 :
1049 : try
1050 : {
1051 0 : Content aContent(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
1052 0 : if (aContent.isDocument())
1053 : {
1054 : // Only if the file exists with length > 0 raise an error
1055 0 : SvStream* pFileStream = createStream_simpleError( aURL.GetMainURL(INetURLObject::NO_DECODE),STREAM_READ);
1056 :
1057 0 : if (pFileStream && pFileStream->Seek(STREAM_SEEK_TO_END))
1058 : {
1059 0 : return sal_False;
1060 : }
1061 0 : delete pFileStream;
1062 0 : }
1063 : }
1064 0 : catch(const Exception&) // an exception is thrown when no file exists
1065 : {
1066 : }
1067 :
1068 0 : sal_Bool bMemoFile = sal_False;
1069 :
1070 0 : sal_Bool bOk = CreateFile(aURL, bMemoFile);
1071 :
1072 0 : FileClose();
1073 :
1074 0 : if (!bOk)
1075 : {
1076 : try
1077 : {
1078 0 : Content aContent(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
1079 0 : aContent.executeCommand( "delete", css::uno::Any( true ) );
1080 : }
1081 0 : catch(const Exception&) // an exception is thrown when no file exists
1082 : {
1083 : }
1084 0 : return sal_False;
1085 : }
1086 :
1087 0 : if (bMemoFile)
1088 : {
1089 0 : OUString aExt = aURL.getExtension();
1090 0 : aURL.setExtension("dbt"); // extension for memo file
1091 0 : Content aMemo1Content(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
1092 :
1093 0 : sal_Bool bMemoAlreadyExists = sal_False;
1094 : try
1095 : {
1096 0 : bMemoAlreadyExists = aMemo1Content.isDocument();
1097 : }
1098 0 : catch(const Exception&) // an exception is thrown when no file exists
1099 : {
1100 : }
1101 0 : if (bMemoAlreadyExists)
1102 : {
1103 0 : aURL.setExtension(aExt); // kill dbf file
1104 : try
1105 : {
1106 0 : Content aMemoContent(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
1107 0 : aMemoContent.executeCommand( "delete", css::uno::Any( true ) );
1108 : }
1109 0 : catch(const Exception&)
1110 : {
1111 :
1112 0 : const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
1113 : STR_COULD_NOT_DELETE_FILE,
1114 : "$name$", aName
1115 0 : ) );
1116 0 : ::dbtools::throwGenericSQLException( sError, *this );
1117 : }
1118 : }
1119 0 : if (!CreateMemoFile(aURL))
1120 : {
1121 0 : aURL.setExtension(aExt); // kill dbf file
1122 0 : Content aMemoContent(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
1123 0 : aMemoContent.executeCommand( "delete", css::uno::Any( true ) );
1124 0 : return sal_False;
1125 : }
1126 0 : m_aHeader.db_typ = dBaseIIIMemo;
1127 : }
1128 : else
1129 0 : m_aHeader.db_typ = dBaseIII;
1130 :
1131 0 : return sal_True;
1132 : }
1133 :
1134 0 : void ODbaseTable::throwInvalidColumnType(const sal_uInt16 _nErrorId,const OUString& _sColumnName)
1135 : {
1136 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::throwInvalidColumnType" );
1137 : try
1138 : {
1139 : // we have to drop the file because it is corrupted now
1140 0 : DropImpl();
1141 : }
1142 0 : catch(const Exception&)
1143 : {
1144 : }
1145 :
1146 0 : const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
1147 : _nErrorId,
1148 : "$columnname$", _sColumnName
1149 0 : ) );
1150 0 : ::dbtools::throwGenericSQLException( sError, *this );
1151 0 : }
1152 :
1153 : // creates in principle dBase IV file format
1154 0 : sal_Bool ODbaseTable::CreateFile(const INetURLObject& aFile, sal_Bool& bCreateMemo)
1155 : {
1156 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::CreateFile" );
1157 0 : bCreateMemo = sal_False;
1158 0 : Date aDate( Date::SYSTEM ); // current date
1159 :
1160 0 : m_pFileStream = createStream_simpleError( aFile.GetMainURL(INetURLObject::NO_DECODE),STREAM_READWRITE | STREAM_SHARE_DENYWRITE | STREAM_TRUNC );
1161 :
1162 0 : if (!m_pFileStream)
1163 0 : return sal_False;
1164 :
1165 0 : sal_uInt8 nDbaseType = dBaseIII;
1166 0 : Reference<XIndexAccess> xColumns(getColumns(),UNO_QUERY);
1167 0 : Reference<XPropertySet> xCol;
1168 0 : const OUString sPropType = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE);
1169 :
1170 : try
1171 : {
1172 0 : const sal_Int32 nCount = xColumns->getCount();
1173 0 : for(sal_Int32 i=0;i<nCount;++i)
1174 : {
1175 0 : xColumns->getByIndex(i) >>= xCol;
1176 : OSL_ENSURE(xCol.is(),"This should be a column!");
1177 :
1178 0 : switch (getINT32(xCol->getPropertyValue(sPropType)))
1179 : {
1180 : case DataType::DOUBLE:
1181 : case DataType::INTEGER:
1182 : case DataType::TIMESTAMP:
1183 : case DataType::LONGVARBINARY:
1184 0 : nDbaseType = VisualFoxPro;
1185 0 : i = nCount; // no more columns need to be checked
1186 0 : break;
1187 : } // switch (getINT32(xCol->getPropertyValue(sPropType)))
1188 : }
1189 : }
1190 0 : catch ( const Exception& e )
1191 : {
1192 : (void)e;
1193 :
1194 : try
1195 : {
1196 : // we have to drop the file because it is corrupted now
1197 0 : DropImpl();
1198 : }
1199 0 : catch(const Exception&) { }
1200 0 : throw;
1201 : }
1202 :
1203 : char aBuffer[21]; // write buffer
1204 0 : memset(aBuffer,0,sizeof(aBuffer));
1205 :
1206 0 : m_pFileStream->Seek(0L);
1207 0 : (*m_pFileStream).WriteUChar( (sal_uInt8) nDbaseType ); // dBase format
1208 0 : (*m_pFileStream).WriteUChar( (sal_uInt8) (aDate.GetYear() % 100) ); // current date
1209 :
1210 :
1211 0 : (*m_pFileStream).WriteUChar( (sal_uInt8) aDate.GetMonth() );
1212 0 : (*m_pFileStream).WriteUChar( (sal_uInt8) aDate.GetDay() );
1213 0 : (*m_pFileStream).WriteUInt32( (sal_uInt32)0 ); // number of data records
1214 0 : (*m_pFileStream).WriteUInt16( (sal_uInt16)((m_pColumns->getCount()+1) * 32 + 1) ); // header information,
1215 : // pColumns contains always an additional column
1216 0 : (*m_pFileStream).WriteUInt16( (sal_uInt16) 0 ); // record length will be determined later
1217 0 : m_pFileStream->Write(aBuffer, 20);
1218 :
1219 0 : sal_uInt16 nRecLength = 1; // Length 1 for deleted flag
1220 0 : sal_Int32 nMaxFieldLength = m_pConnection->getMetaData()->getMaxColumnNameLength();
1221 0 : OUString aName;
1222 0 : const OUString sPropName = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME);
1223 0 : const OUString sPropPrec = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION);
1224 0 : const OUString sPropScale = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE);
1225 :
1226 : try
1227 : {
1228 0 : const sal_Int32 nCount = xColumns->getCount();
1229 0 : for(sal_Int32 i=0;i<nCount;++i)
1230 : {
1231 0 : xColumns->getByIndex(i) >>= xCol;
1232 : OSL_ENSURE(xCol.is(),"This should be a column!");
1233 :
1234 0 : char cTyp( 'C' );
1235 :
1236 0 : xCol->getPropertyValue(sPropName) >>= aName;
1237 :
1238 0 : OString aCol;
1239 0 : if ( DBTypeConversion::convertUnicodeString( aName, aCol, m_eEncoding ) > nMaxFieldLength)
1240 : {
1241 0 : throwInvalidColumnType( STR_INVALID_COLUMN_NAME_LENGTH, aName );
1242 : }
1243 :
1244 0 : (*m_pFileStream).WriteCharPtr( aCol.getStr() );
1245 0 : m_pFileStream->Write(aBuffer, 11 - aCol.getLength());
1246 :
1247 0 : sal_Int32 nPrecision = 0;
1248 0 : xCol->getPropertyValue(sPropPrec) >>= nPrecision;
1249 0 : sal_Int32 nScale = 0;
1250 0 : xCol->getPropertyValue(sPropScale) >>= nScale;
1251 :
1252 0 : bool bBinary = false;
1253 :
1254 0 : switch (getINT32(xCol->getPropertyValue(sPropType)))
1255 : {
1256 : case DataType::CHAR:
1257 : case DataType::VARCHAR:
1258 0 : cTyp = 'C';
1259 0 : break;
1260 : case DataType::DOUBLE:
1261 0 : if (getBOOL(xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY)))) // Currency will be treated separately
1262 0 : cTyp = 'Y';
1263 : else
1264 0 : cTyp = 'B';
1265 0 : break;
1266 : case DataType::INTEGER:
1267 0 : cTyp = 'I';
1268 0 : break;
1269 : case DataType::TINYINT:
1270 : case DataType::SMALLINT:
1271 : case DataType::BIGINT:
1272 : case DataType::DECIMAL:
1273 : case DataType::NUMERIC:
1274 : case DataType::REAL:
1275 0 : cTyp = 'N'; // only dBase 3 format
1276 0 : break;
1277 : case DataType::TIMESTAMP:
1278 0 : cTyp = 'T';
1279 0 : break;
1280 : case DataType::DATE:
1281 0 : cTyp = 'D';
1282 0 : break;
1283 : case DataType::BIT:
1284 0 : cTyp = 'L';
1285 0 : break;
1286 : case DataType::LONGVARBINARY:
1287 0 : bBinary = true;
1288 : // run through
1289 : case DataType::LONGVARCHAR:
1290 0 : cTyp = 'M';
1291 0 : break;
1292 : default:
1293 : {
1294 0 : throwInvalidColumnType(STR_INVALID_COLUMN_TYPE, aName);
1295 : }
1296 : }
1297 :
1298 0 : (*m_pFileStream).WriteChar( cTyp );
1299 0 : if ( nDbaseType == VisualFoxPro )
1300 0 : (*m_pFileStream).WriteUInt32( sal_uInt32(nRecLength-1) );
1301 : else
1302 0 : m_pFileStream->Write(aBuffer, 4);
1303 :
1304 0 : switch(cTyp)
1305 : {
1306 : case 'C':
1307 : OSL_ENSURE(nPrecision < 255, "ODbaseTable::Create: Column zu lang!");
1308 0 : if (nPrecision > 254)
1309 : {
1310 0 : throwInvalidColumnType(STR_INVALID_COLUMN_PRECISION, aName);
1311 : }
1312 0 : (*m_pFileStream).WriteUChar( (sal_uInt8) std::min((unsigned)nPrecision, 255U) ); // field length
1313 0 : nRecLength = nRecLength + (sal_uInt16)::std::min((sal_uInt16)nPrecision, (sal_uInt16)255UL);
1314 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)0 ); // decimals
1315 0 : break;
1316 : case 'F':
1317 : case 'N':
1318 : OSL_ENSURE(nPrecision >= nScale,
1319 : "ODbaseTable::Create: Feldlaenge muss groesser Nachkommastellen sein!");
1320 0 : if (nPrecision < nScale)
1321 : {
1322 0 : throwInvalidColumnType(STR_INVALID_PRECISION_SCALE, aName);
1323 : }
1324 0 : if (getBOOL(xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY)))) // Currency will be treated separately
1325 : {
1326 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)10 ); // standard length
1327 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)4 );
1328 0 : nRecLength += 10;
1329 : }
1330 : else
1331 : {
1332 0 : sal_Int32 nPrec = SvDbaseConverter::ConvertPrecisionToDbase(nPrecision,nScale);
1333 :
1334 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)( nPrec) );
1335 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)nScale );
1336 0 : nRecLength += (sal_uInt16)nPrec;
1337 : }
1338 0 : break;
1339 : case 'L':
1340 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)1 );
1341 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)0 );
1342 0 : ++nRecLength;
1343 0 : break;
1344 : case 'I':
1345 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)4 );
1346 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)0 );
1347 0 : nRecLength += 4;
1348 0 : break;
1349 : case 'Y':
1350 : case 'B':
1351 : case 'T':
1352 : case 'D':
1353 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)8 );
1354 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)0 );
1355 0 : nRecLength += 8;
1356 0 : break;
1357 : case 'M':
1358 0 : bCreateMemo = sal_True;
1359 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)10 );
1360 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)0 );
1361 0 : nRecLength += 10;
1362 0 : if ( bBinary )
1363 0 : aBuffer[0] = 0x06;
1364 0 : break;
1365 : default:
1366 0 : throwInvalidColumnType(STR_INVALID_COLUMN_TYPE, aName);
1367 : }
1368 0 : m_pFileStream->Write(aBuffer, 14);
1369 0 : aBuffer[0] = 0x00;
1370 0 : }
1371 :
1372 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)FIELD_DESCRIPTOR_TERMINATOR ); // end of header
1373 0 : (*m_pFileStream).WriteChar( (char)DBF_EOL );
1374 0 : m_pFileStream->Seek(10L);
1375 0 : (*m_pFileStream).WriteUInt16( nRecLength ); // set record length afterwards
1376 :
1377 0 : if (bCreateMemo)
1378 : {
1379 0 : m_pFileStream->Seek(0L);
1380 0 : if (nDbaseType == VisualFoxPro)
1381 0 : (*m_pFileStream).WriteUChar( (sal_uInt8) FoxProMemo );
1382 : else
1383 0 : (*m_pFileStream).WriteUChar( (sal_uInt8) dBaseIIIMemo );
1384 : } // if (bCreateMemo)
1385 : }
1386 0 : catch ( const Exception& e )
1387 : {
1388 : (void)e;
1389 :
1390 : try
1391 : {
1392 : // we have to drop the file because it is corrupted now
1393 0 : DropImpl();
1394 : }
1395 0 : catch(const Exception&) { }
1396 0 : throw;
1397 : }
1398 0 : return sal_True;
1399 : }
1400 :
1401 :
1402 : // creates in principle dBase III file format
1403 0 : sal_Bool ODbaseTable::CreateMemoFile(const INetURLObject& aFile)
1404 : {
1405 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::CreateMemoFile" );
1406 : // filehandling macro for table creation
1407 0 : m_pMemoStream = createStream_simpleError( aFile.GetMainURL(INetURLObject::NO_DECODE),STREAM_READWRITE | STREAM_SHARE_DENYWRITE);
1408 :
1409 0 : if (!m_pMemoStream)
1410 0 : return sal_False;
1411 :
1412 0 : m_pMemoStream->SetStreamSize(512);
1413 :
1414 0 : m_pMemoStream->Seek(0L);
1415 0 : (*m_pMemoStream).WriteUInt32( sal_uInt32(1) ); // pointer to the first free block
1416 :
1417 0 : m_pMemoStream->Flush();
1418 0 : delete m_pMemoStream;
1419 0 : m_pMemoStream = NULL;
1420 0 : return sal_True;
1421 : }
1422 :
1423 0 : sal_Bool ODbaseTable::Drop_Static(const OUString& _sUrl,sal_Bool _bHasMemoFields,OCollection* _pIndexes )
1424 : {
1425 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::Drop_Static" );
1426 0 : INetURLObject aURL;
1427 0 : aURL.SetURL(_sUrl);
1428 :
1429 0 : sal_Bool bDropped = ::utl::UCBContentHelper::Kill(aURL.GetMainURL(INetURLObject::NO_DECODE));
1430 :
1431 0 : if(bDropped)
1432 : {
1433 0 : if (_bHasMemoFields)
1434 : { // delete the memo fields
1435 0 : aURL.setExtension("dbt");
1436 0 : bDropped = ::utl::UCBContentHelper::Kill(aURL.GetMainURL(INetURLObject::NO_DECODE));
1437 : }
1438 :
1439 0 : if(bDropped)
1440 : {
1441 0 : if(_pIndexes)
1442 : {
1443 : try
1444 : {
1445 0 : sal_Int32 i = _pIndexes->getCount();
1446 0 : while (i)
1447 : {
1448 0 : _pIndexes->dropByIndex(--i);
1449 : }
1450 : }
1451 0 : catch(const SQLException&)
1452 : {
1453 : }
1454 : }
1455 0 : aURL.setExtension("inf");
1456 :
1457 : // as the inf file does not necessarily exist, we aren't allowed to use UCBContentHelper::Kill
1458 : try
1459 : {
1460 0 : ::ucbhelper::Content aDeleteContent( aURL.GetMainURL( INetURLObject::NO_DECODE ), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext() );
1461 0 : aDeleteContent.executeCommand( "delete", makeAny( sal_Bool( sal_True ) ) );
1462 : }
1463 0 : catch(const Exception&)
1464 : {
1465 : // silently ignore this ....
1466 : }
1467 : }
1468 : }
1469 0 : return bDropped;
1470 : }
1471 :
1472 0 : sal_Bool ODbaseTable::DropImpl()
1473 : {
1474 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::DropImpl" );
1475 0 : FileClose();
1476 :
1477 0 : if(!m_pIndexes)
1478 0 : refreshIndexes(); // look for indexes which must be deleted as well
1479 :
1480 0 : sal_Bool bDropped = Drop_Static(getEntry(m_pConnection,m_Name),HasMemoFields(),m_pIndexes);
1481 0 : if(!bDropped)
1482 : {// we couldn't drop the table so we have to reopen it
1483 0 : construct();
1484 0 : if(m_pColumns)
1485 0 : m_pColumns->refresh();
1486 : }
1487 0 : return bDropped;
1488 : }
1489 :
1490 :
1491 0 : sal_Bool ODbaseTable::InsertRow(OValueRefVector& rRow, sal_Bool bFlush,const Reference<XIndexAccess>& _xCols)
1492 : {
1493 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::InsertRow" );
1494 : // fill buffer with blanks
1495 0 : AllocBuffer();
1496 0 : memset(m_pBuffer, 0, m_aHeader.db_slng);
1497 0 : m_pBuffer[0] = ' ';
1498 :
1499 : // Copy new row completely:
1500 : // ... and add at the end as new Record:
1501 0 : sal_Size nTempPos = m_nFilePos;
1502 :
1503 0 : m_nFilePos = (sal_Size)m_aHeader.db_anz + 1;
1504 0 : sal_Bool bInsertRow = UpdateBuffer( rRow, NULL, _xCols, true );
1505 0 : if ( bInsertRow )
1506 : {
1507 0 : sal_Size nFileSize = 0, nMemoFileSize = 0;
1508 :
1509 0 : nFileSize = lcl_getFileSize(*m_pFileStream);
1510 :
1511 0 : if (HasMemoFields() && m_pMemoStream)
1512 : {
1513 0 : m_pMemoStream->Seek(STREAM_SEEK_TO_END);
1514 0 : nMemoFileSize = m_pMemoStream->Tell();
1515 : }
1516 :
1517 0 : if (!WriteBuffer())
1518 : {
1519 0 : m_pFileStream->SetStreamSize(nFileSize); // restore old size
1520 :
1521 0 : if (HasMemoFields() && m_pMemoStream)
1522 0 : m_pMemoStream->SetStreamSize(nMemoFileSize); // restore old size
1523 0 : m_nFilePos = nTempPos; // restore file position
1524 : }
1525 : else
1526 : {
1527 0 : (*m_pFileStream).WriteChar( (char)DBF_EOL ); // write EOL
1528 : // raise number of datasets in the header:
1529 0 : m_pFileStream->Seek( 4L );
1530 0 : (*m_pFileStream).WriteUInt32( m_aHeader.db_anz + 1 );
1531 :
1532 : // if AppendOnly no flush!
1533 0 : if (bFlush)
1534 0 : m_pFileStream->Flush();
1535 :
1536 : // raise number if successfully
1537 0 : m_aHeader.db_anz++;
1538 0 : *rRow.get()[0] = m_nFilePos; // set bookmark
1539 0 : m_nFilePos = nTempPos;
1540 : }
1541 : }
1542 : else
1543 0 : m_nFilePos = nTempPos;
1544 :
1545 0 : return bInsertRow;
1546 : }
1547 :
1548 :
1549 0 : sal_Bool ODbaseTable::UpdateRow(OValueRefVector& rRow, OValueRefRow& pOrgRow,const Reference<XIndexAccess>& _xCols)
1550 : {
1551 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::UpdateRow" );
1552 : // fill buffer with blanks
1553 0 : AllocBuffer();
1554 :
1555 : // position on desired record:
1556 0 : sal_Size nPos = m_aHeader.db_kopf + (long)(m_nFilePos-1) * m_aHeader.db_slng;
1557 0 : m_pFileStream->Seek(nPos);
1558 0 : m_pFileStream->Read((char*)m_pBuffer, m_aHeader.db_slng);
1559 :
1560 0 : sal_Size nMemoFileSize( 0 );
1561 0 : if (HasMemoFields() && m_pMemoStream)
1562 : {
1563 0 : m_pMemoStream->Seek(STREAM_SEEK_TO_END);
1564 0 : nMemoFileSize = m_pMemoStream->Tell();
1565 : }
1566 0 : if (!UpdateBuffer(rRow, pOrgRow, _xCols, false) || !WriteBuffer())
1567 : {
1568 0 : if (HasMemoFields() && m_pMemoStream)
1569 0 : m_pMemoStream->SetStreamSize(nMemoFileSize); // restore old size
1570 : }
1571 : else
1572 : {
1573 0 : m_pFileStream->Flush();
1574 : }
1575 0 : return sal_True;
1576 : }
1577 :
1578 :
1579 0 : sal_Bool ODbaseTable::DeleteRow(const OSQLColumns& _rCols)
1580 : {
1581 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::DeleteRow" );
1582 : // Set the Delete-Flag (be it set or not):
1583 : // Position on desired record:
1584 0 : sal_Size nFilePos = m_aHeader.db_kopf + (long)(m_nFilePos-1) * m_aHeader.db_slng;
1585 0 : m_pFileStream->Seek(nFilePos);
1586 :
1587 0 : OValueRefRow aRow = new OValueRefVector(_rCols.get().size());
1588 :
1589 0 : if (!fetchRow(aRow,_rCols,sal_True,sal_True))
1590 0 : return sal_False;
1591 :
1592 0 : Reference<XPropertySet> xCol;
1593 0 : OUString aColName;
1594 0 : ::comphelper::UStringMixEqual aCase(isCaseSensitive());
1595 0 : for (sal_uInt16 i = 0; i < m_pColumns->getCount(); i++)
1596 : {
1597 0 : Reference<XPropertySet> xIndex = isUniqueByColumnName(i);
1598 0 : if (xIndex.is())
1599 : {
1600 0 : xCol.set(m_pColumns->getByIndex(i), css::uno::UNO_QUERY);
1601 : OSL_ENSURE(xCol.is(),"ODbaseTable::DeleteRow column is null!");
1602 0 : if(xCol.is())
1603 : {
1604 0 : xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
1605 :
1606 0 : Reference<XUnoTunnel> xTunnel(xIndex,UNO_QUERY);
1607 : OSL_ENSURE(xTunnel.is(),"No TunnelImplementation!");
1608 0 : ODbaseIndex* pIndex = reinterpret_cast< ODbaseIndex* >( xTunnel->getSomething(ODbaseIndex::getUnoTunnelImplementationId()) );
1609 : OSL_ENSURE(pIndex,"ODbaseTable::DeleteRow: No Index returned!");
1610 :
1611 0 : OSQLColumns::Vector::const_iterator aIter = _rCols.get().begin();
1612 0 : sal_Int32 nPos = 1;
1613 0 : for(;aIter != _rCols.get().end();++aIter,++nPos)
1614 : {
1615 0 : if(aCase(getString((*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME))),aColName))
1616 0 : break;
1617 : }
1618 0 : if (aIter == _rCols.get().end())
1619 0 : continue;
1620 :
1621 0 : pIndex->Delete(m_nFilePos,*(aRow->get())[nPos]);
1622 : }
1623 : }
1624 0 : }
1625 :
1626 0 : m_pFileStream->Seek(nFilePos);
1627 0 : (*m_pFileStream).WriteUChar( (sal_uInt8)'*' ); // mark the row in the table as deleted
1628 0 : m_pFileStream->Flush();
1629 0 : return sal_True;
1630 : }
1631 :
1632 0 : Reference<XPropertySet> ODbaseTable::isUniqueByColumnName(sal_Int32 _nColumnPos)
1633 : {
1634 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::isUniqueByColumnName" );
1635 0 : if(!m_pIndexes)
1636 0 : refreshIndexes();
1637 0 : if(m_pIndexes->hasElements())
1638 : {
1639 0 : Reference<XPropertySet> xCol;
1640 0 : m_pColumns->getByIndex(_nColumnPos) >>= xCol;
1641 : OSL_ENSURE(xCol.is(),"ODbaseTable::isUniqueByColumnName column is null!");
1642 0 : OUString sColName;
1643 0 : xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= sColName;
1644 :
1645 0 : Reference<XPropertySet> xIndex;
1646 0 : for(sal_Int32 i=0;i<m_pIndexes->getCount();++i)
1647 : {
1648 0 : xIndex.set(m_pIndexes->getByIndex(i), css::uno::UNO_QUERY);
1649 0 : if(xIndex.is() && getBOOL(xIndex->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE))))
1650 : {
1651 0 : Reference<XNameAccess> xCols(Reference<XColumnsSupplier>(xIndex,UNO_QUERY)->getColumns());
1652 0 : if(xCols->hasByName(sColName))
1653 0 : return xIndex;
1654 :
1655 : }
1656 0 : }
1657 : }
1658 0 : return Reference<XPropertySet>();
1659 : }
1660 :
1661 0 : static double toDouble(const OString& rString)
1662 : {
1663 0 : return ::rtl::math::stringToDouble( rString, '.', ',', NULL, NULL );
1664 : }
1665 :
1666 :
1667 0 : sal_Bool ODbaseTable::UpdateBuffer(OValueRefVector& rRow, OValueRefRow pOrgRow, const Reference<XIndexAccess>& _xCols, const bool bForceAllFields)
1668 : {
1669 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::UpdateBuffer" );
1670 : OSL_ENSURE(m_pBuffer,"Buffer is NULL!");
1671 0 : if ( !m_pBuffer )
1672 0 : return sal_False;
1673 0 : sal_Int32 nByteOffset = 1;
1674 :
1675 : // Update fields:
1676 0 : Reference<XPropertySet> xCol;
1677 0 : Reference<XPropertySet> xIndex;
1678 : sal_uInt16 i;
1679 0 : OUString aColName;
1680 0 : const sal_Int32 nColumnCount = m_pColumns->getCount();
1681 0 : ::std::vector< Reference<XPropertySet> > aIndexedCols(nColumnCount);
1682 :
1683 0 : ::comphelper::UStringMixEqual aCase(isCaseSensitive());
1684 :
1685 0 : Reference<XIndexAccess> xColumns = m_pColumns;
1686 : // first search a key that exist already in the table
1687 0 : for (i = 0; i < nColumnCount; ++i)
1688 : {
1689 0 : sal_Int32 nPos = i;
1690 0 : if(_xCols != xColumns)
1691 : {
1692 0 : m_pColumns->getByIndex(i) >>= xCol;
1693 : OSL_ENSURE(xCol.is(),"ODbaseTable::UpdateBuffer column is null!");
1694 0 : xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
1695 :
1696 0 : for(nPos = 0;nPos<_xCols->getCount();++nPos)
1697 : {
1698 : Reference<XPropertySet> xFindCol(
1699 0 : _xCols->getByIndex(nPos), css::uno::UNO_QUERY);
1700 : OSL_ENSURE(xFindCol.is(),"ODbaseTable::UpdateBuffer column is null!");
1701 0 : if(aCase(getString(xFindCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))),aColName))
1702 0 : break;
1703 0 : }
1704 0 : if (nPos >= _xCols->getCount())
1705 0 : continue;
1706 : }
1707 :
1708 0 : ++nPos;
1709 0 : xIndex = isUniqueByColumnName(i);
1710 0 : aIndexedCols[i] = xIndex;
1711 0 : if (xIndex.is())
1712 : {
1713 : // first check if the value is different to the old one and when if it conform to the index
1714 0 : if(pOrgRow.is() && (rRow.get()[nPos]->getValue().isNull() || rRow.get()[nPos] == (pOrgRow->get())[nPos]))
1715 0 : continue;
1716 : else
1717 : {
1718 0 : Reference<XUnoTunnel> xTunnel(xIndex,UNO_QUERY);
1719 : OSL_ENSURE(xTunnel.is(),"No TunnelImplementation!");
1720 0 : ODbaseIndex* pIndex = reinterpret_cast< ODbaseIndex* >( xTunnel->getSomething(ODbaseIndex::getUnoTunnelImplementationId()) );
1721 : OSL_ENSURE(pIndex,"ODbaseTable::UpdateBuffer: No Index returned!");
1722 :
1723 0 : if (pIndex->Find(0,*rRow.get()[nPos]))
1724 : {
1725 : // There is no unique value
1726 0 : if ( aColName.isEmpty() )
1727 : {
1728 0 : m_pColumns->getByIndex(i) >>= xCol;
1729 : OSL_ENSURE(xCol.is(),"ODbaseTable::UpdateBuffer column is null!");
1730 0 : xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
1731 0 : xCol.clear();
1732 : } // if ( !aColName.getLength() )
1733 0 : const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
1734 : STR_DUPLICATE_VALUE_IN_COLUMN
1735 : ,"$columnname$", aColName
1736 0 : ) );
1737 0 : ::dbtools::throwGenericSQLException( sError, *this );
1738 0 : }
1739 : }
1740 : }
1741 : }
1742 :
1743 : // when we are here there is no double key in the table
1744 :
1745 0 : for (i = 0; i < nColumnCount && nByteOffset <= m_nBufferSize ; ++i)
1746 : {
1747 : // Lengths for each data type:
1748 : OSL_ENSURE(i < m_aPrecisions.size(),"Illegal index!");
1749 0 : sal_Int32 nLen = 0;
1750 0 : sal_Int32 nType = 0;
1751 0 : sal_Int32 nScale = 0;
1752 0 : if ( i < m_aPrecisions.size() )
1753 : {
1754 0 : nLen = m_aPrecisions[i];
1755 0 : nType = m_aTypes[i];
1756 0 : nScale = m_aScales[i];
1757 : }
1758 : else
1759 : {
1760 0 : m_pColumns->getByIndex(i) >>= xCol;
1761 0 : if ( xCol.is() )
1762 : {
1763 0 : xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION)) >>= nLen;
1764 0 : xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
1765 0 : xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE)) >>= nScale;
1766 : }
1767 : }
1768 :
1769 0 : bool bSetZero = false;
1770 0 : switch (nType)
1771 : {
1772 : case DataType::INTEGER:
1773 : case DataType::DOUBLE:
1774 : case DataType::TIMESTAMP:
1775 0 : bSetZero = true;
1776 : case DataType::LONGVARBINARY:
1777 : case DataType::DATE:
1778 : case DataType::BIT:
1779 : case DataType::LONGVARCHAR:
1780 0 : nLen = m_aRealFieldLengths[i];
1781 0 : break;
1782 : case DataType::DECIMAL:
1783 0 : nLen = SvDbaseConverter::ConvertPrecisionToDbase(nLen,nScale);
1784 0 : break; // The sign and the comma
1785 : default:
1786 0 : break;
1787 :
1788 : } // switch (nType)
1789 :
1790 0 : sal_Int32 nPos = i;
1791 0 : if(_xCols != xColumns)
1792 : {
1793 0 : m_pColumns->getByIndex(i) >>= xCol;
1794 : OSL_ENSURE(xCol.is(),"ODbaseTable::UpdateBuffer column is null!");
1795 0 : xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
1796 0 : for(nPos = 0;nPos<_xCols->getCount();++nPos)
1797 : {
1798 : Reference<XPropertySet> xFindCol(
1799 0 : _xCols->getByIndex(nPos), css::uno::UNO_QUERY);
1800 0 : if(aCase(getString(xFindCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))),aColName))
1801 0 : break;
1802 0 : }
1803 0 : if (nPos >= _xCols->getCount())
1804 : {
1805 0 : nByteOffset += nLen;
1806 0 : continue;
1807 : }
1808 : }
1809 :
1810 :
1811 :
1812 0 : ++nPos; // the row values start at 1
1813 0 : const ORowSetValue &thisColVal = rRow.get()[nPos]->get();
1814 0 : const bool thisColIsBound = thisColVal.isBound();
1815 0 : const bool thisColIsNull = !thisColIsBound || thisColVal.isNull();
1816 : // don't overwrite non-bound columns
1817 0 : if ( ! (bForceAllFields || thisColIsBound) )
1818 : {
1819 : // No - don't overwrite this field, it has not changed.
1820 0 : nByteOffset += nLen;
1821 0 : continue;
1822 : }
1823 0 : if (aIndexedCols[i].is())
1824 : {
1825 0 : Reference<XUnoTunnel> xTunnel(aIndexedCols[i],UNO_QUERY);
1826 : OSL_ENSURE(xTunnel.is(),"No TunnelImplementation!");
1827 0 : ODbaseIndex* pIndex = reinterpret_cast< ODbaseIndex* >( xTunnel->getSomething(ODbaseIndex::getUnoTunnelImplementationId()) );
1828 : OSL_ENSURE(pIndex,"ODbaseTable::UpdateBuffer: No Index returned!");
1829 : // Update !!
1830 0 : if (pOrgRow.is() && !thisColIsNull)
1831 0 : pIndex->Update(m_nFilePos, *(pOrgRow->get())[nPos], thisColVal);
1832 : else
1833 0 : pIndex->Insert(m_nFilePos, thisColVal);
1834 : }
1835 :
1836 0 : char* pData = (char *)(m_pBuffer + nByteOffset);
1837 0 : if (thisColIsNull)
1838 : {
1839 0 : if ( bSetZero )
1840 0 : memset(pData,0,nLen); // Clear to NULL char ('\0')
1841 : else
1842 0 : memset(pData,' ',nLen); // Clear to space/blank ('\0x20')
1843 0 : nByteOffset += nLen;
1844 : OSL_ENSURE( nByteOffset <= m_nBufferSize ,"ByteOffset > m_nBufferSize!");
1845 0 : continue;
1846 : }
1847 :
1848 : try
1849 : {
1850 0 : switch (nType)
1851 : {
1852 : case DataType::TIMESTAMP:
1853 : {
1854 0 : sal_Int32 nJulianDate = 0, nJulianTime = 0;
1855 0 : lcl_CalcJulDate(nJulianDate,nJulianTime, thisColVal);
1856 : // Exactly 8 bytes to copy:
1857 0 : memcpy(pData,&nJulianDate,4);
1858 0 : memcpy(pData+4,&nJulianTime,4);
1859 : }
1860 0 : break;
1861 : case DataType::DATE:
1862 : {
1863 0 : ::com::sun::star::util::Date aDate;
1864 0 : if(thisColVal.getTypeKind() == DataType::DOUBLE)
1865 0 : aDate = ::dbtools::DBTypeConversion::toDate(thisColVal.getDouble());
1866 : else
1867 0 : aDate = thisColVal;
1868 : char s[9];
1869 : snprintf(s,
1870 : sizeof(s),
1871 : "%04d%02d%02d",
1872 : (int)aDate.Year,
1873 : (int)aDate.Month,
1874 0 : (int)aDate.Day);
1875 :
1876 : // Exactly 8 bytes to copy:
1877 0 : strncpy(pData,s,sizeof s - 1);
1878 0 : } break;
1879 : case DataType::INTEGER:
1880 : {
1881 0 : sal_Int32 nValue = thisColVal;
1882 0 : memcpy(pData,&nValue,nLen);
1883 : }
1884 0 : break;
1885 : case DataType::DOUBLE:
1886 : {
1887 0 : const double d = thisColVal;
1888 0 : m_pColumns->getByIndex(i) >>= xCol;
1889 :
1890 0 : if (getBOOL(xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY)))) // Currency is treated separately
1891 : {
1892 0 : sal_Int64 nValue = 0;
1893 0 : if ( m_aScales[i] )
1894 0 : nValue = (sal_Int64)(d * pow(10.0,(int)m_aScales[i]));
1895 : else
1896 0 : nValue = (sal_Int64)(d);
1897 0 : memcpy(pData,&nValue,nLen);
1898 : } // if (getBOOL(xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY)))) // Currency is treated separately
1899 : else
1900 0 : memcpy(pData,&d,nLen);
1901 : }
1902 0 : break;
1903 : case DataType::DECIMAL:
1904 : {
1905 0 : memset(pData,' ',nLen); // Clear to NULL
1906 :
1907 0 : const double n = thisColVal;
1908 :
1909 : // one, because const_cast GetFormatPrecision on SvNumberFormat is not constant,
1910 : // even though it really could and should be
1911 0 : const OString aDefaultValue( ::rtl::math::doubleToString( n, rtl_math_StringFormat_F, nScale, '.', NULL, 0));
1912 0 : const sal_Int32 nValueLen = aDefaultValue.getLength();
1913 0 : if ( nValueLen <= nLen )
1914 : {
1915 : // Write value right-justified, padded with blanks to the left.
1916 0 : memcpy(pData+nLen-nValueLen,aDefaultValue.getStr(),nValueLen);
1917 : // write the resulting double back
1918 0 : *rRow.get()[nPos] = toDouble(aDefaultValue);
1919 : }
1920 : else
1921 : {
1922 0 : m_pColumns->getByIndex(i) >>= xCol;
1923 : OSL_ENSURE(xCol.is(),"ODbaseTable::UpdateBuffer column is null!");
1924 0 : xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
1925 0 : ::std::list< ::std::pair<const sal_Char* , OUString > > aStringToSubstitutes;
1926 0 : aStringToSubstitutes.push_back(::std::pair<const sal_Char* , OUString >("$columnname$", aColName));
1927 0 : aStringToSubstitutes.push_back(::std::pair<const sal_Char* , OUString >("$precision$", OUString::number(nLen)));
1928 0 : aStringToSubstitutes.push_back(::std::pair<const sal_Char* , OUString >("$scale$", OUString::number(nScale)));
1929 0 : aStringToSubstitutes.push_back(::std::pair<const sal_Char* , OUString >("$value$", OStringToOUString(aDefaultValue,RTL_TEXTENCODING_UTF8)));
1930 :
1931 0 : const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
1932 : STR_INVALID_COLUMN_DECIMAL_VALUE
1933 : ,aStringToSubstitutes
1934 0 : ) );
1935 0 : ::dbtools::throwGenericSQLException( sError, *this );
1936 0 : }
1937 0 : } break;
1938 : case DataType::BIT:
1939 0 : *pData = thisColVal.getBool() ? 'T' : 'F';
1940 0 : break;
1941 : case DataType::LONGVARBINARY:
1942 : case DataType::LONGVARCHAR:
1943 : {
1944 0 : char cNext = pData[nLen]; // Mark's scratch and replaced by 0
1945 0 : pData[nLen] = '\0'; // This is because the buffer is always a sign of greater ...
1946 :
1947 0 : sal_Size nBlockNo = strtol((const char *)pData,NULL,10); // Block number read
1948 :
1949 : // Next initial character restore again:
1950 0 : pData[nLen] = cNext;
1951 0 : if (!m_pMemoStream || !WriteMemo(thisColVal, nBlockNo))
1952 0 : break;
1953 :
1954 0 : OString aBlock(OString::number(nBlockNo));
1955 : //align aBlock at the right of a nLen sequence, fill to the left with '0'
1956 0 : OStringBuffer aStr;
1957 0 : comphelper::string::padToLength(aStr, nLen - aBlock.getLength(), '0');
1958 0 : aStr.append(aBlock);
1959 :
1960 : // Copy characters:
1961 0 : memcpy(pData, aStr.getStr(), nLen);
1962 0 : } break;
1963 : default:
1964 : {
1965 0 : memset(pData,' ',nLen); // Clear to NULL
1966 :
1967 0 : OUString sStringToWrite( thisColVal.getString() );
1968 :
1969 : // convert the string, using the connection's encoding
1970 0 : OString sEncoded;
1971 :
1972 0 : DBTypeConversion::convertUnicodeStringToLength( sStringToWrite, sEncoded, nLen, m_eEncoding );
1973 0 : memcpy( pData, sEncoded.getStr(), sEncoded.getLength() );
1974 :
1975 : }
1976 0 : break;
1977 : }
1978 : }
1979 0 : catch( const SQLException& )
1980 : {
1981 0 : throw;
1982 : }
1983 0 : catch ( const Exception& )
1984 : {
1985 0 : m_pColumns->getByIndex(i) >>= xCol;
1986 : OSL_ENSURE( xCol.is(), "ODbaseTable::UpdateBuffer column is null!" );
1987 0 : if ( xCol.is() )
1988 0 : xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
1989 :
1990 0 : const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
1991 : STR_INVALID_COLUMN_VALUE,
1992 : "$columnname$", aColName
1993 0 : ) );
1994 0 : ::dbtools::throwGenericSQLException( sError, *this );
1995 : }
1996 : // And more ...
1997 0 : nByteOffset += nLen;
1998 : OSL_ENSURE( nByteOffset <= m_nBufferSize ,"ByteOffset > m_nBufferSize!");
1999 : }
2000 0 : return sal_True;
2001 : }
2002 :
2003 :
2004 0 : sal_Bool ODbaseTable::WriteMemo(const ORowSetValue& aVariable, sal_Size& rBlockNr)
2005 : {
2006 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::WriteMemo" );
2007 : // if the BlockNo 0 is given, the block will be appended at the end
2008 0 : sal_Size nSize = 0;
2009 0 : OString aStr;
2010 0 : ::com::sun::star::uno::Sequence<sal_Int8> aValue;
2011 : sal_uInt8 nHeader[4];
2012 0 : const bool bBinary = aVariable.getTypeKind() == DataType::LONGVARBINARY && m_aMemoHeader.db_typ == MemoFoxPro;
2013 0 : if ( bBinary )
2014 : {
2015 0 : aValue = aVariable.getSequence();
2016 0 : nSize = aValue.getLength();
2017 : }
2018 : else
2019 : {
2020 0 : nSize = DBTypeConversion::convertUnicodeString( aVariable.getString(), aStr, m_eEncoding );
2021 : }
2022 :
2023 : // append or overwrite
2024 0 : sal_Bool bAppend = rBlockNr == 0;
2025 :
2026 0 : if (!bAppend)
2027 : {
2028 0 : switch (m_aMemoHeader.db_typ)
2029 : {
2030 : case MemodBaseIII: // dBase III-Memofield, ends with 2 * Ctrl-Z
2031 0 : bAppend = nSize > (512 - 2);
2032 0 : break;
2033 : case MemoFoxPro:
2034 : case MemodBaseIV: // dBase IV-Memofield with length
2035 : {
2036 : char sHeader[4];
2037 0 : m_pMemoStream->Seek(rBlockNr * m_aMemoHeader.db_size);
2038 0 : m_pMemoStream->SeekRel(4L);
2039 0 : m_pMemoStream->Read(sHeader,4);
2040 :
2041 : sal_Size nOldSize;
2042 0 : if (m_aMemoHeader.db_typ == MemoFoxPro)
2043 0 : nOldSize = ((((unsigned char)sHeader[0]) * 256 +
2044 0 : (unsigned char)sHeader[1]) * 256 +
2045 0 : (unsigned char)sHeader[2]) * 256 +
2046 0 : (unsigned char)sHeader[3];
2047 : else
2048 0 : nOldSize = ((((unsigned char)sHeader[3]) * 256 +
2049 0 : (unsigned char)sHeader[2]) * 256 +
2050 0 : (unsigned char)sHeader[1]) * 256 +
2051 0 : (unsigned char)sHeader[0] - 8;
2052 :
2053 : // fits the new length in the used blocks
2054 0 : sal_Size nUsedBlocks = ((nSize + 8) / m_aMemoHeader.db_size) + (((nSize + 8) % m_aMemoHeader.db_size > 0) ? 1 : 0),
2055 0 : nOldUsedBlocks = ((nOldSize + 8) / m_aMemoHeader.db_size) + (((nOldSize + 8) % m_aMemoHeader.db_size > 0) ? 1 : 0);
2056 0 : bAppend = nUsedBlocks > nOldUsedBlocks;
2057 : }
2058 : }
2059 : }
2060 :
2061 0 : if (bAppend)
2062 : {
2063 0 : sal_Size nStreamSize = m_pMemoStream->Seek(STREAM_SEEK_TO_END);
2064 : // fill last block
2065 0 : rBlockNr = (nStreamSize / m_aMemoHeader.db_size) + ((nStreamSize % m_aMemoHeader.db_size) > 0 ? 1 : 0);
2066 :
2067 0 : m_pMemoStream->SetStreamSize(rBlockNr * m_aMemoHeader.db_size);
2068 0 : m_pMemoStream->Seek(STREAM_SEEK_TO_END);
2069 : }
2070 : else
2071 : {
2072 0 : m_pMemoStream->Seek(rBlockNr * m_aMemoHeader.db_size);
2073 : }
2074 :
2075 0 : switch (m_aMemoHeader.db_typ)
2076 : {
2077 : case MemodBaseIII: // dBase III-Memofield, ends with Ctrl-Z
2078 : {
2079 0 : const char cEOF = (char) DBF_EOL;
2080 0 : nSize++;
2081 0 : m_pMemoStream->Write( aStr.getStr(), aStr.getLength() );
2082 0 : (*m_pMemoStream).WriteChar( cEOF ).WriteChar( cEOF );
2083 0 : } break;
2084 : case MemoFoxPro:
2085 : case MemodBaseIV: // dBase IV-Memofeld with length
2086 : {
2087 0 : if ( MemodBaseIV == m_aMemoHeader.db_typ )
2088 0 : (*m_pMemoStream).WriteUChar( (sal_uInt8)0xFF )
2089 0 : .WriteUChar( (sal_uInt8)0xFF )
2090 0 : .WriteUChar( (sal_uInt8)0x08 );
2091 : else
2092 0 : (*m_pMemoStream).WriteUChar( (sal_uInt8)0x00 )
2093 0 : .WriteUChar( (sal_uInt8)0x00 )
2094 0 : .WriteUChar( (sal_uInt8)0x00 );
2095 :
2096 0 : sal_uInt32 nWriteSize = nSize;
2097 0 : if (m_aMemoHeader.db_typ == MemoFoxPro)
2098 : {
2099 0 : if ( bBinary )
2100 0 : (*m_pMemoStream).WriteUChar( (sal_uInt8) 0x00 ); // Picture
2101 : else
2102 0 : (*m_pMemoStream).WriteUChar( (sal_uInt8) 0x01 ); // Memo
2103 0 : for (int i = 4; i > 0; nWriteSize >>= 8)
2104 0 : nHeader[--i] = (sal_uInt8) (nWriteSize % 256);
2105 : }
2106 : else
2107 : {
2108 0 : (*m_pMemoStream).WriteUChar( (sal_uInt8) 0x00 );
2109 0 : nWriteSize += 8;
2110 0 : for (int i = 0; i < 4; nWriteSize >>= 8)
2111 0 : nHeader[i++] = (sal_uInt8) (nWriteSize % 256);
2112 : }
2113 :
2114 0 : m_pMemoStream->Write(nHeader,4);
2115 0 : if ( bBinary )
2116 0 : m_pMemoStream->Write( aValue.getConstArray(), aValue.getLength() );
2117 : else
2118 0 : m_pMemoStream->Write( aStr.getStr(), aStr.getLength() );
2119 0 : m_pMemoStream->Flush();
2120 : }
2121 : }
2122 :
2123 :
2124 : // Write the new block number
2125 0 : if (bAppend)
2126 : {
2127 0 : sal_Size nStreamSize = m_pMemoStream->Seek(STREAM_SEEK_TO_END);
2128 0 : m_aMemoHeader.db_next = (nStreamSize / m_aMemoHeader.db_size) + ((nStreamSize % m_aMemoHeader.db_size) > 0 ? 1 : 0);
2129 :
2130 : // Write the new block number
2131 0 : m_pMemoStream->Seek(0L);
2132 0 : (*m_pMemoStream).WriteUInt32( m_aMemoHeader.db_next );
2133 0 : m_pMemoStream->Flush();
2134 : }
2135 0 : return sal_True;
2136 : }
2137 :
2138 :
2139 : // XAlterTable
2140 0 : void SAL_CALL ODbaseTable::alterColumnByName( const OUString& colName, const Reference< XPropertySet >& descriptor ) throw(SQLException, NoSuchElementException, RuntimeException, std::exception)
2141 : {
2142 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::alterColumnByName" );
2143 0 : ::osl::MutexGuard aGuard(m_aMutex);
2144 0 : checkDisposed(OTableDescriptor_BASE::rBHelper.bDisposed);
2145 :
2146 :
2147 0 : Reference<XDataDescriptorFactory> xOldColumn;
2148 0 : m_pColumns->getByName(colName) >>= xOldColumn;
2149 :
2150 0 : alterColumn(m_pColumns->findColumn(colName)-1,descriptor,xOldColumn);
2151 0 : }
2152 :
2153 0 : void SAL_CALL ODbaseTable::alterColumnByIndex( sal_Int32 index, const Reference< XPropertySet >& descriptor ) throw(SQLException, ::com::sun::star::lang::IndexOutOfBoundsException, RuntimeException, std::exception)
2154 : {
2155 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::alterColumnByIndex" );
2156 0 : ::osl::MutexGuard aGuard(m_aMutex);
2157 0 : checkDisposed(OTableDescriptor_BASE::rBHelper.bDisposed);
2158 :
2159 0 : if(index < 0 || index >= m_pColumns->getCount())
2160 0 : throw IndexOutOfBoundsException(OUString::number(index),*this);
2161 :
2162 0 : Reference<XDataDescriptorFactory> xOldColumn;
2163 0 : m_pColumns->getByIndex(index) >>= xOldColumn;
2164 0 : alterColumn(index,descriptor,xOldColumn);
2165 0 : }
2166 :
2167 0 : void ODbaseTable::alterColumn(sal_Int32 index,
2168 : const Reference< XPropertySet >& descriptor ,
2169 : const Reference< XDataDescriptorFactory >& xOldColumn )
2170 : {
2171 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::alterColumn" );
2172 0 : if(index < 0 || index >= m_pColumns->getCount())
2173 0 : throw IndexOutOfBoundsException(OUString::number(index),*this);
2174 :
2175 0 : ODbaseTable* pNewTable = NULL;
2176 : try
2177 : {
2178 : OSL_ENSURE(descriptor.is(),"ODbaseTable::alterColumn: descriptor can not be null!");
2179 : // creates a copy of the original column and copy all properties from descriptor in xCopyColumn
2180 0 : Reference<XPropertySet> xCopyColumn;
2181 0 : if(xOldColumn.is())
2182 0 : xCopyColumn = xOldColumn->createDataDescriptor();
2183 : else
2184 0 : xCopyColumn = new OColumn(getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers());
2185 :
2186 0 : ::comphelper::copyProperties(descriptor,xCopyColumn);
2187 :
2188 : // creates a temp file
2189 :
2190 0 : OUString sTempName = createTempFile();
2191 :
2192 0 : pNewTable = new ODbaseTable(m_pTables,static_cast<ODbaseConnection*>(m_pConnection));
2193 0 : Reference<XPropertySet> xHoldTable = pNewTable;
2194 0 : pNewTable->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME),makeAny(sTempName));
2195 0 : Reference<XAppend> xAppend(pNewTable->getColumns(),UNO_QUERY);
2196 : OSL_ENSURE(xAppend.is(),"ODbaseTable::alterColumn: No XAppend interface!");
2197 :
2198 : // copy the structure
2199 0 : sal_Int32 i=0;
2200 0 : for(;i < index;++i)
2201 : {
2202 0 : Reference<XPropertySet> xProp;
2203 0 : m_pColumns->getByIndex(i) >>= xProp;
2204 0 : Reference<XDataDescriptorFactory> xColumn(xProp,UNO_QUERY);
2205 0 : Reference<XPropertySet> xCpy;
2206 0 : if(xColumn.is())
2207 0 : xCpy = xColumn->createDataDescriptor();
2208 : else
2209 0 : xCpy = new OColumn(getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers());
2210 0 : ::comphelper::copyProperties(xProp,xCpy);
2211 0 : xAppend->appendByDescriptor(xCpy);
2212 0 : }
2213 0 : ++i; // now insert our new column
2214 0 : xAppend->appendByDescriptor(xCopyColumn);
2215 :
2216 0 : for(;i < m_pColumns->getCount();++i)
2217 : {
2218 0 : Reference<XPropertySet> xProp;
2219 0 : m_pColumns->getByIndex(i) >>= xProp;
2220 0 : Reference<XDataDescriptorFactory> xColumn(xProp,UNO_QUERY);
2221 0 : Reference<XPropertySet> xCpy;
2222 0 : if(xColumn.is())
2223 0 : xCpy = xColumn->createDataDescriptor();
2224 : else
2225 0 : xCpy = new OColumn(getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers());
2226 0 : ::comphelper::copyProperties(xProp,xCpy);
2227 0 : xAppend->appendByDescriptor(xCpy);
2228 0 : }
2229 :
2230 : // construct the new table
2231 0 : if(!pNewTable->CreateImpl())
2232 : {
2233 0 : const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
2234 : STR_COLUMN_NOT_ALTERABLE,
2235 0 : "$columnname$", ::comphelper::getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)))
2236 0 : ) );
2237 0 : ::dbtools::throwGenericSQLException( sError, *this );
2238 : }
2239 :
2240 0 : pNewTable->construct();
2241 :
2242 : // copy the data
2243 0 : copyData(pNewTable,0);
2244 :
2245 : // now drop the old one
2246 0 : if( DropImpl() ) // we don't want to delete the memo columns too
2247 : {
2248 : // rename the new one to the old one
2249 0 : pNewTable->renameImpl(m_Name);
2250 : // release the temp file
2251 0 : pNewTable = NULL;
2252 0 : ::comphelper::disposeComponent(xHoldTable);
2253 : }
2254 : else
2255 : {
2256 0 : pNewTable = NULL;
2257 : }
2258 0 : FileClose();
2259 0 : construct();
2260 0 : if(m_pColumns)
2261 0 : m_pColumns->refresh();
2262 :
2263 : }
2264 0 : catch(const SQLException&)
2265 : {
2266 0 : throw;
2267 : }
2268 0 : catch(const Exception&)
2269 : {
2270 : SAL_WARN( "connectivity.drivers","ODbaseTable::alterColumn: Exception occurred!");
2271 0 : throw;
2272 : }
2273 0 : }
2274 :
2275 0 : Reference< XDatabaseMetaData> ODbaseTable::getMetaData() const
2276 : {
2277 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::getMetaData" );
2278 0 : return getConnection()->getMetaData();
2279 : }
2280 :
2281 0 : void SAL_CALL ODbaseTable::rename( const OUString& newName ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::container::ElementExistException, ::com::sun::star::uno::RuntimeException, std::exception)
2282 : {
2283 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::rename" );
2284 0 : ::osl::MutexGuard aGuard(m_aMutex);
2285 0 : checkDisposed(OTableDescriptor_BASE::rBHelper.bDisposed);
2286 0 : if(m_pTables && m_pTables->hasByName(newName))
2287 0 : throw ElementExistException(newName,*this);
2288 :
2289 :
2290 0 : renameImpl(newName);
2291 :
2292 0 : ODbaseTable_BASE::rename(newName);
2293 :
2294 0 : construct();
2295 0 : if(m_pColumns)
2296 0 : m_pColumns->refresh();
2297 0 : }
2298 : namespace
2299 : {
2300 0 : void renameFile(OConnection* _pConenction,const OUString& oldName,
2301 : const OUString& newName,const OUString& _sExtension)
2302 : {
2303 0 : OUString aName = ODbaseTable::getEntry(_pConenction,oldName);
2304 0 : if(aName.isEmpty())
2305 : {
2306 0 : OUString aIdent = _pConenction->getContent()->getIdentifier()->getContentIdentifier();
2307 0 : if ( aIdent.lastIndexOf('/') != (aIdent.getLength()-1) )
2308 0 : aIdent += "/";
2309 0 : aIdent += oldName;
2310 0 : aName = aIdent;
2311 : }
2312 0 : INetURLObject aURL;
2313 0 : aURL.SetURL(aName);
2314 :
2315 0 : aURL.setExtension( _sExtension );
2316 0 : OUString sNewName(newName + "." + _sExtension);
2317 :
2318 : try
2319 : {
2320 0 : Content aContent(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
2321 :
2322 0 : Sequence< PropertyValue > aProps( 1 );
2323 0 : aProps[0].Name = "Title";
2324 0 : aProps[0].Handle = -1; // n/a
2325 0 : aProps[0].Value = makeAny( sNewName );
2326 0 : Sequence< Any > aValues;
2327 0 : aContent.executeCommand( "setPropertyValues",makeAny(aProps) ) >>= aValues;
2328 0 : if(aValues.getLength() && aValues[0].hasValue())
2329 0 : throw Exception();
2330 : }
2331 0 : catch(const Exception&)
2332 : {
2333 0 : throw ElementExistException(newName,NULL);
2334 0 : }
2335 0 : }
2336 : }
2337 :
2338 0 : void SAL_CALL ODbaseTable::renameImpl( const OUString& newName ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::container::ElementExistException, ::com::sun::star::uno::RuntimeException)
2339 : {
2340 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::getEntry" );
2341 0 : ::osl::MutexGuard aGuard(m_aMutex);
2342 :
2343 0 : FileClose();
2344 :
2345 :
2346 0 : renameFile(m_pConnection,m_Name,newName,m_pConnection->getExtension());
2347 0 : if ( HasMemoFields() )
2348 : { // delete the memo fields
2349 0 : OUString sExt("dbt");
2350 0 : renameFile(m_pConnection,m_Name,newName,sExt);
2351 0 : }
2352 0 : }
2353 :
2354 0 : void ODbaseTable::addColumn(const Reference< XPropertySet >& _xNewColumn)
2355 : {
2356 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::addColumn" );
2357 0 : OUString sTempName = createTempFile();
2358 :
2359 0 : ODbaseTable* pNewTable = new ODbaseTable(m_pTables,static_cast<ODbaseConnection*>(m_pConnection));
2360 0 : Reference< XPropertySet > xHold = pNewTable;
2361 0 : pNewTable->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME),makeAny(sTempName));
2362 : {
2363 0 : Reference<XAppend> xAppend(pNewTable->getColumns(),UNO_QUERY);
2364 0 : sal_Bool bCase = getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers();
2365 : // copy the structure
2366 0 : for(sal_Int32 i=0;i < m_pColumns->getCount();++i)
2367 : {
2368 0 : Reference<XPropertySet> xProp;
2369 0 : m_pColumns->getByIndex(i) >>= xProp;
2370 0 : Reference<XDataDescriptorFactory> xColumn(xProp,UNO_QUERY);
2371 0 : Reference<XPropertySet> xCpy;
2372 0 : if(xColumn.is())
2373 0 : xCpy = xColumn->createDataDescriptor();
2374 : else
2375 : {
2376 0 : xCpy = new OColumn(bCase);
2377 0 : ::comphelper::copyProperties(xProp,xCpy);
2378 : }
2379 :
2380 0 : xAppend->appendByDescriptor(xCpy);
2381 0 : }
2382 0 : Reference<XPropertySet> xCpy = new OColumn(bCase);
2383 0 : ::comphelper::copyProperties(_xNewColumn,xCpy);
2384 0 : xAppend->appendByDescriptor(xCpy);
2385 : }
2386 :
2387 : // construct the new table
2388 0 : if(!pNewTable->CreateImpl())
2389 : {
2390 0 : const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
2391 : STR_COLUMN_NOT_ADDABLE,
2392 0 : "$columnname$", ::comphelper::getString(_xNewColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)))
2393 0 : ) );
2394 0 : ::dbtools::throwGenericSQLException( sError, *this );
2395 : }
2396 :
2397 0 : sal_Bool bAlreadyDroped = sal_False;
2398 : try
2399 : {
2400 0 : pNewTable->construct();
2401 : // copy the data
2402 0 : copyData(pNewTable,pNewTable->m_pColumns->getCount());
2403 : // drop the old table
2404 0 : if(DropImpl())
2405 : {
2406 0 : bAlreadyDroped = sal_True;
2407 0 : pNewTable->renameImpl(m_Name);
2408 : // release the temp file
2409 : }
2410 0 : xHold = pNewTable = NULL;
2411 :
2412 0 : FileClose();
2413 0 : construct();
2414 0 : if(m_pColumns)
2415 0 : m_pColumns->refresh();
2416 : }
2417 0 : catch(const SQLException&)
2418 : {
2419 : // here we know that the old table wasn't dropped before
2420 0 : if(!bAlreadyDroped)
2421 0 : xHold = pNewTable = NULL;
2422 :
2423 0 : throw;
2424 0 : }
2425 0 : }
2426 :
2427 0 : void ODbaseTable::dropColumn(sal_Int32 _nPos)
2428 : {
2429 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::dropColumn" );
2430 0 : OUString sTempName = createTempFile();
2431 :
2432 0 : ODbaseTable* pNewTable = new ODbaseTable(m_pTables,static_cast<ODbaseConnection*>(m_pConnection));
2433 0 : Reference< XPropertySet > xHold = pNewTable;
2434 0 : pNewTable->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME),makeAny(sTempName));
2435 : {
2436 0 : Reference<XAppend> xAppend(pNewTable->getColumns(),UNO_QUERY);
2437 0 : sal_Bool bCase = getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers();
2438 : // copy the structure
2439 0 : for(sal_Int32 i=0;i < m_pColumns->getCount();++i)
2440 : {
2441 0 : if(_nPos != i)
2442 : {
2443 0 : Reference<XPropertySet> xProp;
2444 0 : m_pColumns->getByIndex(i) >>= xProp;
2445 0 : Reference<XDataDescriptorFactory> xColumn(xProp,UNO_QUERY);
2446 0 : Reference<XPropertySet> xCpy;
2447 0 : if(xColumn.is())
2448 0 : xCpy = xColumn->createDataDescriptor();
2449 : else
2450 : {
2451 0 : xCpy = new OColumn(bCase);
2452 0 : ::comphelper::copyProperties(xProp,xCpy);
2453 : }
2454 0 : xAppend->appendByDescriptor(xCpy);
2455 : }
2456 0 : }
2457 : }
2458 :
2459 : // construct the new table
2460 0 : if(!pNewTable->CreateImpl())
2461 : {
2462 0 : xHold = pNewTable = NULL;
2463 0 : const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
2464 : STR_COLUMN_NOT_DROP,
2465 : "$position$", OUString::number(_nPos)
2466 0 : ) );
2467 0 : ::dbtools::throwGenericSQLException( sError, *this );
2468 : }
2469 0 : pNewTable->construct();
2470 : // copy the data
2471 0 : copyData(pNewTable,_nPos);
2472 : // drop the old table
2473 0 : if(DropImpl())
2474 0 : pNewTable->renameImpl(m_Name);
2475 : // release the temp file
2476 :
2477 0 : xHold = pNewTable = NULL;
2478 :
2479 0 : FileClose();
2480 0 : construct();
2481 0 : }
2482 :
2483 0 : OUString ODbaseTable::createTempFile()
2484 : {
2485 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::createTempFile" );
2486 0 : OUString aIdent = m_pConnection->getContent()->getIdentifier()->getContentIdentifier();
2487 0 : if ( aIdent.lastIndexOf('/') != (aIdent.getLength()-1) )
2488 0 : aIdent += "/";
2489 :
2490 0 : OUString sTempName(aIdent);
2491 0 : OUString sExt(OUString(".") + m_pConnection->getExtension());
2492 0 : OUString sName(m_Name);
2493 0 : TempFile aTempFile(sName, &sExt, &sTempName);
2494 0 : if(!aTempFile.IsValid())
2495 0 : getConnection()->throwGenericSQLException(STR_COULD_NOT_ALTER_TABLE, *this);
2496 :
2497 0 : INetURLObject aURL;
2498 0 : aURL.SetSmartProtocol(INET_PROT_FILE);
2499 0 : aURL.SetURL(aTempFile.GetURL());
2500 :
2501 0 : OUString sNewName(aURL.getName().copy(0, aURL.getName().getLength() - sExt.getLength()));
2502 :
2503 0 : return sNewName;
2504 : }
2505 :
2506 0 : void ODbaseTable::copyData(ODbaseTable* _pNewTable,sal_Int32 _nPos)
2507 : {
2508 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::copyData" );
2509 0 : sal_Int32 nPos = _nPos + 1; // +1 because we always have the bookmark clumn as well
2510 0 : OValueRefRow aRow = new OValueRefVector(m_pColumns->getCount());
2511 0 : OValueRefRow aInsertRow;
2512 0 : if(_nPos)
2513 : {
2514 0 : aInsertRow = new OValueRefVector(_pNewTable->m_pColumns->getCount());
2515 0 : ::std::for_each(aInsertRow->get().begin(),aInsertRow->get().end(),TSetRefBound(true));
2516 : }
2517 : else
2518 0 : aInsertRow = aRow;
2519 :
2520 : // we only have to bind the values which we need to copy into the new table
2521 0 : ::std::for_each(aRow->get().begin(),aRow->get().end(),TSetRefBound(true));
2522 0 : if(_nPos && (_nPos < (sal_Int32)aRow->get().size()))
2523 0 : (aRow->get())[nPos]->setBound(false);
2524 :
2525 :
2526 0 : sal_Bool bOk = sal_True;
2527 : sal_Int32 nCurPos;
2528 0 : OValueRefVector::Vector::iterator aIter;
2529 0 : for(sal_uInt32 nRowPos = 0; nRowPos < m_aHeader.db_anz;++nRowPos)
2530 : {
2531 0 : bOk = seekRow( IResultSetHelper::BOOKMARK, nRowPos+1, nCurPos );
2532 0 : if ( bOk )
2533 : {
2534 0 : bOk = fetchRow( aRow, *m_aColumns, sal_True, sal_True);
2535 0 : if ( bOk && !aRow->isDeleted() ) // copy only not deleted rows
2536 : {
2537 : // special handling when pos == 0 then we don't have to distinguish between the two rows
2538 0 : if(_nPos)
2539 : {
2540 0 : aIter = aRow->get().begin()+1;
2541 0 : sal_Int32 nCount = 1;
2542 0 : for(OValueRefVector::Vector::iterator aInsertIter = aInsertRow->get().begin()+1; aIter != aRow->get().end() && aInsertIter != aInsertRow->get().end();++aIter,++nCount)
2543 : {
2544 0 : if(nPos != nCount)
2545 : {
2546 0 : (*aInsertIter)->setValue( (*aIter)->getValue() );
2547 0 : ++aInsertIter;
2548 : }
2549 : }
2550 : }
2551 0 : bOk = _pNewTable->InsertRow(*aInsertRow,sal_True,_pNewTable->m_pColumns);
2552 : OSL_ENSURE(bOk,"Row could not be inserted!");
2553 : }
2554 : else
2555 : OSL_ENSURE(bOk,"Row could not be fetched!");
2556 : }
2557 : else
2558 : {
2559 : OSL_ASSERT(false);
2560 : }
2561 0 : } // for(sal_uInt32 nRowPos = 0; nRowPos < m_aHeader.db_anz;++nRowPos)
2562 0 : }
2563 :
2564 0 : void ODbaseTable::throwInvalidDbaseFormat()
2565 : {
2566 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::throwInvalidDbaseFormat" );
2567 0 : FileClose();
2568 : // no dbase file
2569 :
2570 0 : const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
2571 : STR_INVALID_DBASE_FILE,
2572 : "$filename$", getEntry(m_pConnection,m_Name)
2573 0 : ) );
2574 0 : ::dbtools::throwGenericSQLException( sError, *this );
2575 0 : }
2576 :
2577 0 : void ODbaseTable::refreshHeader()
2578 : {
2579 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::refreshHeader" );
2580 0 : if ( m_aHeader.db_anz == 0 )
2581 0 : readHeader();
2582 0 : }
2583 :
2584 0 : sal_Bool ODbaseTable::seekRow(IResultSetHelper::Movement eCursorPosition, sal_Int32 nOffset, sal_Int32& nCurPos)
2585 : {
2586 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::seekRow" );
2587 :
2588 : // prepare positioning:
2589 : OSL_ENSURE(m_pFileStream,"ODbaseTable::seekRow: FileStream is NULL!");
2590 :
2591 0 : sal_uInt32 nNumberOfRecords = (sal_uInt32)m_aHeader.db_anz;
2592 0 : sal_uInt32 nTempPos = m_nFilePos;
2593 0 : m_nFilePos = nCurPos;
2594 :
2595 0 : switch(eCursorPosition)
2596 : {
2597 : case IResultSetHelper::NEXT:
2598 0 : ++m_nFilePos;
2599 0 : break;
2600 : case IResultSetHelper::PRIOR:
2601 0 : if (m_nFilePos > 0)
2602 0 : --m_nFilePos;
2603 0 : break;
2604 : case IResultSetHelper::FIRST:
2605 0 : m_nFilePos = 1;
2606 0 : break;
2607 : case IResultSetHelper::LAST:
2608 0 : m_nFilePos = nNumberOfRecords;
2609 0 : break;
2610 : case IResultSetHelper::RELATIVE:
2611 0 : m_nFilePos = (((sal_Int32)m_nFilePos) + nOffset < 0) ? 0L
2612 0 : : (sal_uInt32)(((sal_Int32)m_nFilePos) + nOffset);
2613 0 : break;
2614 : case IResultSetHelper::ABSOLUTE:
2615 : case IResultSetHelper::BOOKMARK:
2616 0 : m_nFilePos = (sal_uInt32)nOffset;
2617 0 : break;
2618 : }
2619 :
2620 0 : if (m_nFilePos > (sal_Int32)nNumberOfRecords)
2621 0 : m_nFilePos = (sal_Int32)nNumberOfRecords + 1;
2622 :
2623 0 : if (m_nFilePos == 0 || m_nFilePos == (sal_Int32)nNumberOfRecords + 1)
2624 : goto Error;
2625 : else
2626 : {
2627 0 : sal_Size nEntryLen = m_aHeader.db_slng;
2628 :
2629 : OSL_ENSURE(m_nFilePos >= 1,"SdbDBFCursor::FileFetchRow: ungueltige Record-Position");
2630 0 : sal_Size nPos = m_aHeader.db_kopf + (sal_Size)(m_nFilePos-1) * nEntryLen;
2631 :
2632 0 : m_pFileStream->Seek(nPos);
2633 0 : if (m_pFileStream->GetError() != ERRCODE_NONE)
2634 0 : goto Error;
2635 :
2636 0 : m_pFileStream->Read((char*)m_pBuffer, nEntryLen);
2637 0 : if (m_pFileStream->GetError() != ERRCODE_NONE)
2638 0 : goto Error;
2639 : }
2640 0 : goto End;
2641 :
2642 : Error:
2643 0 : switch(eCursorPosition)
2644 : {
2645 : case IResultSetHelper::PRIOR:
2646 : case IResultSetHelper::FIRST:
2647 0 : m_nFilePos = 0;
2648 0 : break;
2649 : case IResultSetHelper::LAST:
2650 : case IResultSetHelper::NEXT:
2651 : case IResultSetHelper::ABSOLUTE:
2652 : case IResultSetHelper::RELATIVE:
2653 0 : if (nOffset > 0)
2654 0 : m_nFilePos = nNumberOfRecords + 1;
2655 0 : else if (nOffset < 0)
2656 0 : m_nFilePos = 0;
2657 0 : break;
2658 : case IResultSetHelper::BOOKMARK:
2659 0 : m_nFilePos = nTempPos; // last position
2660 : }
2661 0 : return sal_False;
2662 :
2663 : End:
2664 0 : nCurPos = m_nFilePos;
2665 0 : return sal_True;
2666 : }
2667 :
2668 0 : sal_Bool ODbaseTable::ReadMemo(sal_Size nBlockNo, ORowSetValue& aVariable)
2669 : {
2670 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::ReadMemo" );
2671 0 : bool bIsText = true;
2672 :
2673 0 : m_pMemoStream->Seek(nBlockNo * m_aMemoHeader.db_size);
2674 0 : switch (m_aMemoHeader.db_typ)
2675 : {
2676 : case MemodBaseIII: // dBase III-Memofield, ends with Ctrl-Z
2677 : {
2678 0 : const char cEOF = (char) DBF_EOL;
2679 0 : OStringBuffer aBStr;
2680 : static char aBuf[514];
2681 0 : aBuf[512] = 0; // avoid random value
2682 0 : sal_Bool bReady = sal_False;
2683 :
2684 0 : do
2685 : {
2686 0 : m_pMemoStream->Read(&aBuf,512);
2687 :
2688 0 : sal_uInt16 i = 0;
2689 0 : while (aBuf[i] != cEOF && ++i < 512)
2690 : ;
2691 0 : bReady = aBuf[i] == cEOF;
2692 :
2693 0 : aBuf[i] = 0;
2694 0 : aBStr.append(aBuf);
2695 :
2696 0 : } while (!bReady && !m_pMemoStream->IsEof());
2697 :
2698 0 : aVariable = OStringToOUString(aBStr.makeStringAndClear(),
2699 0 : m_eEncoding);
2700 :
2701 0 : } break;
2702 : case MemoFoxPro:
2703 : case MemodBaseIV: // dBase IV-Memofield with length
2704 : {
2705 : char sHeader[4];
2706 0 : m_pMemoStream->Read(sHeader,4);
2707 : // Foxpro stores text and binary data
2708 0 : if (m_aMemoHeader.db_typ == MemoFoxPro)
2709 : {
2710 0 : bIsText = sHeader[3] != 0;
2711 : }
2712 0 : else if (((sal_uInt8)sHeader[0]) != 0xFF || ((sal_uInt8)sHeader[1]) != 0xFF || ((sal_uInt8)sHeader[2]) != 0x08)
2713 : {
2714 0 : return sal_False;
2715 : }
2716 :
2717 0 : sal_uInt32 nLength(0);
2718 0 : (*m_pMemoStream).ReadUInt32( nLength );
2719 :
2720 0 : if (m_aMemoHeader.db_typ == MemodBaseIV)
2721 0 : nLength -= 8;
2722 :
2723 0 : if ( nLength )
2724 : {
2725 0 : if ( bIsText )
2726 : {
2727 0 : OStringBuffer aBuffer(read_uInt8s_ToOString(*m_pMemoStream, nLength));
2728 : //pad it out with ' ' to expected length on short read
2729 0 : sal_Int32 nRequested = sal::static_int_cast<sal_Int32>(nLength);
2730 0 : comphelper::string::padToLength(aBuffer, nRequested, ' ');
2731 0 : aVariable = OStringToOUString(aBuffer.makeStringAndClear(), m_eEncoding);
2732 : } // if ( bIsText )
2733 : else
2734 : {
2735 0 : ::com::sun::star::uno::Sequence< sal_Int8 > aData(nLength);
2736 0 : m_pMemoStream->Read(aData.getArray(),nLength);
2737 0 : aVariable = aData;
2738 : }
2739 : } // if ( nLength )
2740 : }
2741 : }
2742 0 : return sal_True;
2743 : }
2744 :
2745 0 : void ODbaseTable::AllocBuffer()
2746 : {
2747 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::AllocBuffer" );
2748 0 : sal_uInt16 nSize = m_aHeader.db_slng;
2749 : OSL_ENSURE(nSize > 0, "Size too small");
2750 :
2751 0 : if (m_nBufferSize != nSize)
2752 : {
2753 0 : delete m_pBuffer;
2754 0 : m_pBuffer = NULL;
2755 : }
2756 :
2757 : // if there is no buffer available: allocate:
2758 0 : if (m_pBuffer == NULL && nSize > 0)
2759 : {
2760 0 : m_nBufferSize = nSize;
2761 0 : m_pBuffer = new sal_uInt8[m_nBufferSize+1];
2762 : }
2763 0 : }
2764 :
2765 0 : sal_Bool ODbaseTable::WriteBuffer()
2766 : {
2767 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::WriteBuffer" );
2768 : OSL_ENSURE(m_nFilePos >= 1,"SdbDBFCursor::FileFetchRow: ungueltige Record-Position");
2769 :
2770 : // postion on desired record:
2771 0 : sal_Size nPos = m_aHeader.db_kopf + (long)(m_nFilePos-1) * m_aHeader.db_slng;
2772 0 : m_pFileStream->Seek(nPos);
2773 0 : return m_pFileStream->Write((char*) m_pBuffer, m_aHeader.db_slng) > 0;
2774 : }
2775 :
2776 0 : sal_Int32 ODbaseTable::getCurrentLastPos() const
2777 : {
2778 : SAL_INFO( "connectivity.drivers", "dbase Ocke.Janssen@sun.com ODbaseTable::getCurrentLastPos" );
2779 0 : return m_aHeader.db_anz;
2780 : }
2781 :
2782 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|