|
1 /* |
|
2 * Copyright (c) 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 #include "pltables.h" |
|
19 #include "dbsqlconstants.h" |
|
20 #include "cntitem.h" |
|
21 #include "cpcskeymap.h" |
|
22 |
|
23 |
|
24 /** |
|
25 @param aDatabase A handle to the database. |
|
26 @param aProperties A contact properties object. |
|
27 |
|
28 @return A pointer to a new CPplPredictiveSearchTable object. |
|
29 */ |
|
30 CPplPredictiveSearchTable* |
|
31 CPplPredictiveSearchTable::NewL(RSqlDatabase& aDatabase) |
|
32 { |
|
33 RDebug::Print(_L("CPplPredictiveSearchTable::NewL")); |
|
34 CPplPredictiveSearchTable* self = CPplPredictiveSearchTable::NewLC(aDatabase); |
|
35 CleanupStack::Pop(self); |
|
36 RDebug::Print(_L("CPplPredictiveSearchTable::NewL ends")); |
|
37 return self; |
|
38 } |
|
39 |
|
40 |
|
41 /** |
|
42 @param aDatabase A handle to the database. |
|
43 @param aProperties A contact properties object. |
|
44 |
|
45 @return A pointer to a new CPplPredictiveSearchTable object. |
|
46 */ |
|
47 CPplPredictiveSearchTable* |
|
48 CPplPredictiveSearchTable::NewLC(RSqlDatabase& aDatabase) |
|
49 { |
|
50 RDebug::Print(_L("CPplPredictiveSearchTable::NewLC")); |
|
51 CPplPredictiveSearchTable* self = |
|
52 new (ELeave) CPplPredictiveSearchTable(aDatabase); |
|
53 CleanupStack::PushL(self); |
|
54 self->ConstructL(); |
|
55 RDebug::Print(_L("CPplPredictiveSearchTable::NewLC ends")); |
|
56 return self; |
|
57 } |
|
58 |
|
59 /** |
|
60 Destructor |
|
61 */ |
|
62 CPplPredictiveSearchTable::~CPplPredictiveSearchTable() |
|
63 { |
|
64 RDebug::Print(_L("CPplPredictiveSearchTable dtor")); |
|
65 delete iInsertStmnt; |
|
66 delete iUpdateStmnt; |
|
67 delete iDeleteStmnt; |
|
68 delete iKeyMap; |
|
69 RDebug::Print(_L("CPplPredictiveSearchTable dtor ends")); |
|
70 } |
|
71 |
|
72 |
|
73 /** |
|
74 @param aItem A contact item whose data are added to the table. |
|
75 */ |
|
76 void CPplPredictiveSearchTable::CreateInDbL( CContactItem& aItem ) |
|
77 { |
|
78 RDebug::Print(_L("CPplPredictiveSearchTable::CreateInDbL")); |
|
79 WriteToDbL( aItem, iInsertStmnt ); |
|
80 RDebug::Print(_L("CPplPredictiveSearchTable::CreateInDbL ends")); |
|
81 } |
|
82 |
|
83 |
|
84 /** |
|
85 @param aItem A contact item whose data is updated in the database. |
|
86 */ |
|
87 void CPplPredictiveSearchTable::UpdateL( const CContactItem& aItem ) |
|
88 { |
|
89 RDebug::Print(_L("CPplPredictiveSearchTable::UpdateL")); |
|
90 WriteToDbL( aItem, iUpdateStmnt ); |
|
91 RDebug::Print(_L("CPplPredictiveSearchTable::UpdateL ends")); |
|
92 } |
|
93 |
|
94 |
|
95 /** |
|
96 Deletes all the communication addresses for a particular contact item. Should be used when |
|
97 deleting a contact item from the database altogether. |
|
98 |
|
99 @param aItem The contact item whose communcation addresses are to be deleted. |
|
100 */ |
|
101 void CPplPredictiveSearchTable::DeleteL(const CContactItem& aItem, TBool& aLowDiskErrorOccurred) |
|
102 { |
|
103 RDebug::Print(_L("CPplPredictiveSearchTable::DeleteL")); |
|
104 RSqlStatement stmnt; |
|
105 CleanupClosePushL(stmnt); |
|
106 stmnt.PrepareL(iDatabase, iDeleteStmnt->SqlStringL()); |
|
107 |
|
108 // Contact id was not added with iDeleteStmnt->SetParamL() so it can not be |
|
109 // accessed with iDeleteStmnt->ParameterIndex(). |
|
110 // It is the first and only parameter in query |
|
111 const TInt KContactIdParamIndex(KFirstIndex); |
|
112 User::LeaveIfError(stmnt.BindInt(KContactIdParamIndex, aItem.Id())); |
|
113 RDebug::Print(_L("CPplPredictiveSearchTable::DeleteL execute SQL statement")); |
|
114 // Returns the amount of rows that were changed/inserted/deleted |
|
115 TInt status = stmnt.Exec(); |
|
116 RDebug::Print(_L("CPplPredictiveSearchTable::DeleteL rows deleted=%d"), status); |
|
117 CleanupStack::PopAndDestroy(&stmnt); |
|
118 |
|
119 if (status == KErrDiskFull) |
|
120 { |
|
121 RDebug::Print(_L("CPplPredictiveSearchTable::DeleteL disk full")); |
|
122 aLowDiskErrorOccurred = ETrue; |
|
123 } |
|
124 else |
|
125 { |
|
126 RDebug::Print(_L("CPplPredictiveSearchTable::DeleteL status=%d"), status); |
|
127 User::LeaveIfError(status); |
|
128 } |
|
129 RDebug::Print(_L("CPplPredictiveSearchTable::DeleteL")); |
|
130 } |
|
131 |
|
132 |
|
133 /** |
|
134 Creates the comm_addr table and its indexes in the database. |
|
135 */ |
|
136 void CPplPredictiveSearchTable::CreateTableL() |
|
137 { |
|
138 RDebug::Print(_L("CPplPredictiveSearchTable::CreateTableL")); |
|
139 |
|
140 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateStmnt)); |
|
141 |
|
142 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateIndex)); |
|
143 |
|
144 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateView1)); |
|
145 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateView2)); |
|
146 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateView3)); |
|
147 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateView4)); |
|
148 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateView5)); |
|
149 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateView6)); |
|
150 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateView7)); |
|
151 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateView8)); |
|
152 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateView9)); |
|
153 User::LeaveIfError(iDatabase.Exec(KPredSearchCreateView0)); |
|
154 |
|
155 RDebug::Print(_L("CPplPredictiveSearchTable::CreateTableL ends")); |
|
156 } |
|
157 |
|
158 |
|
159 /** |
|
160 Set up the CCntSqlStatement objects held by the class. |
|
161 */ |
|
162 void CPplPredictiveSearchTable::ConstructL() |
|
163 { |
|
164 RDebug::Print(_L("CPplPredictiveSearchTable::ConstructL")); |
|
165 // Statement types |
|
166 TCntSqlStatementType insertType( EInsert, KSqlContactPredSearchTableName() ); |
|
167 TCntSqlStatementType updateType( EUpdate, KSqlContactPredSearchTableName() ); |
|
168 TCntSqlStatementType deleteType( EDelete, KSqlContactPredSearchTableName() ); |
|
169 |
|
170 |
|
171 // Insert new record |
|
172 // INSERT INTO predictivesearch |
|
173 // (first_name_as_number, last_name_as_number, company_name_as_number, contact_id) |
|
174 // VALUES (first_name_as_number value, last_name_as_number value, |
|
175 // company_name_as_number value, contact_id value); |
|
176 // |
|
177 iInsertStmnt = TSqlProvider::GetSqlStatementL(insertType); |
|
178 iInsertStmnt->SetParamL( KPredSearchFirstNameAsNumber, |
|
179 KPredSearchFirstNameAsNumberParam ); |
|
180 iInsertStmnt->SetParamL( KPredSearchLastNameAsNumber, |
|
181 KPredSearchLastNameAsNumberParam ); |
|
182 iInsertStmnt->SetParamL( KPredSearchCompanyNameAsNumber, |
|
183 KPredSearchCompanyNameAsNumberParam ); |
|
184 iInsertStmnt->SetParamL( KPredSearchContactId, |
|
185 KPredSearchContactIdParam ); |
|
186 |
|
187 |
|
188 // Update predictivesearch record (done when contact info or key map changes) |
|
189 // For a statement in the following format: |
|
190 // UPDATE predictivesearch SET |
|
191 // first_name_as_number = [first_name_as_number value], |
|
192 // last_name_as_number = [last_name_as_number value], |
|
193 // company_name_as_number = [company_name_as_number value] |
|
194 // WHERE contact_id = [contact_id value]; |
|
195 // |
|
196 iUpdateStmnt = TSqlProvider::GetSqlStatementL(updateType); |
|
197 iUpdateStmnt->SetParamL( KPredSearchFirstNameAsNumber, |
|
198 KPredSearchFirstNameAsNumberParam ); |
|
199 iUpdateStmnt->SetParamL( KPredSearchLastNameAsNumber, |
|
200 KPredSearchLastNameAsNumberParam ); |
|
201 iUpdateStmnt->SetParamL( KPredSearchCompanyNameAsNumber, |
|
202 KPredSearchCompanyNameAsNumberParam ); |
|
203 iUpdateStmnt->SetParamL( KPredSearchContactId, |
|
204 KPredSearchContactIdParam ); |
|
205 |
|
206 const TInt KWhereContactIdBufSize( |
|
207 KWhereStringEqualsStringFormatText().Size() + |
|
208 KPredSearchContactId().Size() + |
|
209 KPredSearchContactIdParam().Size() ); |
|
210 HBufC* whereContactIdClause = HBufC::NewLC(KWhereContactIdBufSize); |
|
211 // for WHERE contact_id = [contact id value] |
|
212 whereContactIdClause->Des().AppendFormat(KWhereStringEqualsStringFormatText, |
|
213 &KPredSearchContactId, &KPredSearchContactIdParam); |
|
214 iUpdateStmnt->SetConditionL(*whereContactIdClause); |
|
215 |
|
216 |
|
217 // Delete information of a particular contact item |
|
218 // DELETE FROM predictivesearch WHERE contact_id = [contact id value]; |
|
219 // |
|
220 iDeleteStmnt = TSqlProvider::GetSqlStatementL(deleteType); |
|
221 iDeleteStmnt->SetConditionL(*whereContactIdClause); |
|
222 |
|
223 CleanupStack::PopAndDestroy(whereContactIdClause); |
|
224 |
|
225 RDebug::Print(_L("CPplPredictiveSearchTable::ConstructL create key map")); |
|
226 iKeyMap = CPcsKeyMap::NewL(); |
|
227 |
|
228 RDebug::Print(_L("CPplPredictiveSearchTable::ConstructL ends")); |
|
229 } |
|
230 |
|
231 |
|
232 /** |
|
233 Constructor |
|
234 */ |
|
235 CPplPredictiveSearchTable::CPplPredictiveSearchTable(RSqlDatabase& aDatabase) : |
|
236 iDatabase(aDatabase) |
|
237 { |
|
238 } |
|
239 |
|
240 |
|
241 // This function does the common parts of both insert and update operations |
|
242 void CPplPredictiveSearchTable::WriteToDbL( const CContactItem& aItem, |
|
243 CCntSqlStatement* aStatement ) |
|
244 { |
|
245 RDebug::Print(_L("CPplPredictiveSearchTable::WriteToDbL")); |
|
246 RSqlStatement stmnt; |
|
247 CleanupClosePushL( stmnt ); |
|
248 stmnt.PrepareL( iDatabase, aStatement->SqlStringL() ); |
|
249 |
|
250 // Fill parameters and their values |
|
251 CContactItemFieldSet& fieldset = aItem.CardFields(); |
|
252 |
|
253 TInt pos = fieldset.Find( KUidContactFieldGivenName ); |
|
254 if ( pos != KErrNotFound ) |
|
255 { |
|
256 RDebug::Print(_L("CPplPredictiveSearchTable::WriteToDbL first name")); |
|
257 CContactTextField* textfield = fieldset[ pos ].TextStorage(); |
|
258 if ( textfield ) |
|
259 { |
|
260 TPtrC firstName = textfield->Text(); |
|
261 HBufC* firstNameAsNbr = ConvertToNumericRepresentationLC( firstName ); |
|
262 User::LeaveIfError( stmnt.BindText( |
|
263 User::LeaveIfError( stmnt.ParameterIndex( KPredSearchFirstNameAsNumberParam ) ), |
|
264 *firstNameAsNbr ) ); |
|
265 CleanupStack::PopAndDestroy( firstNameAsNbr ); |
|
266 } |
|
267 } |
|
268 |
|
269 pos = fieldset.Find( KUidContactFieldFamilyName ); |
|
270 if ( pos != KErrNotFound ) |
|
271 { |
|
272 RDebug::Print(_L("CPplPredictiveSearchTable::WriteToDbL last name")); |
|
273 CContactTextField* textfield = fieldset[ pos ].TextStorage(); |
|
274 if ( textfield ) |
|
275 { |
|
276 TPtrC lastName = textfield->Text(); |
|
277 HBufC* lastNameAsNbr = ConvertToNumericRepresentationLC( lastName ); |
|
278 User::LeaveIfError( stmnt.BindText( |
|
279 User::LeaveIfError( stmnt.ParameterIndex( KPredSearchLastNameAsNumberParam ) ), |
|
280 *lastNameAsNbr ) ); |
|
281 CleanupStack::PopAndDestroy( lastNameAsNbr ); |
|
282 } |
|
283 } |
|
284 |
|
285 pos = fieldset.Find( KUidContactFieldCompanyName ); |
|
286 if ( pos != KErrNotFound ) |
|
287 { |
|
288 RDebug::Print(_L("CPplPredictiveSearchTable::WriteToDbL company name")); |
|
289 CContactTextField* textfield = fieldset[ pos ].TextStorage(); |
|
290 if ( textfield ) |
|
291 { |
|
292 TPtrC companyName = textfield->Text(); |
|
293 HBufC* companyNameAsNbr = ConvertToNumericRepresentationLC( companyName ); |
|
294 User::LeaveIfError( stmnt.BindText( |
|
295 User::LeaveIfError( stmnt.ParameterIndex( KPredSearchCompanyNameAsNumberParam ) ), |
|
296 *companyNameAsNbr ) ); |
|
297 CleanupStack::PopAndDestroy( companyNameAsNbr ); |
|
298 } |
|
299 } |
|
300 |
|
301 RDebug::Print(_L("CPplPredictiveSearchTable::WriteToDbL set contact id")); |
|
302 // When inserting, contact id is written. |
|
303 // When updating, contact id is the search key. |
|
304 User::LeaveIfError( stmnt.BindInt( |
|
305 User::LeaveIfError( stmnt.ParameterIndex( KPredSearchContactIdParam ) ), |
|
306 aItem.Id() ) ); |
|
307 |
|
308 RDebug::Print(_L("CPplPredictiveSearchTable::WriteToDbL execute SQL statement")); |
|
309 // Execute the SQL statement |
|
310 User::LeaveIfError( stmnt.Exec() ); |
|
311 CleanupStack::PopAndDestroy( &stmnt ); |
|
312 |
|
313 RDebug::Print(_L("CPplPredictiveSearchTable::WriteToDbL ends")); |
|
314 } |
|
315 |
|
316 HBufC* CPplPredictiveSearchTable::ConvertToNumericRepresentationLC( const TDesC& aString ) |
|
317 { |
|
318 RDebug::Print(_L("CPplPredictiveSearchTable::ConvertToNumericRepresentationLC")); |
|
319 HBufC* numericBuf = iKeyMap->GetNumericKeyStringL( aString, EFalse ); |
|
320 CleanupStack::PushL( numericBuf ); |
|
321 RDebug::Print(_L("CPplPredictiveSearchTable::ConvertToNumericRepresentationLC result='%S'"), |
|
322 numericBuf); |
|
323 return numericBuf; |
|
324 } |