|
1 // Copyright (c) 1998-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 #include "UD_STD.H" |
|
17 #include <f32file.h> |
|
18 #include <e32uid.h> |
|
19 |
|
20 // Class CDbContext |
|
21 |
|
22 inline void CDbContext::Open() |
|
23 { |
|
24 ++iRef; |
|
25 } |
|
26 |
|
27 // |
|
28 // Attach an object to this context |
|
29 // |
|
30 void CDbContext::Attach(CDbObject* aObject) |
|
31 { |
|
32 if (aObject && aObject->iContext!=this) |
|
33 { |
|
34 __ASSERT(!aObject->iContext); |
|
35 aObject->iContext=this; |
|
36 Open(); |
|
37 } |
|
38 } |
|
39 |
|
40 // |
|
41 // self destruct when we are no longer referenced |
|
42 // |
|
43 void CDbContext::Close() |
|
44 { |
|
45 __ASSERT(iRef>0); |
|
46 if (--iRef==0) |
|
47 Idle(); |
|
48 } |
|
49 |
|
50 // |
|
51 // default implementation is to delete ourself |
|
52 // |
|
53 void CDbContext::Idle() |
|
54 { |
|
55 delete this; |
|
56 } |
|
57 |
|
58 // Class CDbObject |
|
59 |
|
60 // |
|
61 // Attach aObject to the same context as it's factory |
|
62 // |
|
63 CDbObject* CDbObject::Attach(CDbObject* aObject) |
|
64 { |
|
65 CDbContext* context=iContext; |
|
66 if (context) |
|
67 context->Attach(aObject); |
|
68 return aObject; |
|
69 } |
|
70 |
|
71 // |
|
72 // Library safe destruction of a implementation object |
|
73 // This ensures that all objects from a Driver are deleted BEFORE the library is unloaded |
|
74 // |
|
75 void CDbObject::Destroy(CDbObject* aObject) |
|
76 { |
|
77 if (aObject) |
|
78 { |
|
79 CDbContext* context=aObject->iContext; |
|
80 delete aObject; |
|
81 if (context) |
|
82 context->Close(); |
|
83 } |
|
84 } |
|
85 |
|
86 // |
|
87 // Library safe cleanup-stack function |
|
88 // |
|
89 void CDbObject::PushL() |
|
90 { |
|
91 CleanupStack::PushL(TCleanupItem(TCleanupOperation(Destroy),this)); |
|
92 } |
|
93 |
|
94 |
|
95 // Class CDbSource |
|
96 |
|
97 //SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version of the method. |
|
98 // Encapsulate the 2-phase construction for client access |
|
99 // |
|
100 CDbDatabase* CDbSource::OpenL() |
|
101 { |
|
102 return AuthenticateL(); |
|
103 } |
|
104 |
|
105 // Class RDbNamedDatabase |
|
106 |
|
107 //SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version of the method. |
|
108 LOCAL_C CDbDatabase* OpenDatabaseL(TDbFormat::TOpen aMode, RFs& aFs, const TDesC& aSource, |
|
109 const TDesC& /*aFormat*/) |
|
110 { |
|
111 CDbSource* source=KBuiltinDriver.iFormats[0].OpenL(aFs,aSource,aMode); |
|
112 CleanupStack::PushL(source); |
|
113 CDbDatabase* db=source->OpenL(); |
|
114 CleanupStack::PopAndDestroy(); // source (not needed) |
|
115 return db; |
|
116 } |
|
117 |
|
118 //SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version of the method. |
|
119 //The method may be used in the other cpp files too |
|
120 CDbDatabase* CreateDatabaseL(TDbFormat::TCreate aMode, RFs& aFs, const TDesC& aSource, |
|
121 const TDesC& aFormat) |
|
122 { |
|
123 const TDbFormat& fmt=KBuiltinDriver.iFormats[0]; |
|
124 |
|
125 TUid policyId = KNullUid; |
|
126 TPtrC uidName; |
|
127 |
|
128 ::ExtractUidAndName(aFormat, policyId, uidName); |
|
129 |
|
130 CDbDatabase* db=fmt.CreateL(aFs,aSource,aMode, |
|
131 TUidType(TUid::Uid(fmt.iUid[0]),TUid::Uid(fmt.iUid[1]),policyId)); |
|
132 |
|
133 return db; |
|
134 } |
|
135 |
|
136 |
|
137 /** |
|
138 Creates a new non-secure database. |
|
139 |
|
140 In this "single client" mode, the database cannot be shared with the other clients. |
|
141 The database server is not involved in the operations with the database, the client side |
|
142 database library (edbms.dll) will be used. |
|
143 If the database has to be shared, the following example shows how this may be accomplished: |
|
144 @code |
|
145 RFs fs; |
|
146 TInt err = fs.Connect(); |
|
147 <process the error> |
|
148 _LIT(KDatabaseName, _L("C:\\A.DB")); |
|
149 RDbNamedDatabase db; |
|
150 err = db.Create(fs, KDatabaseName); //Step 1 - create the database using the RFs object |
|
151 <process the error> |
|
152 db.Close(); //Step 2 - close the database |
|
153 RDbs dbs; |
|
154 err = dbs.Connect(); |
|
155 <process the error> |
|
156 err = db.Open(dbs, KDatabaseName); //Step 3 - reopen the database using the RDbs object |
|
157 <process the error> |
|
158 ... |
|
159 @endcode |
|
160 |
|
161 Max allowed database name length (with the extension) is KDbMaxName symbols. |
|
162 |
|
163 For creating a new secure shared database, see RDbNamedDatabase::Create(), which first argument |
|
164 is a RDbs reference. |
|
165 |
|
166 @param aFs Handle to a file server session. |
|
167 @param aSource Database file name. |
|
168 @param aFormat Database format string. It can be omitted in which case the default parameter |
|
169 value (TPtrC()) will be used. |
|
170 @return KErrNone if successful otherwise one of the system-wide error codes, including: |
|
171 KErrAlreadyExists - the database already exists; |
|
172 KErrArgument - bad argument, including null/invaluid uids, the database name includes a null; |
|
173 |
|
174 @see RDbNamedDatabase::Create(RDbs& aDbs, const TDesC& aDatabase, const TDesC& aFormat) |
|
175 |
|
176 @publishedAll |
|
177 @released |
|
178 */ |
|
179 EXPORT_C TInt RDbNamedDatabase::Create(RFs& aFs, const TDesC& aSource, const TDesC& aFormat) |
|
180 { |
|
181 TRAPD(r,iDatabase=CreateDatabaseL(TDbFormat::ECreate,aFs,aSource,aFormat)); |
|
182 return r; |
|
183 } |
|
184 |
|
185 |
|
186 /** |
|
187 Creates a new non-secure database. |
|
188 If a database with the same file name exists, it will be replased. |
|
189 |
|
190 In this "single client" mode, the database cannot be shared with the other clients. |
|
191 The database server is not involved in the operations with the database, the client side |
|
192 database library (edbms.dll) will be used. |
|
193 If the database has to be shared, the following example shows how this may be accomplished: |
|
194 @code |
|
195 RFs fs; |
|
196 TInt err = fs.Connect(); |
|
197 <process the error> |
|
198 _LIT(KDatabaseName, _L("C:\\A.DB")); |
|
199 RDbNamedDatabase db; |
|
200 err = db.Replace(fs, KDatabaseName); //Step 1 - create the database using the RFs object |
|
201 <process the error> |
|
202 db.Close(); //Step 2 - close the database |
|
203 RDbs dbs; |
|
204 err = dbs.Connect(); |
|
205 <process the error> |
|
206 err = db.Open(dbs, KDatabaseName); //Step 3 - reopen the database using the RDbs object |
|
207 <process the error> |
|
208 ... |
|
209 @endcode |
|
210 |
|
211 Max allowed database name length (with the extension) is KDbMaxName symbols. |
|
212 |
|
213 For creating a new secure shared database, see RDbNamedDatabase::Create(), which first argument |
|
214 is a RDbs reference. |
|
215 |
|
216 @param aFs Handle to a file server session. |
|
217 @param aSource Database name. The name format is: <drive>:<path><name>.<ext> |
|
218 @param aFormat Database format string. It can be omitted in which case the default parameter |
|
219 value (TPtrC()) will be used. |
|
220 @return KErrNone if successful otherwise one of the system-wide error codes, including: |
|
221 KErrArgument - bad argument, including null/invaluid uids, the database name includes a null; |
|
222 |
|
223 @see RDbNamedDatabase::Create(RDbs& aDbs, const TDesC& aDatabase, const TDesC& aFormat) |
|
224 |
|
225 @publishedAll |
|
226 @released |
|
227 */ |
|
228 EXPORT_C TInt RDbNamedDatabase::Replace(RFs& aFs, const TDesC& aSource, const TDesC& aFormat) |
|
229 { |
|
230 TRAPD(r,iDatabase=CreateDatabaseL(TDbFormat::EReplace,aFs,aSource,aFormat)); |
|
231 return r; |
|
232 } |
|
233 |
|
234 /** |
|
235 Opens an existing non-secure database. |
|
236 |
|
237 In this "single client" mode, the database cannot be shared with the other clients. |
|
238 The database server is not involved in the operations with the database, the client side |
|
239 database library (edbms.dll) will be used. |
|
240 |
|
241 For opening a new secure shared database, see RDbNamedDatabase::Open(), which first argument |
|
242 is a RDbs reference. |
|
243 |
|
244 @param aFs Handle to a file server session. |
|
245 @param aSource Database name. The name format is: <drive>:<path><name>.<ext> |
|
246 @param aFormat Database format string. It can be omitted in which case the default parameter |
|
247 value (TPtrC()) will be used. |
|
248 @param aMode The mode in which the database is to be accessed. The mode is |
|
249 defined by the TAccess type. |
|
250 @return KErrNone if successful otherwise one of the system-wide error codes, including: |
|
251 KErrNotFound - the database is not found; |
|
252 KErrPathNotFound - the path of database is not found |
|
253 KErrNotSupported - the format is not supported. |
|
254 KErrArgument - bad argument,including null/invaluid uids,the database name is null; |
|
255 KErrPermissionDenied - the caller has not enough rights to do the operation; |
|
256 |
|
257 @see RDbNamedDatabase::Open(RDbs& aDbs, const TDesC& aDatabase, const TDesC& aFormat) |
|
258 |
|
259 @publishedAll |
|
260 @released |
|
261 */ |
|
262 EXPORT_C TInt RDbNamedDatabase::Open(RFs& aFs, const TDesC& aSource, const TDesC& aFormat, |
|
263 TAccess aMode) |
|
264 { |
|
265 TRAPD(r,iDatabase=OpenDatabaseL(TDbFormat::TOpen(aMode),aFs,aSource,aFormat)); |
|
266 return r; |
|
267 } |
|
268 |
|
269 |
|
270 CDbNotifier* CDbSource::NotifierL() |
|
271 { |
|
272 return AttachContext(this,OpenNotifierL()); |
|
273 } |