os/persistentdata/persistentstorage/sql/SRC/Server/SqlSrvDatabase.inl
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
/**
sl@0
    17
 
sl@0
    18
 Retrieves a reference to the textual description of the error returned by the
sl@0
    19
 most recent call to any of the functions:
sl@0
    20
 - CSqlSrvDatabase::ExecL();
sl@0
    21
 - CSqlSrvStatement::NewLC();
sl@0
    22
 - CSqlSrvStatement::Next();
sl@0
    23
 - CSqlSrvStatement::Reset();
sl@0
    24
 - CSqlSrvStatement::Exec();
sl@0
    25
 
sl@0
    26
 Note that the function can only return a reference to text for
sl@0
    27
 database-specific type errors, i.e. those errors that are categorised as of
sl@0
    28
 type ESqlDbError.
sl@0
    29
 
sl@0
    30
 @return A non-modifiable pointer descriptor representing the most recent error
sl@0
    31
 message. Note that message may be NULL, i.e. the descriptor may have
sl@0
    32
 zero length.
sl@0
    33
 
sl@0
    34
 @see CSqlSrvDatabase::ExecL()
sl@0
    35
 @see CSqlSrvStatement::NewLC()
sl@0
    36
 @see CSqlSrvStatement::Next()
sl@0
    37
 @see CSqlSrvStatement::Reset()
sl@0
    38
 @see CSqlSrvStatement::Exec()
sl@0
    39
*/
sl@0
    40
inline TPtrC CSqlSrvDatabase::LastErrorMessage() const
sl@0
    41
	{
sl@0
    42
	const void* errMsg = sqlite3_errmsg16(iDbHandle);//"errMsg" - zero terminated string
sl@0
    43
	TPtrC msg(reinterpret_cast <const TUint16*> (errMsg));//terminating zero removed
sl@0
    44
	return msg;
sl@0
    45
	}
sl@0
    46
	
sl@0
    47
/**
sl@0
    48
Executes one or more 16-bit SQL statements.
sl@0
    49
sl@0
    50
SQL statements of any kind can be executed, but note the following points:
sl@0
    51
- the function does not return any records if the SQL statement type is "SELECT".
sl@0
    52
- if an SQL statement contains one or more parameters, then the function will execute it,
sl@0
    53
giving the parameters default NULL values.
sl@0
    54
sl@0
    55
If the call to this function fails because of a database-specific type error 
sl@0
    56
(i.e. the error is categorised as of type ESqlDbError), then a textual description of
sl@0
    57
the error can be obtained calling CSqlSrvDatabase::LastErrorMessage().
sl@0
    58
sl@0
    59
@param aSqlStmt A zero-terminated string descriptor of 16-bit wide characters containing one or more 
sl@0
    60
                SQL statements. Each statement is separated by a ';' character.
sl@0
    61
                Note that the ExecL() call can modify the content of aSqlStmt parameter.
sl@0
    62
sl@0
    63
@leave  KErrNoMemory, an out of memory condition has occured;
sl@0
    64
        KSqlErrGeneral, a syntax error has occurred - text describing the problem
sl@0
    65
                        can be obtained by calling 	CSqlSrvDatabase::LastErrorMessage();
sl@0
    66
        KErrPermissionDenied, the caller does not satisfy the relevant database security policies.
sl@0
    67
                      Note that the function may also leave with some other database specific 
sl@0
    68
                      errors categorised as ESqlDbError, and other system-wide error codes.
sl@0
    69
                      
sl@0
    70
@see CSqlSrvDatabase::LastErrorMessage()
sl@0
    71
*/
sl@0
    72
inline void CSqlSrvDatabase::ExecL(TDes16& aSqlStmt)
sl@0
    73
	{
sl@0
    74
	__SQLLEAVE_IF_ERROR(::DbExecStmt16(iDbHandle, aSqlStmt));
sl@0
    75
	}
sl@0
    76
	
sl@0
    77
