os/persistentdata/persistentstorage/dbms/pcdbms/utable/UT_SCHMA.CPP
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "UT_STD.H"
sl@0
    17
#include "U32STD_DBMS.H"
sl@0
    18
sl@0
    19
// Class CDbTableIndexDef
sl@0
    20
sl@0
    21
EXPORT_C CDbTableIndexDef::CDbTableIndexDef()
sl@0
    22
	{}
sl@0
    23
sl@0
    24
EXPORT_C CDbTableIndexDef::~CDbTableIndexDef()
sl@0
    25
	{
sl@0
    26
	delete iName;
sl@0
    27
	}
sl@0
    28
sl@0
    29
EXPORT_C void CDbTableIndexDef::ConstructL(const TDesC& aName)
sl@0
    30
//
sl@0
    31
// Construct with an empty key
sl@0
    32
//
sl@0
    33
	{
sl@0
    34
	iName=aName.AllocL();
sl@0
    35
	}
sl@0
    36
sl@0
    37
// Class RDbIndexes
sl@0
    38
sl@0
    39
void RDbIndexes::Close()
sl@0
    40
	{
sl@0
    41
	TSglQueIter<CDbTableIndexDef> iter(iRep);
sl@0
    42
	for (CDbTableIndexDef* def;(def=iter++)!=0;)
sl@0
    43
		delete def;
sl@0
    44
	}
sl@0
    45
sl@0
    46
CDbTableIndexDef* RDbIndexes::Find(const TDesC& aName) const
sl@0
    47
//
sl@0
    48
// Locate and index on the table by name
sl@0
    49
//
sl@0
    50
	{
sl@0
    51
	TSglQueIter<CDbTableIndexDef> iter(CONST_CAST(TSglQue<CDbTableIndexDef>&,iRep));
sl@0
    52
	for (CDbTableIndexDef* def;(def=iter++)!=0;)
sl@0
    53
		if (aName.CompareF(def->Name())==0)
sl@0
    54
			return def;
sl@0
    55
	return 0;
sl@0
    56
	}
sl@0
    57
sl@0
    58
CDbTableIndexDef& RDbIndexes::FindL(const TDesC& aName) const
sl@0
    59
//
sl@0
    60
// Leave if not found
sl@0
    61
//
sl@0
    62
	{
sl@0
    63
	CDbTableIndexDef* def=Find(aName);
sl@0
    64
	if (def==NULL)
sl@0
    65
		__LEAVE(KErrNotFound);
sl@0
    66
	return *def;
sl@0
    67
	}
sl@0
    68
sl@0
    69
EXPORT_C TInt RDbIndexes::Count() const
sl@0
    70
	{
sl@0
    71
	TInt count = 0;
sl@0
    72
	TSglQueIterC<CDbTableIndexDef> iter( iRep );
sl@0
    73
	for ( ; iter++ != 0; )
sl@0
    74
		++count;
sl@0
    75
	return count;
sl@0
    76
	}
sl@0
    77
sl@0
    78
// Class TDbColumnDef
sl@0
    79
sl@0
    80
EXPORT_C void TDbColumnDef::SetL(const TDbColumnDef& aCol)
sl@0
    81
//
sl@0
    82
// Replace assignment as this can leave
sl@0
    83
//
sl@0
    84
	{
sl@0
    85
	iMaxLength=aCol.iMaxLength;
sl@0
    86
	iType=aCol.iType;
sl@0
    87
	iAttributes=aCol.iAttributes;
sl@0
    88
	iFlags=aCol.iFlags;
sl@0
    89
	iName=aCol.iName->AllocL();
sl@0
    90
	}
sl@0
    91
sl@0
    92
EXPORT_C void TDbColumnDef::SetL(const TDbCol& aCol)
sl@0
    93
	{
sl@0
    94
	iMaxLength=aCol.iMaxLength;
sl@0
    95
	iType=TUint8(aCol.iType);
sl@0
    96
	iAttributes=TUint8(aCol.iAttributes);
sl@0
    97
	iName=aCol.iName.AllocL();
sl@0
    98
	}
sl@0
    99
sl@0
   100
void TDbColumnDef::AsTDbCol(TDbCol& aColumn) const
sl@0
   101
	{
sl@0
   102
	aColumn.iType=Type();
sl@0
   103
	aColumn.iMaxLength=iMaxLength;
sl@0
   104
	aColumn.iAttributes=iAttributes;
sl@0
   105
	aColumn.iName=*iName;
sl@0
   106
	}
sl@0
   107
sl@0
   108
// Class HDbColumnSet
sl@0
   109
sl@0
   110
HDbColumnSet* HDbColumnSet::NewL(TInt aCount)
sl@0
   111
	{
sl@0
   112
	return new(User::AllocL(_FOFF(HDbColumnSet,iColumns[aCount]))) HDbColumnSet(aCount);
sl@0
   113
	}
sl@0
   114
