13 * |
13 * |
14 * Description: |
14 * Description: |
15 * |
15 * |
16 */ |
16 */ |
17 |
17 |
18 #include <QFile> |
|
19 #include <QSqlQuery> |
|
20 #include <QSqlRecord> |
|
21 #include <QSqlDatabase> |
|
22 #include <QVariant> |
18 #include <QVariant> |
23 |
|
24 |
19 |
|
20 #include "searchrltwrapper.h" |
25 #include "irsearchresultdb.h" |
21 #include "irsearchresultdb.h" |
26 #include "irqisdsdatastructure.h" |
22 #include "irqisdsdatastructure.h" |
27 |
23 |
|
24 const int KTruncateSize = 255; |
28 |
25 |
29 IRSearchResultDB::IRSearchResultDB() |
26 IRSearchResultDB::IRSearchResultDB() : iSearchRltWrapper(NULL) |
30 { |
27 { |
31 createDBConnection(); |
28 iSearchRltWrapper = new searchRltWrapper(); |
32 } |
29 } |
33 |
30 |
34 IRSearchResultDB::~IRSearchResultDB() |
31 IRSearchResultDB::~IRSearchResultDB() |
35 { |
32 { |
36 if (iDB->isOpen()) |
33 delete iSearchRltWrapper; |
37 { |
|
38 iDB->close(); |
|
39 } |
|
40 |
|
41 delete iDB; |
|
42 iDB = NULL; |
|
43 } |
34 } |
44 |
35 |
45 |
36 |
46 IRQError IRSearchResultDB::cacheChannelList(QList<IRQChannelItem*> *aChannelList) |
37 IRQError IRSearchResultDB::cacheChannelList(QList<IRQChannelItem*> *aChannelList) |
47 { |
38 { |
50 if( NULL == aChannelList ) |
41 if( NULL == aChannelList ) |
51 { |
42 { |
52 return EIRQErrorBadParameter; |
43 return EIRQErrorBadParameter; |
53 } |
44 } |
54 |
45 |
55 if( iDB->isOpen() ) |
46 if( iSearchRltWrapper ) |
56 { |
47 { |
57 clearCache(); |
48 clearCache(); |
58 |
49 |
|
50 //create column struct of table; |
|
51 columnMap* pColMap = new columnMap(); |
|
52 QString num, name, imageURL, shortDescription; |
|
53 int chnlID; |
|
54 bool initFlag = true; |
59 for(int i=0; i<aChannelList->count(); i++) |
55 for(int i=0; i<aChannelList->count(); i++) |
60 { |
56 { |
61 IRQChannelItem* insertItem = aChannelList->at(i); |
57 IRQChannelItem* insertItem = aChannelList->at(i); |
62 QString name = insertItem->channelName; |
58 name = insertItem->channelName; |
63 int channelID = insertItem->channelID; |
59 chnlID = insertItem->channelID; |
64 QString imageURL = insertItem->imageURL; |
60 num.setNum(chnlID); |
65 QString description = insertItem->shortDescription; |
61 imageURL = insertItem->imageURL; |
|
62 shortDescription = insertItem->shortDescription; |
66 |
63 |
67 //if some data overflows, we just skip it.note that |
64 //if some data overflows, we just skip it.note that |
68 //the VARCHAR is word-based, so here we use the real size |
65 //the VARCHAR is word-based, so here we use the real size |
69 if( name.size()>= 256 || imageURL.size() >= 256 || description.size() >= 256 ) |
66 name.truncate(KTruncateSize); |
70 { |
67 imageURL.truncate(KTruncateSize); |
71 continue; |
68 shortDescription.truncate(KTruncateSize); |
72 } |
69 |
|
70 pColMap->clear(); |
|
71 pColMap->insert(channelId, num); |
|
72 pColMap->insert(channelName, name); |
|
73 pColMap->insert(imgUrl, imageURL); |
|
74 pColMap->insert(shortDesc, shortDescription); |
|
75 |
|
76 iSearchRltWrapper->addSearchRlt(pColMap, initFlag); |
|
77 initFlag = false; |
73 |
78 |
74 QSqlQuery query; |
|
75 bool result; |
|
76 |
|
77 result = query.prepare("INSERT INTO searchresult (name, channelID, imageURL, description) " |
|
78 "VALUES (:name, :channelID, :imageURL, :description)"); |
|
79 query.bindValue(":name", name); |
|
80 query.bindValue(":channelID",channelID); |
|
81 query.bindValue(":imageURL", imageURL); |
|
82 query.bindValue(":description", description); |
|
83 |
|
84 result = query.exec(); |
|
85 if( !result ) |
|
86 { |
|
87 ret = EIRQErrorServiceUnavailable; |
|
88 break; |
|
89 } |
|
90 } |
79 } |
|
80 |
|
81 iSearchRltWrapper->addSearchRltFinished(); |
|
82 delete pColMap; |
91 } |
83 } |
92 else |
84 else |
93 { |
85 { |
94 ret = EIRQErrorGeneral; |
86 ret = EIRQErrorGeneral; |
95 } |
87 } |
97 return ret; |
89 return ret; |
98 } |
90 } |
99 |
91 |
100 QList<IRQChannelItem*> *IRSearchResultDB::getCahcedChannelList() |
92 QList<IRQChannelItem*> *IRSearchResultDB::getCahcedChannelList() |
101 { |
93 { |
102 if( !iDB->isOpen() ) |
94 if( !iSearchRltWrapper ) |
103 { |
95 { |
104 return NULL; |
96 return NULL; |
105 } |
97 } |
106 |
98 |
|
99 QList<QVariant*>* searchRlt = NULL; |
107 QList<IRQChannelItem*> *channelList = new QList<IRQChannelItem*>(); |
100 QList<IRQChannelItem*> *channelList = new QList<IRQChannelItem*>(); |
108 QSqlQuery query("SELECT * FROM searchresult"); |
101 searchRlt = iSearchRltWrapper->getSearchRlt(NULL, NULL); |
109 QSqlRecord rec = query.record(); |
102 |
110 int nameCol = rec.indexOf("name"); |
103 if (NULL == searchRlt) |
111 int channelIDCol = rec.indexOf("channelID"); |
104 { |
112 int imageURLCol = rec.indexOf("imageURL"); |
105 return channelList; |
113 int descriptionCol = rec.indexOf("description"); |
106 } |
114 |
107 |
115 while(query.next()) |
108 for(int i = 0; i < searchRlt->size(); i++) |
116 { |
109 { |
117 IRQChannelItem *oneItem = new IRQChannelItem(); |
110 IRQChannelItem *oneItem = new IRQChannelItem(); |
118 oneItem->channelName = query.value(nameCol).toString(); |
111 oneItem->channelName = (*( searchRlt->at(i) + channelName) ).toString(); |
119 oneItem->channelID = query.value(channelIDCol).toInt(); |
112 oneItem->channelID = (*( searchRlt->at(i) + channelId)).toUInt(); |
120 oneItem->imageURL = query.value(imageURLCol).toString(); |
113 oneItem->imageURL = (*( searchRlt->at(i) + imgUrl) ).toString(); |
121 oneItem->shortDescription = query.value(descriptionCol).toString(); |
114 oneItem->shortDescription = (*( searchRlt->at(i) + shortDesc) ).toString(); |
122 channelList->append(oneItem); |
115 channelList->append(oneItem); |
123 } |
116 } |
124 |
117 |
|
118 while(false == searchRlt->isEmpty()) |
|
119 { |
|
120 delete []searchRlt->takeFirst(); |
|
121 } |
|
122 searchRlt->clear(); |
|
123 delete searchRlt; |
|
124 searchRlt = NULL; |
|
125 |
125 return channelList; |
126 return channelList; |
126 } |
127 } |
127 |
128 |
128 IRQError IRSearchResultDB::clearCache() |
129 IRQError IRSearchResultDB::clearCache() |
129 { |
130 { |
130 IRQError ret = EIRQErrorNone; |
131 IRQError ret = EIRQErrorNone; |
131 if( !iDB->open()) |
132 if( !iSearchRltWrapper) |
132 { |
133 { |
133 ret = EIRQErrorServiceUnavailable; |
134 ret = EIRQErrorServiceUnavailable; |
134 } |
135 } |
135 else |
136 else |
136 { |
137 { |
137 QSqlQuery query("DELETE FROM searchresult"); |
138 bool retval = iSearchRltWrapper->deleteSearchRlt(NULL, NULL); |
138 bool ret = query.exec(); |
139 if (!retval) |
139 if( !ret ) |
|
140 { |
140 { |
141 ret = EIRQErrorServiceUnavailable; |
141 ret = EIRQErrorServiceUnavailable; |
142 } |
142 } |
143 } |
143 } |
144 |
144 |
145 return ret; |
145 return ret; |
146 } |
146 } |
147 |
|
148 void IRSearchResultDB::createDBConnection() |
|
149 { |
|
150 bool created = QFile::exists("searchresult.dat"); |
|
151 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); |
|
152 iDB = new QSqlDatabase(db); |
|
153 |
|
154 iDB->setDatabaseName("searchresult.dat"); |
|
155 |
|
156 if (!iDB->open()) |
|
157 { |
|
158 return; |
|
159 } |
|
160 |
|
161 |
|
162 if (!created) |
|
163 { |
|
164 bool dbResult = false; |
|
165 QSqlQuery query; |
|
166 //note: the VARCHAR is word-based but not byte-based. and 255 |
|
167 //means 255 unicode words. |
|
168 dbResult = query.exec("CREATE TABLE searchresult (" |
|
169 "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
|
170 "name VARCHAR(255) NOT NULL, " |
|
171 "channelID INTEGER, " |
|
172 "imageURL VARCHAR(255), " |
|
173 "description VARCHAR(255) NOT NULL)"); |
|
174 |
|
175 if (!dbResult) |
|
176 { |
|
177 return; |
|
178 } |
|
179 } |
|
180 } |
|