/**
sl@0
    78
Executes one or more 8-bit SQL statements.
sl@0
    79
sl@0
    80
SQL statements of any kind can be executed, but note the following points:
sl@0
    81
- the function does not return any records if the SQL statement type is "SELECT".
sl@0
    82
- if an SQL statement contains one or more parameters, then the function will execute it,
sl@0
    83
giving the parameters default NULL values.
sl@0
    84
sl@0
    85
If the call to this function fails because of a database-specific type error 
sl@0
    86
(i.e. the error is categorised as of type ESqlDbError), then a textual description of
sl@0
    87
the error can be obtained calling CSqlSrvDatabase::LastErrorMessage().
sl@0
    88
sl@0
    89
@param aSqlStmt A zero-terminated string descriptor of 8-bit wide characters containing one or more 
sl@0
    90
                SQL statements. Each statement is separated by a ';' character.
sl@0
    91
sl@0
    92
@leave  KErrNoMemory, an out of memory condition has occured;
sl@0
    93
        KSqlErrGeneral, a syntax error has occurred - text describing the problem
sl@0
    94
                        can be obtained by calling 	CSqlSrvDatabase::LastErrorMessage();
sl@0
    95
        KErrPermissionDenied, the caller does not satisfy the relevant database security policies.
sl@0
    96
                      Note that the function may also leave with some other database specific 
sl@0
    97
                      errors categorised as ESqlDbError, and other system-wide error codes.
sl@0
    98
                      
sl@0
    99
@see CSqlSrvDatabase::LastErrorMessage()
sl@0
   100
*/
sl@0
   101
inline void CSqlSrvDatabase::ExecL(const TDesC8& aSqlStmt)
sl@0
   102
	{
sl@0
   103
	__SQLLEAVE_IF_ERROR(::DbExecStmt8(iDbHandle, aSqlStmt));
sl@0
   104
	}
sl@0
   105
	
sl@0
   106
/**
sl@0
   107
Sets the transaction isolation level for the database.
sl@0
   108
sl@0
   109
A transaction isolation level defines the way in which a transaction
sl@0
   110
interacts with other transactions that may be in progress concurrently.
sl@0
   111
sl@0
   112
Transaction isolation levels are defined by the values of the
sl@0
   113
RSqlDatabase::TIsolationLevel enum.
sl@0
   114
sl@0
   115
The default isolation level is RSqlDatabase::ESerializable
sl@0
   116
sl@0
   117
Note that the isolation level is not a persistent property of the database.
sl@0
   118
It is set to the default value, i.e. RSqlDatabase::ESerializable,
sl@0
   119
whenever the database is created or opened.
sl@0
   120
sl@0
   121
@param aLevel The isolation level to be set.
sl@0
   122
sl@0
   123
@leave KErrNotSupported, invalid (not supported) transaction isolation level.
sl@0
   124
						 Current implementation of CSqlSrvDatabase::SetIsolationLevelL()
sl@0
   125
						 does not support RSqlDatabase::EReadCommitted and RSqlDatabase::ERepeatableRead
sl@0
   126
						 transaction isolation levels.
sl@0
   127
*/
sl@0
   128
inline void CSqlSrvDatabase::SetIsolationLevelL(RSqlDatabase::TIsolationLevel aLevel)
sl@0
   129
	{
sl@0
   130
	if(aLevel != RSqlDatabase::EReadUncommitted && aLevel != RSqlDatabase::ESerializable)
sl@0
   131
		{
sl@0
   132
		__SQLLEAVE(KErrNotSupported);
sl@0
   133
		}
sl@0
   134
	iAuthorizerDisabled = ETrue;
sl@0
   135
	TInt err = ::DbExecStmt8(iDbHandle, aLevel == RSqlDatabase::EReadUncommitted ? KReadUncommittedPragma() : KSerializablePragma());
sl@0
   136
	iAuthorizerDisabled = EFalse;
sl@0
   137
	__SQLLEAVE_IF_ERROR(err);
sl@0
   138
	}
sl@0
   139
	
sl@0
   140
/**
sl@0
   141
@return sqlite3 handle
sl@0
   142
sl@0
   143
@panic SqlDb 7 In _DEBUG mode. iDbHandle is NULL.
sl@0
   144
*/
sl@0
   145
