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