|
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 <QFile> |
|
19 #include <QSqlQuery> |
|
20 #include <QSqlRecord> |
|
21 #include <QSqlDatabase> |
|
22 #include <QVariant> |
|
23 |
|
24 |
|
25 #include "irsearchresultdb.h" |
|
26 #include "irqisdsdatastructure.h" |
|
27 |
|
28 |
|
29 IRSearchResultDB::IRSearchResultDB() |
|
30 { |
|
31 createDBConnection(); |
|
32 } |
|
33 |
|
34 IRSearchResultDB::~IRSearchResultDB() |
|
35 { |
|
36 if (iDB->isOpen()) |
|
37 { |
|
38 iDB->close(); |
|
39 } |
|
40 |
|
41 delete iDB; |
|
42 iDB = NULL; |
|
43 } |
|
44 |
|
45 |
|
46 IRQError IRSearchResultDB::cacheChannelList(QList<IRQChannelItem*> *aChannelList) |
|
47 { |
|
48 IRQError ret = EIRQErrorNone; |
|
49 |
|
50 if( NULL == aChannelList ) |
|
51 { |
|
52 return EIRQErrorBadParameter; |
|
53 } |
|
54 |
|
55 if( iDB->isOpen() ) |
|
56 { |
|
57 clearCache(); |
|
58 |
|
59 for(int i=0; i<aChannelList->count(); i++) |
|
60 { |
|
61 IRQChannelItem* insertItem = aChannelList->at(i); |
|
62 QString name = insertItem->channelName; |
|
63 int channelID = insertItem->channelID; |
|
64 QString imageURL = insertItem->imageURL; |
|
65 QString description = insertItem->shortDescription; |
|
66 |
|
67 QSqlQuery query; |
|
68 bool result; |
|
69 |
|
70 result = query.prepare("INSERT INTO searchresult (name, channelID, imageURL, description) " |
|
71 "VALUES (:name, :channelID, :imageURL, :description)"); |
|
72 query.bindValue(":name", name); |
|
73 query.bindValue(":channelID",channelID); |
|
74 query.bindValue(":imageURL", imageURL); |
|
75 query.bindValue(":description", description); |
|
76 |
|
77 result = query.exec(); |
|
78 if( !result ) |
|
79 { |
|
80 ret = EIRQErrorServiceUnavailable; |
|
81 break; |
|
82 } |
|
83 } |
|
84 } |
|
85 else |
|
86 { |
|
87 ret = EIRQErrorGeneral; |
|
88 } |
|
89 |
|
90 return ret; |
|
91 } |
|
92 |
|
93 QList<IRQChannelItem*> *IRSearchResultDB::getCahcedChannelList() |
|
94 { |
|
95 if( !iDB->isOpen() ) |
|
96 { |
|
97 return NULL; |
|
98 } |
|
99 |
|
100 QList<IRQChannelItem*> *channelList = new QList<IRQChannelItem*>(); |
|
101 QSqlQuery query("SELECT * FROM searchresult"); |
|
102 QSqlRecord rec = query.record(); |
|
103 int nameCol = rec.indexOf("name"); |
|
104 int channelIDCol = rec.indexOf("channelID"); |
|
105 int imageURLCol = rec.indexOf("imageURL"); |
|
106 int descriptionCol = rec.indexOf("description"); |
|
107 |
|
108 while(query.next()) |
|
109 { |
|
110 IRQChannelItem *oneItem = new IRQChannelItem(); |
|
111 oneItem->channelName = query.value(nameCol).toString(); |
|
112 oneItem->channelID = query.value(channelIDCol).toInt(); |
|
113 oneItem->imageURL = query.value(imageURLCol).toString(); |
|
114 oneItem->shortDescription = query.value(descriptionCol).toString(); |
|
115 channelList->append(oneItem); |
|
116 } |
|
117 |
|
118 return channelList; |
|
119 } |
|
120 |
|
121 IRQError IRSearchResultDB::clearCache() |
|
122 { |
|
123 IRQError ret = EIRQErrorNone; |
|
124 if( !iDB->open()) |
|
125 { |
|
126 ret = EIRQErrorServiceUnavailable; |
|
127 } |
|
128 else |
|
129 { |
|
130 QSqlQuery query("DELETE FROM searchresult"); |
|
131 bool ret = query.exec(); |
|
132 if( !ret ) |
|
133 { |
|
134 ret = EIRQErrorServiceUnavailable; |
|
135 } |
|
136 } |
|
137 |
|
138 return ret; |
|
139 } |
|
140 |
|
141 void IRSearchResultDB::createDBConnection() |
|
142 { |
|
143 bool created = QFile::exists("searchresult.dat"); |
|
144 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); |
|
145 iDB = new QSqlDatabase(db); |
|
146 |
|
147 iDB->setDatabaseName("searchresult.dat"); |
|
148 |
|
149 if (!iDB->open()) |
|
150 { |
|
151 return; |
|
152 } |
|
153 |
|
154 |
|
155 if (!created) |
|
156 { |
|
157 bool dbResult = false; |
|
158 QSqlQuery query; |
|
159 dbResult = query.exec("CREATE TABLE searchresult (" |
|
160 "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
|
161 "name VARCHAR(255) NOT NULL, " |
|
162 "channelID INTEGER, " |
|
163 "imageURL VARCHAR(255), " |
|
164 "description VARCHAR(255) NOT NULL)"); |
|
165 |
|
166 if (!dbResult) |
|
167 { |
|
168 return; |
|
169 } |
|
170 } |
|
171 } |