inline sqlite3* CSqlSrvDatabase::RawDbHandle() const
sl@0
   146
	{
sl@0
   147
	__ASSERT_DEBUG(iDbHandle != NULL, __SQLPANIC(ESqlPanicInternalError));
sl@0
   148
	return iDbHandle;	
sl@0
   149
	}
sl@0
   150
sl@0
   151
/**
sl@0
   152
@return A pointer to the database security policies object.
sl@0
   153
sl@0
   154
Note that there may be no security policies in force for this database (if it is not a secure database).
sl@0
   155
sl@0
   156
@see CSqlSecurityPolicy
sl@0
   157
*/	
sl@0
   158
inline const CSqlSecurityPolicy* CSqlSrvDatabase::SecurityPolicy() const
sl@0
   159
	{
sl@0
   160
	return iSecurityPolicy;	
sl@0
   161
	}
sl@0
   162
sl@0
   163
/**
sl@0
   164
Installs user defined collations.
sl@0
   165
sl@0
   166
At the moment 5 user defined collations with the following names are installed:
sl@0
   167
- CompareC0 - 16-bit strings collated comaprison at level 0;
sl@0
   168
- CompareC1 - 16-bit strings collated comaprison at level 1;
sl@0
   169
- CompareC2 - 16-bit strings collated comaprison at level 2;
sl@0
   170
- CompareC3 - 16-bit strings collated comaprison at level 3;
sl@0
   171
- CompareF  - 16-bit strings folded comaprison;
sl@0
   172
sl@0
   173
These user defined collations can be used in the following cases:
sl@0
   174
sl@0
   175
- as column constraint in "CREATE TABLE" SQL statements. For example:
sl@0
   176
@code
sl@0
   177
  CREATE TABLE A(Col1 TEXT COLLATE CompareC1)
sl@0
   178
@endcode
sl@0
   179
In this case every time when Col1 values have to be compared, the SQL server will use CompareC1 collation.
sl@0
   180
sl@0
   181
- as column constraint in "CREATE INDEX" SQL statements. For example:
sl@0
   182
@code
sl@0
   183
  CREATE INDEX I ON A(Col1 COLLATE CompareC2)
sl@0
   184
@endcode
sl@0
   185
In this case SQL server will use CompareC2 collation to compare Col1 values when using the index.
sl@0
   186
sl@0
   187
- In "ORDER BY" clause of "SELECT" SQL statements. For example:
sl@0
   188
@code
sl@0
   189
  SELECT * FROM A ORDER BY Col1 COLLATE CompareC3
sl@0
   190
@endcode
sl@0
   191
In this case SQL server will use CompareC3 collation to compare Col1 values when building the result set.
sl@0
   192
sl@0
   193
This function is part of CSqlSrvDatabase instance initialization.
sl@0
   194
sl@0
   195
@leave The function may leave with some database specific errors categorised as ESqlDbError.
sl@0
   196
sl@0
   197
@see TSqlCollate
sl@0
   198
@see TSqlCollate::InstallCollationsL()
sl@0
   199
*/
sl@0
   200
inline void CSqlSrvDatabase::InstallCollationsL()
sl@0
   201
	{
sl@0
   202
	TSqlCollationUtil collationUtil(iDbHandle);
sl@0
   203
	collationUtil.InstallCollationsL();
sl@0
   204
	}
sl@0
   205
sl@0
   206
/**
sl@0
   207
Stores the initial settings in the system settings table of a database. 
sl@0
   208
The initial settings are the specified collation dll name and version 0
sl@0
   209
of the database configuration file.
sl@0
   210
The collation dll name is used to uniquely identify the collation 
sl@0
   211
method which is used by the collation comparison methods: 
sl@0
   212
TDesC16::CompareC(), TDesC16::MatchC(), TDesC16::FindC().
sl@0
   213
This function is used when a new database is created.
sl@0
   214
sl@0
   215
@param aCollationDllName Collation dll name. It uniquely identifies the current collation method in use.
sl@0
   216
                         If the default collation method changes later then the database will be reindexed 
sl@0
   217
                         and the new collation dll name will replace the existing one in the settings table.
sl@0
   218
@param aDbConfigFileVersion Current config file version or KSqlNullDbConfigFileVersion
sl@0
   219
@param aCompactionMode Database compaction mode, one of TSqlCompactionMode enum item values (except ESqlCompactionNotSet)
sl@0
   220
sl@0
   221
@see TSqlCompactionMode
sl@0
   222
sl@0
   223
@panic SqlDb 4 In _DEBUG mode if aCompactionMode parameter value is invalid.
sl@0
   224
*/
sl@0
   225
