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