os/persistentdata/persistentstorage/dbms/pcdbms/udbms/UD_DRVR.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 EXPORT_C TInt RDbNamedDatabase::Create(RDbs &, const TDesC& aDb, const TDesC& aFormat)
   186 	{
   187 	RFs theFs;
   188 	TInt r = theFs.Connect();
   189 	if(r != KErrNone)return r;
   190 	r = Create(theFs, aDb, aFormat);
   191 	theFs.Close();
   192 	return r;
   193 	}
   194 
   195 
   196 /**
   197 Creates a new non-secure database. 
   198 If a database with the same file name exists, it will be replased.
   199 
   200 In this "single client" mode, the database cannot be shared with the other clients.
   201 The database server is not involved in the operations with the database, the client side 
   202 database library (edbms.dll) will be used.
   203 If the database has to be shared, the following example shows how this may be accomplished:
   204 @code
   205 RFs fs;
   206 TInt err = fs.Connect();
   207 <process the error>
   208 _LIT(KDatabaseName, _L("C:\\A.DB"));
   209 RDbNamedDatabase db;
   210 err = db.Replace(fs, KDatabaseName);	//Step 1 - create the database using the RFs object
   211 <process the error>
   212 db.Close();								//Step 2 - close the database
   213 RDbs dbs;
   214 err = dbs.Connect();
   215 <process the error>
   216 err = db.Open(dbs, KDatabaseName);		//Step 3 - reopen the database using the RDbs object
   217 <process the error>
   218 ...
   219 @endcode
   220 
   221 Max allowed database name length (with the extension) is KDbMaxName symbols.
   222 
   223 For creating a new secure shared database, see RDbNamedDatabase::Create(), which first argument 
   224 is a RDbs reference.
   225 
   226 @param aFs Handle to a file server session.
   227 @param aSource Database name. The name format is: <drive>:<path><name>.<ext>
   228 @param aFormat Database format string. It can be omitted in which case the default parameter
   229 			   value (TPtrC()) will be used.
   230 @return KErrNone if successful otherwise one of the system-wide error codes, including:
   231 		KErrArgument - bad argument, including null/invaluid uids, the database name includes a null;
   232 
   233 @see RDbNamedDatabase::Create(RDbs& aDbs, const TDesC& aDatabase, const TDesC& aFormat)
   234 
   235 @publishedAll
   236 @released
   237 */
   238 EXPORT_C TInt RDbNamedDatabase::Replace(RFs& aFs, const TDesC& aSource, const TDesC& aFormat)
   239 	{
   240 	TRAPD(r,iDatabase=CreateDatabaseL(TDbFormat::EReplace,aFs,aSource,aFormat));
   241 	return r;
   242 	}
   243 
   244 /**
   245 Opens an existing non-secure database.
   246 
   247 In this "single client" mode, the database cannot be shared with the other clients.
   248 The database server is not involved in the operations with the database, the client side 
   249 database library (edbms.dll) will be used.
   250 
   251 For opening a new secure shared database, see RDbNamedDatabase::Open(), which first argument 
   252 is a RDbs reference.
   253 
   254 @param aFs Handle to a file server session.
   255 @param aSource Database name. The name format is: <drive>:<path><name>.<ext>
   256 @param aFormat Database format string. It can be omitted in which case the default parameter
   257 			   value (TPtrC()) will be used.
   258 @param aMode The mode in which the database is to be accessed. The mode is 
   259 defined by the TAccess type.
   260 @return KErrNone if successful otherwise one of the system-wide error codes, including:
   261 		KErrNotFound - the database is not found;
   262 		KErrPathNotFound - the path of database is not found 
   263 		KErrNotSupported - the format is not supported.
   264 		KErrArgument - bad argument,including null/invaluid uids,the database name is null;
   265 		KErrPermissionDenied - the caller has not enough rights to do the operation;
   266 
   267 @see RDbNamedDatabase::Open(RDbs& aDbs, const TDesC& aDatabase, const TDesC& aFormat)
   268 
   269 @publishedAll
   270 @released
   271 */
   272 EXPORT_C TInt RDbNamedDatabase::Open(RFs& aFs, const TDesC& aSource, const TDesC& aFormat,
   273                                      TAccess aMode)
   274 	{
   275 	TRAPD(r,iDatabase=OpenDatabaseL(TDbFormat::TOpen(aMode),aFs,aSource,aFormat));
   276 	return r;
   277 	}
   278 
   279 EXPORT_C TInt RDbNamedDatabase::Open(RDbs &, const TDesC& aSource, const TDesC& aFormat)
   280 	{
   281 	RFs theFs;
   282 	TInt r = theFs.Connect();
   283 	if(r != KErrNone)
   284 		return r;
   285 	r = Open(theFs, aSource, aFormat );
   286 	theFs.Close();
   287 	return r;
   288 	}
   289 	
   290 
   291 CDbNotifier* CDbSource::NotifierL()
   292 	{
   293 	return AttachContext(this,OpenNotifierL());
   294 	}