|
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 /** |
|
17 @file |
|
18 @internalComponent |
|
19 @released |
|
20 */ |
|
21 |
|
22 #include <e32test.h> |
|
23 #include <s32mem.h> |
|
24 #include <badesca.h> //For CDesCArray |
|
25 #include <cntstd.h> |
|
26 #include "dbdbmsdumper.h" |
|
27 #include "dbhtmltags.h" |
|
28 #include <CoreAppstest\TestServer.h> |
|
29 |
|
30 _LIT(KErrorMessage, "DBDbmsDumper Failed"); |
|
31 const TInt KMaxPathLength = 100; |
|
32 const TInt KBufferLength = 80; |
|
33 |
|
34 /** |
|
35 Class destructor |
|
36 */ |
|
37 CDbDbmsDumper::~CDbDbmsDumper() |
|
38 { |
|
39 delete iBuffer; |
|
40 delete iTableNames; |
|
41 iFile.Close(); |
|
42 iDatabase.Close(); |
|
43 iDbsSession.Close(); |
|
44 iFsSession.Close(); |
|
45 } |
|
46 |
|
47 /** |
|
48 Second phase constructor for CDbDbmsDumper |
|
49 |
|
50 @param aDbName name of the database to be dumped. |
|
51 @param aOutputDir directory where html file will be generated |
|
52 */ |
|
53 void CDbDbmsDumper::ConstructL(const TDesC& aDbName, const TDesC& aOutputDir) |
|
54 { |
|
55 // Output File Name |
|
56 _LIT(KOutputFile,"DbDump.html"); |
|
57 TBuf<KMaxPathLength> path(aOutputDir); |
|
58 path.Append(KOutputFile); |
|
59 |
|
60 User::LeaveIfError(iFsSession.Connect()); |
|
61 User::LeaveIfError(iDbsSession.Connect()); |
|
62 |
|
63 // Copy the Contacts database from the contacts server private directory to |
|
64 // the DBMS private directory |
|
65 RPIMTestServer serv; |
|
66 User::LeaveIfError(serv.Connect()); |
|
67 |
|
68 _LIT(KDBContactsPrivFile, "?:\\private\\10003a73\\Contacts.cdb"); |
|
69 _LIT(KDBDBMsPrivFile, "?:\\private\\100012a5\\DBS_100065FF_Contacts.cdb"); |
|
70 |
|
71 TBuf<sizeof(KDBContactsPrivFile)> contactsDBPrivFilePath(KDBContactsPrivFile); |
|
72 TBuf<sizeof(KDBDBMsPrivFile)> dBMsPrivFilePath(KDBDBMsPrivFile); |
|
73 |
|
74 contactsDBPrivFilePath[0] = (TUint8) RFs::GetSystemDriveChar(); |
|
75 dBMsPrivFilePath[0] = (TUint8) RFs::GetSystemDriveChar(); |
|
76 |
|
77 serv.CopyFileL(contactsDBPrivFilePath, dBMsPrivFilePath); |
|
78 serv.Close(); |
|
79 |
|
80 _LIT(KCntDbSecureFormat,"secure[100065FF]"); |
|
81 |
|
82 // Contact databases are in DBMS private directory |
|
83 User::LeaveIfError(iDatabase.Open(iDbsSession, aDbName, KCntDbSecureFormat)); |
|
84 User::LeaveIfError(iFile.Replace(iFsSession, path, EFileWrite) ); |
|
85 |
|
86 iBuffer = HBufC8::NewL(KBufferLength); |
|
87 iTableNames = iDatabase.TableNamesL(); |
|
88 } |
|
89 |
|
90 /** |
|
91 Return a concrete CDbDbmsDumper object |
|
92 |
|
93 @param aDbName name of the database to be dumped. |
|
94 @param aOutputDir directory where html file will be generated |
|
95 */ |
|
96 CDbDbmsDumper* CDbDbmsDumper::NewLC(const TDesC& aDbName, const TDesC& aOutputDir) |
|
97 { |
|
98 CDbDbmsDumper* self = new (ELeave) CDbDbmsDumper(); |
|
99 CleanupStack::PushL(self); |
|
100 self->ConstructL(aDbName, aOutputDir); |
|
101 return self; |
|
102 } |
|
103 |
|
104 /** |
|
105 Returns number of tables that exist in the curent dbms database |
|
106 |
|
107 */ |
|
108 TInt CDbDbmsDumper::NumberOfTables() const |
|
109 { |
|
110 return iTableNames->Count(); |
|
111 } |
|
112 |
|
113 /** |
|
114 Output one table to the html file. Traverse a table and output each row |
|
115 in the html file |
|
116 |
|
117 @param aTableIndex table index in the table array |
|
118 |
|
119 */ |
|
120 void CDbDbmsDumper::OutputTableToFileL(TInt aTableIndex) |
|
121 { |
|
122 iFile.Write(KTable); |
|
123 OutputTableNameToFile(aTableIndex); |
|
124 CDesCArray* colNames = OutputTableColHeadingsToFileLC(aTableIndex); |
|
125 User::LeaveIfError(iDbTable.Open(iDatabase, (*iTableNames)[aTableIndex], RDbRowSet::EReadOnly) ); |
|
126 User::LeaveIfError(iDbTable.SetNoIndex() ); // set rowset order as underlying row order |
|
127 iDbTable.BeginningL(); // set cursor to start of table |
|
128 iDbTable.NextL(); |
|
129 |
|
130 // output the table's rows |
|
131 while (!iDbTable.AtEnd() ) |
|
132 { |
|
133 iFile.Write(KRow); |
|
134 OutputTableColDataToFileL(*colNames); |
|
135 iDbTable.NextL(); |
|
136 iFile.Write(KRowEnd); |
|
137 } |
|
138 |
|
139 iDbTable.Close(); |
|
140 iFile.Write(KTableEnd); |
|
141 CleanupStack::PopAndDestroy(colNames); |
|
142 } |
|
143 |
|
144 /** |
|
145 Output table name to the html file |
|
146 |
|
147 @param aTableIndex table index in the table array |
|
148 |
|
149 */ |
|
150 void CDbDbmsDumper::OutputTableNameToFile(TInt aTableIndex) |
|
151 { |
|
152 iFile.Write(KBold); |
|
153 iBuffer->Des().Copy( (*iTableNames)[aTableIndex]); // write (*iTableNames)[aTableIndex] to the file. |
|
154 iFile.Write(*iBuffer); |
|
155 iFile.Write(KBoldEnd); |
|
156 } |
|
157 |
|
158 /** |
|
159 Outputs table header for the tables created in the html file |
|
160 The header contains the column names |
|
161 |
|
162 @param aTableIndex table index in the table array |
|
163 |
|
164 */ |
|
165 CDesCArray* CDbDbmsDumper::OutputTableColHeadingsToFileLC(TInt aTableIndex) |
|
166 { |
|
167 CDesCArraySeg* colNames = new (ELeave) CDesCArraySeg(8); |
|
168 CleanupStack::PushL(colNames); |
|
169 CDbColSet* colSet = iDatabase.ColSetL( (*iTableNames)[aTableIndex] ); |
|
170 CleanupStack::PushL(colSet); |
|
171 const TInt KMaxCols( (colSet->Count() ) ); |
|
172 TInt counter = 0; |
|
173 iFile.Write(KHdRow); |
|
174 |
|
175 // cols are from 1, to maxCols. not from 0 to maxCols-1. |
|
176 for (counter = 1; counter <= KMaxCols; ++counter) |
|
177 { |
|
178 iFile.Write(KCell); |
|
179 iBuffer->Des().Copy( (*colSet)[counter].iName ); |
|
180 colNames->AppendL((*colSet)[counter].iName); |
|
181 iFile.Write(*iBuffer); |
|
182 iFile.Write(KCellEnd); |
|
183 } |
|
184 |
|
185 iFile.Write(KRowEnd); |
|
186 CleanupStack::PopAndDestroy(colSet); |
|
187 return colNames; |
|
188 } |
|
189 |
|
190 /** |
|
191 Output a table row to the html file |
|
192 |
|
193 @param colNames descriptor array containing rows to be written in the resulting html file |
|
194 |
|
195 */ |
|
196 void CDbDbmsDumper::OutputTableColDataToFileL(const CDesCArray& colNames) |
|
197 { |
|
198 TInt counter = 0; |
|
199 const TInt KNumberOfCols( (iDbTable.ColCount() )); |
|
200 iDbTable.GetL(); // get the row to be output |
|
201 |
|
202 // cols are from 1, to maxCols. not from 0 to KNumberOfCols-1. |
|
203 for (counter = 1; counter <= KNumberOfCols; ++counter) // for each column value in this row |
|
204 { |
|
205 iFile.Write(KCell); |
|
206 |
|
207 if (iDbTable.IsColNull(counter) ) |
|
208 { |
|
209 iBuffer->Des().Format(KNullCol); // write out 'NULL' to the file |
|
210 } |
|
211 else |
|
212 { |
|
213 switch ( iDbTable.ColType(counter) ) |
|
214 { |
|
215 case EDbColInt8: |
|
216 case EDbColInt16: |
|
217 case EDbColInt32: |
|
218 { |
|
219 TInt32 val = iDbTable.ColInt32(counter); |
|
220 iBuffer->Des().Format(KInt32String, val); |
|
221 } |
|
222 break; |
|
223 case EDbColBit: |
|
224 case EDbColUint8: |
|
225 case EDbColUint16: |
|
226 case EDbColUint32: |
|
227 { |
|
228 TUint32 val = iDbTable.ColUint32(counter); |
|
229 iBuffer->Des().Format(KUInt32String, val); |
|
230 } |
|
231 break; |
|
232 case EDbColInt64: |
|
233 { |
|
234 TInt64 val = iDbTable.ColInt64(counter); |
|
235 iBuffer->Des().Format(KUInt64String, I64HIGH(val), I64LOW(val) ); |
|
236 } |
|
237 break; |
|
238 case EDbColReal32: |
|
239 { |
|
240 TReal32 val = iDbTable.ColReal32(counter); |
|
241 iBuffer->Des().Format(KRealString, val); |
|
242 } |
|
243 break; |
|
244 case EDbColReal64: |
|
245 { |
|
246 TReal64 val = iDbTable.ColReal64(counter); |
|
247 iBuffer->Des().Format(KRealString, val); |
|
248 } |
|
249 break; |
|
250 case EDbColDateTime: |
|
251 { |
|
252 TTime val = iDbTable.ColTime(counter); |
|
253 TBuf<80> tmpBuffer; |
|
254 val.FormatL(tmpBuffer, KFormatDate); |
|
255 iBuffer->Des().Copy(tmpBuffer); |
|
256 } |
|
257 break; |
|
258 case EDbColBinary: |
|
259 case EDbColText8: |
|
260 { |
|
261 TPtrC8 val = iDbTable.ColDes8(counter); |
|
262 iBuffer->Des().Copy(val); |
|
263 } |
|
264 break; |
|
265 case EDbColText16: |
|
266 { |
|
267 TPtrC16 val = iDbTable.ColDes16(counter); |
|
268 iBuffer->Des().Copy(val); |
|
269 } |
|
270 break; |
|
271 case EDbColLongText8: |
|
272 iBuffer->Des().Format(KLongText8); |
|
273 break; |
|
274 case EDbColLongText16: |
|
275 LongBinaryL(counter); |
|
276 break; |
|
277 case EDbColLongBinary: |
|
278 if (colNames[counter-1] == KFieldHeaderColName) |
|
279 { |
|
280 FieldHeaderColL(counter); |
|
281 } |
|
282 else |
|
283 { |
|
284 LongBinaryL(counter); |
|
285 } |
|
286 break; |
|
287 default: |
|
288 iBuffer->Des().Format(KUnknownMessage); |
|
289 break; |
|
290 } // switch |
|
291 } // if |
|
292 |
|
293 iFile.Write(*iBuffer); |
|
294 iFile.Write(KCellEnd); |
|
295 |
|
296 } // for |
|
297 } |
|
298 |
|
299 |
|
300 /** |
|
301 Output the header blob to resulting html file |
|
302 |
|
303 @param aFieldIndex column index containing the header values in dbms database |
|
304 |
|
305 */ |
|
306 void CDbDbmsDumper::FieldHeaderColL(TInt aFieldIndex) |
|
307 { |
|
308 TAutoClose<RDbColReadStream> headerBlob; |
|
309 TAutoClose<RStoreReadStream> stream; |
|
310 |
|
311 headerBlob.iObj.OpenL(iDbTable, aFieldIndex); |
|
312 CEmbeddedStore* headerStore=CEmbeddedStore::FromLC(headerBlob.iObj); |
|
313 stream.iObj.OpenLC(*headerStore, headerStore->Root()); |
|
314 |
|
315 DumpStreamL(stream.iObj); |
|
316 |
|
317 CleanupStack::PopAndDestroy(&stream.iObj); //OpenLC |
|
318 CleanupStack::PopAndDestroy(headerStore); |
|
319 } |
|
320 |
|
321 /** |
|
322 Output the values stored in binary blob fields in dbms database |
|
323 |
|
324 @param aFieldIndex column index containing the binary values in dbms database |
|
325 |
|
326 */ |
|
327 void CDbDbmsDumper::LongBinaryL(TInt aFieldIndex) |
|
328 { |
|
329 TAutoClose<RDbColReadStream> rdStream; |
|
330 rdStream.iObj.OpenL(iDbTable,aFieldIndex); |
|
331 |
|
332 DumpStreamL(rdStream.iObj); |
|
333 } |
|
334 |
|
335 /** |
|
336 Utility method. Used to read from read stream and output to html file |
|
337 |
|
338 */ |
|
339 void CDbDbmsDumper::DumpStreamL(RReadStream& stream) |
|
340 { |
|
341 const TInt KStreamSize = stream.Source()->SizeL(); |
|
342 |
|
343 iFile.Write(KDumpFont); |
|
344 |
|
345 _LIT8(KSizeMessage, "size is %d <BR>"); |
|
346 _LIT8(KEightSpaces, " "); |
|
347 _LIT8(KByteFormat, "%02x "); |
|
348 _LIT8(KFormatString, "%S <BR>\r\n"); |
|
349 |
|
350 iBuffer->Des().Format(KSizeMessage, KStreamSize); |
|
351 iFile.Write(*iBuffer); |
|
352 |
|
353 TBuf8<8> str(KEightSpaces); //Reserve 8 symbols |
|
354 |
|
355 TInt counter(0); |
|
356 while (counter < KStreamSize) |
|
357 { |
|
358 for(TInt i=0; i < 8; ++i) |
|
359 { |
|
360 if (counter >= KStreamSize) |
|
361 { |
|
362 break; |
|
363 } |
|
364 //format byte in a printable character |
|
365 TInt8 byte( stream.ReadInt8L() ); |
|
366 iBuffer->Des().Format(KByteFormat, byte); |
|
367 iFile.Write(*iBuffer); |
|
368 |
|
369 str[i] = (byte>32 ? byte : '.'); |
|
370 ++counter; |
|
371 } |
|
372 iBuffer->Des().Format(KFormatString, &str); |
|
373 iFile.Write(*iBuffer); |
|
374 } |
|
375 |
|
376 iFile.Write(KDumpFontEnd); |
|
377 } |
|
378 |
|
379 /** |
|
380 Dump the content of a dbms database in a html file |
|
381 |
|
382 */ |
|
383 void CDbDbmsDumper::OutputDBContentsL() |
|
384 { |
|
385 // Output Title |
|
386 _LIT8(KHdr,"<H2>Contacts Model DB Dump:</H2>"); |
|
387 iFile.Write(KHdr); |
|
388 |
|
389 // Preferences Table Name that will appear first in the output file |
|
390 _LIT(KPrefTableName,"PREFERENCES"); |
|
391 |
|
392 TInt counter = 0; |
|
393 const TInt KNumTables( (iTableNames->Count() )); |
|
394 TBool outputPrefTable( (EFalse) ); |
|
395 |
|
396 // if a Table named Preferences Exists in the DB output it first |
|
397 while (!outputPrefTable && counter < KNumTables) |
|
398 { |
|
399 if ((*iTableNames)[counter++].Compare(KPrefTableName) == 0) |
|
400 { |
|
401 OutputTableToFileL(--counter); |
|
402 outputPrefTable = ETrue; |
|
403 } |
|
404 } |
|
405 |
|
406 // for each table - output all tables except the Preferences Table |
|
407 for (counter = 0; counter < KNumTables; ++counter) |
|
408 { |
|
409 if ((*iTableNames)[counter].Compare(KPrefTableName) != 0) |
|
410 { |
|
411 OutputTableToFileL(counter); |
|
412 } |
|
413 } |
|
414 } |
|
415 |
|
416 void RunDumpL() |
|
417 { |
|
418 // Location of Output Directory. (File will be called DbDump.html). Final "\" required on path. |
|
419 _LIT(KOutputDir, "?:\\"); |
|
420 TBuf<sizeof(KOutputDir)> outputDir(KOutputDir); |
|
421 outputDir[0] = (TUint8) RFs::GetSystemDriveChar(); |
|
422 |
|
423 // Location of Contacts DB |
|
424 _LIT(KDBName, "?:Contacts.cdb"); |
|
425 TBuf<sizeof(KDBName)> fileDBName(KDBName); |
|
426 fileDBName[0] = (TUint8) RFs::GetSystemDriveChar(); |
|
427 |
|
428 // dump dbms database |
|
429 CDbDbmsDumper* dbmsDumper = CDbDbmsDumper::NewLC(fileDBName, outputDir); |
|
430 dbmsDumper->OutputDBContentsL(); |
|
431 CleanupStack::PopAndDestroy(dbmsDumper); |
|
432 } |
|
433 |
|
434 GLDEF_C TInt E32Main() |
|
435 { |
|
436 __UHEAP_MARK; |
|
437 CActiveScheduler* scheduler = new CActiveScheduler; |
|
438 if (scheduler) |
|
439 { |
|
440 CActiveScheduler::Install(scheduler); |
|
441 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
442 if (cleanup) |
|
443 { |
|
444 TRAPD(err,RunDumpL() ); |
|
445 __ASSERT_ALWAYS(err == KErrNone, User::Panic(KErrorMessage,err) ); |
|
446 delete cleanup; |
|
447 } |
|
448 delete scheduler; |
|
449 } |
|
450 __UHEAP_MARKEND; |
|
451 return KErrNone; |
|
452 } |