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