sl@0
   115
HDbColumnSet::HDbColumnSet(TInt aCount)
sl@0
   116
	: iAttributes(0), iIndex(NULL), iEnd(&iColumns[aCount])
sl@0
   117
	{
sl@0
   118
	Mem::FillZ(&iColumns[0],aCount*sizeof(iColumns[0]));
sl@0
   119
	}
sl@0
   120
sl@0
   121
HDbColumnSet::~HDbColumnSet()
sl@0
   122
	{
sl@0
   123
	User::Free(iIndex);
sl@0
   124
	const TIteratorC end=End();
sl@0
   125
	for (TIteratorC iter=Begin();iter<end;++iter)
sl@0
   126
		delete iter->iName;
sl@0
   127
	}
sl@0
   128
sl@0
   129
EXPORT_C TInt HDbColumnSet::Count() const
sl@0
   130
	{
sl@0
   131
	return End()-Begin();
sl@0
   132
	}
sl@0
   133
sl@0
   134
void HDbColumnSet::Complete()
sl@0
   135
//
sl@0
   136
// Called on completion of the column set
sl@0
   137
//
sl@0
   138
	{
sl@0
   139
	TUint attrib=0;
sl@0
   140
	const TIteratorC end=End();
sl@0
   141
	for (TIteratorC iter=Begin();iter<end;++iter)
sl@0
   142
		{
sl@0
   143
		if (TDbCol::IsLong(iter->Type()))
sl@0
   144
			attrib|=ELongColumns;
sl@0
   145
		if (iter->iAttributes&TDbCol::EAutoIncrement)
sl@0
   146
			attrib|=EAutoIncrement;
sl@0
   147
		}
sl@0
   148
	iAttributes=attrib;
sl@0
   149
	}
sl@0
   150
sl@0
   151
EXPORT_C HDbColumnSet::TIteratorC HDbColumnSet::ColumnL(const TDesC& aColumn) const
sl@0
   152
//
sl@0
   153
// Lookup name in the name index (binary search)
sl@0
   154
//
sl@0
   155
	{
sl@0
   156
	const TIteratorC* index=IndexL();
sl@0
   157
	TInt left=0;
sl@0
   158
	TInt right=Count();
sl@0
   159
	while (left<right)
sl@0
   160
		{
sl@0
   161
		TInt mid=(left+right)>>1;
sl@0
   162
		TInt c=index[mid]->iName->CompareF(aColumn);
sl@0
   163
		if (c<0)
sl@0
   164
			left=mid+1;
sl@0
   165
		else if (c>0)
sl@0
   166
			right=mid;
sl@0
   167
		else
sl@0
   168
			return index[mid];		// matched entry
sl@0
   169
		}
sl@0
   170
	return 0;		// no match
sl@0
   171
	}
sl@0
   172
sl@0
   173
EXPORT_C TDbColNo HDbColumnSet::ColNoL(const TDesC& aColumn) const
sl@0
   174
//
sl@0
   175
// Return ordinal for given column name
sl@0
   176
//
sl@0
   177
	{
sl@0
   178
	TIteratorC col=ColumnL(aColumn);
sl@0
   179
	return col ? 1+(col-Begin()) : KDbNullColNo;
sl@0
   180
	}
sl@0
   181
sl@0
   182
const HDbColumnSet::TIteratorC* HDbColumnSet::IndexL() const
sl@0
   183
//
sl@0
   184
// Return the by-name lookup index, building it if required
sl@0
   185
//
sl@0
   186
	{
sl@0
   187
	TIteratorC* index=iIndex;
sl@0
   188
	if (!index)
sl@0
   189
		{
sl@0
   190
		CONST_CAST(TIteratorC*&,iIndex)=index=(TIteratorC*)User::AllocL(Count()*sizeof(TIteratorC));
sl@0
   191
		TInt ii=0;
sl@0
   192
		TIteratorC col=Begin();
sl@0
   193
		TIteratorC end=End();
sl@0
   194
		do
sl@0
   195
			{
sl@0
   196
			// binary search for insertion point
sl@0
   197
			TInt left=0;
sl@0
   198
			TInt right=ii;
sl@0
   199
			while (left<right)
sl@0
   200
				{
sl@0
   201
				TInt mid=(left+right)>>1;
sl@0
   202
				TInt c=index[mid]->iName->CompareF(*col->iName);
sl@0
   203
				__ASSERT(c!=0);		// names are unique
sl@0
   204
				if (c<0)
sl@0
   205
					left=mid+1;
sl@0
   206
				else
sl@0
   207
					right=mid;
sl@0
   208
				}
sl@0
   209
			// insert the entry
sl@0
   210
			Mem::Move(index+left+1,index+left,(ii-left)*sizeof(TIteratorC));
sl@0
   211
			index[left]=col;
sl@0
   212
			} while (++ii,++col<end);
sl@0
   213
		}
sl@0
   214
	return index;
sl@0
   215
	}
