|
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 <QtGlobal> |
|
19 #include <QString> |
|
20 #include <QStringList> |
|
21 #include <QSqlQuery> |
|
22 #include <QFile> |
|
23 #include <QMutex> |
|
24 #include <QVariant> |
|
25 #include <QtDebug> |
|
26 #include <QByteArray> |
|
27 #include "irdbwrapper.h" |
|
28 #include "irsqlstr.h" |
|
29 #include "irdb.h" |
|
30 |
|
31 #define IRDB_ERR_NONE (0x00) |
|
32 #define IRDB_ERR_INSERT (0x01) |
|
33 #define IRDB_ERR_UPDATE (0x02) |
|
34 #define IRDB_ERR_SELECT (0x03) |
|
35 #define IRDB_ERR_DELETE (0x04) |
|
36 #define IRDB_ERR_OPEN (0x05) |
|
37 #define IRDB_ERR_CHANGE (0x06) |
|
38 #define IRDB_ERR_ILLEGAL (0x07) |
|
39 #define IRDB_ERR_WRAPPER (0x08) |
|
40 #define IRDB_CID_EXISTED (0x10) |
|
41 #define IRDB_CID_UNEXISTED (0x11) |
|
42 #define IRDB_CID_USER_DEFINE (0xF0000000) |
|
43 |
|
44 |
|
45 IRDB* IRDB::mpIRDBInstance = NULL; |
|
46 QMutex IRDB::mMutex; |
|
47 |
|
48 IRDB::IRDB(): m_RefCnt(0) |
|
49 { |
|
50 } |
|
51 |
|
52 IRDB::~IRDB() |
|
53 { |
|
54 } |
|
55 |
|
56 IRDB* IRDB::getInstance() |
|
57 { |
|
58 mMutex.lock(); |
|
59 bool existed = true; |
|
60 |
|
61 if(NULL == mpIRDBInstance) |
|
62 { |
|
63 mpIRDBInstance = new IRDB(); |
|
64 existed = false; |
|
65 } |
|
66 |
|
67 ++mpIRDBInstance->m_RefCnt; |
|
68 mMutex.unlock(); |
|
69 |
|
70 if(false == existed) |
|
71 { |
|
72 if(!mpIRDBInstance->createIRDB()) |
|
73 { |
|
74 //remove IR database; |
|
75 QFile::remove(IRDBFile); |
|
76 mpIRDBInstance->releaseInstance(); |
|
77 } |
|
78 } |
|
79 return mpIRDBInstance; |
|
80 } |
|
81 |
|
82 void IRDB::releaseInstance() |
|
83 { |
|
84 mMutex.lock(); |
|
85 if ((--m_RefCnt) == 0) |
|
86 { |
|
87 delete this; |
|
88 mpIRDBInstance = NULL; |
|
89 } |
|
90 mMutex.unlock(); |
|
91 } |
|
92 |
|
93 bool IRDB::createIRDB() |
|
94 { |
|
95 bool ret = true; |
|
96 |
|
97 m_sqlDB = QSqlDatabase::addDatabase("QSQLITE"); |
|
98 m_sqlDB.setDatabaseName(IRDBName); |
|
99 |
|
100 if( !QFile::exists(IRDBFile) ) |
|
101 { |
|
102 mMutex.lock(); |
|
103 if(!m_sqlDB.isOpen()) |
|
104 { |
|
105 m_sqlDB.open(); |
|
106 mMutex.unlock(); |
|
107 |
|
108 ret = createIRTable(); |
|
109 m_sqlDB.close(); |
|
110 } |
|
111 else |
|
112 { |
|
113 mMutex.unlock(); |
|
114 ret = false; |
|
115 } |
|
116 } |
|
117 |
|
118 return ret; |
|
119 } |
|
120 |
|
121 bool IRDB::destoryIRDB() |
|
122 { |
|
123 bool ret = true; |
|
124 mMutex.lock(); |
|
125 if(!m_sqlDB.isOpen()) |
|
126 { |
|
127 QFile::remove (IRDBFile); |
|
128 mMutex.unlock(); |
|
129 } |
|
130 else |
|
131 { |
|
132 mMutex.unlock(); |
|
133 ret = false; |
|
134 } |
|
135 |
|
136 return ret; |
|
137 } |
|
138 |
|
139 //if the IR database file in disk exists, we don't need to call this function; |
|
140 bool IRDB::createIRTable() |
|
141 { |
|
142 bool Rlt = true; |
|
143 |
|
144 QStringList sqlList; |
|
145 QSqlQuery sqlQuery(m_sqlDB); |
|
146 |
|
147 sqlList<<CREATE_TABEL_IRBUFF<<CREATE_TABEL_CHANNELHISTORY<<CREATE_TABEL_SEARCHRLT<<CREATE_TABEL_CHANNELINFO \ |
|
148 <<CREATE_TABEL_URLINFO<<CREATE_TABEL_IMG<<CREATE_TABEL_ADVERTISEMENT<<CREATE_TABEL_SONGHISTORY<<CREATE_TABEL_FAVORITES \ |
|
149 \ |
|
150 <<TRI_INSERT_IRBUFF<<TRI_UPDATE_IRBUFF \ |
|
151 <<TRI_INSERT_SEARCHRLT<<TRI_DELETE_SEARCHRLT \ |
|
152 <<TRI_INSERT_CHANNELINFO<<TRI_UPDATE_CHANNELINFO<<TRI_DELETE_CHANNELINFO \ |
|
153 <<TRI_INSERT_CHANNELHISTORY<<TRI_DELETE_CHANNELHISTORY \ |
|
154 <<TRI_INSERT_ADVERTISEMENT<<TRI_UPDATE_ADVERTISEMENT \ |
|
155 \ |
|
156 <<TRI_INSERT_SONGHISTORY<<TRI_UPDATE_SONGHISTORY<<TRI_DELETE_SONGHISTORY \ |
|
157 <<TRI_INSERT_FAVORITES<<TRI_UPDATE_FAVORITES<<TRI_DELETE_FAVORITES \ |
|
158 \ |
|
159 <<IRVIEW_CHANNELINFO1<<IRVIEW_CHANNELINFO<<IRVIEW_CHANNELHISTORY<<IR_VIEW_SRH_USERCID \ |
|
160 <<IRVIEW_FAVORITES<<IRVIEW_SEARCHRLT<<IRVIEW_SONGHISTORY ; |
|
161 |
|
162 |
|
163 for(int i = 0; i<sqlList.size();i++) |
|
164 { |
|
165 Rlt = sqlQuery.exec(sqlList.at(i)); |
|
166 |
|
167 if(Rlt == false) |
|
168 { |
|
169 break; |
|
170 } |
|
171 } |
|
172 |
|
173 sqlQuery.clear(); |
|
174 |
|
175 //remove string in sqlList; |
|
176 for(int i = 0; i<sqlList.size();i++) |
|
177 { |
|
178 sqlList.removeFirst(); |
|
179 } |
|
180 |
|
181 return Rlt; |
|
182 } |
|
183 |
|
184 |
|
185 bool IRDB::openIRDBConnection() |
|
186 { |
|
187 bool ret = true; |
|
188 mMutex.lock(); |
|
189 |
|
190 if(m_sqlDB.isOpen()) |
|
191 { |
|
192 ret = false; |
|
193 } |
|
194 else |
|
195 { |
|
196 ret = m_sqlDB.open(); |
|
197 } |
|
198 |
|
199 mMutex.unlock(); |
|
200 return ret; |
|
201 } |
|
202 |
|
203 |
|
204 IRDBCHAR IRDB::chgRowIRDB(QString aInsSqlStr, |
|
205 QString aUpdSqlStr, |
|
206 uint* const retCid, |
|
207 const QString& aCondSqlStr, |
|
208 const QString& condUserCidStr, |
|
209 const QList<QByteArray>* apImgList) |
|
210 { |
|
211 IRDBCHAR ret = IRDB_ERR_NONE; |
|
212 //channelId value 0 don't be used in CIRDB; |
|
213 uint channelId = 0; |
|
214 //sql string for search cid according to nickname and url; |
|
215 QString strSrhCid = IRDBSrhUserCid + condUserCidStr; |
|
216 QString strSrhDataSet = IRDBSrhCIdFrmView + aCondSqlStr; |
|
217 |
|
218 //initialize the channelId for application level; |
|
219 if(retCid) |
|
220 { |
|
221 *retCid = 0; |
|
222 } |
|
223 |
|
224 if(false == openIRDBConnection()) |
|
225 { |
|
226 return IRDB_ERR_OPEN; |
|
227 } |
|
228 |
|
229 QSqlQuery iIRQuery(m_sqlDB); |
|
230 |
|
231 if(NULL != condUserCidStr) |
|
232 { |
|
233 if(false == iIRQuery.exec(strSrhCid)) |
|
234 { |
|
235 iIRQuery.clear(); |
|
236 m_sqlDB.close(); |
|
237 ret = IRDB_ERR_SELECT; |
|
238 } |
|
239 else |
|
240 { |
|
241 iIRQuery.next(); |
|
242 channelId = iIRQuery.value(0).toInt(); |
|
243 } |
|
244 } |
|
245 |
|
246 if(ret != IRDB_ERR_NONE) |
|
247 { |
|
248 return ret; |
|
249 } |
|
250 |
|
251 /*for below case, because it is a channel inputted by user. |
|
252 * Although the channelId can be gotten according to the condstr, |
|
253 * if the channelId is defined by iSDS, still new user-defined channelId will be allocated. |
|
254 */ |
|
255 if(channelId < IRDB_CID_USER_DEFINE) |
|
256 { |
|
257 channelId = 0; |
|
258 } |
|
259 |
|
260 if(0 == channelId ) |
|
261 { |
|
262 //new channelId should be allocated. |
|
263 if( false == iIRQuery.exec(IRDBSrhCIdMaxFrmChannelInfo) ) |
|
264 { |
|
265 iIRQuery.clear(); |
|
266 m_sqlDB.close(); |
|
267 ret = IRDB_ERR_SELECT; |
|
268 } |
|
269 |
|
270 if(ret != IRDB_ERR_NONE) |
|
271 { |
|
272 return ret; |
|
273 } |
|
274 |
|
275 iIRQuery.next(); |
|
276 |
|
277 if(iIRQuery.value(0).toInt()< IRDB_CID_USER_DEFINE ) |
|
278 { |
|
279 channelId = (uint)IRDB_CID_USER_DEFINE; |
|
280 } |
|
281 else |
|
282 { |
|
283 channelId = (uint)iIRQuery.value(0).toInt() + 1; |
|
284 } |
|
285 |
|
286 //if a userdefined channelid is created, the value will be returned to application level; |
|
287 if(retCid) |
|
288 { |
|
289 *retCid = channelId; |
|
290 } |
|
291 } |
|
292 else |
|
293 { |
|
294 /* |
|
295 * for case: although channelId can be gotten, but the updSqlStr is invalid, |
|
296 * operation should be stopped |
|
297 */ |
|
298 if(false == iIRQuery.exec(strSrhDataSet)) |
|
299 { |
|
300 iIRQuery.clear(); |
|
301 m_sqlDB.close(); |
|
302 ret = IRDB_ERR_SELECT; |
|
303 } |
|
304 else |
|
305 { |
|
306 iIRQuery.next(); |
|
307 if(0 == iIRQuery.value(0).toInt()) |
|
308 { |
|
309 iIRQuery.clear(); |
|
310 m_sqlDB.close(); |
|
311 ret = IRDB_ERR_UPDATE; |
|
312 } |
|
313 } |
|
314 } |
|
315 |
|
316 if(IRDB_ERR_NONE == ret ) |
|
317 { |
|
318 iIRQuery.clear(); |
|
319 |
|
320 aInsSqlStr.insert(aInsSqlStr.indexOf(')'), QString(" ,channelId")); |
|
321 aInsSqlStr.insert(aInsSqlStr.lastIndexOf(')'),QString(" ,")+QString::number(channelId)); |
|
322 |
|
323 /* |
|
324 * Since process will go ahead to call |
|
325 * EIRDB IRDB::chgRowIRDB(const QString& aInsSqlStr, const QString& aUpdSqlStr, uint channelId, bool bPreHandle), |
|
326 * for the synchoronization concern, the m_sqlDB can't be closed until all the process is compeleted; |
|
327 * m_sqlDB.close(); |
|
328 */ |
|
329 ret = chgRowIRDB(aInsSqlStr, aUpdSqlStr, channelId, aCondSqlStr, apImgList, true); |
|
330 } |
|
331 /* else |
|
332 { |
|
333 iIRQuery.clear(); |
|
334 m_sqlDB.close(); |
|
335 } |
|
336 */ |
|
337 return ret; |
|
338 } |
|
339 |
|
340 |
|
341 |
|
342 // if the action invoked from 'go to station view', don't use this function |
|
343 IRDBCHAR IRDB::chgRowIRDB(const QString& aInsSqlStr, |
|
344 const QString& aUpdSqlStr, |
|
345 const uint& channelId, |
|
346 const QString& aCondSqlStr, |
|
347 const QList<QByteArray>* apImgList, |
|
348 bool bPreHandle) |
|
349 { |
|
350 IRDBCHAR ret = IRDB_ERR_NONE; |
|
351 QString strIsCIDExist = IRDBSrhCIdCntFrmChannelInfo + QString::number(channelId) +";"; |
|
352 QString updSqlStr; |
|
353 |
|
354 /* |
|
355 * if the function call isn't invoked from |
|
356 * EIRDB IRDB::chgRowIRDB(QString aInsSqlStr, QString aUpdSqlStr, const QString& aCondStr), |
|
357 * db connection should be opened again; |
|
358 */ |
|
359 if(false == bPreHandle) |
|
360 { |
|
361 if(false == openIRDBConnection()) |
|
362 { |
|
363 return IRDB_ERR_OPEN; |
|
364 } |
|
365 } |
|
366 |
|
367 QSqlQuery iIRQuery(m_sqlDB); |
|
368 |
|
369 /*If channelId isn't 0, then two cases should be checked. |
|
370 * First, if channelId exists in the channelInfo, update operation will be excuted. |
|
371 * Second, if channelId doesn't exists in channelInfo table, insert operation will be excuted. |
|
372 */ |
|
373 if(false == iIRQuery.exec(strIsCIDExist)) |
|
374 { |
|
375 iIRQuery.clear(); |
|
376 m_sqlDB.close(); |
|
377 return IRDB_ERR_SELECT; |
|
378 } |
|
379 |
|
380 iIRQuery.next(); |
|
381 |
|
382 /*if channelID existed, we need to get the preset data*/ |
|
383 if (0 != iIRQuery.value(0).toInt()) |
|
384 { |
|
385 /*trigger the action, |
|
386 * All the data recorded in CiRDB, its channelID equal to here , |
|
387 * will be composed to one row in IRBuff table; |
|
388 */ |
|
389 if(false == iIRQuery.exec(IRDBInsertIRBuff + QString::number(channelId)+",1, 2)")) |
|
390 { |
|
391 ret = IRDB_ERR_INSERT; |
|
392 } |
|
393 else |
|
394 { |
|
395 if(aCondSqlStr.isEmpty()) |
|
396 { |
|
397 updSqlStr = aUpdSqlStr + " where channelId = " + QString::number(channelId); |
|
398 } |
|
399 else |
|
400 { |
|
401 updSqlStr += aCondSqlStr; |
|
402 } |
|
403 if(NULL == apImgList) |
|
404 { |
|
405 if(false == iIRQuery.exec(aUpdSqlStr)) |
|
406 { |
|
407 ret = IRDB_ERR_UPDATE; |
|
408 } |
|
409 } |
|
410 else |
|
411 { |
|
412 iIRQuery.prepare(aUpdSqlStr); |
|
413 iIRQuery.bindValue(strBLogo, apImgList->at(bLogoInsert)); |
|
414 iIRQuery.bindValue(strSLogo, apImgList->at(sLogoInsert)); |
|
415 if(false == iIRQuery.exec()) |
|
416 { |
|
417 ret = IRDB_ERR_UPDATE; |
|
418 } |
|
419 } |
|
420 |
|
421 } |
|
422 } |
|
423 else /*since there is not uid, we insert directly*/ |
|
424 { |
|
425 if(NULL == apImgList) |
|
426 { |
|
427 if(false == iIRQuery.exec(aInsSqlStr)) |
|
428 { |
|
429 ret = IRDB_ERR_INSERT; |
|
430 } |
|
431 } |
|
432 else |
|
433 { |
|
434 iIRQuery.prepare(aInsSqlStr); |
|
435 iIRQuery.bindValue(strBLogo, apImgList->at(bLogoInsert)); |
|
436 iIRQuery.bindValue(strSLogo, apImgList->at(sLogoInsert)); |
|
437 if(false == iIRQuery.exec()) |
|
438 { |
|
439 ret = IRDB_ERR_UPDATE; |
|
440 } |
|
441 } |
|
442 } |
|
443 |
|
444 iIRQuery.clear(); |
|
445 m_sqlDB.close(); |
|
446 |
|
447 return ret; |
|
448 } |
|
449 |
|
450 IRDBCHAR IRDB::chgRowSongHistory(const QString& aInsSqlStr, |
|
451 const QString& aUpdSqlStr, |
|
452 const QString& aCondSqlStr, |
|
453 const QString& aSongName, |
|
454 const uint& channelId) |
|
455 { |
|
456 QString strSltRecordFromSongHistory = IRDBSrhRecordCntFrmSongHistory + aCondSqlStr; |
|
457 QString updStr; |
|
458 bool bDone = false; |
|
459 IRDBCHAR ret = IRDB_ERR_NONE; |
|
460 |
|
461 |
|
462 if( (aSongName.isEmpty()) || (0 == channelId) ) |
|
463 { |
|
464 return IRDB_ERR_ILLEGAL; |
|
465 } |
|
466 |
|
467 if(IRDB_CID_EXISTED != isChannelIdExisted(channelId)) |
|
468 { |
|
469 return IRDB_ERR_ILLEGAL; |
|
470 } |
|
471 |
|
472 if(false == openIRDBConnection()) |
|
473 { |
|
474 return IRDB_ERR_OPEN; |
|
475 } |
|
476 |
|
477 QSqlQuery iIRQuery(m_sqlDB); |
|
478 |
|
479 //if update string is true, first excute update string; |
|
480 if(false == aCondSqlStr.isEmpty()) |
|
481 { |
|
482 if(false == iIRQuery.exec(strSltRecordFromSongHistory)) |
|
483 { |
|
484 iIRQuery.clear(); |
|
485 m_sqlDB.close(); |
|
486 return IRDB_ERR_SELECT; |
|
487 } |
|
488 |
|
489 iIRQuery.next(); |
|
490 if (0 != iIRQuery.value(0).toInt()) |
|
491 { |
|
492 /* |
|
493 * if row can be selected from songHistory Table, |
|
494 * it is a stirng which is to be updated. |
|
495 */ |
|
496 bDone = true; |
|
497 updStr = aUpdSqlStr + aCondSqlStr; |
|
498 if( false == iIRQuery.exec(updStr) ) |
|
499 { |
|
500 ret = IRDB_ERR_INSERT; |
|
501 } |
|
502 } |
|
503 } |
|
504 |
|
505 if(false == bDone) |
|
506 { |
|
507 if(false == iIRQuery.exec(aInsSqlStr)) |
|
508 { |
|
509 ret = IRDB_ERR_UPDATE; |
|
510 } |
|
511 } |
|
512 |
|
513 iIRQuery.clear(); |
|
514 m_sqlDB.close(); |
|
515 |
|
516 return ret; |
|
517 |
|
518 } |
|
519 |
|
520 |
|
521 IRDBCHAR IRDB::isChannelIdExisted(uint channelId) |
|
522 { |
|
523 QString strIsCIDExist = IRDBSrhCIdCntFrmChannelInfo + QString::number(channelId); |
|
524 IRDBCHAR ret = IRDB_ERR_NONE; |
|
525 |
|
526 if(0 == channelId) |
|
527 { |
|
528 return IRDB_CID_UNEXISTED; |
|
529 } |
|
530 |
|
531 if(false == openIRDBConnection()) |
|
532 { |
|
533 return IRDB_ERR_OPEN; |
|
534 } |
|
535 |
|
536 QSqlQuery iIRQuery(m_sqlDB); |
|
537 |
|
538 //iIRQuery.clear(); |
|
539 if(false == iIRQuery.exec(strIsCIDExist)) |
|
540 { |
|
541 ret = IRDB_ERR_SELECT; |
|
542 } |
|
543 else |
|
544 { |
|
545 iIRQuery.next(); |
|
546 if(0 != iIRQuery.value(0).toInt()) |
|
547 { |
|
548 ret = IRDB_CID_EXISTED; |
|
549 } |
|
550 } |
|
551 |
|
552 iIRQuery.clear(); |
|
553 m_sqlDB.close(); |
|
554 |
|
555 return ret ; |
|
556 } |
|
557 |
|
558 IRDBCHAR IRDB::chgRowFavorites(const QString& aInsSqlStr, const QString& aUpdSqlStr, const uint& channelId, bool& bNewRow) |
|
559 { |
|
560 IRDBCHAR ret = IRDB_ERR_NONE; |
|
561 QString strIsCIDExist = IRDBSrhCIdCntFrmChannelInfo + QString::number(channelId) +";"; |
|
562 QString strIsCIDFavorites = IRDBSrhCIdCntFrmFavorites + QString::number(channelId) +";"; |
|
563 QString updSqlStr = aUpdSqlStr + "where channelid = " + QString::number(channelId); |
|
564 |
|
565 if(false == openIRDBConnection()) |
|
566 { |
|
567 return IRDB_ERR_OPEN; |
|
568 } |
|
569 |
|
570 QSqlQuery iIRQuery(m_sqlDB); |
|
571 |
|
572 if(false == iIRQuery.exec(strIsCIDExist)) |
|
573 { |
|
574 iIRQuery.clear(); |
|
575 m_sqlDB.close(); |
|
576 ret = IRDB_ERR_SELECT; |
|
577 } |
|
578 |
|
579 if(ret != IRDB_ERR_NONE) |
|
580 { |
|
581 return ret; |
|
582 } |
|
583 |
|
584 iIRQuery.next(); |
|
585 |
|
586 /*if channelID existed in channelInfo*/ |
|
587 if (0 != iIRQuery.value(0).toInt()) |
|
588 { |
|
589 if(false != iIRQuery.exec(strIsCIDFavorites)) |
|
590 { |
|
591 //if this channelId is in Favorties, this is a row need to update; |
|
592 iIRQuery.next(); |
|
593 if(0 != iIRQuery.value(0).toInt()) |
|
594 { |
|
595 bNewRow = false; |
|
596 |
|
597 if( false == iIRQuery.exec(updSqlStr) ) |
|
598 { |
|
599 ret = IRDB_ERR_UPDATE; |
|
600 } |
|
601 } |
|
602 else //this is a new row which need to be insert favorites; |
|
603 { |
|
604 bNewRow = true; |
|
605 |
|
606 if( false == iIRQuery.exec(aInsSqlStr) ) |
|
607 { |
|
608 ret = IRDB_ERR_INSERT; |
|
609 } |
|
610 } |
|
611 } |
|
612 else |
|
613 { |
|
614 ret = IRDB_ERR_SELECT; |
|
615 } |
|
616 } |
|
617 else /*channelId don't be recorded in channelinfo, illegal data*/ |
|
618 { |
|
619 ret = IRDB_ERR_ILLEGAL; |
|
620 } |
|
621 |
|
622 iIRQuery.clear(); |
|
623 m_sqlDB.close(); |
|
624 |
|
625 return ret ; |
|
626 } |
|
627 |
|
628 |
|
629 IRDBCHAR IRDB::deleteRow(const QString& aDltSqlStr) |
|
630 { |
|
631 IRDBCHAR ret = IRDB_ERR_NONE; |
|
632 |
|
633 /*rows in channelInfo and img can't be removed directly.*/ |
|
634 if( 0 == QString::compare("channelInfo", aDltSqlStr, Qt::CaseInsensitive) ) |
|
635 { |
|
636 return IRDB_ERR_DELETE; |
|
637 } |
|
638 |
|
639 if( 0 == QString::compare("img", aDltSqlStr, Qt::CaseInsensitive) ) |
|
640 { |
|
641 return IRDB_ERR_DELETE; |
|
642 } |
|
643 |
|
644 if(false == openIRDBConnection()) |
|
645 { |
|
646 return IRDB_ERR_OPEN; |
|
647 } |
|
648 |
|
649 QSqlQuery iIRQuery(m_sqlDB); |
|
650 if(false == iIRQuery.exec(aDltSqlStr)) |
|
651 { |
|
652 ret = IRDB_ERR_DELETE; |
|
653 } |
|
654 |
|
655 iIRQuery.clear(); |
|
656 m_sqlDB.close(); |
|
657 |
|
658 return ret; |
|
659 } |
|
660 |
|
661 IRDBCHAR IRDB::selectRowIRDB(IRDBWrapper* const apWrapper, const QString& aCondStr, bool bIsSrhCid, |
|
662 QList<uint>* pQListCIDSet, QList<QVariant*>* pIRDataSet) |
|
663 { |
|
664 QString strSrhCid; |
|
665 IRDBCHAR ret = IRDB_ERR_NONE; |
|
666 |
|
667 if(true == bIsSrhCid) |
|
668 { |
|
669 strSrhCid = IRDBSrhCIdFrmView + aCondStr; |
|
670 } |
|
671 else |
|
672 { |
|
673 strSrhCid = IRDBSrhAllFrmView + aCondStr; |
|
674 } |
|
675 |
|
676 if(false == openIRDBConnection()) |
|
677 { |
|
678 return IRDB_ERR_OPEN; |
|
679 } |
|
680 |
|
681 QSqlQuery iIRQuery(m_sqlDB); |
|
682 |
|
683 |
|
684 if(false == iIRQuery.exec(strSrhCid)) |
|
685 { |
|
686 iIRQuery.clear(); |
|
687 m_sqlDB.close(); |
|
688 ret = IRDB_ERR_SELECT; |
|
689 } |
|
690 |
|
691 if(ret != IRDB_ERR_NONE) |
|
692 { |
|
693 return ret; |
|
694 } |
|
695 |
|
696 if(!apWrapper) |
|
697 { |
|
698 iIRQuery.clear(); |
|
699 m_sqlDB.close(); |
|
700 ret = IRDB_ERR_SELECT; |
|
701 } |
|
702 |
|
703 if(true == bIsSrhCid) |
|
704 { |
|
705 if(false == apWrapper->loadDataOfChannelIdCB(iIRQuery, pQListCIDSet)) |
|
706 { |
|
707 ret = IRDB_ERR_WRAPPER; |
|
708 } |
|
709 } |
|
710 else |
|
711 { |
|
712 if(false == apWrapper->loadDataOfIRDBCB(iIRQuery, pIRDataSet)) |
|
713 { |
|
714 ret = IRDB_ERR_WRAPPER; |
|
715 } |
|
716 } |
|
717 |
|
718 iIRQuery.clear(); |
|
719 m_sqlDB.close(); |
|
720 |
|
721 return ret; |
|
722 } |
|
723 |
|
724 IRDBCHAR IRDB::selectRow(IRDBWrapper* const apWrapper, const QString& aCondStr, QList<QVariant*>* pDataSet) |
|
725 { |
|
726 IRDBCHAR ret = IRDB_ERR_NONE; |
|
727 |
|
728 if(!apWrapper) |
|
729 { |
|
730 ret = IRDB_ERR_SELECT; |
|
731 } |
|
732 |
|
733 if(false == openIRDBConnection()) |
|
734 { |
|
735 return IRDB_ERR_OPEN; |
|
736 } |
|
737 |
|
738 QSqlQuery iIRQuery(m_sqlDB); |
|
739 |
|
740 |
|
741 if(false == iIRQuery.exec(aCondStr)) |
|
742 { |
|
743 iIRQuery.clear(); |
|
744 m_sqlDB.close(); |
|
745 ret = IRDB_ERR_SELECT; |
|
746 } |
|
747 |
|
748 if(ret != IRDB_ERR_NONE) |
|
749 { |
|
750 return ret; |
|
751 } |
|
752 |
|
753 if(false == apWrapper->getIRTableCB(iIRQuery, pDataSet)) |
|
754 { |
|
755 ret = IRDB_ERR_WRAPPER; |
|
756 } |
|
757 |
|
758 |
|
759 iIRQuery.clear(); |
|
760 m_sqlDB.close(); |
|
761 |
|
762 return ret; |
|
763 } |
|
764 |
|
765 /* |
|
766 * INSERT/DELETE row, in urlInfo table; |
|
767 * step: |
|
768 * 1/ first delete all the rows whose channelID equals to input channelId, |
|
769 * 2/ insert all the rows belonged to one channelId to urlinfo |
|
770 */ |
|
771 IRDBCHAR IRDB::resetUrlInfo(const QStringList& aInsSqlList, const uint& channelId) |
|
772 { |
|
773 QString strDltRow = IRDBDltRowFrmUrlInfoByCId + QString::number(channelId) +";"; |
|
774 QString strIsCIDExist = IRDBSrhCIdCntFrmChannelInfo + QString::number(channelId) +";"; |
|
775 |
|
776 IRDBCHAR ret = IRDB_ERR_NONE; |
|
777 |
|
778 if(false == openIRDBConnection()) |
|
779 { |
|
780 return IRDB_ERR_OPEN; |
|
781 } |
|
782 |
|
783 QSqlQuery iIRQuery(m_sqlDB); |
|
784 |
|
785 if(false == iIRQuery.exec(strIsCIDExist)) |
|
786 { |
|
787 iIRQuery.clear(); |
|
788 m_sqlDB.close(); |
|
789 return IRDB_ERR_SELECT; |
|
790 } |
|
791 |
|
792 iIRQuery.next(); |
|
793 |
|
794 /*if channelID existed in channelInfo*/ |
|
795 if (0 != iIRQuery.value(0).toInt()) |
|
796 { |
|
797 if(false == iIRQuery.exec(strDltRow)) |
|
798 { |
|
799 ret = IRDB_ERR_DELETE; |
|
800 } |
|
801 else |
|
802 { |
|
803 for(int i = 0; i<aInsSqlList.size();i++) |
|
804 { |
|
805 if(false == iIRQuery.exec(aInsSqlList.at(i)) ) |
|
806 { |
|
807 ret = IRDB_ERR_INSERT; |
|
808 break; |
|
809 } |
|
810 } |
|
811 } |
|
812 } |
|
813 else |
|
814 { |
|
815 ret = IRDB_ERR_ILLEGAL; |
|
816 } |
|
817 |
|
818 iIRQuery.clear(); |
|
819 m_sqlDB.close(); |
|
820 |
|
821 return ret; |
|
822 } |
|
823 |
|
824 /* |
|
825 * only update imgUrl, imgLocalFile in img table; |
|
826 */ |
|
827 IRDBCHAR IRDB::updRowImg(const uint& channelId, const QString& updSqlStr, QList<QByteArray>* apImgList) |
|
828 { |
|
829 QString strIsCIDExist = IRDBSrhCIdCntFrmChannelInfo + QString::number(channelId) +";"; |
|
830 IRDBCHAR ret = IRDB_ERR_NONE; |
|
831 |
|
832 if(false == openIRDBConnection()) |
|
833 { |
|
834 return IRDB_ERR_OPEN; |
|
835 } |
|
836 |
|
837 QSqlQuery iIRQuery(m_sqlDB); |
|
838 |
|
839 if(false == iIRQuery.exec(strIsCIDExist)) |
|
840 { |
|
841 iIRQuery.clear(); |
|
842 m_sqlDB.close(); |
|
843 return IRDB_ERR_SELECT; |
|
844 } |
|
845 |
|
846 iIRQuery.next(); |
|
847 |
|
848 /*if channelID existed in channelInfo*/ |
|
849 if (0 != iIRQuery.value(0).toInt()) |
|
850 { |
|
851 if(NULL == apImgList) |
|
852 { |
|
853 if(false == iIRQuery.exec(updSqlStr)) |
|
854 { |
|
855 ret = IRDB_ERR_UPDATE; |
|
856 } |
|
857 } |
|
858 else |
|
859 { |
|
860 iIRQuery.prepare(updSqlStr); |
|
861 iIRQuery.bindValue(strBLogo, apImgList->at(bLogoInsert)); |
|
862 iIRQuery.bindValue(strSLogo, apImgList->at(sLogoInsert)); |
|
863 if(false == iIRQuery.exec()) |
|
864 { |
|
865 ret = IRDB_ERR_UPDATE; |
|
866 } |
|
867 } |
|
868 } |
|
869 else |
|
870 { |
|
871 ret = IRDB_ERR_ILLEGAL; |
|
872 } |
|
873 |
|
874 iIRQuery.clear(); |
|
875 m_sqlDB.close(); |
|
876 |
|
877 return ret; |
|
878 |
|
879 } |
|
880 |
|
881 IRDBCHAR IRDB::updRowChannelHistory(const uint& aChannelId) |
|
882 { |
|
883 QString strIsCIDExist = IRDBSrhCIdCntFrmChannelHistory + QString::number(aChannelId) + ";"; |
|
884 QString updStr = "update channelHistory set channelId = " + QString::number(aChannelId) + "where channelId = " + QString::number(aChannelId) +";"; |
|
885 IRDBCHAR ret = IRDB_ERR_NONE; |
|
886 |
|
887 if(false == openIRDBConnection()) |
|
888 { |
|
889 return IRDB_ERR_OPEN; |
|
890 } |
|
891 |
|
892 //if channelId isn't in channelHistory, return; |
|
893 QSqlQuery iIRQuery(m_sqlDB); |
|
894 |
|
895 if(false == iIRQuery.exec(strIsCIDExist)) |
|
896 { |
|
897 iIRQuery.clear(); |
|
898 m_sqlDB.close(); |
|
899 return IRDB_ERR_SELECT; |
|
900 } |
|
901 |
|
902 iIRQuery.next(); |
|
903 |
|
904 /*if channelID existed in channelHistory*/ |
|
905 if (0 != iIRQuery.value(0).toInt()) |
|
906 { |
|
907 if(false == iIRQuery.exec(updStr)) |
|
908 { |
|
909 ret = IRDB_ERR_DELETE; |
|
910 } |
|
911 } |
|
912 else |
|
913 { |
|
914 ret = IRDB_ERR_ILLEGAL; |
|
915 } |
|
916 |
|
917 return ret; |
|
918 } |
|
919 |
|
920 |