66 //! \fn getError() |
63 //! \fn getError() |
67 /*! |
64 /*! |
68 \brief Get the last error message from the DataStoreManager object. |
65 \brief Get the last error message from the DataStoreManager object. |
69 \return The last error message string of the DSM object. |
66 \return The last error message string of the DSM object. |
70 */ |
67 */ |
71 QString DataStoreManager::getError() const{ |
68 QString DataStoreManager::getErrorText() const{ |
72 return m_last_msg; |
69 return lastMsg; |
73 } |
70 } |
74 |
71 |
75 /* Refactor this. Think Signals. */ |
72 SmfRelationId DataStoreManager::create(SmfProvider *aProvider, SmfContact *aContact){ |
76 QList <SMFSocialProfile> DataStoreManager::getAllRelated(const SMFUserProfile& user_profile) { |
73 |
77 QList <SMFSocialProfile> related_profiles; |
74 QString userId, contactUrl, localId, managerUri, presenceState, presenceText, statusText; |
78 int usr_id = user_profile.userID(); |
75 QDateTime dateTime; |
79 |
76 Int64 timeStampInSeconds; |
80 if (!(db.isOpen())){ |
77 |
81 if(!(db.open())){ |
78 QContactGuid guid = aContact->value("Guid").value<QContactGuid>(); |
82 state = ERROR; |
79 userId = guid.guid(); |
83 m_last_msg = db.lastError().text(); |
80 |
84 return related_profiles; |
81 QContactUrl url = aContact->value("Url").value<QContactUrl>(); |
85 /* Do something to signal an error. Just returning an empty list is NOT ok */ |
82 contactUrl = url.url(); |
86 } |
83 |
87 } |
84 QContactId contactId = aContact->value("ContactId").value<QContactId>(); |
88 |
85 localId = contactId.localId() ; |
89 QSqlQuery qry; |
86 managerUri = contactId.managerUri(); |
90 qry.prepare("SELECT social_profiles.social_profile_id , social_profiles.user_id , social_profiles.sns_id , " |
87 |
91 " social_profiles.profile_url , social_profiles.screen_alias" |
88 QContactTimestamp time = aContact->value("Timestamp").value<QContactTimestamp>(); |
92 " FROM social_profiles JOIN user_profiles WHERE user_profiles.user_id = social_profiles.user_id" |
89 dateTime = time.created(); |
93 " AND social_profiles.user_id = :user_id"); |
90 //Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). |
94 qry.bindValue(":user_id", usr_id); |
91 timeStampInSeconds = dateTime.toTime_t() ; |
95 |
92 |
96 if(!(qry.exec())){ |
93 QContactPresence presence = aContact->value("Presence").value<QContactPresence>(); |
97 m_last_msg = qry.lastError().text(); |
94 presenceState = QString::number( (int)presence.presenceState() ); |
98 qry.finish(); |
95 presenceText = presence.presenceStateText(); |
99 /* Do something to signal an error. Just returning an empty list is NOT ok */ |
96 statusText = presence.customMessage(); |
100 return related_profiles; |
97 |
101 } |
98 QString snsName = aProvider->serviceName(); |
102 |
99 QString snsUrl = (aProvider->serviceUrl()).toString(); |
103 while(qry.next()) |
100 QString snsDesc = aProvider->description(); |
104 { |
101 |
105 SMFSocialProfile _profile(qry.value(2).toInt()/* sns ID*/, |
102 |
106 qry.value(1).toInt() /* user ID */, |
103 const int contactID = addContactToTable( userId, contactUrl, localId, managerUri, snsName, snsDesc, snsUrl, presenceState, presenceText , statusText, timeStampInSeconds); |
107 qry.value(3).toString()/* url */, |
104 return QString::number( addRelationToTable( contactID ) ); |
108 qry.value(4).toString()/* alias */); |
105 } |
109 related_profiles << _profile; |
106 |
110 |
107 SmfError DataStoreManager::associate( SmfRelationId aRelation, |
111 } |
108 const SmfContact* aContact, |
112 /* |
109 SmfProvider* aProvider){ |
113 for( int i = 0; i < related_profiles.size(); i++){ |
110 |
114 qDebug() << related_profiles.at(i).associatedSnsID() << ", " << related_profiles.at(i).associatedUserID() |
111 if( ! relationIfExist( aRelation.toInt()) ) |
115 << related_profiles.at(i).profileURL() << related_profiles.at(i).screenAlias() ; |
112 return SmfErrInvalidRelation; |
116 } |
113 |
117 */ |
114 QString userId, contactUrl, localId, managerUri, presenceState, presenceText, statusText; |
118 qry.finish(); |
115 QDateTime dateTime; |
119 db.close(); |
116 Int64 timeStampInSeconds; |
120 |
117 |
121 return related_profiles; |
118 QContactGuid guid = aContact->value("Guid").value<QContactGuid>(); |
122 |
119 userId = guid.guid(); |
123 } |
120 |
124 |
121 QContactUrl url = aContact->value("Url").value<QContactUrl>(); |
125 |
122 contactUrl = url.url(); |
126 SMFSocialProfile DataStoreManager::getRelatedByService(const SMFUserProfile& user_profile, const SMFSocialNetworkingSite& sns){ |
123 |
127 |
124 QContactId contactId = aContact->value("ContactId").value<QContactId>(); |
128 SMFSocialProfile _profile; |
125 localId = contactId.localId() ; |
129 int usr_id = user_profile.userID(); |
126 managerUri = contactId.managerUri(); |
130 int sns_id = sns.snsID(); |
127 |
131 |
128 QContactTimestamp time = aContact->value("Timestamp").value<QContactTimestamp>(); |
132 if (!(db.isOpen())){ |
129 dateTime = time.created(); |
133 if(!(db.open())){ |
130 //Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). |
134 state = ERROR; |
131 timeStampInSeconds = dateTime.toTime_t() ; |
135 m_last_msg = db.lastError().text(); |
132 |
136 return _profile; |
133 QContactPresence presence = aContact->value("Presence").value<QContactPresence>(); |
137 /* Do something to signal an error. Just returning an empty list is NOT ok */ |
134 presenceState = presence.presenceState(); |
138 } |
135 presenceText = presence.presenceStateText(); |
139 } |
136 statusText = presence.customMessage(); |
140 |
137 |
141 QSqlQuery qry; |
138 QString snsName = aProvider->serviceName(); |
142 qry.prepare("SELECT social_profiles.social_profile_id , social_profiles.user_id , social_profiles.sns_id , " |
139 QString snsUrl = (aProvider->serviceUrl()).toString(); |
143 " social_profiles.profile_url , social_profiles.screen_alias" |
140 QString snsDesc = aProvider->description(); |
144 " FROM social_profiles JOIN user_profiles WHERE user_profiles.user_id = social_profiles.user_id" |
141 |
145 " AND social_profiles.user_id = :user_id AND social_profiles.sns_id = :sns_id"); |
142 const int contactID = addContactToTable( userId, contactUrl, localId, managerUri, snsName, snsDesc, snsUrl, presenceState, presenceText , statusText, timeStampInSeconds); |
146 qry.bindValue(":user_id", usr_id); |
143 if( socialProfileBaseID <= addRelationToTable( contactID, aRelation.toInt() ) ) |
147 qry.bindValue(":sns_id", sns_id); |
144 return SmfNoError ; |
148 |
145 else |
149 if(!(qry.exec())){ |
146 return SmfDbOpeningError; |
150 m_last_msg = qry.lastError().text(); |
147 } |
151 qry.finish(); |
148 |
152 /* Do something to signal an error. Just returning an empty list is NOT ok */ |
149 /** remove contact from a relation */ |
153 return _profile; |
150 SmfError DataStoreManager::remove(SmfRelationId aRelation, const SmfContact* aContact){ |
154 } |
151 |
155 |
152 int contactId, relationId; |
156 if(qry.next()) { |
153 QString userId, contactUrl, localId, managerUri; |
157 _profile.setAssociatedSnsID(qry.value(2).toInt()); |
154 |
158 _profile.setAssociatedUserID(qry.value(1).toInt()); |
155 relationId = aRelation.toInt(); |
159 _profile.setProfileURL(qry.value(3).toString()); |
156 |
160 _profile.setScreenAlias(qry.value(4).toString()); |
157 if(SmfDbOpeningError == openDB()) |
161 } |
158 return SmfDbOpeningError; |
162 /* |
159 |
163 qDebug() << _profile.associatedSnsID() << ", " << _profile.associatedUserID() << ", " |
160 QSqlQuery qry; |
164 << _profile.profileURL() << ", "<< _profile.screenAlias() ; |
161 |
165 */ |
162 QContactGuid guid = aContact->value("Guid").value<QContactGuid>(); |
166 qry.finish(); |
163 userId = guid.guid(); |
167 db.close(); |
164 |
168 |
165 if ( ! userId.isNull() ) |
169 return _profile; |
166 { |
170 } |
167 QContactUrl url = aContact->value("Url").value<QContactUrl>(); |
171 |
168 contactUrl = url.url(); |
172 /* Cannot return something like this. Make this async */ |
169 |
173 SMFUserProfile DataStoreManager::getUserProfile(const QString& name, const QString& contact_id){ |
170 qry.prepare("SELECT contactId FROM contact where userId=:userId AND contactUrl=:contactUrl"); |
174 SMFUserProfile _profile; |
171 qry.bindValue(":userId", userId); |
175 |
172 qry.bindValue(":contactUrl", contactUrl); |
176 if (!(db.isOpen())){ |
173 } |
177 if(!(db.open())){ |
174 else |
178 state = ERROR; |
175 { |
179 m_last_msg = db.lastError().text(); |
176 QContactId contactId = aContact->value("ContactId").value<QContactId>(); |
180 return _profile; |
177 localId = contactId.localId() ; |
181 /* Do something to signal an error. Just returning an empty list is NOT ok */ |
178 managerUri = contactId.managerUri(); |
182 } |
179 |
183 } |
180 qry.prepare("SELECT contactId FROM contact where localId=:localId AND managerUri=:managerUri"); |
184 |
181 qry.bindValue(":localId", localId); |
185 QSqlQuery qry; |
182 qry.bindValue(":managerUri", managerUri); |
186 qry.prepare("SELECT user_id , name , contact_id FROM user_profiles WHERE name=:name AND contact_id=:contact_id"); |
183 } |
187 qry.bindValue(":name", name); |
184 |
188 qry.bindValue(":contact_id", contact_id); |
185 if(SmfDbQueryExecutonError == executeQuery(qry)) |
189 |
186 return SmfDbQueryExecutonError; |
190 if(!(qry.exec())){ |
187 |
191 m_last_msg = qry.lastError().text(); |
188 if(qry.next()) |
192 qry.finish(); |
189 contactId = qry.value(0).toInt(); |
193 /* Do something to signal an error. Just returning an empty list is NOT ok */ |
190 |
194 return _profile; |
191 deleteContactFromTable(relationId, contactId); |
195 } |
192 |
196 |
193 qry.finish(); |
197 if(qry.next()){ |
194 db.close(); |
198 _profile.setName(qry.value(1).toString()); |
195 return SmfNoError; |
199 _profile.setContactID(qry.value(2).toString()); |
196 |
200 _profile.setUserID(qry.value(0).toInt()); |
197 } |
201 } |
198 |
202 |
199 /** returns first relation item in the relation when exists, NULL otherwise */ |
203 qry.finish(); |
200 SmfRelationItem* DataStoreManager::searchById(const SmfRelationId aRelation){ |
204 db.close(); |
201 |
205 |
202 const int relationId = aRelation.toInt(); |
206 return _profile; |
203 int contactId, contactIndex; |
207 } |
204 |
208 |
205 if(SmfDbOpeningError == openDB()) |
209 /*! Fetches the Social Networking site identified by \a name */ |
206 return NULL; |
210 SMFSocialNetworkingSite DataStoreManager::getSNSEntry(const QString& name){ |
207 |
211 SMFSocialNetworkingSite sns; |
208 QSqlQuery qry; |
212 |
209 qry.prepare("SELECT contactId, contactIndex FROM relation where relationId=:relationId LIMIT 1"); |
213 if (!(db.isOpen())){ |
210 qry.bindValue(":relationId", relationId); |
214 if(!(db.open())){ |
211 |
215 state = ERROR; |
212 if(SmfDbQueryExecutonError == executeQuery(qry)) |
216 m_last_msg = db.lastError().text(); |
213 return NULL; |
217 return sns; |
214 |
218 /* Do something to signal an error. Just returning an empty list is NOT ok */ |
215 if(qry.next()){ |
219 } |
216 contactId = qry.value(0).toInt(); |
220 } |
217 contactIndex = qry.value(1).toInt(); |
221 |
218 } |
222 QSqlQuery qry; |
219 |
223 qry.prepare("SELECT sns_id, sns_name, sns_url FROM sns_base WHERE sns_name=:name"); |
220 qry.prepare("SELECT * FROM contact where contactId=:contactId"); |
224 qry.bindValue(":name", name); |
221 qry.bindValue(":contactId", contactId); |
225 |
222 |
226 if(!qry.exec()){ |
223 if(SmfDbQueryExecutonError == executeQuery(qry)) |
227 m_last_msg = qry.lastError().text(); |
224 return NULL; |
228 qry.finish(); |
225 |
229 return sns; |
226 if(qry.next()) |
230 } |
227 createRelationItem(qry, contactIndex); |
231 |
228 |
232 if(qry.next()){ |
229 qry.finish(); |
233 sns.setSnsID(qry.value(0).toInt()); |
230 db.close(); |
234 sns.setSnsName(qry.value(1).toString()); |
231 return iSmsfRelationItem; |
235 sns.setSnsURL(qry.value(2).toString()); |
232 |
236 } |
233 } |
237 |
234 |
238 qry.finish(); |
235 SmfRelationItem* DataStoreManager::getContact(SmfRelationId aRelation, quint32 aIndex){ |
239 db.close(); |
236 |
240 |
237 const int relationId = aRelation.toInt(); |
241 return sns; |
238 int contactId; |
242 } |
239 |
243 |
240 if(SmfDbOpeningError == openDB()) |
244 /* Actually all these 'save' functions can be written as a template */ |
241 return NULL; |
245 void DataStoreManager::saveUserProfile(const SMFUserProfile& user_profile){ |
242 |
246 |
243 QSqlQuery qry; |
247 if (!(db.isOpen())){ |
244 qry.prepare("SELECT contactId FROM relation where relationId=:relationId AND contactIndex=:contactIndex"); |
248 if(!(db.open())){ |
245 qry.bindValue(":relationId", relationId); |
249 state = ERROR; |
246 qry.bindValue(":contactIndex", aIndex); |
250 m_last_msg = db.lastError().text(); |
247 |
251 } |
248 if(SmfDbQueryExecutonError == executeQuery(qry)) |
252 } |
249 return NULL; |
253 |
250 |
254 QSqlQuery qry; |
251 if(qry.next()){ |
255 qry.prepare("UPDATE user_profiles" |
252 contactId = qry.value(0).toInt(); |
256 " SET name=:name , contact_id=:contact_id" |
253 } |
257 " WHERE user_id=:user_id"); |
254 |
258 qry.bindValue(":name", user_profile.name()); |
255 qry.prepare("SELECT * FROM contact where contactId=:contactId"); |
259 qry.bindValue(":contact_id", user_profile.contactID()); |
256 qry.bindValue(":contactId", contactId); |
260 qry.bindValue(":user_id", user_profile.userID()); |
257 |
261 |
258 if(SmfDbQueryExecutonError == executeQuery(qry)) |
262 if(!(qry.exec())){ |
259 return NULL; |
263 m_last_msg = qry.lastError().text(); |
260 |
264 qry.finish(); |
261 if(qry.next()) |
265 } |
262 createRelationItem(qry, aIndex); |
266 |
263 |
267 qry.finish(); |
264 qry.finish(); |
268 db.close(); |
265 db.close(); |
269 } |
266 return iSmsfRelationItem; |
270 |
267 } |
271 void DataStoreManager::saveSocialProfile(const SMFSocialProfile& social_profile){ |
268 |
272 |
269 /** returns relation Id for a given contacts if exists, NULL otherwise */ |
273 if (!(db.isOpen())){ |
270 SmfRelationId DataStoreManager::searchByContact( SmfContact aContact){ |
274 if(!(db.open())){ |
271 |
275 state = ERROR; |
272 int contactId, relationId; |
276 m_last_msg = db.lastError().text(); |
273 QString userId, contactUrl, localId, managerUri; |
277 } |
274 |
278 } |
275 if(SmfDbOpeningError == openDB()) |
279 |
276 return QString(); |
280 QSqlQuery qry; |
277 |
281 qry.prepare("UPDATE social_profiles" |
278 QSqlQuery qry; |
282 " SET user_id=:user_id, sns_id=:sns_id, profile_url=:profile_url, screen_alias=:screen_alias" |
279 |
283 " WHERE social_profile_id=:social_profile_id"); |
280 QContactGuid guid = aContact.value("Guid").value<QContactGuid>(); |
284 qry.bindValue(":user_id", social_profile.associatedUserID()); |
281 userId = guid.guid(); |
285 qry.bindValue(":sns_id", social_profile.associatedSnsID()); |
282 |
286 qry.bindValue(":profile_url", social_profile.profileURL()); |
283 if ( ! userId.isNull() ) |
287 qry.bindValue(":screen_alias", social_profile.screenAlias()); |
284 { |
288 qry.bindValue(":social_profile_id", social_profile.profileID()); |
285 QContactUrl url = aContact.value("Url").value<QContactUrl>(); |
289 |
286 contactUrl = url.url(); |
290 if(!(qry.exec())){ |
287 |
291 m_last_msg = qry.lastError().text(); |
288 qry.prepare("SELECT contactId FROM contact where userId=:userId AND contactUrl=:contactUrl"); |
292 qry.finish(); |
289 qry.bindValue(":userId", userId); |
293 } |
290 qry.bindValue(":contactUrl", contactUrl); |
294 |
291 } |
295 qry.finish(); |
292 else |
296 db.close(); |
293 { |
297 } |
294 QContactId contactId = aContact.value("ContactId").value<QContactId>(); |
298 |
295 localId = contactId.localId() ; |
299 void DataStoreManager::saveSNSEntry(const SMFSocialNetworkingSite& sns){ |
296 managerUri = contactId.managerUri(); |
300 |
297 |
301 if (!(db.isOpen())){ |
298 qry.prepare("SELECT contactId FROM contact where localId=:localId AND managerUri=:managerUri"); |
302 if(!(db.open())){ |
299 qry.bindValue(":localId", localId); |
303 state = ERROR; |
300 qry.bindValue(":managerUri", managerUri); |
304 m_last_msg = db.lastError().text(); |
301 } |
305 } |
302 |
306 } |
303 if(SmfDbQueryExecutonError == executeQuery(qry)) |
307 |
304 return QString(); |
308 QSqlQuery qry; |
305 |
309 qry.prepare("UPDATE sns_base" |
306 if(qry.next()) |
310 " SET sns_name=:sns_name, sns_url=:sns_url" |
307 contactId = qry.value(0).toInt(); |
311 " WHERE sns_id=:sns_id"); |
308 |
312 qry.bindValue(":sns_name", sns.snsName()); |
309 qry.prepare("SELECT relationId FROM relation where contactId=:contactId"); |
313 qry.bindValue(":sns_url", sns.snsURL()); |
310 qry.bindValue(":contactId", contactId); |
314 qry.bindValue(":sns_id", sns.snsID()); |
311 |
315 |
312 if(SmfDbQueryExecutonError == executeQuery(qry)) |
316 if(!(qry.exec())){ |
313 return QString(); |
317 m_last_msg = qry.lastError().text(); |
314 |
318 qry.finish(); |
315 if(qry.next()){ |
319 } |
316 relationId = qry.value(0).toInt(); |
320 |
317 } |
321 qry.finish(); |
318 |
322 db.close(); |
319 qry.finish(); |
323 |
320 db.close(); |
324 } |
321 return QString::number( relationId ); |
325 |
322 |
326 void DataStoreManager::modifyRelation(SMFSocialProfile& sns, SMFUserProfile& new_user_profile){ |
323 } |
327 sns.setAssociatedUserID(new_user_profile.userID()); |
324 |
328 } |
325 /** contacts and their provider */ |
329 |
326 DSMContactPckg* DataStoreManager::get(SmfRelationId aRelation, quint32 aIndex){ |
330 |
327 |
331 int DataStoreManager::addUserProfile(SMFUserProfile& user_profile){ |
328 const int relationId = aRelation.toInt(); |
332 int user_id = user_profile.userID(); |
329 int contactId; |
333 |
330 |
334 /* Check Required to identify multiple Entries */ |
331 if(SmfDbOpeningError == openDB()) |
335 |
332 return NULL; |
336 if (!(db.isOpen())){ |
333 |
337 if(!(db.open())){ |
334 QSqlQuery qry; |
338 state = ERROR; |
335 qry.prepare("SELECT contactId FROM relation where relationId=:relationId AND contactIndex=:contactIndex"); |
339 m_last_msg = db.lastError().text(); |
336 qry.bindValue(":relationId", relationId); |
340 return user_id; |
337 qry.bindValue(":contactIndex", aIndex); |
341 } |
338 |
342 } |
339 if(SmfDbQueryExecutonError == executeQuery(qry)) |
343 |
340 return NULL; |
344 QSqlQuery qry; |
341 |
345 qry.prepare("INSERT INTO user_profiles (name, contact_id) VALUES (:name, :contact_id)"); |
342 if(qry.next()){ |
346 qry.bindValue(":name", user_profile.name()); |
343 contactId = qry.value(0).toInt(); |
347 qry.bindValue(":contact_id", user_profile.contactID()); |
344 } |
348 |
345 |
349 if(!(qry.exec())){ |
346 qry.prepare("SELECT * FROM contact where contactId=:contactId"); |
350 m_last_msg = qry.lastError().text(); |
347 qry.bindValue(":contactId", contactId); |
351 qry.finish(); |
348 |
352 return user_id; |
349 if(SmfDbQueryExecutonError == executeQuery(qry)) |
353 } |
350 return NULL; |
354 |
351 |
355 qry.prepare("SELECT user_id FROM user_profiles WHERE name=:name AND contact_id=:contact_id"); |
352 if(qry.next()){ |
356 qry.bindValue(":name", user_profile.name()); |
353 iDSMContactPckgItem = new DSMContactPckg; |
357 qry.bindValue(":contact_id", user_profile.contactID()); |
354 |
358 |
355 iDSMContactPckgItem->userId = qry.value(0).toString(); |
359 if(!(qry.exec())){ |
356 iDSMContactPckgItem->contactUrl = qry.value(1).toString(); |
360 m_last_msg = qry.lastError().text(); |
357 iDSMContactPckgItem->localId = qry.value(2).toString(); |
361 qry.finish(); |
358 iDSMContactPckgItem->managerUri = qry.value(3).toString(); |
362 return user_id; |
359 iDSMContactPckgItem->snsName = qry.value(4).toString(); |
363 } |
360 iDSMContactPckgItem->snsDesc = qry.value(5).toString(); |
364 |
361 iDSMContactPckgItem->snsUrl = qry.value(6).toString(); |
365 if(qry.next()){ |
362 iDSMContactPckgItem->presenceState = qry.value(7).toString(); |
366 user_id = qry.value(0).toInt(); |
363 iDSMContactPckgItem->presenceText = qry.value(8).toString(); |
367 user_profile.setUserID(user_id); |
364 iDSMContactPckgItem->statusText = qry.value(9).toString(); |
368 } |
365 iDSMContactPckgItem->timeStamp = qry.value(10).toLongLong(); |
369 |
366 iDSMContactPckgItem->contactIndex = relationId; |
370 qry.finish(); |
367 iDSMContactPckgItem->relationId = aIndex; |
371 db.close(); |
368 } |
372 |
369 |
373 return user_id; |
370 qry.finish(); |
374 |
371 db.close(); |
375 |
372 return iDSMContactPckgItem; |
376 } |
373 } |
377 |
374 |
378 int DataStoreManager::deleteUserProfile( SMFUserProfile& user_profile){ |
375 /** list of contacts and their provider */ |
379 |
376 QList<SmfRelationItem> DataStoreManager::getAll(SmfRelationId aRelation){ |
380 if (!(db.isOpen())){ |
377 QList<SmfRelationItem> relationItemList; |
381 if(!(db.open())){ |
378 const int relationId = aRelation.toInt(); |
382 state = ERROR; |
379 int contactId, contactIndex; |
383 m_last_msg = db.lastError().text(); |
380 |
384 return -1; |
381 if(SmfDbOpeningError == openDB()) |
385 } |
382 return relationItemList; |
386 } |
383 |
387 |
384 QSqlQuery qry; |
388 QSqlQuery qry; |
385 qry.prepare("SELECT contactId, contactIndex FROM relation where relationId=:relationId"); |
389 qry.prepare("DELETE FROM user_profiles WHERE user_id=:user_id"); |
386 qry.bindValue(":relationId", relationId); |
390 qry.bindValue(":user_id", user_profile.userID()); |
387 |
391 |
388 if(SmfDbQueryExecutonError == executeQuery(qry)) |
392 if(!(qry.exec())){ |
389 return relationItemList; |
393 m_last_msg = qry.lastError().text(); |
390 |
394 qry.finish(); |
391 while(qry.next()){ |
395 return -1; |
392 contactId = qry.value(0).toInt(); |
396 } |
393 contactIndex = qry.value(1).toInt(); |
397 |
394 |
398 qry.finish(); |
395 QSqlQuery innerqry; |
399 db.close(); |
396 innerqry.prepare("SELECT * FROM contact where contactId=:contactId"); |
400 |
397 innerqry.bindValue(":contactId", contactId); |
401 return 0; |
398 |
402 } |
399 if(SmfDbQueryExecutonError == executeQuery(innerqry)) |
403 |
400 return relationItemList; |
404 int DataStoreManager::addSocialProfile(SMFSocialProfile& social_profile){ |
401 |
405 int social_prof_id = social_profile.profileID(); |
402 if(innerqry.next()){ |
406 |
403 createRelationItem(innerqry, contactIndex); |
407 /* Check Required to identify multiple entries */ |
404 relationItemList << *iSmsfRelationItem; |
408 |
405 } |
409 if (!(db.isOpen())){ |
406 innerqry.finish(); |
410 if(!(db.open())){ |
407 }// end while |
411 state = ERROR; |
408 |
412 m_last_msg = db.lastError().text(); |
409 qry.finish(); |
413 return social_prof_id; |
410 db.close(); |
414 } |
411 return relationItemList; |
415 } |
412 } |
416 |
413 |
417 /* There might be a request to add a social profile with either user and/or sns profile(s) |
414 void DataStoreManager::createRelationItem( QSqlQuery & aQry, int aIndex){ |
418 not stored in the database. What to do in that case? Automatically store the other profiles too? |
415 |
419 */ |
416 SmfContact * smfContact; |
420 QSqlQuery qry; |
417 SmfProvider * smfProvider; |
421 qry.prepare("INSERT INTO social_profiles (user_id, sns_id, profile_url, screen_alias) " |
418 QString snsName, snsDesc; |
422 "VALUES (:user_id, :sns_id, :profile_url, :screen_alias)"); |
419 QUrl snsUrl; |
423 qry.bindValue(":user_id", social_profile.associatedUserID()); |
420 |
424 qry.bindValue(":sns_id", social_profile.associatedSnsID()); |
421 QVariant userId = QVariant::fromValue( aQry.value(0).toString() ); |
425 qry.bindValue(":profile_url", social_profile.profileURL()); |
422 QVariant contactUrl = QVariant::fromValue(aQry.value(1).toString() ); |
426 qry.bindValue(":screen_alias", social_profile.screenAlias()); |
423 |
427 |
424 QString StrLocalId = aQry.value(2).toString() ; |
428 if(!(qry.exec())){ |
425 QString StrManagerUri = aQry.value(3).toString() ; |
429 m_last_msg = qry.lastError().text(); |
426 QContactId qContactId; |
430 qry.finish(); |
427 qContactId.setLocalId( StrLocalId.toInt() ); |
431 return social_prof_id; |
428 qContactId.setManagerUri( StrManagerUri ); |
432 } |
429 QVariant contactId = QVariant::fromValue(qContactId); |
433 |
430 |
434 qry.prepare("SELECT social_profile_id FROM social_profiles " |
431 int presenceState = aQry.value(7).toInt() ; |
435 "WHERE user_id=:user_id AND sns_id=:sns_id AND profile_url=:profile_url AND screen_alias=:screen_alias"); |
432 QString presenceStateText = aQry.value(8).toString() ; |
436 qry.bindValue(":user_id", social_profile.associatedUserID()); |
433 QString statusText = aQry.value(9).toString(); |
437 qry.bindValue(":sns_id", social_profile.associatedSnsID()); |
434 QContactPresence qContactPresence; |
438 qry.bindValue(":profile_url", social_profile.profileURL()); |
435 qContactPresence.setPresenceState(static_cast<QContactPresence::PresenceState>(presenceState)); |
439 qry.bindValue(":screen_alias", social_profile.screenAlias()); |
436 qContactPresence.setPresenceStateText(presenceStateText); |
440 |
437 qContactPresence.setCustomMessage(statusText); |
441 if(!(qry.exec())){ |
438 QVariant contactPresence = QVariant::fromValue(qContactPresence); |
442 m_last_msg = qry.lastError().text(); |
439 |
443 qry.finish(); |
440 QDateTime dateTime; |
444 return social_prof_id; |
441 dateTime.setTime_t ( aQry.value(10).toLongLong() ) ; |
445 } |
442 QContactTimestamp qContactTimestamp; |
446 |
443 qContactTimestamp.setCreated(dateTime); |
447 if(qry.next()){ |
444 QVariant timeStamp = QVariant::fromValue( qContactTimestamp ); |
448 social_prof_id = qry.value(0).toInt(); |
445 |
449 social_profile.setProfileID(social_prof_id); |
446 smfContact = new SmfContact; |
450 } |
447 smfContact->setValue("Guid", userId); |
451 |
448 smfContact->setValue("Url", contactUrl); |
452 qry.finish(); |
449 smfContact->setValue("ContactId", contactId); |
453 db.close(); |
450 smfContact->setValue("Presence", contactPresence); |
454 |
451 smfContact->setValue("Timestamp", timeStamp); |
455 return social_prof_id; |
452 |
456 } |
453 smfProvider = new SmfProvider; |
457 |
454 snsName = aQry.value(4).toString(); |
458 int DataStoreManager::deleteSocialProfile(SMFSocialProfile& social_profile){ |
455 snsDesc = aQry.value(5).toString();; |
459 |
456 snsUrl = aQry.value(6).toUrl(); |
460 if (!(db.isOpen())){ |
457 |
461 if(!(db.open())){ |
458 smfProvider->setServiceName( snsName ); |
462 state = ERROR; |
459 smfProvider->setDescription( snsDesc ); |
463 m_last_msg = db.lastError().text(); |
460 smfProvider->setServiceUrl( snsUrl ); |
464 return -1; |
461 |
465 } |
462 iSmsfRelationItem = new SmfRelationItem; |
466 } |
463 iSmsfRelationItem->setIndex( aIndex ); |
467 |
464 iSmsfRelationItem->setProvider(*(smfProvider) ); |
468 QSqlQuery qry; |
465 iSmsfRelationItem->setContact(*( smfContact )); |
469 qry.prepare("DELETE FROM social_profiles WHERE social_profile_id=:social_profile_id"); |
466 } |
470 qry.bindValue(":social_profile_id", social_profile.profileID()); |
467 uint DataStoreManager::count(SmfRelationId relation){ |
471 |
468 |
472 if(!(qry.exec())){ |
469 const int relationId = relation.toInt(); |
473 m_last_msg = qry.lastError().text(); |
470 return count( relationId ); |
474 qry.finish(); |
471 } |
475 return -1; |
472 |
476 } |
473 SmfError DataStoreManager::deleteRelation(SmfRelationId relation){ |
477 |
474 const int relationId = relation.toInt(); |
478 qry.finish(); |
475 return deleteRelationFromTable( relationId ); |
479 db.close(); |
476 } |
480 |
477 |
481 return 0; |
478 QList<SmfRelationId> DataStoreManager::getAllRelations(){ |
482 } |
479 QList<SmfRelationId> relationList; |
483 |
480 |
484 int DataStoreManager::addSNSEntry( SMFSocialNetworkingSite& sns){ |
481 if(SmfDbOpeningError == openDB()) |
485 int sns_id = sns.snsID(); |
482 return relationList; |
486 |
483 |
487 if (!(db.isOpen())){ |
484 QSqlQuery qry; |
488 if(!(db.open())){ |
485 qry.prepare("SELECT DISTINCT relationId FROM relation"); |
489 state = ERROR; |
486 |
490 m_last_msg = db.lastError().text(); |
487 if(SmfDbQueryExecutonError == executeQuery(qry)) |
491 return sns_id; |
488 return relationList; |
492 } |
489 |
493 } |
490 while(qry.next()){ |
494 |
491 relationList << qry.value(0).toString(); |
495 QSqlQuery qry; |
492 } |
496 qry.prepare("INSERT INTO sns_base (sns_name, sns_url) VALUES (:sns_name, :sns_url)"); |
493 |
497 qry.bindValue(":sns_name", sns.snsName()); |
494 qry.finish(); |
498 qry.bindValue(":sns_url", sns.snsURL()); |
495 db.close(); |
499 |
496 return relationList; |
500 if(!(qry.exec())){ |
497 } |
501 m_last_msg = qry.lastError().text(); |
498 |
502 qry.finish(); |
499 int DataStoreManager::openDB(){ |
503 return sns_id; |
500 if (!(db.isOpen())){ |
504 } |
501 if(!(db.open())){ |
505 |
502 state = ERROR; |
506 qry.prepare("SELECT sns_id FROM sns_base WHERE sns_name=:sns_name AND sns_url=:sns_url"); |
503 lastMsg = db.lastError().text(); |
507 qry.bindValue(":sns_name", sns.snsName()); |
504 return SmfDbOpeningError; |
508 qry.bindValue(":sns_url", sns.snsURL()); |
505 } |
509 |
506 } |
510 if(!(qry.exec())){ |
507 return SmfNoError; |
511 m_last_msg = qry.lastError().text(); |
508 } |
512 qry.finish(); |
509 |
513 return sns_id; |
510 int DataStoreManager::executeQuery(QSqlQuery& qry){ |
514 } |
511 if(!(qry.exec())){ |
515 |
512 lastMsg = qry.lastError().text(); |
516 if(qry.next()){ |
513 qry.finish(); |
517 sns_id = qry.value(0).toInt(); |
514 return SmfDbQueryExecutonError; |
518 sns.setSnsID(sns_id); |
515 } |
519 } |
516 return SmfNoError; |
520 |
517 } |
521 qry.finish(); |
518 |
522 db.close(); |
519 |
523 |
520 |
524 return sns_id; |
521 DataStoreManager::DataStoreManager(const QString& dbName, QObject* parent):QObject(parent){ |
525 } |
|
526 |
|
527 int DataStoreManager::deleteSNSEntry(SMFSocialNetworkingSite& sns){ |
|
528 |
|
529 if (!(db.isOpen())){ |
|
530 if(!(db.open())){ |
|
531 state = ERROR; |
|
532 m_last_msg = db.lastError().text(); |
|
533 return -1; |
|
534 } |
|
535 } |
|
536 |
|
537 QSqlQuery qry; |
|
538 qry.prepare("DELETE FROM sns_base WHERE sns_id=:sns_id"); |
|
539 qry.bindValue(":sns_id", sns.snsID()); |
|
540 |
|
541 if(!(qry.exec())){ |
|
542 m_last_msg = qry.lastError().text(); |
|
543 qry.finish(); |
|
544 return -1; |
|
545 } |
|
546 |
|
547 qry.finish(); |
|
548 db.close(); |
|
549 |
|
550 return 0; |
|
551 } |
|
552 |
|
553 int DataStoreManager::createRelation(const SMFUserProfile& user_profile, SMFSocialProfile& social_profile){ |
|
554 social_profile.setAssociatedUserID(user_profile.userID()); |
|
555 /* Save the socialProfile */ |
|
556 //saveSocialProfile(social_profile); |
|
557 return 0; |
|
558 } |
|
559 |
|
560 int DataStoreManager::deleteRelation(const SMFUserProfile&, SMFSocialProfile& social_profile){ |
|
561 social_profile.setAssociatedUserID(-1); |
|
562 /* Save the profile */ |
|
563 //saveSocialProfile(social_profile); |
|
564 return 0; |
|
565 } |
|
566 |
|
567 |
|
568 DataStoreManager::DataStoreManager(const QString& db_name, QObject* parent):QObject(parent){ |
|
569 db = QSqlDatabase::addDatabase("QSQLITE"); |
522 db = QSqlDatabase::addDatabase("QSQLITE"); |
570 db.setDatabaseName(db_name); |
523 db.setDatabaseName(dbName); |
571 } |
524 } |
572 |
525 |
573 bool DataStoreManager::InitializeDataBase(){ |
526 bool DataStoreManager::InitializeDataBase(){ |
574 |
527 |
575 if(!db.open()){ |
528 if(!db.open()){ |
576 state = ERROR; |
529 state = ERROR; |
577 m_last_msg = db.lastError().text(); |
530 lastMsg = db.lastError().text(); |
578 return FALSE; |
531 return FALSE; |
579 } |
532 } |
580 else |
533 else |
581 state = READY; |
534 state = READY; |
582 |
535 |
583 QSqlQuery create_tables[3]; |
536 const int tableCount = 2; |
584 |
537 |
585 create_tables[0].prepare("CREATE TABLE IF NOT EXISTS user_profiles (user_id INTEGER PRIMARY KEY AUTOINCREMENT , name TEXT, contact_id TEXT)"); |
538 QSqlQuery smfDsmTables[tableCount]; |
586 create_tables[1].prepare("CREATE TABLE IF NOT EXISTS sns_base (sns_id INTEGER PRIMARY KEY AUTOINCREMENT , sns_name TEXT, sns_url TEXT)"); |
539 |
587 create_tables[2].prepare("CREATE TABLE IF NOT EXISTS social_profiles (social_profile_id INTEGER PRIMARY KEY AUTOINCREMENT , user_id INTEGER, sns_id INTEGER, profile_url TEXT, screen_alias TEXT)"); |
540 smfDsmTables[0].prepare("CREATE TABLE IF NOT EXISTS contact (contactId INTEGER PRIMARY KEY AUTOINCREMENT," |
588 |
541 "contactGuid TEXT," |
589 |
542 "contactUrl TEXT," |
590 |
543 "localId TEXT," |
591 for(int i = 0; i < 3; i++){ |
544 "managerUri TEXT," |
592 if(!create_tables[i].exec()){ |
545 "snsName TEXT," |
|
546 "snsDesc TEXT," |
|
547 "snsUrl TEXT)," |
|
548 "presenceState INTEGER," |
|
549 "presenceText TEXT," |
|
550 "timeStamp INTEGER )"); |
|
551 |
|
552 smfDsmTables[1].prepare("CREATE TABLE IF NOT EXISTS relation (relationId INTEGER," |
|
553 " contactId INTEGER," |
|
554 " contactIndex INTEGER)" ); |
|
555 |
|
556 |
|
557 for(int i = 0; i < tableCount; i++){ |
|
558 if(!smfDsmTables[i].exec()){ |
593 state = ERROR; |
559 state = ERROR; |
594 m_last_msg = create_tables[i].lastError().text(); |
560 lastMsg = smfDsmTables[i].lastError().text(); |
595 return FALSE; |
561 return FALSE; |
596 } |
562 } |
597 create_tables[i].finish(); |
563 smfDsmTables[i].finish(); |
598 } |
564 } |
599 |
565 |
600 db.close(); |
566 db.close(); |
601 state = CLOSED; |
567 state = CLOSED; |
602 return TRUE; |
568 return TRUE; |
603 } |
569 } |
|
570 |
|
571 |
|
572 int DataStoreManager::addContactToTable( const QString &aLocalId, |
|
573 const QString &aManagerUri, |
|
574 const QString &aSnsName, |
|
575 const QString &aSnsDesc, |
|
576 const QString &aSnsUrl){ |
|
577 |
|
578 int contactId; |
|
579 |
|
580 if(SmfDbOpeningError == openDB()) |
|
581 return SmfDbOpeningError; |
|
582 |
|
583 QSqlQuery qry; |
|
584 qry.prepare("INSERT INTO contact (userId, contactUrl, localId , managerUri, snsName," |
|
585 " snsDesc, snsUrl, presenceState, presenceText, statusText, timeStamp )" |
|
586 " VALUES (:userId, :contactUrl, :localId , :managerUri, :snsName," |
|
587 " :snsDesc, :snsUrl, :presenceState, :presenceText, :statusText, :timeStamp )"); |
|
588 qry.bindValue(":userId", NULL); |
|
589 qry.bindValue(":contactUrl", NULL); |
|
590 qry.bindValue(":localId", aLocalId); |
|
591 qry.bindValue(":managerUri", aManagerUri); |
|
592 qry.bindValue(":snsName", aSnsName); |
|
593 qry.bindValue(":snsDesc", aSnsDesc); |
|
594 qry.bindValue(":snsUrl", aSnsUrl); |
|
595 qry.bindValue(":presenceState", NULL); |
|
596 qry.bindValue(":presenceText", NULL); |
|
597 qry.bindValue(":statusText", NULL); |
|
598 qry.bindValue(":timeStamp", 0); |
|
599 |
|
600 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
601 return SmfDbQueryExecutonError; |
|
602 |
|
603 qry.prepare("SELECT contactId FROM contact WHERE localId=:localId AND managerUri=:managerUri"); |
|
604 qry.bindValue(":localId", aLocalId); |
|
605 qry.bindValue(":managerUri", aManagerUri); |
|
606 |
|
607 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
608 return SmfDbQueryExecutonError; |
|
609 |
|
610 if(qry.next()){ |
|
611 contactId = qry.value(0).toInt(); |
|
612 } |
|
613 |
|
614 qry.finish(); |
|
615 db.close(); |
|
616 return contactId; |
|
617 |
|
618 } |
|
619 |
|
620 int DataStoreManager::addContactToTable( const QString &aUserId, |
|
621 const QString &aContactUrl, |
|
622 const QString &aLocalId, |
|
623 const QString &aManagerUri, |
|
624 const QString &aSnsName, |
|
625 const QString &aSnsDesc, |
|
626 const QString &aSnsUrl, |
|
627 const QString &aPresenceState, |
|
628 const QString &aPresenceText, |
|
629 const QString &aStatusText, |
|
630 const Int64 aTimeStamp ){ |
|
631 |
|
632 int contactId; |
|
633 |
|
634 if(SmfDbOpeningError == openDB()) |
|
635 return SmfDbOpeningError; |
|
636 |
|
637 QSqlQuery qry; |
|
638 qry.prepare("INSERT INTO contact (userId, contactUrl, localId , managerUri, snsName," |
|
639 " snsDesc, snsUrl, PresenceState, PresenceText, statusText, timeStamp )" |
|
640 " VALUES (:userId, :contactUrl, :localId , :managerUri, :snsName," |
|
641 " :snsDesc, :snsUrl, :PresenceState, :PresenceText, statusText, :timeStamp )"); |
|
642 |
|
643 qry.bindValue(":userId", aUserId); qry.bindValue(":contactUrl", aContactUrl); |
|
644 qry.bindValue(":localId", aLocalId); qry.bindValue(":managerUri", aManagerUri); |
|
645 qry.bindValue(":snsName", aSnsName); qry.bindValue(":snsDesc", aSnsDesc); |
|
646 qry.bindValue(":snsUrl", aSnsUrl); qry.bindValue(":PresenceState", aPresenceState); |
|
647 qry.bindValue(":PresenceText", aPresenceText); qry.bindValue(":statusText", aStatusText); |
|
648 qry.bindValue(":timeStamp", aTimeStamp); |
|
649 |
|
650 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
651 return SmfDbQueryExecutonError; |
|
652 |
|
653 qry.prepare("SELECT contactId FROM contact WHERE (localId=:localId AND managerUri=:managerUri) " |
|
654 "OR (aContactGuid=:aContactGuid AND aContactUrl=:aContactUrl)"); |
|
655 qry.bindValue(":localId", aLocalId); qry.bindValue(":managerUri", aManagerUri); |
|
656 qry.bindValue(":userId", aUserId); qry.bindValue(":aContactUrl", aContactUrl); |
|
657 |
|
658 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
659 return SmfDbQueryExecutonError; |
|
660 |
|
661 if(qry.next()){ |
|
662 contactId = qry.value(0).toInt(); |
|
663 } |
|
664 |
|
665 qry.finish(); |
|
666 db.close(); |
|
667 return contactId; |
|
668 } |
|
669 |
|
670 SmfError DataStoreManager::removeContactFromTable(const int aContactId){ |
|
671 |
|
672 if(SmfDbOpeningError == openDB()) |
|
673 return SmfDbOpeningError; |
|
674 |
|
675 QSqlQuery qry; |
|
676 qry.prepare("DELETE FROM contact WHERE contactId=:contactId"); |
|
677 qry.bindValue(":contactId", aContactId); |
|
678 |
|
679 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
680 return SmfDbQueryExecutonError; |
|
681 |
|
682 qry.finish(); |
|
683 db.close(); |
|
684 |
|
685 return SmfNoError; |
|
686 } |
|
687 |
|
688 |
|
689 |
|
690 TBool DataStoreManager::contactIfExist( const int aContactId){ |
|
691 |
|
692 TBool contactExist = EFalse; |
|
693 if(SmfDbOpeningError == openDB()) |
|
694 return EFalse; |
|
695 |
|
696 QSqlQuery qry; |
|
697 qry.prepare("SELECT contactId FROM contact WHERE contactId=:contactId"); |
|
698 qry.bindValue(":contactId", aContactId); |
|
699 |
|
700 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
701 return EFalse; |
|
702 |
|
703 if( qry.first() ) |
|
704 contactExist = ETrue; |
|
705 |
|
706 qry.finish(); |
|
707 db.close(); |
|
708 |
|
709 return contactExist; |
|
710 } |
|
711 |
|
712 TBool DataStoreManager::relationIfExist( const int aRelationId){ |
|
713 |
|
714 TBool contactExist = EFalse; |
|
715 if(SmfDbOpeningError == openDB()) |
|
716 return EFalse; |
|
717 |
|
718 QSqlQuery qry; |
|
719 qry.prepare("SELECT relationId FROM relation WHERE relationId=:relationId"); |
|
720 qry.bindValue(":relationId", aRelationId); |
|
721 |
|
722 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
723 return EFalse; |
|
724 |
|
725 if( qry.first() ) |
|
726 contactExist = ETrue; |
|
727 |
|
728 qry.finish(); |
|
729 db.close(); |
|
730 |
|
731 return contactExist; |
|
732 } |
|
733 |
|
734 |
|
735 const int DataStoreManager::findMaxIndexValue(const int aRelationId ){ |
|
736 |
|
737 int contactIndex = 0; |
|
738 |
|
739 if(SmfDbOpeningError == openDB()) |
|
740 return SmfDbOpeningError; |
|
741 |
|
742 QSqlQuery qry; |
|
743 qry.prepare("SELECT MAX( contactIndex ) FROM (" |
|
744 "SELECT contactIndex FROM relation WHERE (relationId=:relationId ))"); |
|
745 qry.bindValue(":relationId", aRelationId); |
|
746 |
|
747 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
748 return SmfDbQueryExecutonError; |
|
749 |
|
750 if(qry.next()){ |
|
751 contactIndex = qry.value(0).toInt(); |
|
752 } |
|
753 |
|
754 qry.finish(); |
|
755 db.close(); |
|
756 return contactIndex; |
|
757 } |
|
758 |
|
759 const int DataStoreManager::findMaxRelationId( ){ |
|
760 |
|
761 int maxRelationId = socialProfileBaseID; |
|
762 |
|
763 if(SmfDbOpeningError == openDB()) |
|
764 return SmfDbOpeningError; |
|
765 |
|
766 QSqlQuery qry; |
|
767 qry.prepare("SELECT MAX( relationId ) FROM relation"); |
|
768 |
|
769 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
770 return SmfDbQueryExecutonError; |
|
771 |
|
772 if(qry.next()){ |
|
773 maxRelationId = qry.value(0).toInt(); |
|
774 } |
|
775 |
|
776 qry.finish(); |
|
777 db.close(); |
|
778 return maxRelationId; |
|
779 } |
|
780 |
|
781 const int DataStoreManager::addRelationToTable(const int aContactId, int aRelationId ){ |
|
782 |
|
783 int relationId, contactIndex; |
|
784 |
|
785 if( aRelationId == ENewRelation ){ |
|
786 relationId = findMaxRelationId() + 1; |
|
787 contactIndex = 0; |
|
788 } |
|
789 else{ |
|
790 relationId = aRelationId; |
|
791 contactIndex = 1 + ( findMaxIndexValue( aRelationId ) ) ; |
|
792 } |
|
793 |
|
794 if(SmfDbOpeningError == openDB()) |
|
795 return SmfDbOpeningError; |
|
796 |
|
797 QSqlQuery qry; |
|
798 qry.prepare("INSERT INTO relation (relationId, contactId, contactIndex )" |
|
799 " VALUES (:relationId, :contactId, :contactIndex )"); |
|
800 qry.bindValue(":relationId", relationId); |
|
801 qry.bindValue(":contactId", aContactId); |
|
802 qry.bindValue(":contactIndex", contactIndex); |
|
803 |
|
804 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
805 return SmfDbQueryExecutonError; |
|
806 |
|
807 qry.finish(); |
|
808 db.close(); |
|
809 return relationId; |
|
810 } |
|
811 |
|
812 int DataStoreManager::searchContactId(const int aRelationId, const int aIndex){ |
|
813 |
|
814 int contactId = SmfDbQueryExecutonError; |
|
815 |
|
816 if(SmfDbOpeningError == openDB()) |
|
817 return SmfDbOpeningError; |
|
818 |
|
819 QSqlQuery qry; |
|
820 qry.prepare("SELECT contactId FROM relation WHERE (relationId=:relationId AND contactIndex = :contactIndex)"); |
|
821 |
|
822 qry.bindValue(":relationId", aRelationId); |
|
823 qry.bindValue(":contactIndex", aIndex); |
|
824 |
|
825 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
826 return SmfDbQueryExecutonError; |
|
827 |
|
828 if(qry.next()){ |
|
829 contactId = qry.value(0).toInt(); |
|
830 } |
|
831 |
|
832 qry.finish(); |
|
833 db.close(); |
|
834 return contactId; |
|
835 } |
|
836 |
|
837 |
|
838 int DataStoreManager::searchRelationId(const int aContactId){ |
|
839 |
|
840 int relationId = SmfDbQueryExecutonError; |
|
841 |
|
842 if(SmfDbOpeningError == openDB()) |
|
843 return SmfDbOpeningError; |
|
844 |
|
845 QSqlQuery qry; |
|
846 qry.prepare("SELECT relationId FROM relation WHERE ( contactId = :contactId)"); |
|
847 qry.bindValue(":contactId", aContactId); |
|
848 |
|
849 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
850 return SmfDbQueryExecutonError; |
|
851 |
|
852 if(qry.next()){ |
|
853 relationId = qry.value(0).toInt(); |
|
854 } |
|
855 |
|
856 qry.finish(); |
|
857 db.close(); |
|
858 return relationId; |
|
859 } |
|
860 |
|
861 |
|
862 QList<int> DataStoreManager::searchContactIdList(const int aRelationId){ |
|
863 |
|
864 QList<int> contactList; |
|
865 |
|
866 if(SmfDbOpeningError == openDB()) |
|
867 return contactList; |
|
868 |
|
869 QSqlQuery qry; |
|
870 qry.prepare("SELECT contactId FROM relation WHERE ( relationId = :relationId)"); |
|
871 qry.bindValue(":relationId", aRelationId); |
|
872 |
|
873 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
874 return contactList; |
|
875 |
|
876 while(qry.next()){ |
|
877 contactList << qry.value(0).toInt(); |
|
878 } |
|
879 |
|
880 qry.finish(); |
|
881 db.close(); |
|
882 return contactList; |
|
883 } |
|
884 |
|
885 |
|
886 SmfError DataStoreManager::deleteRelationFromTable(const int aRelationId){ |
|
887 |
|
888 int contactIndex; |
|
889 if(SmfDbOpeningError == openDB()) |
|
890 return SmfDbOpeningError; |
|
891 |
|
892 QSqlQuery qry; |
|
893 qry.prepare("SELECT contactId FROM relation WHERE relationId = :relationId "); |
|
894 qry.bindValue(":relationId", aRelationId); |
|
895 |
|
896 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
897 return SmfDbQueryExecutonError; |
|
898 |
|
899 while(qry.next()){ |
|
900 contactIndex = qry.value(0).toInt(); |
|
901 QSqlQuery innerQry; |
|
902 innerQry.prepare("DELETE FROM contact WHERE contactId=:contactId "); |
|
903 innerQry.bindValue(":contactId", contactIndex); |
|
904 |
|
905 if(SmfDbQueryExecutonError == executeQuery(innerQry)) |
|
906 return SmfDbQueryExecutonError; |
|
907 innerQry.finish(); |
|
908 } |
|
909 |
|
910 qry.prepare("DELETE FROM relation WHERE ( relationId = :relationId)"); |
|
911 qry.bindValue(":relationId", aRelationId); |
|
912 |
|
913 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
914 return SmfDbQueryExecutonError; |
|
915 |
|
916 qry.finish(); |
|
917 db.close(); |
|
918 return SmfNoError; |
|
919 } |
|
920 |
|
921 SmfError DataStoreManager::deleteContactFromTable(const int aRelationId, const int aContactId){ |
|
922 |
|
923 int contactIndex = SmfDbContactNotExist; |
|
924 |
|
925 if(SmfDbOpeningError == openDB()) |
|
926 return SmfDbOpeningError; |
|
927 |
|
928 QSqlQuery qry; |
|
929 qry.prepare("SELECT contactIndex FROM relation WHERE ( relationId = :relationId AND contactId=:contactId)"); |
|
930 qry.bindValue(":relationId", aRelationId); |
|
931 qry.bindValue(":contactId", aContactId); |
|
932 |
|
933 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
934 return SmfDbQueryExecutonError; |
|
935 |
|
936 if(qry.next()){ |
|
937 contactIndex = qry.value(0).toInt(); |
|
938 |
|
939 qry.prepare("DELETE FROM relation WHERE ( relationId = :relationId AND contactId=:contactId)"); |
|
940 qry.bindValue(":relationId", aRelationId); |
|
941 qry.bindValue(":contactId", aContactId); |
|
942 |
|
943 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
944 return SmfDbQueryExecutonError; |
|
945 |
|
946 qry.prepare("DELETE FROM contact WHERE contactId=:contactId "); |
|
947 qry.bindValue(":contactId", aContactId); |
|
948 |
|
949 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
950 return SmfDbQueryExecutonError; |
|
951 } |
|
952 |
|
953 manageContactIndex(aRelationId, contactIndex ); |
|
954 |
|
955 qry.finish(); |
|
956 db.close(); |
|
957 return SmfNoError; |
|
958 } |
|
959 |
|
960 |
|
961 void DataStoreManager::manageContactIndex(const int aRelationId, const int aContactIndex ){ |
|
962 |
|
963 QSqlQuery qry; |
|
964 qry.prepare("UPDATE relation SET contactIndex = contactIndex - 1 " |
|
965 "WHERE ( relationId = :relationId AND contactIndex > :contactIndex )"); |
|
966 qry.bindValue(":relationId", aRelationId); |
|
967 qry.bindValue(":contactIndex", aContactIndex); |
|
968 executeQuery(qry); |
|
969 qry.finish(); |
|
970 } |
|
971 |
|
972 uint DataStoreManager::count(const int aRelationId){ |
|
973 uint count = -1; |
|
974 |
|
975 if(SmfDbOpeningError == openDB()) |
|
976 return count; |
|
977 |
|
978 QSqlQuery qry; |
|
979 qry.prepare("SELECT count(*) FROM relation WHERE ( relationId = :relationId)"); |
|
980 qry.bindValue(":relationId", aRelationId); |
|
981 |
|
982 if(SmfDbQueryExecutonError == executeQuery(qry)) |
|
983 return count; |
|
984 |
|
985 if(qry.next()){ |
|
986 count = qry.value(0).toInt(); |
|
987 } |
|
988 |
|
989 qry.finish(); |
|
990 db.close(); |
|
991 return count; |
|
992 } |