|
1 // Copyright (c) 2003-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 "UD_STD.H" |
|
17 |
|
18 const TInt KPakArrayGranularity=0x80; |
|
19 |
|
20 // class TDbCol |
|
21 EXPORT_C TDbCol::TDbCol(const TDesC &aName,TDbColType aType) |
|
22 : iType(aType), iAttributes(0), iName(aName) |
|
23 { |
|
24 iMaxLength=(aType==EDbColText8 || aType==EDbColText16) |
|
25 ? KDbDefaultTextColLength : KDbUndefinedLength; |
|
26 } |
|
27 |
|
28 EXPORT_C TDbCol::TDbCol(const TDesC &aName,TDbColType aType,TInt aMaxLength) |
|
29 : iType(aType), iMaxLength(aMaxLength), iAttributes(0), iName(aName) |
|
30 /** Constructs a TDbCol with the given name, optional type and optional maximum |
|
31 length. |
|
32 |
|
33 Note: The iAttributes member is initialised to 0. |
|
34 |
|
35 @param aName The column name. |
|
36 @param aType The column type. |
|
37 @param aMaxLength If present, this specifies a limit on how many characters |
|
38 may be stored in a Text column, or how many bytes can be stored in a Binary |
|
39 column. By default this is set to KDbDefaultTextColLength for Text (but not |
|
40 LongText) columns and KDbUndefinedLength for other columns. */ |
|
41 {} |
|
42 |
|
43 // class CDbColSet |
|
44 EXPORT_C CDbColSet::CDbColSet() |
|
45 : iColumns(KPakArrayGranularity) |
|
46 /** Constructs an empty column set. */ |
|
47 {} |
|
48 |
|
49 EXPORT_C CDbColSet* CDbColSet::NewL() |
|
50 /** Constructs a new empty column set and returns a pointer to it. |
|
51 |
|
52 @return A pointer to the column set object. */ |
|
53 { |
|
54 return new(ELeave) CDbColSet; |
|
55 } |
|
56 |
|
57 EXPORT_C CDbColSet* CDbColSet::NewLC() |
|
58 /** Constructs a new empty column set and returns a pointer to it — placing |
|
59 the pointer onto the cleanup stack. This allows the column set object and |
|
60 allocated resources to be cleaned up if a subsequent leave occurs. |
|
61 |
|
62 @return A pointer to the column set object. */ |
|
63 { |
|
64 CDbColSet* cs=NewL(); |
|
65 CleanupStack::PushL(cs); |
|
66 return cs; |
|
67 } |
|
68 |
|
69 EXPORT_C CDbColSet::~CDbColSet() |
|
70 /** Frees resources owned by the object. */ |
|
71 {} |
|
72 |
|
73 EXPORT_C const TDbCol* CDbColSet::Col(const TDesC &aColName) const |
|
74 /** Returns a named column definition in the set. If such a column does not |
|
75 exist in the set NULL is returned. |
|
76 |
|
77 @param aColName The name of the column to find. |
|
78 @return A pointer to the column definition found, or NULL. */ |
|
79 { |
|
80 TDbColNo col=ColNo(aColName); |
|
81 if (col==KDbNullColNo) |
|
82 return NULL; |
|
83 else |
|
84 return &(*this)[col]; |
|
85 } |
|
86 |
|
87 EXPORT_C TDbColNo CDbColSet::ColNo(const TDesC &aColName) const |
|
88 /** Returns the ordinal for a particular column name in this column set. If |
|
89 such a column does not exist in the set the special ordinal KDbNullColNo is |
|
90 returned. |
|
91 |
|
92 This function is particularly important when accessing data through a rowset. |
|
93 If the set of columns to be returned by a rowset is not explicitly specified, |
|
94 no assumptions should be made about the ordering of the columns returned. |
|
95 In such a case, in order to access the column data a column ordinal is required, |
|
96 and this can be obtained by using this function on the column set returned |
|
97 by RDbRowSet::ColSetL(). |
|
98 |
|
99 @param aColName The name of the column to find. |
|
100 @return The ordinal number of the column. */ |
|
101 { |
|
102 TInt pos; |
|
103 TKeyArrayPak key(_FOFF(TDbCol,iName),ECmpFolded); |
|
104 if (iColumns.Find(TDbCol(aColName),key,pos)) |
|
105 return KDbNullColNo; |
|
106 else |
|
107 return pos+1; |
|
108 } |
|
109 |
|
110 EXPORT_C CDbColSet& CDbColSet::AddL(const TDbCol& aCol) |
|
111 /** Adds a column to the column set. |
|
112 |
|
113 @param aCol The column to add to the set. |
|
114 @return A reference to this object. */ |
|
115 { |
|
116 iColumns.AppendL(aCol,REINTERPRET_CAST(const TUint8*,aCol.iName.Ptr())+aCol.iName.Size()-REINTERPRET_CAST(const TUint8*,&aCol)); |
|
117 return *this; |
|
118 } |
|
119 |
|
120 EXPORT_C void CDbColSet::Remove(const TDesC &aColName) |
|
121 /** Removes the named column from the set. |
|
122 |
|
123 @param aColName The name of the column to remove from the set. */ |
|
124 { |
|
125 TDbColNo col=ColNo(aColName); |
|
126 __ASSERT_ALWAYS(col!=KDbNullColNo,Panic(EDbInvalidColumn)); |
|
127 iColumns.Delete(col-1); |
|
128 } |
|
129 |
|
130 // Class TDbColSetIter |
|
131 EXPORT_C TDbColSetIter::TDbColSetIter(const CDbColSet& aColSet) |
|
132 : iIndex(-1),iArray(&aColSet.iColumns) |
|
133 /** Constructs a column set iterator over a column set. The iterator now |
|
134 references the first column in the set. |
|
135 |
|
136 @param aColSet The column set to iterate over. */ |
|
137 { |
|
138 ++(*this); |
|
139 } |
|
140 |
|
141 EXPORT_C TDbColSetIter& TDbColSetIter::operator++() |
|
142 /** Moves the iterator to the next column in the set -- post increment operator. |
|
143 |
|
144 Note that this is implemented in terms of the pre-increment operator, and |
|
145 is less efficient. |
|
146 |
|
147 @param Unused: required for the C++ compiler to resolve the ambiguity with |
|
148 the pre-increment operator. |
|
149 @return A copy of this iterator, referring to the column definition before |
|
150 the increment operation is performed. */ |
|
151 { |
|
152 __ASSERT(iIndex<iArray->Count()); |
|
153 iColumn=++iIndex<iArray->Count() ? &(*iArray)[iIndex] : 0; |
|
154 return *this; |
|
155 } |
|
156 |
|
157 // class TDbKeyCol |
|
158 EXPORT_C TDbKeyCol::TDbKeyCol(const TDesC& aName,TOrder anOrder) |
|
159 : iOrder(anOrder),iLength(KDbUndefinedLength),iName(aName) |
|
160 {} |
|
161 |
|
162 EXPORT_C TDbKeyCol::TDbKeyCol(const TDesC& aName,TInt aLength,TOrder anOrder) |
|
163 : iOrder(anOrder),iLength(aLength),iName(aName) |
|
164 /** Constructs an object with the given name, ordering and optional truncation |
|
165 length. |
|
166 |
|
167 @param aName The column name. |
|
168 @param aLength If present, this specifies a limit on how many characters of |
|
169 a Text or LongText column are used for the key. This should only be used for |
|
170 the last key column in an index. It is required for keys on LongText columns. |
|
171 @param anOrder The ordering for the key column. By default this is ascending |
|
172 order. */ |
|
173 {} |
|
174 |
|
175 // class CDbKey |
|
176 |
|
177 EXPORT_C CDbKey::CDbKey() |
|
178 : iKeys(KPakArrayGranularity) |
|
179 /** Constructs an empty key. It is initialised to non-unique, non-primary and |
|
180 normal text comparison. */ |
|
181 {} |
|
182 |
|
183 EXPORT_C CDbKey *CDbKey::NewL() |
|
184 /** Constructs and returns a pointer to a new empty key. |
|
185 |
|
186 @return A pointer to the key object. */ |
|
187 { |
|
188 return new(ELeave) CDbKey; |
|
189 } |
|
190 |
|
191 EXPORT_C CDbKey *CDbKey::NewLC() |
|
192 /** Constructs and returns a new empty key and return a pointer to it, leaving |
|
193 a pointer to the key object on the cleanup stack. This allows the key object |
|
194 and allocated resources to be cleaned up if a subsequent leave occurs. |
|
195 |
|
196 @return A pointer to the key object. */ |
|
197 { |
|
198 CDbKey* key=NewL(); |
|
199 CleanupStack::PushL(key); |
|
200 return key; |
|
201 } |
|
202 |
|
203 EXPORT_C CDbKey::~CDbKey() |
|
204 /** Frees resources owned by the object. */ |
|
205 {} |
|
206 |
|
207 EXPORT_C CDbKey& CDbKey::AddL(const TDbKeyCol& aKey) |
|
208 /** Adds a key column to the end of the key. |
|
209 |
|
210 @param aKeyCol The key column to add to the key. |
|
211 @return A reference to this object. */ |
|
212 { |
|
213 iKeys.AppendL(aKey,REINTERPRET_CAST(const TUint8*,aKey.iName.Ptr())+aKey.iName.Size()-REINTERPRET_CAST(const TUint8*,&aKey)); |
|
214 return *this; |
|
215 } |
|
216 |
|
217 EXPORT_C void CDbKey::Remove(const TDesC& aColName) |
|
218 /** Removes the named column from the key. |
|
219 |
|
220 @param aColName The name of the column to remove from the key. */ |
|
221 { |
|
222 TInt pos; |
|
223 TKeyArrayPak key(_FOFF(TDbKeyCol,iName),ECmpFolded); |
|
224 __ASSERT_ALWAYS(!iKeys.Find(TDbKeyCol(aColName),key,pos),Panic(EDbInvalidColumn)); |
|
225 iKeys.Delete(pos); |
|
226 } |
|
227 |
|
228 EXPORT_C void CDbKey::Clear() |
|
229 /** Resets the key to its initial empty state. */ |
|
230 { |
|
231 iKeys.Reset(); |
|
232 iAttributes=0; |
|
233 iComparison=EDbCompareNormal; |
|
234 } |
|
235 |
|
236 // Class TDbLookupKey |
|
237 |
|
238 TDbLookupKey::SColumn& TDbLookupKey::NextKey() |
|
239 { |
|
240 return iKey[iCount++]; |
|
241 } |
|
242 |
|
243 void TDbLookupKey::Add(TInt aKey) |
|
244 { |
|
245 SColumn& key=NextKey(); |
|
246 key.iType=EDbColInt32; |
|
247 key.iInt32=aKey; |
|
248 } |
|
249 |
|
250 void TDbLookupKey::Add(TUint aKey) |
|
251 { |
|
252 SColumn& key=NextKey(); |
|
253 key.iType=EDbColUint32; |
|
254 key.iUint32=aKey; |
|
255 } |
|
256 |
|
257 void TDbLookupKey::Add(TInt64 aKey) |
|
258 { |
|
259 SColumn& key=NextKey(); |
|
260 key.iType=EDbColInt64; |
|
261 key.iInt64()=aKey; |
|
262 } |
|
263 |
|
264 void TDbLookupKey::Add(TReal32 aKey) __SOFTFP |
|
265 { |
|
266 SColumn& key=NextKey(); |
|
267 key.iType=EDbColReal32; |
|
268 key.iReal32=aKey; |
|
269 } |
|
270 |
|
271 void TDbLookupKey::Add(TReal64 aKey) __SOFTFP |
|
272 { |
|
273 SColumn& key=NextKey(); |
|
274 key.iType=EDbColReal64; |
|
275 key.iReal64=aKey; |
|
276 } |
|
277 |
|
278 void TDbLookupKey::Add(TTime aKey) |
|
279 { |
|
280 SColumn& key=NextKey(); |
|
281 key.iType=EDbColDateTime; |
|
282 key.iTime()=aKey; |
|
283 } |
|
284 |
|
285 void TDbLookupKey::Add(const TDesC8& aKey) |
|
286 { |
|
287 SColumn& key=NextKey(); |
|
288 key.iType=EDbColText8; |
|
289 key.iDes8.iPtr=aKey.Ptr(); |
|
290 key.iDes8.iLength=aKey.Length(); |
|
291 } |
|
292 |
|
293 void TDbLookupKey::Add(const TDesC16& aKey) |
|
294 { |
|
295 SColumn& key=NextKey(); |
|
296 key.iType=EDbColText16; |
|
297 key.iDes16.iPtr=aKey.Ptr(); |
|
298 key.iDes16.iLength=aKey.Length(); |
|
299 } |
|
300 |
|
301 // Class TDbSeekKey |
|
302 |
|
303 TDbLookupKey& TDbSeekKey::Check() |
|
304 { |
|
305 __ASSERT_ALWAYS(iKey.Count()<iMaxKeys,Panic(EDbTooManyKeys)); |
|
306 return iKey; |
|
307 } |
|
308 |
|
309 EXPORT_C TDbSeekKey& TDbSeekKey::Add(TInt aKey) |
|
310 /** Appends a key value for an TInt8, TInt16 or TInt32 column. |
|
311 |
|
312 @param aKey The key value to lookup. |
|
313 @return A reference to this database key value object. */ |
|
314 { |
|
315 Check().Add(aKey); |
|
316 return *this; |
|
317 } |
|
318 |
|
319 EXPORT_C TDbSeekKey& TDbSeekKey::Add(TUint aKey) |
|
320 /** Appends a key value for a Bit, TUint8, TUint16 or TUint32 column. |
|
321 |
|
322 @param aKey The key value to lookup. |
|
323 @return A reference to this database key value object. */ |
|
324 { |
|
325 Check().Add(aKey); |
|
326 return *this; |
|
327 } |
|
328 |
|
329 EXPORT_C TDbSeekKey& TDbSeekKey::Add(TInt64 aKey) |
|
330 /** Appends a key value for an TInt64 column. |
|
331 |
|
332 @param aKey The key value to lookup. |
|
333 @return A reference to this database key value object. */ |
|
334 { |
|
335 Check().Add(aKey); |
|
336 return *this; |
|
337 } |
|
338 |
|
339 EXPORT_C TDbSeekKey& TDbSeekKey::Add(TReal32 aKey) __SOFTFP |
|
340 /** Appends a key value for a TReal32 column. |
|
341 |
|
342 @param aKey The key value to lookup. |
|
343 @return A reference to this database key value object. */ |
|
344 { |
|
345 Check().Add(aKey); |
|
346 return *this; |
|
347 } |
|
348 |
|
349 EXPORT_C TDbSeekKey& TDbSeekKey::Add(TReal64 aKey) __SOFTFP |
|
350 /** Appends a key value for a TReal64 column. |
|
351 |
|
352 @param aKey The key value to lookup. |
|
353 @return A reference to this database key value object. */ |
|
354 { |
|
355 Check().Add(aKey); |
|
356 return *this; |
|
357 } |
|
358 |
|
359 EXPORT_C TDbSeekKey& TDbSeekKey::Add(TTime aKey) |
|
360 /** Appends a key value for a DateTime column. |
|
361 |
|
362 @param aKey The key value to lookup. TTime may be either local time or universal time. |
|
363 DBMS doesn't interpret the value of TTime, it is left up to the user to decide which should be used. |
|
364 @return A reference to this database key value object. */ |
|
365 { |
|
366 Check().Add(aKey); |
|
367 return *this; |
|
368 } |
|
369 |
|
370 EXPORT_C TDbSeekKey& TDbSeekKey::Add(const TDesC8& aKey) |
|
371 /** Appends a key value for a non-Unicode text column. |
|
372 |
|
373 Note that the seek key does not copy the text data contained by the descriptor. |
|
374 This needs to be retained until the seek key is no longer required. |
|
375 |
|
376 @param aKey The key value to lookup. |
|
377 @return A reference to this database key value object. */ |
|
378 { |
|
379 Check().Add(aKey); |
|
380 return *this; |
|
381 } |
|
382 |
|
383 EXPORT_C TDbSeekKey& TDbSeekKey::Add(const TDesC16& aKey) |
|
384 /** Appends a key value for a Unicode text column. |
|
385 |
|
386 Note that the seek key does not copy the text data contained by the descriptor. |
|
387 This needs to be retained until the seek key is no longer required. |
|
388 |
|
389 @param aKey The key value to lookup. |
|
390 @return A reference to this database key value object. */ |
|
391 { |
|
392 Check().Add(aKey); |
|
393 return *this; |
|
394 } |
|
395 |
|
396 // Class CDbNames |
|
397 |
|
398 inline CDbNames::CDbNames() |
|
399 : iList(KPakArrayGranularity) |
|
400 {} |
|
401 |
|
402 CDbNames* CDbNames::NewLC() |
|
403 { |
|
404 CDbNames* self=new(ELeave) CDbNames; |
|
405 CleanupStack::PushL(self); |
|
406 return self; |
|
407 } |
|
408 |
|
409 CDbNames::~CDbNames() |
|
410 {} |
|
411 |
|
412 EXPORT_C void CDbNames::AddL(const TDesC& aName) |
|
413 { |
|
414 TDbNameC name(aName); |
|
415 iList.AppendL(name,(const TUint8*)name.Ptr()+name.Size()-(const TUint8*)&name); |
|
416 } |
|
417 |
|
418 //////////////////////////////////////////////////////////////////////////////////////////////// |
|
419 // Class CDbStrings |
|
420 |
|
421 /** |
|
422 Granularity of CDbStrings array. |
|
423 @internalComponent |
|
424 */ |
|
425 const TInt KPakArrayGranularity2 = KDbMaxStrLen * 2; |
|
426 |
|
427 /** |
|
428 @internalComponent |
|
429 */ |
|
430 inline CDbStrings::CDbStrings() : |
|
431 iList(KPakArrayGranularity2) |
|
432 { |
|
433 } |
|
434 |
|
435 /** |
|
436 Standard phase-one creation method for CDbStrings objects. |
|
437 @return A pointer to the created CDbStrings object. |
|
438 @leave KErrNoMemory - Out of memory. |
|
439 */ |
|
440 CDbStrings* CDbStrings::NewLC() |
|
441 { |
|
442 CDbStrings* self = new(ELeave) CDbStrings; |
|
443 CleanupStack::PushL(self); |
|
444 return self; |
|
445 } |
|
446 |
|
447 /** |
|
448 @internalComponent |
|
449 */ |
|
450 CDbStrings::~CDbStrings() |
|
451 { |
|
452 } |
|
453 |
|
454 /** |
|
455 Adds a string to the array. |
|
456 @param aStr String to be added to the array. |
|
457 @leave KErrNoMemory - Out of memory. |
|
458 @internalComponent |
|
459 */ |
|
460 void CDbStrings::AddL(const TDesC& aStr) |
|
461 { |
|
462 TDbStringC str(aStr); |
|
463 iList.AppendL(str, (const TUint8*)str.Ptr() + str.Size() - (const TUint8*)&str); |
|
464 } |