|
1 /* |
|
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "pltables.h" |
|
20 #include "dbsqlconstants.h" |
|
21 |
|
22 |
|
23 const TInt KArrayGranularity = 4; |
|
24 |
|
25 /** |
|
26 @param aDatabase A handle to the database. |
|
27 |
|
28 @return A pointer to a new CPplGroupsTable object. |
|
29 */ |
|
30 CPplGroupsTable* CPplGroupsTable::NewL(RSqlDatabase& aDatabase) |
|
31 { |
|
32 CPplGroupsTable* self = CPplGroupsTable::NewLC(aDatabase); |
|
33 CleanupStack::Pop(self); |
|
34 return self; |
|
35 } |
|
36 |
|
37 /** |
|
38 @param aDatabase A handle to the database. |
|
39 |
|
40 @return A pointer to a new CPplGroupsTable object. |
|
41 */ |
|
42 CPplGroupsTable* CPplGroupsTable::NewLC(RSqlDatabase& aDatabase) |
|
43 { |
|
44 CPplGroupsTable* self = new (ELeave) CPplGroupsTable(aDatabase); |
|
45 CleanupStack::PushL(self); |
|
46 self->ConstructL(); |
|
47 return self; |
|
48 } |
|
49 |
|
50 /** |
|
51 Set up the CCntSqlStatement objects held by the class. |
|
52 */ |
|
53 void CPplGroupsTable::ConstructL() |
|
54 { |
|
55 // Statement types |
|
56 TCntSqlStatementType insertType(EInsert, KSqlContactGroupTableName); |
|
57 TCntSqlStatementType selectType(ESelect, KSqlContactGroupTableName); |
|
58 TCntSqlStatementType updateType(EUpdate, KSqlContactGroupTableName); |
|
59 TCntSqlStatementType deleteType(EDelete, KSqlContactGroupTableName); |
|
60 TCntSqlStatementType countContactsType(ESelect, KSqlContactTableName); |
|
61 |
|
62 // Where clauses |
|
63 |
|
64 // sizes of the clauses |
|
65 const TInt KWhereGroupClauseBufSize(KGroupContactGroupId().Size() + |
|
66 KWhereStringEqualsStringFormatText().Size() + KGroupContactGroupIdParam().Size() ); |
|
67 const TInt KWhereMemberClauseBufSize(KGroupContactGroupMemberId().Size() + |
|
68 KWhereStringEqualsStringFormatText().Size() + KGroupContactGroupMemberIdParam().Size() ); |
|
69 const TInt KWhereOrClauseBufSize(KWhereGroupClauseBufSize + KSqlOr().Size() + KWhereMemberClauseBufSize); |
|
70 |
|
71 // for WHERE contact_group_id = [contact id value] |
|
72 HBufC* whereGroupIdClause = HBufC::NewLC(KWhereGroupClauseBufSize); |
|
73 whereGroupIdClause->Des().AppendFormat(KWhereStringEqualsStringFormatText, |
|
74 &KGroupContactGroupId, &KGroupContactGroupIdParam ); |
|
75 |
|
76 // for WHERE contact_group_member_id = [contact id value] |
|
77 HBufC* whereMemberIdClause = HBufC::NewLC(KWhereMemberClauseBufSize); |
|
78 whereMemberIdClause->Des().AppendFormat(KWhereStringEqualsStringFormatText, |
|
79 &KGroupContactGroupMemberId, &KGroupContactGroupMemberIdParam ); |
|
80 |
|
81 // for WHERE contact_group_id = [contact id value] |
|
82 // OR contact_group_member_id = [contact id value] |
|
83 HBufC* whereGroupOrMemberIdClause = HBufC::NewLC(KWhereOrClauseBufSize); |
|
84 TPtr whereGroupOrMemberIdClausePtr = whereGroupOrMemberIdClause->Des(); |
|
85 whereGroupOrMemberIdClausePtr.AppendFormat(KWhereStringEqualsStringFormatText, |
|
86 &KGroupContactGroupId, &KGroupContactGroupIdParam); |
|
87 whereGroupOrMemberIdClausePtr.Append(KSqlOr); |
|
88 whereGroupOrMemberIdClausePtr.AppendFormat(KWhereStringEqualsStringFormatText, |
|
89 &KGroupContactGroupMemberId, &KGroupContactGroupMemberIdParam); |
|
90 |
|
91 // for WHERE contact_id = [contact_id] |
|
92 HBufC* whereContactIdClause = HBufC::NewLC(KWhereGroupClauseBufSize); |
|
93 whereContactIdClause->Des().AppendFormat(KWhereStringEqualsStringFormatText, |
|
94 &KContactId, &KContactIdParam ); |
|
95 |
|
96 // INSERT |
|
97 |
|
98 // insert group-member relationships |
|
99 // For a statement in the following format: |
|
100 // INSERT INTO groups |
|
101 // (group_id, contact_group_id, contact_group_member_id) |
|
102 // VALUES (NULL, [contact group id value], [contact group member id value]); |
|
103 // |
|
104 iInsertStmnt = TSqlProvider::GetSqlStatementL(insertType); |
|
105 iInsertStmnt->SetParamL(KGroupContactGroupId(), KGroupContactGroupIdParam()); |
|
106 iInsertStmnt->SetParamL(KGroupContactGroupMemberId(), KGroupContactGroupMemberIdParam()); |
|
107 |
|
108 // SELECT |
|
109 |
|
110 // select group id |
|
111 // For a statement in the following format: |
|
112 // SELECT contact_group_id FROM groups |
|
113 // WHERE contact_group_member_id = [contact id value]; |
|
114 // |
|
115 iSelectGroupsStmnt = TSqlProvider::GetSqlStatementL(selectType); |
|
116 iSelectGroupsStmnt->SetParamL(KGroupContactGroupId(), KNullDesC() ); |
|
117 iSelectGroupsStmnt->SetConditionL(*whereMemberIdClause); |
|
118 |
|
119 // select member id |
|
120 // For a statement in the following format: |
|
121 // SELECT contact_group_member_id FROM groups |
|
122 // WHERE contact_group_id = [contact id value]; |
|
123 // |
|
124 iSelectMembersStmnt = TSqlProvider::GetSqlStatementL(selectType); |
|
125 iSelectMembersStmnt->SetParamL(KGroupContactGroupMemberId(), KNullDesC() ); |
|
126 iSelectMembersStmnt->SetConditionL(*whereGroupIdClause); |
|
127 |
|
128 // DELETE |
|
129 |
|
130 // delete all where group or member equals id |
|
131 // For a statement in the following format: |
|
132 // DELETE FROM groups WHERE contact_group_id = [contact id value] |
|
133 // OR contact_group_member_id = [contact id value]; |
|
134 // |
|
135 iDeleteStmnt = TSqlProvider::GetSqlStatementL(deleteType); |
|
136 iDeleteStmnt->SetConditionL(*whereGroupOrMemberIdClause); |
|
137 |
|
138 // SELECT |
|
139 |
|
140 // SELECt count(*) FROM contact WHERE contact_id = [contact_id] |
|
141 iCountContactsStmnt = TSqlProvider::GetSqlStatementL(countContactsType); |
|
142 iCountContactsStmnt->SetParamL(KSqlCount, KSpace); |
|
143 iCountContactsStmnt->SetConditionL(*whereContactIdClause); |
|
144 |
|
145 CleanupStack::PopAndDestroy(4, whereGroupIdClause); // and whereContactIdClause, whereMemberIdClause, whereGroupOrMemberIdClause |
|
146 } |
|
147 |
|
148 |
|
149 /** |
|
150 Destructor |
|
151 |
|
152 Tidy up CCntSqlStatement objects |
|
153 */ |
|
154 CPplGroupsTable::~CPplGroupsTable() |
|
155 { |
|
156 delete iInsertStmnt; |
|
157 delete iSelectGroupsStmnt; |
|
158 delete iSelectMembersStmnt; |
|
159 delete iDeleteStmnt; |
|
160 delete iCountContactsStmnt; |
|
161 } |
|
162 |
|
163 |
|
164 /** |
|
165 Does nothing. An empty implementation to override the pure virtual method in the base class. |
|
166 */ |
|
167 void CPplGroupsTable::CreateInDbL(CContactItem& /*aItem*/) |
|
168 { |
|
169 // Do nothing. |
|
170 |
|
171 // Inserting new records in the groups table is done through the UpdateL() method. |
|
172 } |
|
173 |
|
174 |
|
175 /** |
|
176 ReadL has a dual functionality. If passed parameter is a group, will be filled to contacts |
|
177 bellonging to that group. Otherwirse, the contact item will be filed with all groups to which |
|
178 it belongs |
|
179 |
|
180 @param aItem Reference to contact item. |
|
181 */ |
|
182 void CPplGroupsTable::ReadL(CContactItem& aItem) |
|
183 { |
|
184 const TContactItemId KItemId(aItem.Id() ); |
|
185 const TUid KType(aItem.Type() ); |
|
186 if (KType == KUidContactGroup) |
|
187 { |
|
188 CContactGroup& group = static_cast<CContactGroup&>(aItem); |
|
189 group.ResetItems(); |
|
190 group.SetItems(GetListForItemL(KItemId, ETrue)); |
|
191 } |
|
192 |
|
193 |
|
194 if (KType == KUidContactCard || KType == KUidContactOwnCard || KType == KUidContactICCEntry || KType == KUidContactGroup) |
|
195 { |
|
196 CContactItemPlusGroup& item = static_cast<CContactItemPlusGroup&>(aItem); |
|
197 item.ResetGroups(); |
|
198 item.SetGroups(GetListForItemL(KItemId, EFalse)); |
|
199 } |
|
200 } |
|
201 |
|
202 |
|
203 /** |
|
204 Updates informations related to passed contact item within group table |
|
205 |
|
206 @param aItem Reference to contact item. |
|
207 */ |
|
208 void CPplGroupsTable::UpdateL(const CContactItem& aItem) |
|
209 { |
|
210 // Only write to the table if it's a group. If we add the relationship from both sides |
|
211 // (i.e. once for the group and once for the item) we will have duplicate records. |
|
212 if (aItem.Type() == KUidContactGroup) |
|
213 { |
|
214 WriteGroupMembersL(aItem); |
|
215 } |
|
216 } |
|
217 |
|
218 |
|
219 /** |
|
220 Deletes group informations related to passed contact item from group table |
|
221 |
|
222 @param aItem Reference to contact item. |
|
223 @param aLowDiskErrorOccurred out parameter; will be set to ETrue if there was an attempt to delete |
|
224 in low disk condition |
|
225 */ |
|
226 void CPplGroupsTable::DeleteL(const CContactItem& aItem, TBool& aLowDiskErrorOccurred) |
|
227 { |
|
228 DeleteItemL(aItem.Id(), aLowDiskErrorOccurred); |
|
229 } |
|
230 |
|
231 |
|
232 /** |
|
233 Creates the groups table and its indexes in the database. |
|
234 */ |
|
235 void CPplGroupsTable::CreateTableL() |
|
236 { |
|
237 User::LeaveIfError(iDatabase.Exec(KGroupsCreateStmnt() ) ); |
|
238 } |
|
239 |
|
240 |
|
241 /** |
|
242 CPplGroupsTable constructor |
|
243 |
|
244 @param aDatabase reference to contact database |
|
245 */ |
|
246 CPplGroupsTable::CPplGroupsTable(RSqlDatabase& aDatabase) : |
|
247 iDatabase(aDatabase) |
|
248 { |
|
249 } |
|
250 |
|
251 |
|
252 /** |
|
253 GetListForItemL has a dual nature. If aIsGroup is ETrue, a list of contact items belonging to |
|
254 specified group is returned. Otherwise a list of group ids to which contact id belongs is returned. |
|
255 |
|
256 @param aItemId contact item id |
|
257 @param aIsGroup ETrue if the method will fill a group. |
|
258 */ |
|
259 CContactIdArray* CPplGroupsTable::GetListForItemL(TContactItemId aItemId, TBool aIsGroup) |
|
260 { |
|
261 /* |
|
262 // Check if group membership information was not requested or if the item |
|
263 // is not derived from CContactItemPlusGroup. |
|
264 if (!(aType == KUidContactGroup || aType == KUidContactCard || |
|
265 aType == KUidContactOwnCard || aType == KUidContactICCEntry) ) |
|
266 { |
|
267 return NULL; |
|
268 } |
|
269 */ |
|
270 |
|
271 // build the RSqlStatement |
|
272 RSqlStatement stmnt; |
|
273 CleanupClosePushL(stmnt); |
|
274 TInt idIndex; |
|
275 |
|
276 // build the CCntSqlStatement statement |
|
277 const TInt KWhereParamIndex(KFirstIndex); // only one parameter in the query |
|
278 if (aIsGroup) |
|
279 { |
|
280 // group -> select members |
|
281 stmnt.PrepareL(iDatabase, iSelectMembersStmnt->SqlStringL() ); |
|
282 User::LeaveIfError(stmnt.BindInt(KWhereParamIndex, aItemId ) ); |
|
283 idIndex = stmnt.ColumnIndex(KGroupContactGroupMemberId() ); |
|
284 } |
|
285 else |
|
286 { |
|
287 // member -> select groups |
|
288 stmnt.PrepareL(iDatabase, iSelectGroupsStmnt->SqlStringL() ); |
|
289 User::LeaveIfError(stmnt.BindInt(KWhereParamIndex, aItemId ) ); |
|
290 idIndex = stmnt.ColumnIndex(KGroupContactGroupId() ); |
|
291 } |
|
292 User::LeaveIfError(idIndex); |
|
293 // fetch the list of any matching ids |
|
294 CContactIdArray* items = CContactIdArray::NewLC(); |
|
295 TInt err(KErrNone); |
|
296 while ((err = stmnt.Next() ) == KSqlAtRow) |
|
297 { |
|
298 items->AddL(stmnt.ColumnInt(idIndex) ); |
|
299 } |
|
300 |
|
301 // leave if we didn't complete going through the results properly |
|
302 if(err != KSqlAtEnd) |
|
303 { |
|
304 User::Leave(err); |
|
305 } |
|
306 |
|
307 CleanupStack::Pop(items); |
|
308 CleanupStack::PopAndDestroy(&stmnt); |
|
309 return items; |
|
310 } |
|
311 |
|
312 |
|
313 /** |
|
314 Persist the items belonging to curent group into group table |
|
315 |
|
316 @param aGroup referece to a contact group |
|
317 */ |
|
318 void CPplGroupsTable::WriteGroupMembersL(const CContactItem& aGroup) |
|
319 { |
|
320 if (aGroup.Type() != KUidContactGroup) |
|
321 { |
|
322 return; |
|
323 } |
|
324 |
|
325 const TContactItemId KGroupId(aGroup.Id() ); |
|
326 |
|
327 // make sure we clear out any previous, out-of-date data |
|
328 TBool lowDiskErr(EFalse); |
|
329 DeleteItemL(KGroupId, lowDiskErr); |
|
330 if (lowDiskErr) |
|
331 { |
|
332 User::Leave(KErrDiskFull); |
|
333 } |
|
334 |
|
335 // build the RSqlStatement |
|
336 RSqlStatement stmnt; |
|
337 CleanupClosePushL(stmnt); |
|
338 stmnt.PrepareL(iDatabase, iInsertStmnt->SqlStringL() ); |
|
339 const TInt KGroupIdIndex(KFirstIndex); // first parameter in query... |
|
340 const TInt KMemberIdIndex(KGroupIdIndex + 1); // ...and the second parameter |
|
341 |
|
342 // copy and sort the member id array so we can see if there are duplicates |
|
343 const CContactIdArray* contactIdArray = static_cast<const CContactGroup&>(aGroup).ItemsContained(); //does not take the ownership |
|
344 |
|
345 const TInt arrayCount = contactIdArray->Count(); |
|
346 CArrayFixFlat<TContactItemId>* sortedList = new(ELeave) CArrayFixFlat<TContactItemId>(KArrayGranularity); |
|
347 CleanupStack::PushL(sortedList); |
|
348 for(TInt loop = 0;loop < arrayCount; ++loop) |
|
349 { |
|
350 sortedList->AppendL((*contactIdArray)[loop]); |
|
351 } |
|
352 TKeyArrayFix key(0,ECmpTInt); |
|
353 sortedList->Sort(key); |
|
354 |
|
355 // insert the group-member relationships |
|
356 const TInt KCountStmntParamIndex(KFirstIndex); // first and only parameter in query |
|
357 const TInt listLen(sortedList->Count() ); |
|
358 TInt lastId(0); |
|
359 for (TInt i = 0; i < listLen; ++i) |
|
360 { |
|
361 TInt itemId((*sortedList)[i]); |
|
362 |
|
363 //check if a contact item with itemId id really exists in contact database |
|
364 RSqlStatement countStmnt; |
|
365 CleanupClosePushL(countStmnt); |
|
366 countStmnt.PrepareL(iDatabase, iCountContactsStmnt->SqlStringL() ); |
|
367 User::LeaveIfError(countStmnt.BindInt(KCountStmntParamIndex, itemId) ); |
|
368 TInt count = 0; |
|
369 TInt err = KErrNone; |
|
370 if((err = countStmnt.Next() ) == KSqlAtRow) |
|
371 { |
|
372 count = countStmnt.ColumnInt(iCountContactsStmnt->ParameterIndex(KSqlCount) ); |
|
373 } |
|
374 else |
|
375 { |
|
376 User::LeaveIfError(err); |
|
377 } |
|
378 |
|
379 if(count == 0) |
|
380 { |
|
381 User::Leave(KErrNotFound); |
|
382 } |
|
383 CleanupStack::PopAndDestroy(&countStmnt); |
|
384 |
|
385 // only insert this if we haven't already seen it |
|
386 if (itemId != lastId || i == 0) |
|
387 { |
|
388 User::LeaveIfError(stmnt.BindInt(KGroupIdIndex, KGroupId) ); |
|
389 User::LeaveIfError(stmnt.BindInt(KMemberIdIndex, itemId) ); |
|
390 User::LeaveIfError(stmnt.Exec() ); |
|
391 User::LeaveIfError(stmnt.Reset() ); |
|
392 } |
|
393 lastId = itemId; |
|
394 } |
|
395 |
|
396 CleanupStack::PopAndDestroy(2, &stmnt); // and sortedList |
|
397 } |
|
398 |
|
399 |
|
400 /** |
|
401 Deletes information about group for the passed contact item id |
|
402 |
|
403 @param aItemId contact item id |
|
404 @param aLowDiskErrorOccurred out parameter; will be set to ETrue if there was a deletion in |
|
405 low disk condition |
|
406 */ |
|
407 void CPplGroupsTable::DeleteItemL(TContactItemId aItemId, TBool& aLowDiskErrorOccurred) |
|
408 { |
|
409 RSqlStatement stmnt; |
|
410 CleanupClosePushL(stmnt); |
|
411 stmnt.PrepareL(iDatabase, iDeleteStmnt->SqlStringL() ); |
|
412 |
|
413 const TInt KGroupIdIndex(KFirstIndex); // first parameter in query... |
|
414 const TInt KMemberIdIndex(KGroupIdIndex + 1); // ...and the second parameter |
|
415 User::LeaveIfError(stmnt.BindInt(KGroupIdIndex, aItemId) ); |
|
416 User::LeaveIfError(stmnt.BindInt(KMemberIdIndex, aItemId) ); |
|
417 TInt err = stmnt.Exec(); |
|
418 CleanupStack::PopAndDestroy(&stmnt); |
|
419 |
|
420 if (err == KErrDiskFull) |
|
421 { |
|
422 aLowDiskErrorOccurred = ETrue; |
|
423 } |
|
424 else |
|
425 { |
|
426 User::LeaveIfError(err); |
|
427 } |
|
428 } |