Update ooo320-m1
[ooovba.git] / connectivity / source / drivers / dbase / DIndex.cxx
blob9d25b18e00ce8718224e74af2ec96c7f9b1dc1b8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DIndex.cxx,v $
10 * $Revision: 1.43.56.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "dbase/DIndex.hxx"
34 #include "dbase/DIndexColumns.hxx"
35 #include <com/sun/star/lang/DisposedException.hpp>
36 #include "connectivity/sdbcx/VColumn.hxx"
37 #include <comphelper/sequence.hxx>
38 #include "dbase/DTable.hxx"
39 #include "dbase/DIndexIter.hxx"
40 #include <tools/config.hxx>
41 #include "connectivity/CommonTools.hxx"
42 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
43 #include <com/sun/star/sdbc/XResultSet.hpp>
44 #include <com/sun/star/sdbcx/XRowLocate.hpp>
45 #include <com/sun/star/sdbc/XRow.hpp>
46 #include <comphelper/extract.hxx>
47 #include <unotools/localfilehelper.hxx>
48 #include <unotools/ucbhelper.hxx>
49 #include <comphelper/types.hxx>
50 #include <connectivity/dbexception.hxx>
51 #include "dbase/DResultSet.hxx"
52 #include "diagnose_ex.h"
53 #include <comphelper/types.hxx>
54 #include "resource/dbase_res.hrc"
55 #include <unotools/sharedunocomponent.hxx>
57 using namespace ::comphelper;
58 // -------------------------------------------------------------------------
59 using namespace connectivity;
60 using namespace utl;
61 using namespace ::cppu;
62 using namespace connectivity::file;
63 using namespace connectivity::sdbcx;
64 using namespace connectivity::dbase;
65 using namespace com::sun::star::sdbc;
66 using namespace com::sun::star::sdbcx;
67 using namespace com::sun::star::uno;
68 using namespace com::sun::star::beans;
69 using namespace com::sun::star::lang;
71 IMPLEMENT_SERVICE_INFO(ODbaseIndex,"com.sun.star.sdbcx.driver.dbase.Index","com.sun.star.sdbcx.Index");
72 // -------------------------------------------------------------------------
73 ODbaseIndex::ODbaseIndex(ODbaseTable* _pTable) : OIndex(sal_True/*_pTable->getConnection()->getMetaData()->storesMixedCaseQuotedIdentifiers()*/)
74 ,m_pFileStream(NULL)
75 ,m_nCurNode(NODE_NOTFOUND)
76 ,m_pTable(_pTable)
78 m_aHeader.db_pagecount = m_aHeader.db_rootpage = m_aHeader.db_keytype = m_aHeader.db_maxkeys = m_aHeader.db_keylen = 0;
79 m_aHeader.db_name[0] = '\0';
80 construct();
82 // -------------------------------------------------------------------------
83 ODbaseIndex::ODbaseIndex( ODbaseTable* _pTable,
84 const NDXHeader& _rHeader,
85 const ::rtl::OUString& _rName)
86 :OIndex(_rName,::rtl::OUString(),_rHeader.db_unique,sal_False,sal_False,sal_True) // _pTable->getConnection()->getMetaData()->storesMixedCaseQuotedIdentifiers()
87 ,m_pFileStream(NULL)
88 ,m_aHeader(_rHeader)
89 ,m_nCurNode(NODE_NOTFOUND)
90 ,m_pTable(_pTable)
92 construct();
94 // -----------------------------------------------------------------------------
95 ODbaseIndex::~ODbaseIndex()
97 closeImpl();
99 // -------------------------------------------------------------------------
100 void ODbaseIndex::refreshColumns()
102 ::osl::MutexGuard aGuard( m_aMutex );
104 TStringVector aVector;
105 if(!isNew())
107 OSL_ENSURE(m_pFileStream,"FileStream is not opened!");
108 OSL_ENSURE(m_aHeader.db_name[0] != '\0',"Invalid name for the column!");
109 aVector.push_back(::rtl::OUString::createFromAscii(m_aHeader.db_name));
112 if(m_pColumns)
113 m_pColumns->reFill(aVector);
114 else
115 m_pColumns = new ODbaseIndexColumns(this,m_aMutex,aVector);
117 //--------------------------------------------------------------------------
118 Sequence< sal_Int8 > ODbaseIndex::getUnoTunnelImplementationId()
120 static ::cppu::OImplementationId * pId = 0;
121 if (! pId)
123 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
124 if (! pId)
126 static ::cppu::OImplementationId aId;
127 pId = &aId;
130 return pId->getImplementationId();
133 // XUnoTunnel
134 //------------------------------------------------------------------
135 sal_Int64 ODbaseIndex::getSomething( const Sequence< sal_Int8 > & rId ) throw (RuntimeException)
137 return (rId.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
138 ? reinterpret_cast< sal_Int64 >( this )
139 : ODbaseIndex_BASE::getSomething(rId);
141 //------------------------------------------------------------------
142 ONDXPagePtr ODbaseIndex::getRoot()
144 openIndexFile();
145 if (!m_aRoot.Is())
147 m_nRootPage = m_aHeader.db_rootpage;
148 m_nPageCount = m_aHeader.db_pagecount;
149 m_aRoot = CreatePage(m_nRootPage,NULL,TRUE);
151 return m_aRoot;
153 //------------------------------------------------------------------
154 sal_Bool ODbaseIndex::openIndexFile()
156 if(!m_pFileStream)
158 ::rtl::OUString sFile = getCompletePath();
159 if(UCBContentHelper::Exists(sFile))
161 m_pFileStream = OFileTable::createStream_simpleError(sFile, STREAM_READWRITE | STREAM_NOCREATE | STREAM_SHARE_DENYWRITE);
162 if (!m_pFileStream)
163 m_pFileStream = OFileTable::createStream_simpleError(sFile,STREAM_READ | STREAM_NOCREATE | STREAM_SHARE_DENYNONE);
164 if(m_pFileStream)
166 m_pFileStream->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
167 m_pFileStream->SetBufferSize(PAGE_SIZE);
168 (*m_pFileStream) >> *this;
171 if(!m_pFileStream)
173 const ::rtl::OUString sError( m_pTable->getConnection()->getResources().getResourceStringWithSubstitution(
174 STR_COULD_NOT_LOAD_FILE,
175 "$filename$", sFile
176 ) );
177 ::dbtools::throwGenericSQLException( sError, *this );
181 return m_pFileStream != NULL;
183 //------------------------------------------------------------------
184 OIndexIterator* ODbaseIndex::createIterator(OBoolOperator* pOp,
185 const OOperand* pOperand)
187 openIndexFile();
188 return new OIndexIterator(this, pOp, pOperand);
190 //------------------------------------------------------------------
191 BOOL ODbaseIndex::ConvertToKey(ONDXKey* rKey, sal_uInt32 nRec, const ORowSetValue& rValue)
193 OSL_ENSURE(m_pFileStream,"FileStream is not opened!");
194 // Sucht ein bestimmten Wert im Index
195 // Wenn der Index Unique ist, interssiert der Key nicht, sonst ja
198 if (m_aHeader.db_keytype == 0)
200 *rKey = ONDXKey(rValue.getString(), nRec );
202 else
204 if (rValue.isNull())
205 *rKey = ONDXKey(rValue.getDouble(), DataType::DOUBLE, nRec );
206 else
207 *rKey = ONDXKey(rValue.getDouble(), nRec );
210 catch (Exception&)
212 OSL_ASSERT(0);
213 return FALSE;
215 return TRUE;
218 //------------------------------------------------------------------
219 BOOL ODbaseIndex::Find(sal_uInt32 nRec, const ORowSetValue& rValue)
221 openIndexFile();
222 OSL_ENSURE(m_pFileStream,"FileStream is not opened!");
223 // Sucht ein bestimmten Wert im Index
224 // Wenn der Index Unique ist, interssiert der Key nicht, sonst ja
225 ONDXKey aKey;
226 return ConvertToKey(&aKey, nRec, rValue) && getRoot()->Find(aKey);
229 //------------------------------------------------------------------
230 BOOL ODbaseIndex::Insert(sal_uInt32 nRec, const ORowSetValue& rValue)
232 openIndexFile();
233 OSL_ENSURE(m_pFileStream,"FileStream is not opened!");
234 ONDXKey aKey;
236 // Existiert der Wert bereits
237 // Find immer verwenden um das aktuelle Blatt zu bestimmen
238 if (!ConvertToKey(&aKey, nRec, rValue) || (getRoot()->Find(aKey) && isUnique()))
239 return FALSE;
241 ONDXNode aNewNode(aKey);
243 // einfuegen in das aktuelle Blatt
244 if (!m_aCurLeaf.Is())
245 return FALSE;
247 BOOL bResult = m_aCurLeaf->Insert(aNewNode);
248 Release(bResult);
250 return bResult;
253 //------------------------------------------------------------------
254 BOOL ODbaseIndex::Update(sal_uInt32 nRec, const ORowSetValue& rOldValue,
255 const ORowSetValue& rNewValue)
257 openIndexFile();
258 OSL_ENSURE(m_pFileStream,"FileStream is not opened!");
259 ONDXKey aKey;
260 if (!ConvertToKey(&aKey, nRec, rNewValue) || (isUnique() && getRoot()->Find(aKey)))
261 return FALSE;
262 else
263 return Delete(nRec, rOldValue) && Insert(nRec,rNewValue);
266 //------------------------------------------------------------------
267 BOOL ODbaseIndex::Delete(sal_uInt32 nRec, const ORowSetValue& rValue)
269 openIndexFile();
270 OSL_ENSURE(m_pFileStream,"FileStream is not opened!");
271 // Existiert der Wert bereits
272 // Find immer verwenden um das aktuelle Blatt zu bestimmen
273 ONDXKey aKey;
274 if (!ConvertToKey(&aKey, nRec, rValue) || !getRoot()->Find(aKey))
275 return FALSE;
277 ONDXNode aNewNode(aKey);
279 // einfuegen in das aktuelle Blatt
280 if (!m_aCurLeaf.Is())
281 return FALSE;
282 #if OSL_DEBUG_LEVEL > 1
283 m_aRoot->PrintPage();
284 #endif
286 return m_aCurLeaf->Delete(m_nCurNode);
288 //------------------------------------------------------------------
289 void ODbaseIndex::Collect(ONDXPage* pPage)
291 if (pPage)
292 m_aCollector.push_back(pPage);
294 //------------------------------------------------------------------
295 void ODbaseIndex::Release(BOOL bSave)
297 // Freigeben der Indexressourcen
298 m_bUseCollector = FALSE;
300 if (m_aCurLeaf.Is())
302 m_aCurLeaf->Release(bSave);
303 m_aCurLeaf.Clear();
306 // Wurzel freigeben
307 if (m_aRoot.Is())
309 m_aRoot->Release(bSave);
310 m_aRoot.Clear();
312 // alle Referenzen freigeben, bevor der FileStream geschlossen wird
313 for (ULONG i = 0; i < m_aCollector.size(); i++)
314 m_aCollector[i]->QueryDelete();
316 m_aCollector.clear();
318 // Header modifiziert ?
319 if (bSave && (m_aHeader.db_rootpage != m_nRootPage ||
320 m_aHeader.db_pagecount != m_nPageCount))
322 m_aHeader.db_rootpage = m_nRootPage;
323 m_aHeader.db_pagecount = m_nPageCount;
324 (*m_pFileStream) << *this;
326 m_nRootPage = m_nPageCount = 0;
327 m_nCurNode = NODE_NOTFOUND;
329 closeImpl();
331 // -----------------------------------------------------------------------------
332 void ODbaseIndex::closeImpl()
334 if(m_pFileStream)
336 delete m_pFileStream;
337 m_pFileStream = NULL;
340 //------------------------------------------------------------------
341 ONDXPage* ODbaseIndex::CreatePage(sal_uInt32 nPagePos, ONDXPage* pParent, BOOL bLoad)
343 OSL_ENSURE(m_pFileStream,"FileStream is not opened!");
345 ONDXPage* pPage;
346 if ( !m_aCollector.empty() )
348 pPage = *(m_aCollector.rbegin());
349 m_aCollector.pop_back();
350 pPage->SetPagePos(nPagePos);
351 pPage->SetParent(pParent);
353 else
354 pPage = new ONDXPage(*this, nPagePos, pParent);
356 if (bLoad)
357 (*m_pFileStream) >> *pPage;
359 return pPage;
362 //------------------------------------------------------------------
363 SvStream& connectivity::dbase::operator >> (SvStream &rStream, ODbaseIndex& rIndex)
365 rStream.Seek(0);
366 rStream.Read(&rIndex.m_aHeader,PAGE_SIZE);
368 /* OJ: no longer needed
369 // Text convertierung
370 ByteString aText(rIndex.m_aHeader.db_name);
371 // aText.Convert(rIndex.m_pTable->getConnection()->GetCharacterSet(), m_pTable->getConnection()->getTextEncoding());
372 // aText.Convert(rIndex.m_pTable->getConnection()->GetCharacterSet(), m_pTable->getConnection()->getTextEncoding());
373 strcpy(rIndex.m_aHeader.db_name,aText.GetBuffer());
375 rIndex.m_nRootPage = rIndex.m_aHeader.db_rootpage;
376 rIndex.m_nPageCount = rIndex.m_aHeader.db_pagecount;
377 return rStream;
379 //------------------------------------------------------------------
380 SvStream& connectivity::dbase::operator << (SvStream &rStream, ODbaseIndex& rIndex)
382 rStream.Seek(0);
383 /* OJ: no longer needed
384 ByteString aText(rIndex.m_aHeader.db_name);
385 // aText.Convert(m_pTable->getConnection()->getTextEncoding(), rIndex.m_pTable->getConnection()->GetCharacterSet());
386 strcpy(rIndex.m_aHeader.db_name,aText.GetBuffer());
388 OSL_VERIFY_EQUALS( rStream.Write(&rIndex.m_aHeader,PAGE_SIZE), PAGE_SIZE, "Write not successful: Wrong header size for dbase index!");
389 return rStream;
391 // -------------------------------------------------------------------------
392 ::rtl::OUString ODbaseIndex::getCompletePath()
394 ::rtl::OUString sDir = m_pTable->getConnection()->getURL();
395 sDir += OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DELIMITER);
396 sDir += m_Name;
397 sDir += ::rtl::OUString::createFromAscii(".ndx");
398 return sDir;
400 //------------------------------------------------------------------
401 void ODbaseIndex::createINFEntry()
403 // inf Datei abgleichen
404 String sEntry = m_Name;
405 sEntry += String::CreateFromAscii(".ndx");
407 ::rtl::OUString sCfgFile(m_pTable->getConnection()->getURL());
408 sCfgFile += OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DELIMITER);
409 sCfgFile += m_pTable->getName();
410 sCfgFile += ::rtl::OUString::createFromAscii(".inf");
412 String sPhysicalPath;
413 LocalFileHelper::ConvertURLToPhysicalName(sCfgFile,sPhysicalPath);
415 Config aInfFile(sPhysicalPath);
416 aInfFile.SetGroup(dBASE_III_GROUP);
418 USHORT nSuffix = aInfFile.GetKeyCount();
419 ByteString aNewEntry,aKeyName;
420 BOOL bCase = isCaseSensitive();
421 while (!aNewEntry.Len())
423 aNewEntry = "NDX";
424 aNewEntry += ByteString::CreateFromInt32(++nSuffix);
425 for (USHORT i = 0; i < aInfFile.GetKeyCount(); i++)
427 aKeyName = aInfFile.GetKeyName(i);
428 if (bCase ? aKeyName == aNewEntry : aKeyName.EqualsIgnoreCaseAscii(aNewEntry))
430 aNewEntry.Erase();
431 break;
435 aInfFile.WriteKey(aNewEntry,ByteString(sEntry,m_pTable->getConnection()->getTextEncoding()));
437 // -------------------------------------------------------------------------
438 BOOL ODbaseIndex::DropImpl()
440 closeImpl();
442 ::rtl::OUString sPath = getCompletePath();
443 if(UCBContentHelper::Exists(sPath))
445 if(!UCBContentHelper::Kill(sPath))
446 m_pTable->getConnection()->throwGenericSQLException(STR_COULD_NOT_DELETE_INDEX,*m_pTable);
449 // InfDatei abgleichen
451 ::rtl::OUString sCfgFile(m_pTable->getConnection()->getURL());
452 sCfgFile += OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DELIMITER);
453 sCfgFile += m_pTable->getName();
454 sCfgFile += ::rtl::OUString::createFromAscii(".inf");
456 String sPhysicalPath;
457 String sNDX(sCfgFile);
458 OSL_VERIFY_RES( LocalFileHelper::ConvertURLToPhysicalName(sNDX,sPhysicalPath),"Can not convert Config Filename into Physical Name!");
460 Config aInfFile(sPhysicalPath);
461 aInfFile.SetGroup(dBASE_III_GROUP);
462 USHORT nKeyCnt = aInfFile.GetKeyCount();
463 ByteString aKeyName;
464 String sEntry = m_Name;
465 sEntry += String::CreateFromAscii(".ndx");
467 // delete entries from the inf file
468 for (USHORT nKey = 0; nKey < nKeyCnt; nKey++)
470 // Verweist der Key auf ein Indexfile?...
471 aKeyName = aInfFile.GetKeyName( nKey );
472 if (aKeyName.Copy(0,3) == "NDX")
474 if(sEntry == String(aInfFile.ReadKey(aKeyName),m_pTable->getConnection()->getTextEncoding()))
476 aInfFile.DeleteKey(aKeyName);
477 break;
481 return TRUE;
483 // -------------------------------------------------------------------------
484 void ODbaseIndex::impl_killFileAndthrowError_throw(sal_uInt16 _nErrorId,const ::rtl::OUString& _sFile)
486 closeImpl();
487 if(UCBContentHelper::Exists(_sFile))
488 UCBContentHelper::Kill(_sFile);
489 m_pTable->getConnection()->throwGenericSQLException(_nErrorId,*this);
491 //------------------------------------------------------------------
492 BOOL ODbaseIndex::CreateImpl()
494 // Anlegen des Index
495 const ::rtl::OUString sFile = getCompletePath();
496 if(UCBContentHelper::Exists(sFile))
498 const ::rtl::OUString sError( m_pTable->getConnection()->getResources().getResourceStringWithSubstitution(
499 STR_COULD_NOT_CREATE_INDEX_NAME,
500 "$filename$", sFile
501 ) );
502 ::dbtools::throwGenericSQLException( sError, *this );
504 // Index ist nur einstufig
505 if (m_pColumns->getCount() > 1)
506 m_pTable->getConnection()->throwGenericSQLException(STR_ONL_ONE_COLUMN_PER_INDEX,*this);
508 Reference<XFastPropertySet> xCol(m_pColumns->getByIndex(0),UNO_QUERY);
510 // ist die Spalte schon indiziert ?
511 if ( !xCol.is() )
512 ::dbtools::throwFunctionSequenceException(*this);
513 // else if (pColumn && pColumn->IsIndexed())
514 // {
515 // String aText = String(OResId(STR_STAT_INDEX_COLUMN_ALREADY_INDEXED));
516 // aText.SearchAndReplace(String::CreateFromAscii("#"),pColumn->GetName());
517 // aStatus.Set(SDB_STAT_ERROR,
518 // String::CreateFromAscii("01000"),
519 // aStatus.CreateErrorMessage(aText),
520 // 0, String() );
521 // return FALSE;
522 // }
524 // create the index file
525 m_pFileStream = OFileTable::createStream_simpleError(sFile,STREAM_READWRITE | STREAM_SHARE_DENYWRITE | STREAM_TRUNC);
526 if (!m_pFileStream)
528 const ::rtl::OUString sError( m_pTable->getConnection()->getResources().getResourceStringWithSubstitution(
529 STR_COULD_NOT_LOAD_FILE,
530 "$filename$", sFile
531 ) );
532 ::dbtools::throwGenericSQLException( sError, *this );
535 m_pFileStream->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
536 m_pFileStream->SetBufferSize(PAGE_SIZE);
537 m_pFileStream->SetFiller('\0');
539 // Zunaechst muss das Ergebnis sortiert sein
540 utl::SharedUNOComponent<XStatement> xStmt;
541 utl::SharedUNOComponent<XResultSet> xSet;
542 String aName;
545 xStmt.set( m_pTable->getConnection()->createStatement(), UNO_SET_THROW);
547 aName = getString(xCol->getFastPropertyValue(PROPERTY_ID_NAME));
549 const String aQuote(m_pTable->getConnection()->getMetaData()->getIdentifierQuoteString());
550 String aStatement;
551 aStatement.AssignAscii("SELECT ");
552 aStatement += aQuote;
553 aStatement += aName;
554 aStatement += aQuote;
555 aStatement.AppendAscii(" FROM ");
556 aStatement += aQuote;
557 aStatement += m_pTable->getName().getStr();
558 aStatement += aQuote;
559 aStatement.AppendAscii(" ORDER BY ");
560 aStatement += aQuote;
561 aStatement += aName;
562 aStatement += aQuote;
564 // if (!m_IsUnique) // zusaetzlich sortierung mit der bookmarkspalte
565 // {
566 // aStatement.AppendAscii(" ,");
567 // aStatement += aQuote;
568 // aStatement.AppendAscii("[BOOKMARK]"); // this is a special column
569 // aStatement += aQuote;
570 // }
572 xSet.set( xStmt->executeQuery(aStatement),UNO_SET_THROW );
574 catch(const Exception& )
576 impl_killFileAndthrowError_throw(STR_COULD_NOT_CREATE_INDEX,sFile);
578 if (!xSet.is())
580 impl_killFileAndthrowError_throw(STR_COULD_NOT_CREATE_INDEX,sFile);
583 // Setzen der Headerinfo
584 memset(&m_aHeader,0,sizeof(m_aHeader));
585 sal_Int32 nType = 0;
586 ::vos::ORef<OSQLColumns> aCols = m_pTable->getTableColumns();
587 const Reference< XPropertySet > xTableCol(*find(aCols->get().begin(),aCols->get().end(),aName,::comphelper::UStringMixEqual(isCaseSensitive())));
589 xTableCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
591 m_aHeader.db_keytype = (nType == DataType::VARCHAR || nType == DataType::CHAR) ? 0 : 1;
592 m_aHeader.db_keylen = (m_aHeader.db_keytype) ? 8 : (USHORT)getINT32(xTableCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION)));
593 m_aHeader.db_keylen = (( m_aHeader.db_keylen - 1) / 4 + 1) * 4;
594 m_aHeader.db_maxkeys = (PAGE_SIZE - 4) / (8 + m_aHeader.db_keylen);
595 if ( m_aHeader.db_maxkeys < 3 )
597 impl_killFileAndthrowError_throw(STR_COULD_NOT_CREATE_INDEX_KEYSIZE,sFile);
600 m_pFileStream->SetStreamSize(PAGE_SIZE);
602 ByteString aCol(aName,m_pTable->getConnection()->getTextEncoding());
603 strncpy(m_aHeader.db_name,aCol.GetBuffer(),std::min((USHORT)sizeof(m_aHeader.db_name), aCol.Len()));
604 m_aHeader.db_unique = m_IsUnique ? 1: 0;
605 m_aHeader.db_keyrec = m_aHeader.db_keylen + 8;
607 // modifizierung am Header werden ueber Unterschiede zw. HeaderInfo und nRootPage
608 // bzw. nPageCout erkannt
610 m_nRootPage = 1;
611 m_nPageCount = 2;
613 // ODatabaseType eType = m_aHeader.db_keytype == 0 ? DataType::VARCHAR : DataType::DOUBLE;
614 m_aCurLeaf = m_aRoot = CreatePage(m_nRootPage);
615 m_aRoot->SetModified(TRUE);
617 m_bUseCollector = TRUE;
619 // ULONG nRowsLeft = pCursor->RowCount();
620 sal_Int32 nRowsLeft = 0;
621 Reference<XRow> xRow(xSet,UNO_QUERY);
623 if(xSet->last())
625 Reference< XUnoTunnel> xTunnel(xSet,UNO_QUERY);
626 ODbaseResultSet* pDbaseRes = NULL;
627 if(xTunnel.is())
628 pDbaseRes = reinterpret_cast< ODbaseResultSet* >( xTunnel->getSomething(ODbaseResultSet::getUnoTunnelImplementationId()) );
629 OSL_ENSURE(pDbaseRes,"No dbase resultset found? What's going on here!");
630 Reference<XRowLocate> xRowLocate(xSet,UNO_QUERY);
631 nRowsLeft = xSet->getRow();
633 xSet->beforeFirst();
634 ORowSetValue atmpValue=ORowSetValue();
635 ONDXKey aKey(atmpValue, nType, 0);
636 ONDXKey aInsertKey(atmpValue, nType, 0);
637 // Erzeugen der Indexstruktur
638 while (xSet->next())
640 // ODbRow& rRow = *pCursor->GetRow();
641 ORowSetValue aValue(m_aHeader.db_keytype ? ORowSetValue(xRow->getDouble(1)) : ORowSetValue(xRow->getString(1)));
642 // ueberpruefen auf doppelten eintrag
643 if (m_IsUnique && m_nCurNode != NODE_NOTFOUND)
645 aKey.setValue(aValue);
646 if (aKey == (*m_aCurLeaf)[m_nCurNode].GetKey())
648 impl_killFileAndthrowError_throw(STR_COULD_NOT_CREATE_INDEX_NOT_UNIQUE,sFile);
651 aInsertKey.setValue(aValue);
652 aInsertKey.setRecord(pDbaseRes->getCurrentFilePos());
654 ONDXNode aNewNode(aInsertKey);
655 if (!m_aCurLeaf->Insert(aNewNode, --nRowsLeft))
656 break;
660 if(nRowsLeft)
662 impl_killFileAndthrowError_throw(STR_COULD_NOT_CREATE_INDEX,sFile);
664 Release();
665 createINFEntry();
666 return sal_True;
668 // -----------------------------------------------------------------------------
669 // -----------------------------------------------------------------------------
670 void SAL_CALL ODbaseIndex::acquire() throw()
672 ODbaseIndex_BASE::acquire();
674 // -----------------------------------------------------------------------------
675 void SAL_CALL ODbaseIndex::release() throw()
677 ODbaseIndex_BASE::release();
679 // -----------------------------------------------------------------------------