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