sl@0
   216
sl@0
   217
// Class CDbTableDef
sl@0
   218
sl@0
   219
EXPORT_C CDbTableDef::CDbTableDef()
sl@0
   220
	{}
sl@0
   221
sl@0
   222
EXPORT_C CDbTableDef::~CDbTableDef()
sl@0
   223
	{
sl@0
   224
	delete iColumns;
sl@0
   225
	delete iName;
sl@0
   226
	iIndexes.Close();
sl@0
   227
	}
sl@0
   228
sl@0
   229
EXPORT_C void CDbTableDef::ConstructL(const TDesC& aName,TInt aColumnCount)
sl@0
   230
	{
sl@0
   231
	iName=aName.AllocL();
sl@0
   232
	iColumns=HDbColumnSet::NewL(aColumnCount);
sl@0
   233
	}
sl@0
   234
sl@0
   235
void CDbTableDef::ExchangeColumnSet(HDbColumnSet* aSet)
sl@0
   236
	{
sl@0
   237
	delete iColumns;
sl@0
   238
	iColumns=aSet;
sl@0
   239
	Changed();
sl@0
   240
	}
sl@0
   241
sl@0
   242
EXPORT_C void CDbTableDef::Changed()
sl@0
   243
//
sl@0
   244
// evaluate cached info about the set
sl@0
   245
//
sl@0
   246
	{
sl@0
   247
	iColumns->Complete();
sl@0
   248
	}
sl@0
   249
sl@0
   250
const CDbTableIndexDef* CDbTableDef::FindKey(const TDesC& aColumn,TBool aFirstColumn) const
sl@0
   251
//
sl@0
   252
// Find an index which keys on the given column
sl@0
   253
//
sl@0
   254
	{
sl@0
   255
	TSglQueIterC<CDbTableIndexDef> indexes(Indexes().AsQue());
sl@0
   256
	for (const CDbTableIndexDef* def;(def=indexes++)!=0;)
sl@0
   257
		{
sl@0
   258
		const CDbKey& key=def->Key();
sl@0
   259
		TInt ii=0;
sl@0
   260
		do
sl@0
   261
			{
sl@0
   262
			if (aColumn.CompareF(key[ii].iName)==0)
sl@0
   263
				return def;
sl@0
   264
			if (aFirstColumn)
sl@0
   265
				break;
sl@0
   266
			} while (++ii<key.Count());
sl@0
   267
		}
sl@0
   268
	return 0;
sl@0
   269
	}
sl@0
   270
sl@0
   271
// Class RDbTableSchema
sl@0
   272
sl@0
   273
void RDbTableSchema::Discard()
sl@0
   274
	{
sl@0
   275
	TSglQueIter<CDbTableDef> iter(iRep);
sl@0
   276
	for (CDbTableDef* def;(def=iter++)!=0;)
sl@0
   277
		delete def;
sl@0
   278
	iRep.Reset();
sl@0
   279
	iLoaded=EFalse;
sl@0
   280
	}
sl@0
   281
sl@0
   282
CDbTableDef* RDbTableSchema::Find(const TDesC& aTable)
sl@0
   283
	{
sl@0
   284
	__ASSERT(IsLoaded());
sl@0
   285
	TSglQueIter<CDbTableDef> iter(iRep);
sl@0
   286
	for (CDbTableDef* def;(def=iter++)!=0;)
sl@0
   287
		if (aTable.CompareF(def->Name())==0)
sl@0
   288
			return def;
sl@0
   289
	return 0;
sl@0
   290
	}
sl@0
   291
sl@0
   292
CDbTableDef& RDbTableSchema::FindL(const TDesC& aTable)
sl@0
   293
//
sl@0
   294
// Leave if not found
sl@0
   295
//
sl@0
   296
	{
sl@0
   297
	CDbTableDef* def=Find(aTable);
sl@0
   298
	if (def==NULL)
sl@0
   299
		__LEAVE(KErrNotFound);
sl@0
   300
	return *def;
sl@0
   301
	}
sl@0
   302
sl@0
   303
// Class RDbTables
sl@0
   304
sl@0
   305
void RDbTables::Close()
sl@0
   306
	{
sl@0
   307
	TSglQueIter<CDbTable> iter(iRep);
sl@0
   308
	for (CDbTable* table;(table=iter++)!=0;)
sl@0
   309
		table->Discard();
sl@0
   310
	}
sl@0
   311
sl@0
   312
CDbTable* RDbTables::Find(const TDesC& aTable)
sl@0
   313
	{
sl@0
   314
	TSglQueIter<CDbTable> iter(iRep);
sl@0
   315
	for (CDbTable* table;(table=iter++)!=0;)
sl@0
   316
		if (aTable.CompareF(table->Def().Name())==0)
sl@0
   317
			return table;
sl@0
   318
	return 0;
sl@0
   319
	}