persistentstorage/dbms/sdbms/SD_CLI.CPP
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     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 // DBMS client/server session class
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "SD_STD.H"
       
    19 
       
    20 const TInt KTimesToRetryConnection = 2;
       
    21 
       
    22 
       
    23 // Class RDbs
       
    24 
       
    25 /** Returns the version of the DBMS server.
       
    26 
       
    27 @return Version information. */
       
    28 EXPORT_C TVersion RDbs::Version()
       
    29 	{
       
    30 	return TVersion(KDbmsMajorVersion,KDbmsMinorVersion,KDbmsBuildVersion);
       
    31 	}
       
    32 
       
    33 /** Makes a connection with the DBMS server. This function causes the server to 
       
    34 start, if it is not already running.
       
    35 
       
    36 This should be the first function called on an RDbs object after it is created.
       
    37 
       
    38 Once a connection has been established, databases can be opened through the 
       
    39 server.
       
    40 
       
    41 Note that:
       
    42 
       
    43 Threads can make any number of simultaneous connections to the DBMS server.
       
    44 
       
    45 The session must stay open until all DBMS objects opened through the server 
       
    46 have been closed.
       
    47 
       
    48 The session is terminated by calling the Close() member function provided 
       
    49 by the RSessionBase class.
       
    50 
       
    51 @return KErrNone if successful, otherwise another of the system-wide error 
       
    52 codes. */
       
    53 EXPORT_C TInt RDbs::Connect()
       
    54 	{
       
    55 	TInt retry = KTimesToRetryConnection;
       
    56 	for (;;)
       
    57 		{
       
    58 		TInt r=DoConnect();
       
    59 		if (r!=KErrNotFound && r!=KErrServerTerminated)
       
    60 			return r;
       
    61 		if (--retry==0)
       
    62 			return r;
       
    63 		r=Dbs::Start();
       
    64 		if (r!=KErrNone && r!=KErrAlreadyExists)
       
    65 			return r;
       
    66 		}
       
    67 	}
       
    68 
       
    69 /** Marks the start point for checking the number of DBMS objects allocated in 
       
    70 this session.
       
    71 
       
    72 The function takes the current number of allocated DBMS objects as its benchmark 
       
    73 number.
       
    74 
       
    75 A call to this function is normally followed by a later call to ResourceCheck() 
       
    76 which expects that the number of allocated DBMS objects to be the same as 
       
    77 this benchmark number. */
       
    78 EXPORT_C void RDbs::ResourceMark()
       
    79 	{
       
    80 	SessionMessage(EDbsResourceMark);
       
    81 	}
       
    82 
       
    83 /** Checks that the number of DBMS objects allocated in this session is the same 
       
    84 as the benchmark number recorded by an earlier call to ResourceMark().
       
    85 
       
    86 The function raises a CSession 2 panic if the current number of DBMS objects 
       
    87 is not the same as that recorded by an earlier call to ResourceMark(). */
       
    88 EXPORT_C void RDbs::ResourceCheck()
       
    89 	{
       
    90 	SessionMessage(EDbsResourceCheck);
       
    91 	}
       
    92 
       
    93 /** Returns the number of DBMS objects allocated in this session.
       
    94 
       
    95 @return The number of DBMS allocated objects. */
       
    96 EXPORT_C TInt RDbs::ResourceCount()
       
    97 	{
       
    98 	return SessionMessage(EDbsResourceCount);
       
    99 	}
       
   100 
       
   101 EXPORT_C void RDbs::SetHeapFailure(TInt aTAllocFail,TInt aRate)
       
   102 	{
       
   103 	SendReceive(DbsMessage(EDbsSetHeapFailure,KDbsSessionHandle),TIpcArgs(aTAllocFail,aRate));
       
   104 	}
       
   105 
       
   106 TInt RDbs::SessionMessage(TInt aFunction)
       
   107 	{
       
   108 	return SendReceive(DbsMessage(aFunction,KDbsSessionHandle));
       
   109 	}
       
   110 
       
   111 // Create the session
       
   112 TInt RDbs::DoConnect()
       
   113 	{
       
   114 	return CreateSession(KDbsServerName,Version());
       
   115 	}
       
   116 
       
   117 /**
       
   118 Reserves a prederfined amount of disk space on aDrive drive. 
       
   119 At the moment the max possible amount, allowed by the file server, is reserved on aDrive drive.
       
   120 
       
   121 Use this call to ensure that if your "delete records" transaction fails because of "out of
       
   122 disk space" circumstances, you can get an access to the already reserved disk space and 
       
   123 complete your transaction successfully the second time.
       
   124 
       
   125 There is no strong, 100% guarantee, that the reserved disk space will always help the client
       
   126 in "low memory" situations.
       
   127 
       
   128 @param aDriveNo Drive number to reserve space on
       
   129 @param aSpace This parameter is not used at the moment. The caller shall set it to 0.
       
   130 @return KErrNoMemory Out of memory
       
   131         KErrArgument Invalid aDriveNo.
       
   132         KErrInUse The space has already been reserved
       
   133         RFs::ReserveDriveSpace() return value
       
   134 @see RFs::ReserveDriveSpace()
       
   135 */
       
   136 EXPORT_C TInt RDbs::ReserveDriveSpace(TInt aDriveNo, TInt /*aSpace*/)
       
   137 	{
       
   138 	return SendReceive(DbsMessage(EDbsReserveDriveSpace, KDbsSessionHandle), TIpcArgs(aDriveNo));
       
   139 	}
       
   140 
       
   141 /**
       
   142 The method frees the reserved by the DBMS session disk space.
       
   143 
       
   144 @param aDriveNo Drive number, which reserved space has to be freed.
       
   145 @panic In debug mode there will be a panic with the line number as an error code if 
       
   146        there is no reserved disk space for aDrive. 
       
   147 @panic In debug mode there will be a panic with the line number as an error code if 
       
   148        the reserved disk space is granted but not released.
       
   149 */
       
   150 EXPORT_C void RDbs::FreeReservedSpace(TInt aDriveNo)
       
   151     {
       
   152 	(void)SendReceive(DbsMessage(EDbsFreeReservedSpace, KDbsSessionHandle), TIpcArgs(aDriveNo));
       
   153     }
       
   154 
       
   155 /**
       
   156 Grants access to a given area on a given drive for RDbs session.
       
   157 Note this session must have reserved space on this particular drive in order to be 
       
   158 granted access to the reserved area.
       
   159 
       
   160 @param aDriveNo Drive number with a reserved disk space, an access to which is requested.
       
   161 @return KErrArgument Invalid drive or there is no reserved disk space on aDriveNo.
       
   162         KErrInUse An access to the reserved space has already been given.
       
   163         RFs::GetReserveAccess() return values
       
   164 @see RFs::GetReserveAccess()
       
   165 */
       
   166 EXPORT_C TInt RDbs::GetReserveAccess(TInt aDriveNo)
       
   167 	{
       
   168 	return SendReceive(DbsMessage(EDbsReserveGetAccess, KDbsSessionHandle), TIpcArgs(aDriveNo));
       
   169 	}
       
   170 
       
   171 /**
       
   172 Revokes access on a given drive for RDbs session.
       
   173 
       
   174 @param aDriveNo Drive number with a reserved disk space, the access to which has to be released.
       
   175 @return KErrNone.
       
   176 @panic In debug mode there will be a panic with the line number as an error code if 
       
   177        there is no reserved disk space for aDrive. 
       
   178 @panic In debug mode there will be a panic with the line number as an error code if 
       
   179        there is no granted access to the reserved disk space. 
       
   180 */
       
   181 EXPORT_C TInt RDbs::ReleaseReserveAccess(TInt aDriveNo)
       
   182 	{
       
   183 	(void)SendReceive(DbsMessage(EDbsReserveReleaseAccess, KDbsSessionHandle), TIpcArgs(aDriveNo));
       
   184     return KErrNone;
       
   185 	}
       
   186 
       
   187 // Class RDbNamedDatabase
       
   188 
       
   189 
       
   190 /**
       
   191 Opens an existing shared secure or non-secure database.
       
   192 Max allowed database name length (with the extension) is KDbMaxName symbols.
       
   193 
       
   194 In this "client-server" mode the database can be shared with the other clients.
       
   195 
       
   196 For opening a single, non-shareable connection to the database, see RDbNamedDatabase::Open(), which first
       
   197 argument is a RFs reference.
       
   198 
       
   199 @param aDbs A reference to DBMS session instance.
       
   200 @param aDatabase The name of the file that hosts the database. If this is
       
   201                  a secure database, then the format of the name must be:
       
   202                  \<drive\>:\<database file name excluding the path\>.
       
   203                  If this is a non-secure database, then aDatabase has to be the full database file name.
       
   204 @param aFormat Database format string. For shared secure databases the string format is: "SECURE[UID]", 
       
   205 			   where UID is the database security policy UID. "SECURE" keyword is case insensitive.
       
   206 			   For shared non-secure databases, the default parameter value (TPtrC()) can be used.
       
   207 @return KErrNone if successful otherwise one of the system-wide error codes, including:
       
   208 		KErrNotFound - the database is not found;
       
   209 		KErrArgument - bad argument, including null/invaluid uids, the database name includes a path;
       
   210 		KErrPermissionDenied - the caller has not enough rights to do the operation;
       
   211 
       
   212 @capability Note For a secure shared database, the caller must satisfy the read,
       
   213             the write or the schema access policy for the database.
       
   214 
       
   215 @see RDbNamedDatabase::Open(RFs& aFs, const TDesC& aSource, const TDesC& aFormat, TAccess aMode).
       
   216 
       
   217 @publishedAll
       
   218 @released
       
   219 */
       
   220 EXPORT_C TInt RDbNamedDatabase::Open(RDbs& aDbs, const TDesC& aDatabase, const TDesC& aFormat)
       
   221 	{
       
   222 	TRAPD(r,iDatabase=CDbsDatabase::NewL(aDbs,aDatabase,aFormat));
       
   223 	return r;
       
   224 	}
       
   225