inline void CSqlSrvDatabase::StoreSettingsL(const TDesC& aCollationDllName, TInt aDbConfigFileVersion, TSqlCompactionMode aCompactionMode)
sl@0
   226
	{
sl@0
   227
	__ASSERT_DEBUG(aCompactionMode == ESqlCompactionManual || aCompactionMode == ESqlCompactionBackground || aCompactionMode == ESqlCompactionAuto, __SQLPANIC(ESqlPanicBadArgument));
sl@0
   228
#if !defined(__SQL_DISABLE_SYMBIAN_SETTINGS_TABLE__)		
sl@0
   229
	TSqlDbSysSettings dbSettings(iDbHandle);
sl@0
   230
	dbSettings.StoreSettingsL(KMainDb16, aCollationDllName, aDbConfigFileVersion, aCompactionMode);
sl@0
   231
#else
sl@0
   232
	aCollationDllName.Ptr(); // to avoid compile-time warning
sl@0
   233
#endif // !(__SQL_DISABLE_SYMBIAN_SETTINGS_TABLE__)		
sl@0
   234
	}
sl@0
   235
sl@0
   236
/**
sl@0
   237
This function returns the number of database rows that were updated/inserted/deleted
sl@0
   238
by the most recently completed INSERT, UPDATE, or DELETE statement.
sl@0
   239
*/
sl@0
   240
inline TInt CSqlSrvDatabase::LastChangesCount()
sl@0
   241
	{
sl@0
   242
	return sqlite3_changes(iDbHandle);
sl@0
   243
	}
sl@0
   244
sl@0
   245
/**
sl@0
   246
@return Returns the ROWID of the most recent successful INSERT into the database from this database connection.
sl@0
   247
        If no successful inserts have ever occurred on this database connection, zero is returned.
sl@0
   248
*/
sl@0
   249
inline TInt64 CSqlSrvDatabase::LastInsertedRowId()
sl@0
   250
	{
sl@0
   251
	return sqlite3_last_insert_rowid(iDbHandle);
sl@0
   252
	}
sl@0
   253
sl@0
   254
//Check that the caller has at least one of {Read, Write, Schema} policies and leave with 
sl@0
   255
//KErrPermissionDenied if it is not true.
sl@0
   256
//The "schema" policy check should be evaluated last in order to prevent too many platsec warnings reported in the log files.
sl@0
   257
//(It is very likely that the caller has either "read", "write" or both database security policies)
sl@0
   258
inline void CSqlSrvDatabase::BasicSecurityPolicyCheckL(const CSqlSecurityPolicy& aSecurityPolicy)
sl@0
   259
	{
sl@0
   260
	MSqlPolicyInspector& inspector = ::SqlServer().SecurityInspector();
sl@0
   261
	if(!(inspector.Check(aSecurityPolicy.DbPolicy(RSqlSecurityPolicy::EReadPolicy)) ||
sl@0
   262
	     inspector.Check(aSecurityPolicy.DbPolicy(RSqlSecurityPolicy::EWritePolicy))||
sl@0
   263
	     inspector.Check(aSecurityPolicy.DbPolicy(RSqlSecurityPolicy::ESchemaPolicy))))
sl@0
   264
	     	{
sl@0
   265
	     	__SQLLEAVE(KErrPermissionDenied);		
sl@0
   266
	     	}
sl@0
   267
	}
sl@0
   268
sl@0
   269
/**
sl@0
   270
Current database connection transaction state.
sl@0
   271
sl@0
   272
@return True, the current database connection is in transaction, false otherwise.
sl@0
   273
*/
sl@0
   274
inline TBool CSqlSrvDatabase::InTransaction() const
sl@0
   275
	{
sl@0
   276
	return !sqlite3_get_autocommit(iDbHandle);		
sl@0
   277
	}
sl@0
   278