|
1 /* |
|
2 * Copyright (c) 2010 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 * Retrieving maptile path from lookup db implementation |
|
16 * |
|
17 */ |
|
18 |
|
19 #include <bautils.h> |
|
20 #include <maptileservice.h> |
|
21 #include "maptiledblookuptable.h" |
|
22 |
|
23 // select all from |
|
24 _LIT( KSelectAllFrom, "SELECT * FROM " ); |
|
25 |
|
26 // string 'where' |
|
27 _LIT( KStringWhere, " WHERE " ); |
|
28 |
|
29 // string ' = ' |
|
30 _LIT( KStringEqual, " = " ); |
|
31 |
|
32 // string 'And' |
|
33 _LIT( KStringAnd, " AND " ); |
|
34 |
|
35 |
|
36 _LIT(KQueryByMaptileState,"SELECT * FROM cntmaptilelookuptable WHERE cntuid = %d AND ( fetchingstatus = %d OR fetchingstatus = %d )"); |
|
37 |
|
38 // ----------------------------------------------------------------------------- |
|
39 // CLookupMapTileDatabase::CLookupMapTileDatabase() |
|
40 // Default constructor. |
|
41 // ----------------------------------------------------------------------------- |
|
42 // |
|
43 CLookupMapTileDatabase::CLookupMapTileDatabase() |
|
44 { |
|
45 } |
|
46 |
|
47 // ----------------------------------------------------------------------------- |
|
48 // CLookupMapTileDatabase::~CLookupMapTileDatabase() |
|
49 // Destructor. |
|
50 // ----------------------------------------------------------------------------- |
|
51 // |
|
52 CLookupMapTileDatabase::~CLookupMapTileDatabase() |
|
53 { |
|
54 |
|
55 // close the database |
|
56 iItemsDatabase.Close(); |
|
57 |
|
58 // close the file session |
|
59 iFsSession.Close(); |
|
60 } |
|
61 |
|
62 // ----------------------------------------------------------------------------- |
|
63 // CLookupMapTileDatabase::~CLookupMapTileDatabase() |
|
64 // Creates an object of this class and pushes to cleanup stack. |
|
65 // ----------------------------------------------------------------------------- |
|
66 // |
|
67 CLookupMapTileDatabase* CLookupMapTileDatabase::NewLC( const TDesC& aLookupTableName ) |
|
68 { |
|
69 |
|
70 CLookupMapTileDatabase* self = new (ELeave) CLookupMapTileDatabase; |
|
71 CleanupStack::PushL(self); |
|
72 self->ConstructL( aLookupTableName ); |
|
73 return self; |
|
74 } |
|
75 |
|
76 |
|
77 // ----------------------------------------------------------------------------- |
|
78 // CLookupMapTileDatabase::NewL() |
|
79 // Creates an object of this class. |
|
80 // ----------------------------------------------------------------------------- |
|
81 // |
|
82 CLookupMapTileDatabase* CLookupMapTileDatabase::NewL( const TDesC& aLookupTableName ) |
|
83 { |
|
84 CLookupMapTileDatabase* self = CLookupMapTileDatabase::NewLC( aLookupTableName ); |
|
85 CleanupStack::Pop( self ); |
|
86 return self; |
|
87 } |
|
88 |
|
89 |
|
90 // ----------------------------------------------------------------------------- |
|
91 // CLookupMapTileDatabase::ConstructL() |
|
92 // 2nd phase contructor. |
|
93 // ----------------------------------------------------------------------------- |
|
94 // |
|
95 void CLookupMapTileDatabase::ConstructL( const TDesC& aLookupTableName ) |
|
96 { |
|
97 |
|
98 User::LeaveIfError( iFsSession.Connect() ); |
|
99 |
|
100 iDbFileName.Copy( KLookupDbPath ); |
|
101 iDbFileName.Append( aLookupTableName ); |
|
102 |
|
103 iDatabaseExists = EFalse; |
|
104 |
|
105 if( BaflUtils::FileExists( iFsSession, iDbFileName ) ) |
|
106 { |
|
107 // database exists |
|
108 iDatabaseExists = ETrue; |
|
109 } |
|
110 } |
|
111 |
|
112 // ----------------------------------------------------------------------------- |
|
113 // CLookupMapTileDatabase::FindNumberOfAddressL() |
|
114 // find the number of address associated with the aId. |
|
115 // ----------------------------------------------------------------------------- |
|
116 // |
|
117 int CLookupMapTileDatabase::FindNumberOfAddressL( int& aId ) |
|
118 { |
|
119 int count = 0; |
|
120 |
|
121 // Create a query to find the item. |
|
122 TFileName queryBuffer; |
|
123 queryBuffer.Format( KQueryByMaptileState,aId, |
|
124 MapTileService::MapTileFetchingInProgress, |
|
125 MapTileService::MapTileFetchingNetworkError ); |
|
126 |
|
127 TInt ret = iItemsDatabase.Open( iFsSession, iDbFileName ); |
|
128 |
|
129 if( ret != KErrNone ) |
|
130 { |
|
131 //if already opened , close and open again |
|
132 iItemsDatabase.Close(); |
|
133 User::LeaveIfError( iItemsDatabase.Open( iFsSession, iDbFileName ) ); |
|
134 } |
|
135 |
|
136 User::LeaveIfError( iItemsDatabase.Begin() ); |
|
137 // Create a view of the table with the above query. |
|
138 RDbView myView; |
|
139 myView.Prepare( iItemsDatabase, TDbQuery( queryBuffer ) ); |
|
140 CleanupClosePushL( myView ); |
|
141 myView.EvaluateAll(); |
|
142 myView.FirstL(); |
|
143 |
|
144 |
|
145 while (myView.AtRow()) |
|
146 { |
|
147 count++; |
|
148 myView.NextL(); |
|
149 } |
|
150 |
|
151 CleanupStack::PopAndDestroy( &myView ); // myView |
|
152 |
|
153 //Close the database |
|
154 iItemsDatabase.Close(); |
|
155 |
|
156 return count; |
|
157 } |
|
158 |
|
159 // ----------------------------------------------------------------------------- |
|
160 // CLookupMapTileDatabase::FindEntryL() |
|
161 // Finds an entry in the lookup table. |
|
162 // ----------------------------------------------------------------------------- |
|
163 // |
|
164 void CLookupMapTileDatabase::FindEntryL( TLookupItem& aLookupItem ) |
|
165 { |
|
166 |
|
167 // used to check whether entry available or not |
|
168 TBool entryAvailable = EFalse; |
|
169 |
|
170 if ( iDatabaseExists ) |
|
171 { |
|
172 |
|
173 // Create a query to find the item. |
|
174 TFileName queryBuffer; |
|
175 queryBuffer.Copy( KSelectAllFrom ); |
|
176 queryBuffer.Append( KMapTileLookupTable ); |
|
177 queryBuffer.Append( KStringWhere ); |
|
178 queryBuffer.Append( NCntColUid ); |
|
179 queryBuffer.Append( KStringEqual ); |
|
180 queryBuffer.AppendNum( aLookupItem.iUid ); |
|
181 queryBuffer.Append( KStringAnd ); |
|
182 queryBuffer.Append( NColSource ); |
|
183 queryBuffer.Append( KStringEqual ); |
|
184 queryBuffer.AppendNum( aLookupItem.iSource ); |
|
185 |
|
186 TInt ret = iItemsDatabase.Open( iFsSession, iDbFileName ); |
|
187 |
|
188 if( ret != KErrNone ) |
|
189 { |
|
190 //if already opened , close and open again |
|
191 iItemsDatabase.Close(); |
|
192 User::LeaveIfError( iItemsDatabase.Open( iFsSession, iDbFileName ) ); |
|
193 } |
|
194 User::LeaveIfError( iItemsDatabase.Begin() ); |
|
195 // Create a view of the table with the above query. |
|
196 RDbView myView; |
|
197 myView.Prepare( iItemsDatabase, TDbQuery( queryBuffer ) ); |
|
198 CleanupClosePushL( myView ); |
|
199 myView.EvaluateAll(); |
|
200 myView.FirstL(); |
|
201 |
|
202 if( myView.AtRow() ) |
|
203 { |
|
204 // Item found. get the details. |
|
205 myView.GetL(); |
|
206 if( aLookupItem.iUid == myView.ColUint( KColumnUid ) ) |
|
207 { |
|
208 aLookupItem.iFilePath.Copy( myView.ColDes16( KColumnFilePath ) ); |
|
209 aLookupItem.iFetchingStatus = myView.ColUint( KColumnMapTileFetchingStatus ); |
|
210 entryAvailable = ETrue; |
|
211 } |
|
212 } |
|
213 |
|
214 CleanupStack::PopAndDestroy( &myView ); // myView |
|
215 |
|
216 //Close the database |
|
217 iItemsDatabase.Close(); |
|
218 } |
|
219 |
|
220 //No entry found |
|
221 if( !entryAvailable ) |
|
222 { |
|
223 User::Leave( KErrNotFound ); |
|
224 } |
|
225 } |
|
226 |
|
227 // End of file |
|
228 |