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