/** * Method to fetch Registration token of the Authentication app from RegTokenValidityTable * @param aAuthAppId The ID of the Authentication app * @param aRegToken [out] The Registration token of the authentication app */ void CSmfCredMgrDbUser::readRegistrationTokenL(const TDesC& aAuthAppId, TDesC& aRegToken) { TInt err(KErrNone); RSqlStatement sqlReadStatement; TInt paramIndex(KErrNone); err = sqlReadStatement.Prepare(iDataBase, KSmfDbReadRegistrationToken); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); paramIndex = sqlReadStatement.ParameterIndex(_L(":iID")); err = sqlReadStatement.BindText(paramIndex, aAuthAppId); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); while ((err = sqlReadStatement.Next()) == KSqlAtRow) { //sometimes sqlStmt.Next returns KSqlAtRow even if no row is present if (!sqlReadStatement.IsNull(0)) { aRegToken = sqlReadStatement.ColumnTextL(0);//aKey.AllocL(); } else { __ASSERT_DEBUG( 0, User::Invariant()); } } sqlReadStatement.Close(); }
/** * Method to fetch Authentication app ID from RegTokenValidityTable * giving the reg token * @param aRegToken The Registration token of the authentication app * @param aAuthAppId [out] The ID of the Authentication app */ void CSmfCredMgrDbUser::readAuthAppIdInRegTokenTable(const TDesC& aRegToken, TDes& aAuthAppId) { TInt err(KErrNone); RSqlStatement sqlReadStatement; TInt paramIndex(KErrNone); TInt64 duration; TBuf<KMaxBufSize> tokenBuf(aRegToken); err = sqlReadStatement.Prepare(iDataBase, KSmfDbReadAuthAppIdInRegTokenTable); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); paramIndex = sqlReadStatement.ParameterIndex(_L(":iID")); err = sqlReadStatement.BindText(paramIndex, tokenBuf); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); while ((err = sqlReadStatement.Next()) == KSqlAtRow) { //sometimes sqlStmt.Next returns KSqlAtRow even if no row is present if (!sqlReadStatement.IsNull(0)) { duration = sqlReadStatement.ColumnInt(1); if (duration) //to be checked with epoch date-time { sqlReadStatement.ColumnText(0, aAuthAppId); } } else { __ASSERT_DEBUG( 0, User::Invariant()); } } sqlReadStatement.Close(); }
/** * Method to fetch all the URLs associated with the Authentication app * @param aAuthAppId The ID of the Authentication app * @param aArray [out] The array to be updated with URLs */ void CSmfCredMgrDbUser::readUrlL(const TDesC& aAuthAppId, RPointerArray<HBufC>& aArray) { TInt err(KErrNone); RSqlStatement sqlReadStatement; TInt paramIndex(KErrNone); err = sqlReadStatement.Prepare(iDataBase, KSmfDbReadURL); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); paramIndex = sqlReadStatement.ParameterIndex(_L(":iID")); err = sqlReadStatement.BindText(paramIndex, aAuthAppId); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); while ((err = sqlReadStatement.Next()) == KSqlAtRow) { //sometimes sqlStmt.Next returns KSqlAtRow even if no row is present if (!sqlReadStatement.IsNull(0)) { TBuf<KMaxBufSize> urlBuf; HBufC* buf = HBufC::NewL(KMaxBufSize); sqlReadStatement.ColumnText(0, urlBuf); buf->Des().Copy(urlBuf); aArray.Append(buf); } else { __ASSERT_DEBUG( 0, User::Invariant()); } } sqlReadStatement.Close(); }
/** * Method to fetch Validity of the Authentication app * @param aAuthAppId The ID of the Authentication app * @param aValidity [out] The time by which the Auth set will expire */ void CSmfCredMgrDbUser::readValidity(const TDesC& aAuthAppId, TInt64& aValidity) { TInt err(KErrNone); RSqlStatement sqlReadStatement; TInt paramIndex(KErrNone); err = sqlReadStatement.Prepare(iDataBase, KSmfDbReadValidity); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); paramIndex = sqlReadStatement.ParameterIndex(_L(":iID")); err = sqlReadStatement.BindText(paramIndex, aAuthAppId); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); while ((err = sqlReadStatement.Next()) == KSqlAtRow) { //sometimes sqlStmt.Next returns KSqlAtRow even if no row is present if (!sqlReadStatement.IsNull(0)) { aValidity = sqlReadStatement.ColumnInt64(1); } else { __ASSERT_DEBUG( 0, User::Invariant()); } } sqlReadStatement.Close(); }
TBool CPredictiveSearchSynchronizer::CheckIfPredSearchTableExistsL( const TDesC& aTableName) const { PRINT1(_L("CPredictiveSearchSynchronizer::CheckIfPredSearchTableExistsL table='%S'"), &aTableName); _LIT(KSelectFirstTableFormat, "SELECT name FROM sqlite_master WHERE type='table' AND name='%S';"); TInt bufSize = KSelectFirstTableFormat().Length() + aTableName.Length(); HBufC* select = HBufC::NewLC(bufSize); select->Des().AppendFormat(KSelectFirstTableFormat, &aTableName); RSqlStatement stmnt; CleanupClosePushL(stmnt); stmnt.PrepareL(iDatabase, *select); TBool tableExists = (stmnt.Next() == KSqlAtRow); CleanupStack::PopAndDestroy(&stmnt); CleanupStack::PopAndDestroy(select); PRINT1(_L("CPredictiveSearchSynchronizer::CheckIfPredSearchTablesExistL return %d"), tableExists); return tableExists; }
/** Searches the contacts database to find any contact items with an exact match on the address supplied. @param aCommAddr A descriptor containing the address to be found in the database. @param aAddrType The type of addresses that is being sought. @return An array of contact IDs which match the supplied address. */ CContactIdArray* CPplCommAddrTable::MatchNonPhoneAddrL(const TDesC& aCommAddr, TCommAddrType aAddrType) { // build statement RSqlStatement stmnt; CleanupClosePushL(stmnt); stmnt.PrepareL(iDatabase, iMatchSelectStmnt->SqlStringL() ); const TInt KValueParamIndex(KFirstParam); // first parameter in query... const TInt KTypeParamIndex(KValueParamIndex + 1); // ...and the second. User::LeaveIfError(stmnt.BindText(KValueParamIndex, aCommAddr) ); User::LeaveIfError(stmnt.BindInt(KTypeParamIndex, aAddrType) ); // fetch the list of any matching contact ids CContactIdArray* idArray = CContactIdArray::NewLC(); TInt err(KErrNone); const TInt KContactIdIdx(iMatchSelectStmnt->ParameterIndex(KCommAddrContactId() ) ); while ((err = stmnt.Next() ) == KSqlAtRow) { idArray->AddL(stmnt.ColumnInt(KContactIdIdx) ); } // leave if we didn't complete going through the results properly if(err != KSqlAtEnd) { User::Leave(err); } CleanupStack::Pop(idArray); CleanupStack::PopAndDestroy(&stmnt); return idArray; }
/** * Method to read the IsEnabled flag from plugin Id table * @param aPluginID The ID of the plugin * @param aFlag [out] The flag that indicates the plugin id is enables or disabled */ void CSmfCredMgrDbUser::readFlagInPluginIdTable(const TDesC& aPluginID, TInt& aFlag) { TInt err(KErrNone); RSqlStatement sqlReadStatement; TInt paramIndex(KErrNone); err = sqlReadStatement.Prepare(iDataBase, KSmfDbReadFlagInPluginTable); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); paramIndex = sqlReadStatement.ParameterIndex(_L(":iID")); err = sqlReadStatement.BindText(paramIndex, aPluginID); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); while ((err = sqlReadStatement.Next()) == KSqlAtRow) { //sometimes sqlStmt.Next returns KSqlAtRow even if no row is present if (!sqlReadStatement.IsNull(0)) { aFlag = sqlReadStatement.ColumnInt(0); } } sqlReadStatement.Close(); }
// ----------------------------------------------------------------------------- // CUpnpSecurityDbConnection::GetAllFilenamesL // Get all filenames. // ----------------------------------------------------------------------------- // void CUpnpSecurityDbConnection::GetAllFilenamesL( RPointerArray<HBufC>& aFilenameArray ) { TInt err; RSqlStatement statement; User::LeaveIfError( statement.Prepare( iDatabase, KUpnpSecSqlSelectAllFiles ) ); CleanupClosePushL( statement ); TInt columnIndex = statement.ColumnIndex( KUpnpSecSqlFilename ); while ( (err = statement.Next()) == KSqlAtRow ) { TPtrC data = statement.ColumnTextL( columnIndex ); HBufC* filename = data.AllocLC(); aFilenameArray.AppendL( filename ); CleanupStack::Pop( filename ); } if ( err != KSqlAtEnd ) { User::LeaveIfError( err ); } CleanupStack::PopAndDestroy( &statement ); }
TBool CPredictiveSearchSynchronizer::ReadMailAddressesL(CContactItem& aContact) { PRINT(_L("CPredictiveSearchSynchronizer::ReadMailAddressesL")); // SELECT value FROM comm_addr // WHERE contact_id = [contact id value] AND type = [type value]; _LIT(KSelectMailAddrFormat, "SELECT %S FROM %S WHERE %S = %d AND %S = %d;"); const TInt KContactIdLength = 10; const TInt KCommAddrTypeLength = 2; // CPplCommAddrTable::EEmailAddress is enum TInt bufSize = KSelectMailAddrFormat().Length() + KCommAddrValue().Length() + KSqlContactCommAddrTableName().Length() + KCommAddrContactId().Length() + KContactIdLength + KCommAddrType().Length() + KCommAddrTypeLength; HBufC* sqlStatement = HBufC::NewLC(bufSize); sqlStatement->Des().AppendFormat(KSelectMailAddrFormat, &KCommAddrValue, &KSqlContactCommAddrTableName, &KCommAddrContactId, aContact.Id(), &KCommAddrType, CPplCommAddrTable::EEmailAddress); RSqlStatement stmnt; CleanupClosePushL(stmnt); PRINT1(_L("prepare SQL statement:%S"), sqlStatement); stmnt.PrepareL(iDatabase, *sqlStatement); const TInt KValueIndex = 0; TBool foundMailAddress(EFalse); TInt err(KErrNone); while ((err = stmnt.Next()) == KSqlAtRow) { TPtrC value; if (stmnt.ColumnText(KValueIndex, value) == KErrNone) { PRINT2(_L(" id=%d, found mail address=%S"), aContact.Id(), &value); CContactItemField* field = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldEMail); CContactTextField* textfield = field->TextStorage(); textfield->SetTextL(value); aContact.AddFieldL(*field); // Takes ownership CleanupStack::Pop(field); foundMailAddress = ETrue; } } if (err != KSqlAtEnd) { PRINT1(_L("CPredictiveSearchSynchronizer::ReadMailAddressesL SQL err=%d"), err); User::Leave(err); } CleanupStack::PopAndDestroy(&stmnt); CleanupStack::PopAndDestroy(sqlStatement); PRINT1(_L("CPredictiveSearchSynchronizer::ReadMailAddressesL return %d"), foundMailAddress); return foundMailAddress; }
/** @SYMTestCaseID SYSLIB-SQL-CT-1645 @SYMTestCaseDesc Testing database operations on a secure database. The test application's capabilities allow write-only access to the test secure database. Verify that any other kind of a database operation will fail with KErrPermissionDenied error. @SYMTestPriority High @SYMTestActions Testing database operations on a secure database. @SYMTestExpectedResults Test must not fail @SYMREQ REQ5792 REQ5793 */ void WriteOnlyDatabaseTest() { TInt err = TheDb.Open(KTestDbName); TEST2(err, KErrNone); //Attempt to modify the database schema err = TheDb.Exec(_L("CREATE TABLE C(FFF TEXT)")); TEST2(err, KErrPermissionDenied); err = TheDb.Exec(_L("CREATE TRIGGER upd_a_b1 UPDATE OF B1 ON A BEGIN UPDATE B SET F3 = 'AAAA' WHERE F2 = A.F1; END;")); TEST2(err, KErrPermissionDenied); err = TheDb.Exec(_L("CREATE TEMP TRIGGER upd_a_b1 UPDATE OF B1 ON A BEGIN UPDATE B SET F3 = 'AAAA' WHERE F2 = A.F1; END;")); TEST2(err, KErrPermissionDenied);//Temp trigger which attempts to update one of the tables. err = TheDb.Exec(_L("CREATE VIEW V1 AS SELECT * FROM A")); TEST2(err, KErrPermissionDenied); err = TheDb.Exec(_L("CREATE TEMP VIEW V1 AS SELECT * FROM A")); TEST(err >= 0); err = TheDb.Exec(_L("DROP VIEW V1")); TEST(err >= 0); //Attempt to update the user data (but it includes a READ operation) err = TheDb.Exec(_L("UPDATE A SET F1 = 11 WHERE F1 = 1")); TEST2(err, KErrPermissionDenied); //Attempt to update the user data (unconditional UPDATE, no READ operations) err = TheDb.Exec(_L("UPDATE A SET F1 = 11")); TEST(err >= 0); //Attempt to delete the user data (but it includes a READ operation) err = TheDb.Exec(_L("DELETE FROM B WHERE F2 = 2")); TEST2(err, KErrPermissionDenied); //Attempt to delete the user data (unconditional DELETE, no READ operations) err = TheDb.Exec(_L("DELETE FROM A")); TEST(err >= 0); //Restore the deleted table A err = TheDb.Exec(_L("INSERT INTO A(F1,B1) VALUES(1,x'41414141414141414141');INSERT INTO A(F1,B1) VALUES(2,x'42424242424242424242');INSERT INTO A(F1,B1) VALUES(3,x'43434343434343434343');INSERT INTO A(F1,B1) VALUES(4,x'44444444444444444444');")); TEST(err >= 0); //Attempt to insert new user data err = TheDb.Exec(_L("INSERT INTO B(F2, F3, B2) VALUES(22, 'AAA', x'47474747474747474747')")); TEST2(err, 1); //Attempt to change the isolation level. err = TheDb.SetIsolationLevel(RSqlDatabase::ESerializable); TEST2(err, KErrNone); err = TheDb.SetIsolationLevel(RSqlDatabase::EReadUncommitted); TEST2(err, KErrNone); //Attempt to read the user data RSqlStatement stmt; err = stmt.Prepare(TheDb, _L("SELECT A.F1 FROM B,A WHERE A.F1 = B.F2")); TEST2(err, KErrPermissionDenied); //Attempt to read the system data err = stmt.Prepare(TheDb, _L("SELECT * FROM SQLITE_MASTER")); TEST2(err, KErrNone); err = stmt.Next(); TEST2(err, KSqlAtRow); TPtrC p; err = stmt.ColumnText(0, p); TEST2(err, KErrNone); RDebug::Print(_L("Value=%S\r\n"), &p); stmt.Close(); TheDb.Close(); }
// ----------------------------------------------------------------------------- // CUpnpSecurityDbConnection::Validate // Check validity of database. // ----------------------------------------------------------------------------- // TBool CUpnpSecurityDbConnection::Validate() { TBool result = EFalse; RSqlStatement statement; TInt err = statement.Prepare( iDatabase, KUpnpSecSqlValidateTableExistence ); result = !err && (statement.Next() == KSqlAtRow); statement.Close(); return result; }
/** GetListForItemL has a dual nature. If aIsGroup is ETrue, a list of contact items belonging to specified group is returned. Otherwise a list of group ids to which contact id belongs is returned. @param aItemId contact item id @param aIsGroup ETrue if the method will fill a group. */ CContactIdArray* CPplGroupsTable::GetListForItemL(TContactItemId aItemId, TBool aIsGroup) { /* // Check if group membership information was not requested or if the item // is not derived from CContactItemPlusGroup. if (!(aType == KUidContactGroup || aType == KUidContactCard || aType == KUidContactOwnCard || aType == KUidContactICCEntry) ) { return NULL; } */ // build the RSqlStatement RSqlStatement stmnt; CleanupClosePushL(stmnt); TInt idIndex; // build the CCntSqlStatement statement const TInt KWhereParamIndex(KFirstIndex); // only one parameter in the query if (aIsGroup) { // group -> select members stmnt.PrepareL(iDatabase, iSelectMembersStmnt->SqlStringL() ); User::LeaveIfError(stmnt.BindInt(KWhereParamIndex, aItemId ) ); idIndex = stmnt.ColumnIndex(KGroupContactGroupMemberId() ); } else { // member -> select groups stmnt.PrepareL(iDatabase, iSelectGroupsStmnt->SqlStringL() ); User::LeaveIfError(stmnt.BindInt(KWhereParamIndex, aItemId ) ); idIndex = stmnt.ColumnIndex(KGroupContactGroupId() ); } User::LeaveIfError(idIndex); // fetch the list of any matching ids CContactIdArray* items = CContactIdArray::NewLC(); TInt err(KErrNone); while ((err = stmnt.Next() ) == KSqlAtRow) { items->AddL(stmnt.ColumnInt(idIndex) ); } // leave if we didn't complete going through the results properly if(err != KSqlAtEnd) { User::Leave(err); } CleanupStack::Pop(items); CleanupStack::PopAndDestroy(&stmnt); return items; }
/** Create view contact object based on given sql statement. @return CViewContact object or NULL the contact is not found. */ CViewContact* CCntPplViewSession::CreateViewItemL(RSqlStatement& aSqlStmt, const CCntSqlStatement& aCntSqlStmt, const TContactViewPreferences& aViewPrefs) { if (! iContactsFile.IsOpened()) { User::Leave(KErrInUse); } CViewContact* viewContact = InitialiseViewItemL(aSqlStmt, aCntSqlStmt, aViewPrefs); if(!viewContact) { //Couldn't find the contact. return NULL; } CleanupStack::PushL(viewContact); if(viewContact->ContactType() == CViewContact::EContactItem) { FillViewItemL(*viewContact, aSqlStmt, aViewPrefs); } TUid typeUid = GetContactFieldMatchUid(*viewContact, aViewPrefs); if(typeUid != KUidContactFieldNone) { RSqlStatement contactSqlStmt; CleanupClosePushL(contactSqlStmt); /* we have to reload the row from SQL database to cache in text header and text blob */ contactSqlStmt.PrepareL(iContactsFile.NamedDatabase(), iSqlSmtSelectAllFieldsById.SqlStringL()); User::LeaveIfError(contactSqlStmt.BindInt(KFirstIndex, viewContact->Id())); //Bind item id into the condition. TInt err = contactSqlStmt.Next(); if(err == KSqlAtEnd) { //Should never be here, we found it in InitialiseViewItemL //but couldn't find the same contact in same database. CleanupStack::PopAndDestroy(viewContact); return NULL; } User::LeaveIfError(err); /* set first field with possible content for group or unsorted contact */ CContactDatabase::TTextFieldMinimal buf; TextFieldL(contactSqlStmt, iSqlSmtSelectAllFieldsById, iContactProperties.SystemTemplateL(), typeUid, buf); viewContact->SetFirstFieldForBlankContactL(buf); CleanupStack::PopAndDestroy(&contactSqlStmt); } //if(typeUid != 0) CleanupStack::Pop(viewContact); return viewContact; }
/** * Method to Key-Secret pairs of the Authentication app * @param aAuthAppId The ID of the Authentication app * @param aArray [out] The array containing the key-secret pair */ void CSmfCredMgrDbUser::readAuthTokensL(const TDesC& aAuthAppId, RArray< TSmfAuthToken>& aArray) { TInt err(KErrNone); RSqlStatement sqlReadStatement; TInt paramIndex(KErrNone); err = sqlReadStatement.Prepare(iDataBase, KSmfDbReadAuthTokens); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); paramIndex = sqlReadStatement.ParameterIndex(_L(":iID")); err = sqlReadStatement.BindText(paramIndex, aAuthAppId); __ASSERT_DEBUG( (err >= KErrNone), User::Invariant()); while ((err = sqlReadStatement.Next()) == KSqlAtRow) { //sometimes sqlStmt.Next returns KSqlAtRow even if no row is present if (!sqlReadStatement.IsNull(0)) { TSmfAuthToken Set; Set.iKey = HBufC::NewL(KMaxAuthTokenLength); Set.iSecret = HBufC::NewL(KMaxAuthTokenLength); TBuf<KMaxBufSize> keyBuf; TBuf<KMaxBufSize> secretBuf; sqlReadStatement.ColumnText(0, keyBuf); sqlReadStatement.ColumnText(1, secretBuf); Set.iKey->Des().Copy(keyBuf); Set.iSecret->Des().Copy(secretBuf); //add it to the array aArray.Append(Set); } else { __ASSERT_DEBUG( 0, User::Invariant()); } } sqlReadStatement.Close(); }
bool QSymSQLResultPrivate::fetchNext(bool initialFetch) { int res; if (skipRow) { // already fetched Q_ASSERT(!initialFetch); skipRow = false; return skippedStatus; } skipRow = initialFetch; res = stmt.Next(); switch(res) { case KSqlAtRow: return true; case KSqlAtEnd: stmt.Reset(); return false; case KSqlErrGeneral: // KSqlErrGeneral is a generic error code and we must call stmt.Reset() // to get the specific error message. stmt.Reset(); q->setLastError(qMakeError(access, QCoreApplication::translate("QSymSQLResult", "Unable to fetch row"), QSqlError::ConnectionError, res)); q->setAt(QSql::AfterLastRow); return false; case KSqlErrMisuse: case KSqlErrBusy: default: // something wrong, don't get col info, but still return false q->setLastError(qMakeError(access, QCoreApplication::translate("QSymSQLResult", "Unable to fetch row"), QSqlError::ConnectionError, res)); stmt.Reset(); q->setAt(QSql::AfterLastRow); return false; } return false; }
/** @SYMTestCaseID PDS-SQLITE3SEC-UT-4041 @SYMTestCaseDesc SQL server single-select performance test. The test selects one randomly chosen record and stores the execution time for later use (comparison and printing). @SYMTestPriority High @SYMTestActions SQL server single-select performance test. @SYMTestExpectedResults Test must not fail @SYMREQ REQ11320 */ static void SqlServerSingleSelectTest(const char aSingleSelectSql[], TInt aSelectRecId) { TheTest.Next( _L("@SYMTestCaseID:PDS-SQLITE3SEC-UT-4041")); (void)KillProcess(KSqlSrvName); TInt err = TheDb.Open(KTestDbName); TEST2(err, KErrNone); TheSqlBuf.Copy(TPtrC8((const TUint8*)aSingleSelectSql)); TheSqlBuf.AppendNum((TInt64)aSelectRecId); RSqlStatement stmt; err = stmt.Prepare(TheDb, TheSqlBuf); TEST2(err, KErrNone); TInt recCnt = 0; TUint32 fc = FastCounterValue(); while((err = stmt.Next()) == KSqlAtRow) { TInt64 i64 = stmt.ColumnInt64(0); UNUSED_VAR(i64); TReal d = stmt.ColumnReal(1); UNUSED_VAR(d); TPtrC t; err = stmt.ColumnText(2, t); TEST2(err, KErrNone); UNUSED_PTR(t); TPtrC8 b; err = stmt.ColumnBinary(3, b); TEST2(err, KErrNone); UNUSED_PTR(b); ++recCnt; } StorePerfTestResult(EPerfTestSqlMode, EPerfTestSingleSelect, FastCounterValue() - fc); TEST2(err, KSqlAtEnd); TEST2(recCnt, 1); stmt.Close(); TheDb.Close(); }
/** Utility method used to read binary blob fields from contacts database. Provides a mechanism to fill a contact item with informations stored in binary blobs within contact database. A reference to the contact item to be fill has to be provided. A template has to be provided if the contact item is based on a template. Template can be NULL. Also a view definition can be provided to filter which fields are read from blob fields. @param aItem Contact item to be filled with information from binary blob field. @param aView View definition specifying what item fields should be read from binary blob field @param aTemplate Contact item representing a template based on which aItem should be read. Can be NULL @param aDatabase RSqlDatabase reference. @leave KErrNotFound if the specified contact item does not exist any more in contact database */ void TCntPersistenceUtility::ReadBinaryBlobL(CContactItem& aItem, const CContactItemViewDef& aView, const CContactItem* aTemplate, RSqlDatabase& aDatabase) { HBufC* selectString = HBufC::NewLC(KSelectTwoFields().Length() + KContactBinaryFieldHeader().Length() + KContactBinaryFields().Length() + KSqlContactTableName().Length() + KContactId().Length()); TPtr ptrSelectString = selectString->Des(); ptrSelectString.Format(KSelectTwoFields, &KContactBinaryFieldHeader, &KContactBinaryFields, &KSqlContactTableName, &KContactId, aItem.Id()); RSqlStatement selectStmt; CleanupClosePushL(selectStmt); User::LeaveIfError(selectStmt.Prepare(aDatabase, ptrSelectString)); TInt err = selectStmt.Next(); if(err != KSqlAtRow) { if(err == KSqlAtEnd) { User::Leave(KErrNotFound); } else { User::Leave(err); } } TPtrC8 binaryHeader; selectStmt.ColumnBinary(User::LeaveIfError(selectStmt.ColumnIndex(KContactBinaryFieldHeader)), binaryHeader); RDesReadStream binaryHeaderStream(binaryHeader); CleanupClosePushL(binaryHeaderStream); CEmbeddedStore* binaryHeaderStore = CEmbeddedStore::FromLC(binaryHeaderStream); TPtrC8 binaryFields; selectStmt.ColumnBinary(User::LeaveIfError(selectStmt.ColumnIndex(KContactBinaryFields)), binaryFields); RDesReadStream binaryFieldsStream(binaryFields); CleanupClosePushL(binaryFieldsStream); CEmbeddedStore* binaryBlobStore = CEmbeddedStore::FromLC(binaryFieldsStream); ReadBinaryBlobL(*binaryHeaderStore, *binaryBlobStore, aItem, aView, aTemplate); CleanupStack::PopAndDestroy(6, selectString); //binaryHeaderStore, binaryHeaderStrean, binaryBlobStore, binaryBlobStream, selectStmt, selectString }
// ----------------------------------------------------------------------------- // CUpnpSecurityDbConnection::GetAllIpAddressesL // Get all ips. // ----------------------------------------------------------------------------- // void CUpnpSecurityDbConnection::GetAllIpAddressesL( RArray<TInetAddr>& aAddressArray ) { TInt err; RSqlStatement statement; User::LeaveIfError( statement.Prepare( iDatabase, KUpnpSecSqlSelectAllIpAddresses ) ); CleanupClosePushL( statement ); TInt columnIndex = statement.ColumnIndex( KUpnpSecSqlIp ); while ( (err = statement.Next()) == KSqlAtRow ) { TUint32 data = statement.ColumnInt( columnIndex ); aAddressArray.AppendL( TInetAddr( data, 0 ) ); } if ( err != KSqlAtEnd ) { User::LeaveIfError( err ); } CleanupStack::PopAndDestroy( &statement ); }
/** Persist the items belonging to curent group into group table @param aGroup referece to a contact group */ void CPplGroupsTable::WriteGroupMembersL(const CContactItem& aGroup) { if (aGroup.Type() != KUidContactGroup) { return; } const TContactItemId KGroupId(aGroup.Id() ); // make sure we clear out any previous, out-of-date data TBool lowDiskErr(EFalse); DeleteItemL(KGroupId, lowDiskErr); if (lowDiskErr) { User::Leave(KErrDiskFull); } // build the RSqlStatement RSqlStatement stmnt; CleanupClosePushL(stmnt); stmnt.PrepareL(iDatabase, iInsertStmnt->SqlStringL() ); const TInt KGroupIdIndex(KFirstIndex); // first parameter in query... const TInt KMemberIdIndex(KGroupIdIndex + 1); // ...and the second parameter // copy and sort the member id array so we can see if there are duplicates const CContactIdArray* contactIdArray = static_cast<const CContactGroup&>(aGroup).ItemsContained(); //does not take the ownership const TInt arrayCount = contactIdArray->Count(); CArrayFixFlat<TContactItemId>* sortedList = new(ELeave) CArrayFixFlat<TContactItemId>(KArrayGranularity); CleanupStack::PushL(sortedList); for(TInt loop = 0;loop < arrayCount; ++loop) { sortedList->AppendL((*contactIdArray)[loop]); } TKeyArrayFix key(0,ECmpTInt); sortedList->Sort(key); // insert the group-member relationships const TInt KCountStmntParamIndex(KFirstIndex); // first and only parameter in query const TInt listLen(sortedList->Count() ); TInt lastId(0); for (TInt i = 0; i < listLen; ++i) { TInt itemId((*sortedList)[i]); //check if a contact item with itemId id really exists in contact database RSqlStatement countStmnt; CleanupClosePushL(countStmnt); countStmnt.PrepareL(iDatabase, iCountContactsStmnt->SqlStringL() ); User::LeaveIfError(countStmnt.BindInt(KCountStmntParamIndex, itemId) ); TInt count = 0; TInt err = KErrNone; if((err = countStmnt.Next() ) == KSqlAtRow) { count = countStmnt.ColumnInt(iCountContactsStmnt->ParameterIndex(KSqlCount) ); } else { User::LeaveIfError(err); } if(count == 0) { User::Leave(KErrNotFound); } CleanupStack::PopAndDestroy(&countStmnt); // only insert this if we haven't already seen it if (itemId != lastId || i == 0) { User::LeaveIfError(stmnt.BindInt(KGroupIdIndex, KGroupId) ); User::LeaveIfError(stmnt.BindInt(KMemberIdIndex, itemId) ); User::LeaveIfError(stmnt.Exec() ); User::LeaveIfError(stmnt.Reset() ); } lastId = itemId; } CleanupStack::PopAndDestroy(2, &stmnt); // and sortedList }
/** @SYMTestCaseID SYSLIB-SQL-CT-1613 @SYMTestCaseDesc Multiple connections to the same database from different threads. Each thread inserts set of record to the same table. Verify that all expected records and their column values meet the expectations. @SYMTestPriority High @SYMTestActions Testing SQL engine behaviour when having mutiple connections to the same database from different threads. @SYMTestExpectedResults Test must not fail @SYMREQ REQ5792 REQ5793 */ void TestMultiConnDiffThread() { //Create a test database RDebug::Print(_L("+++:MainThread: Create test database\r\n")); RSqlDatabase db; TInt err = db.Create(KTestDbName1); TEST2(err, KErrNone); //Create a test table RDebug::Print(_L("+++:MainThread: Create a table in the test database\r\n")); _LIT8(KCreateSql, "CREATE TABLE A(Id INTEGER PRIMARY KEY)"); err = db.Exec(KCreateSql); TEST(err >= 0); const TInt KThreadCnt = 4; const TInt KRange = 100; const TInt KIsolationLevelCnt = 2; TPtrC KIsolationLevelName[KIsolationLevelCnt] = {_L("Read Uncommitted"), _L("Serializable")}; const RSqlDatabase::TIsolationLevel KIsolationLevels[KIsolationLevelCnt] = { RSqlDatabase::EReadUncommitted, RSqlDatabase::ESerializable}; const TInt KTransTypeCnt = 2; //Do the tests: // - doing each per thread database operation in a single transaction; // - doing all per thread database operations in a single transaction; for(TInt transType=0;transType<KTransTypeCnt;++transType) { //For both supported isolation levels: read uncommitted and serializable for(TInt isolLevel=0;isolLevel<KIsolationLevelCnt;++isolLevel) { TInt low = 1; TInt high = KRange; RDebug::Print(_L("+++:MainThread: Test: thread count %d, records %d, trans type %d, isolation level: %S\r\n"), KThreadCnt, KRange, transType, &KIsolationLevelName[isolLevel]); RThread thread[KThreadCnt]; TRequestStatus status[KThreadCnt]; TThreadData data[KThreadCnt]; //Create the test threads and run them. Each thread establishes a connection with the test database //and attempts to write set of records in the test table. TInt j; for(j=0;j<KThreadCnt;++j,low=high+1,high+=KRange) { data[j].iTransType = transType; data[j].iIsolationLevel = KIsolationLevels[isolLevel]; data[j].iLowRecNo = low; data[j].iHighRecNo = high; _LIT(KThreadName,"Thr-"); TBuf<32> threadName(KThreadName); threadName.AppendNum((TInt64)j + 1); TEST2(thread[j].Create(threadName, &ThreadFunc, 0x2000, 0x1000, 0x10000, (void*)&data[j], EOwnerThread), KErrNone); thread[j].Logon(status[j]); TEST2(status[j].Int(), KRequestPending); thread[j].Resume(); } User::After(2000000); //Wait until threads finish the database operations and close them. for(j=0;j<KThreadCnt;++j) { User::WaitForRequest(status[j]); TEST(thread[j].ExitType() != EExitPanic); thread[j].Close(); } //Check that all records which are esupposed to be in the database, are there. RDebug::Print(_L("+++:MainThread: Check that all records have been written\r\n")); _LIT8(KSelectSql1, "SELECT COUNT(*) FROM A;"); RSqlStatement stmt; err = stmt.Prepare(db, KSelectSql1); TEST2(err, KErrNone); err = stmt.Next(); TEST2(err, KSqlAtRow); TInt cnt = stmt.ColumnInt(0); TEST2(cnt, KThreadCnt * KRange); stmt.Close(); //Check that all records have expected column values. RDebug::Print(_L("+++:MainThread: Check that all records have expected column values\r\n")); _LIT8(KSelectSql2, "SELECT * FROM A;"); err = stmt.Prepare(db, KSelectSql2); TEST2(err, KErrNone); for(TInt k=0;k<(KThreadCnt*KRange);++k) { err = stmt.Next(); TEST2(err, KSqlAtRow); TInt val = stmt.ColumnInt(0); TEST(val > 0 && val <= (KThreadCnt * KRange)); } stmt.Close(); //Prepare for the next test run - delete all records. RDebug::Print(_L("+++:MainThread: Delete all records\r\n")); _LIT8(KDeleteSql, "DELETE FROM A"); err = db.Exec(KDeleteSql); TEST(err >= 0); }//end of "for(TInt isolLevel=0;isolLevel<KIsolationLevelCnt;++isolLevel)" }//end of "for(TInt transType=0;transType<KTransTypeCnt;++transType)" db.Close(); RDebug::Print(_L("+++:MainThread: Delete the test database\r\n")); (void)RSqlDatabase::Delete(KTestDbName1); }
/** @SYMTestCaseID SYSLIB-SQL-CT-1614 @SYMTestCaseDesc Verifying that when having 2 database connections in different threads, both set the isolation level to "Read Uncommitted", the reading thread can make "dirty read" operations (can read the updated but not committed yet record values made by the writing thread). @SYMTestPriority High @SYMTestActions Testing "Read Uncommitted" database isolation level. @SYMTestExpectedResults Test must not fail @SYMREQ REQ5792 REQ5793 */ void TestIsolationLevel() { RDebug::Print(_L("+++:MainThread: Create critical sections\r\n")); TEST2(UpdateThreadCrS.CreateLocal(), KErrNone); UpdateThreadCrS.Wait(); TEST2(MainThreadCrS.CreateLocal(), KErrNone); MainThreadCrS.Wait(); RDebug::Print(_L("+++:MainThread: Create test database\r\n")); RSqlDatabase db; TInt err = db.Create(KTestDbName1); TEST2(err, KErrNone); RDebug::Print(_L("+++:MainThread: Set the isolation level to \"Read uncommitted\"\r\n")); err = db.SetIsolationLevel(RSqlDatabase::EReadUncommitted); TEST2(err, KErrNone); RDebug::Print(_L("+++:MainThread: Create a table in the test database\r\n")); _LIT8(KCreateSql, "CREATE TABLE A(Id INTEGER)"); err = db.Exec(KCreateSql); TEST(err >= 0); RDebug::Print(_L("+++:MainThread: Insert one record in the table\r\n")); _LIT8(KInsertSql, "INSERT INTO A(Id) VALUES("); TBuf8<64> sql(KInsertSql); sql.AppendNum((TInt64)KInitialValue); sql.Append(_L(")")); err = db.Exec(sql); TEST2(err, 1); RDebug::Print(_L("+++:MainThread: Create the \"update\" thread\r\n")); _LIT(KThreadName, "UpdTh"); RThread thread; TEST2(thread.Create(KThreadName, &UpdateThreadFunc, 0x2000, 0x1000, 0x10000, NULL, EOwnerThread), KErrNone); TRequestStatus status; thread.Logon(status); TEST2(status.Int(), KRequestPending); thread.Resume(); RDebug::Print(_L("+++:MainThread: Wait for record update completion...\r\n")); MainThreadCrS.Wait(); RDebug::Print(_L("+++:MainThread: Read the record and check the data...\r\n")); _LIT8(KSelectSql, "SELECT * FROM A"); RSqlStatement stmt; err = stmt.Prepare(db, KSelectSql); TEST2(err, KErrNone); err = stmt.Next(); TEST2(err, KSqlAtRow); TInt val = stmt.ColumnInt(0); TEST(val == KUpdatedValue); stmt.Close(); RDebug::Print(_L("+++:MainThread: Notify the update thread that it can rollback\r\n")); UpdateThreadCrS.Signal(); RDebug::Print(_L("+++:MainThread: Wait for rollback completion...\r\n")); MainThreadCrS.Wait(); RDebug::Print(_L("+++:MainThread: Read the record and check the data...\r\n")); err = stmt.Prepare(db, KSelectSql); TEST2(err, KErrNone); err = stmt.Next(); TEST2(err, KSqlAtRow); val = stmt.ColumnInt(0); TEST2(val, KInitialValue); stmt.Close(); User::WaitForRequest(status); thread.Close(); db.Close(); RDebug::Print(_L("+++:MainThread: Delete the test database\r\n")); (void)RSqlDatabase::Delete(KTestDbName1); RDebug::Print(_L("+++:MainThread: Close critical sections\r\n")); MainThreadCrS.Close(); UpdateThreadCrS.Close(); }
/** @SYMTestCaseID SYSLIB-SQL-CT-1646 @SYMTestCaseDesc Testing database operations on a secure database. The test application's capabilities allow read/write access to the test secure database. Verify that any other kind of a database operation will fail with KErrPermissionDenied error. @SYMTestPriority High @SYMTestActions Testing database operations on a secure database. @SYMTestExpectedResults Test must not fail @SYMREQ REQ5792 REQ5793 */ void ReadWriteDatabaseTest() { RSqlDatabase db; TInt err = TheDb.Open(KTestDbName); TEST2(err, KErrNone); //Attempt to modify the database schema err = TheDb.Exec(_L("CREATE TABLE C(FFF TEXT)")); TEST2(err, KErrPermissionDenied); err = TheDb.Exec(_L("CREATE TEMP TABLE TBL1(COL1 INTEGER, COL2 INTEGER)")); TEST(err >= 0); err = TheDb.Exec(_L("CREATE TEMP TRIGGER del1 AFTER DELETE ON TBL1 BEGIN DELETE FROM A; END;")); TEST(err >= 0); err = TheDb.Exec(_L("DROP TRIGGER del1")); TEST(err >= 0); err = TheDb.Exec(_L("CREATE TEMP VIEW V1 AS SELECT * FROM TBL1")); TEST(err >= 0); err = TheDb.Exec(_L("DROP VIEW V1")); TEST(err >= 0); err = TheDb.Exec(_L("CREATE INDEX I1 ON TBL1(COL2)")); TEST(err >= 0); err = TheDb.Exec(_L("DROP INDEX I1")); TEST(err >= 0); err = TheDb.Exec(_L("DROP TABLE TBL1")); TEST(err >= 0); err = TheDb.Exec(_L("ANALYZE A")); TEST2(err, KErrPermissionDenied); err = TheDb.Exec(_L("CREATE VIEW V2 AS SELECT * FROM A")); TEST2(err, KErrPermissionDenied); //Attempt to update the user data (but it includes a READ operation) err = TheDb.Exec(_L("UPDATE A SET F1 = 11 WHERE F1 = 1")); TEST(err >= 0); //Attempt to update the user data (unconditional UPDATE, no READ operations) err = TheDb.Exec(_L("UPDATE A SET F1 = 11")); TEST(err >= 0); //Attempt to delete the user data (but it includes a READ operation) err = TheDb.Exec(_L("DELETE FROM B WHERE F2 = 2")); TEST(err >= 0); //Attempt to delete the user data (unconditional DELETE, no READ operations) err = TheDb.Exec(_L("DELETE FROM A")); TEST(err >= 0); //Restore the deleted table A err = TheDb.Exec(_L("INSERT INTO A(F1,B1) VALUES(1,x'41414141414141414141');INSERT INTO A(F1,B1) VALUES(2,x'42424242424242424242');INSERT INTO A(F1,B1) VALUES(3,x'43434343434343434343');INSERT INTO A(F1,B1) VALUES(4,x'44444444444444444444');")); TEST(err >= 0); //Restore the deleted record in table B err = TheDb.Exec(_L("INSERT INTO B(F2, F3, B2) VALUES(2, 'ABC', x'45454545454545454545');")); TEST2(err, 1); //Attempt to insert new user data err = TheDb.Exec(_L("INSERT INTO B(F2, F3, B2) VALUES(6, 'GHI', x'47474747474747474747');")); TEST2(err, 1); //Attempt to read the user data RSqlStatement stmt; err = stmt.Prepare(TheDb, _L("SELECT A.F1 FROM B,A WHERE A.F1 = B.F2")); TEST2(err, KErrNone); //ColumnCount() has no capabilities assigned TInt colCnt = stmt.ColumnCount(); TEST2(colCnt, 1); //DeclaredColumnType() has no capabilities assigned TSqlColumnType colType; err = stmt.DeclaredColumnType(0, colType); TEST2(err, KErrNone); TEST2(colType, ESqlInt); err = stmt.Next(); TEST2(err, KSqlAtRow); RDebug::Print(_L("Value=%d\r\n"), stmt.ColumnInt(0)); err = stmt.Next(); TEST2(err, KSqlAtRow); RDebug::Print(_L("Value=%d\r\n"), stmt.ColumnInt(0)); stmt.Close(); //Attempt to read the system data err = stmt.Prepare(TheDb, _L("SELECT * FROM SQLITE_MASTER")); TEST2(err, KErrNone); err = stmt.Next(); TEST2(err, KSqlAtRow); TPtrC p; err = stmt.ColumnText(0, p); TEST2(err, KErrNone); RDebug::Print(_L("Value=%S\r\n"), &p); stmt.Close(); NonSecureDbTest(); TheDb.Close(); }
/** @SYMTestCaseID SYSLIB-SQL-CT-1612 @SYMTestCaseDesc Two connections to the same database in the same thread. Create a test database and insert some records from both connections. Verify that all records were inserted successfully. @SYMTestPriority High @SYMTestActions Testing SQL engine behaviour when having mutiple connections to the same database in the same thread. @SYMTestExpectedResults Test must not fail @SYMREQ REQ5792 REQ5793 */ void TestMultiConnSameThread() { //Connection 1 RSqlDatabase db1; TInt err = db1.Create(KTestDbName1); TEST2(err, KErrNone); //Create test database RDebug::Print(_L("###Create test database\r\n")); _LIT8(KCreateSql, "CREATE TABLE A(Id INTEGER PRIMARY KEY AUTOINCREMENT, Data INTEGER)"); err = db1.Exec(KCreateSql); TEST(err >= 0); //Connection 2 RSqlDatabase db2; err = db2.Open(KTestDbName1); TEST2(err, KErrNone); //Insert some records using both connections RDebug::Print(_L("###Insert some records\r\n")); const TInt KRecNum = 100; _LIT8(KInsertSql, "INSERT INTO A(Data) VALUES("); for(TInt i=0;i<KRecNum;++i) { TBuf8<100> sql(KInsertSql); sql.AppendNum((TInt64)i + 1); sql.Append(_L(");")); err = (i%2) ? db1.Exec(sql) : db2.Exec(sql); if(err < 0) { TPtrC msg = (i%2) ? db1.LastErrorMessage() : db2.LastErrorMessage(); RDebug::Print(_L("##Db Error msg: \"%S\"\n\r"), &msg); } TEST2(err, 1); } //Check the database content RDebug::Print(_L("###Check the database content\r\n")); _LIT8(KSelectSql, "SELECT * FROM A"); RSqlStatement stmt; err = stmt.Prepare(db1, KSelectSql); TEST2(err, KErrNone); for(TInt j=0;j<KRecNum;++j) { err = stmt.Next(); TEST2(err, KSqlAtRow); TEST(stmt.AtRow()); TInt id = stmt.ColumnInt(0); TInt data = stmt.ColumnInt(1); TEST(id == data); } stmt.Close(); //Cleanup db2.Close(); db1.Close(); RDebug::Print(_L("###Delete the test database\r\n")); (void)RSqlDatabase::Delete(KTestDbName1); }
/** Reads the contact item from the database using the given contact item ID. @param aItemId The Id number of the contact to be read. @param aView Specifies the fields to be read. @param aInfoToRead not used @param aSessionId The ID of the session that issued the request. This is used to prevent Phonebook Synchroniser deadlock. @param aIccOpenCheck Specifies if validation with the Phonebook Synchroniser is needed for this contact. @leave KErrArgument if the itemID can't be set within select statement @leave KErrNotFound if a contact item with aItemId does not exist within contact database @leave KSqlErrBusy the database file is locked; thrown if RSqlStatement::Next() returns this error @leave KErrNoMemory an out of memory condition has occurred - the statement will be reset;thrown if RSqlStatement::Next() returns this error @leave KSqlErrGeneral a run-time error has occured - this function must not be called again;thrown if RSqlStatement::Next() returns this error @leave KSqlErrMisuse this function has been called after a previous call returned KSqlAtEnd or KSqlErrGeneral.thrown if RSqlStatement::Next() returns this error @leave KSqlErrStmtExpired the SQL statement has expired (if new functions or collating sequences have been registered or if an authorizer function has been added or changed); thrown if RSqlStatement::Next() returns this error @return CContactItem created from reading the database tables. */ CContactItem* CPplContactItemManager::ReadLC(TContactItemId aItemId, const CContactItemViewDef& aView, TInt aInfoToRead, TUint aSessionId, TBool aIccOpenCheck) const { CContactTemplate* sysTemplate = NULL; if (aItemId != KGoldenTemplateId) { sysTemplate = const_cast<CContactTemplate*>(&iContactProperties.SystemTemplateL()); } RSqlStatement selectStmt; CleanupClosePushL(selectStmt); User::LeaveIfError(selectStmt.Prepare(iDatabase, iSelectStatement->SqlStringL())); TInt err = selectStmt.BindInt(KFirstParam, aItemId); if(err != KErrNone) { User::Leave(KErrArgument); } CContactItem* item = NULL; TUid type(KNullUid); if((err = selectStmt.Next()) == KSqlAtRow) { TInt contactId = selectStmt.ColumnInt(iSelectStatement->ParameterIndex(KContactId)); TInt templateId = selectStmt.ColumnInt(iSelectStatement->ParameterIndex(KContactTemplateId)); TInt typeFlags = selectStmt.ColumnInt(iSelectStatement->ParameterIndex(KContactTypeFlags)); type = TCntPersistenceUtility::TypeFlagsToContactTypeUid(typeFlags); item = CContactItem::NewLC(type); item->SetId(contactId); TPtrC guid = selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactGuidString)); item->SetUidStringL(guid); TInt attr = (typeFlags & EContactAttributes_Mask) >> EContactAttributes_Shift; item->SetAttributes(attr); item->SetTemplateRefId(templateId); item->SetLastModified(TTime(selectStmt.ColumnInt64(iSelectStatement->ParameterIndex(KContactLastModified)))); item->SetCreationDate(TTime(selectStmt.ColumnInt64(iSelectStatement->ParameterIndex(KContactCreationDate)))); item->SetAccessCount(selectStmt.ColumnInt(iSelectStatement->ParameterIndex(KContactAccessCount))); RArray<TPtrC> fastAccessFields; CleanupClosePushL(fastAccessFields); fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactFirstName))); fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactLastName))); fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactCompanyName))); fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactFirstNamePrn))); fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactLastNamePrn))); fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactCompanyNamePrn))); //set first name, last name, company name, first name pronunciation, last name pronunciation, company name pronunciation for (TInt fieldNum = item->CardFields().Count() - 1; fieldNum>=0; --fieldNum) { CContactItemField& textField = (item->CardFields())[fieldNum]; const TInt nameFieldIndex = NameFieldIndex(textField); // Check if field is first name, last name, company name, // first name pronunciation, last name pronunciation, company name pronunciation. if (nameFieldIndex != KErrNotFound) { HBufC* text = HBufC::NewLC(fastAccessFields[nameFieldIndex].Size()); text->Des() = fastAccessFields[nameFieldIndex]; textField.TextStorage()->SetText(text); CleanupStack::PopAndDestroy(text); } } CleanupStack::PopAndDestroy(&fastAccessFields); }
void CPredictiveSearchSynchronizer::CreatePredSearchTablesL(TBool aAllTables) { PRINT1(_L("CPredictiveSearchSynchronizer::CreatePredSearchTablesL all=%d"), aAllTables); if (aAllTables) { i12keyTable.CreateTableL(); iSettingsTable.CreateTableL(); iSettingsTable.StoreCurrentLanguageL(); } iQwertyTable.CreateTableL(); _LIT(KSelectAllContactsFormat, "SELECT %S,%S,%S FROM %S;"); TInt bufSize = KSelectAllContactsFormat().Length() + KContactId().Length() + KContactFirstName().Length() + KContactLastName().Length() + KSqlContactTableName().Length(); HBufC* sqlStatement = HBufC::NewLC(bufSize); sqlStatement->Des().AppendFormat(KSelectAllContactsFormat, &KContactId, &KContactFirstName, &KContactLastName, &KSqlContactTableName); RSqlStatement stmnt; CleanupClosePushL(stmnt); PRINT1(_L("CreatePredSearchTablesL prepare SQL statement:%S"), sqlStatement); stmnt.PrepareL(iDatabase, *sqlStatement); const TInt KContactIdIndex = 0; const TInt KFirstNameIndex = 1; const TInt KLastNameIndex = 2; TInt err(KErrNone); while ((err = stmnt.Next()) == KSqlAtRow) { PRINT(_L("CreatePredSearchTablesL create CContactItem")); TInt id = KUidContactCardValue; TUid uid; uid.iUid = id; CContactItem* contact = CContactItem::NewLC(uid); contact->SetId(stmnt.ColumnInt(KContactIdIndex)); // If first name exists, write it to contact item TPtrC firstName; if (stmnt.ColumnText(KFirstNameIndex, firstName) == KErrNone) { CContactItemField* field = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName); CContactTextField* textfield = field->TextStorage(); textfield->SetTextL(firstName); contact->AddFieldL(*field); // Takes ownership CleanupStack::Pop(field); } TPtrC lastName; if (stmnt.ColumnText(KLastNameIndex, lastName) == KErrNone) { CContactItemField* field = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName); CContactTextField* textfield = field->TextStorage(); textfield->SetTextL(lastName); contact->AddFieldL(*field); // Takes ownership CleanupStack::Pop(field); } PRINT(_L("CreatePredSearchTablesL create entry to tables")); if (aAllTables) { i12keyTable.CreateInDbL(*contact); } if (ReadMailAddressesL(*contact)) { iQwertyTable.CreateInDbL(*contact); } CleanupStack::PopAndDestroy(contact); } // Leave if we didn't complete going through the results properly if (err != KSqlAtEnd) { PRINT1(_L("CreatePredSearchTablesL SQL err=%d"), err); User::Leave(err); } CleanupStack::PopAndDestroy(&stmnt); CleanupStack::PopAndDestroy(sqlStatement); PRINT(_L("CPredictiveSearchSynchronizer::CreatePredSearchTablesL ends")); }
/** Removes comm addresses from the 3 lists that are already in the database and have been updated. It takes the 3 lists in as parameters and modifies them accordingly. It also populates the list of comm address ids that are free to be recycled during updating. */ void CPplCommAddrTable::RemoveNonUpdatedAddrsL(RArray<TMatch>& aNewPhones, RArray<TPtrC>& aNewEmails, RArray<TPtrC>& aNewSips, RArray<TInt>& aFreeCommAddrIds, const TInt aItemId,CPplCommAddrTable::TCommAddrExtraInfoType aExtraInfoType ) { // build the RSqlStatement RSqlStatement stmnt; CleanupClosePushL(stmnt); stmnt.PrepareL(iDatabase, iWholeSelectStmnt->SqlStringL() ); const TInt KContactIdParamIndex(KFirstIndex); // first and only parameter in the query User::LeaveIfError(stmnt.BindInt(KContactIdParamIndex, aItemId) ) ; // fetch the results from the query and compare them with the new comm_addrs we have TInt err(KErrNone); while ((err = stmnt.Next() ) == KSqlAtRow) { const TInt KType(stmnt.ColumnInt(iWholeSelectStmnt->ParameterIndex(KCommAddrType() ) ) ); if (KType == EPhoneNumber) { TMatch phoneNumber; TPtrC valString = stmnt.ColumnTextL(iWholeSelectStmnt->ParameterIndex(KCommAddrValue() ) ); TPtrC extValString = stmnt.ColumnTextL(iWholeSelectStmnt->ParameterIndex(KCommAddrExtraValue() ) ); TInt extTypeInfoString = stmnt.ColumnInt(iWholeSelectStmnt->ParameterIndex(KCommAddrExtraTypeInfo() ) ); User::LeaveIfError(TLex(valString).Val(phoneNumber.iLowerSevenDigits) ); User::LeaveIfError(TLex(extValString).Val(phoneNumber.iUpperDigits) ); TInt matchIndex(aNewPhones.Find(phoneNumber, TIdentityRelation<TMatch>(&TMatch::Equals) ) ); // remove any phone numbers from the new list if we already // have them in the db and they haven't changed... if (matchIndex != KErrNotFound && (extTypeInfoString == aExtraInfoType)) { aNewPhones.Remove(matchIndex); } // ...and add any spare ids to the recycle list else { aFreeCommAddrIds.AppendL( stmnt.ColumnInt(iWholeSelectStmnt->ParameterIndex(KCommAddrId() ) ) ); } } else // is Email or SIP { TPtrC valString = stmnt.ColumnTextL(iWholeSelectStmnt->ParameterIndex(KCommAddrValue() ) ); TInt matchIndex(0); // remove any email and sip addresses from the new list if // we already have them in the db and they haven't changed... if (KType == EEmailAddress) { matchIndex = aNewEmails.Find(valString); if (matchIndex != KErrNotFound) { aNewEmails.Remove(matchIndex); } } else // SIP { matchIndex = aNewSips.Find(valString); if (matchIndex != KErrNotFound) { aNewSips.Remove(matchIndex); } } // ...and add any spare ids to the recycle list if (matchIndex == KErrNotFound) { aFreeCommAddrIds.AppendL( stmnt.ColumnInt(iWholeSelectStmnt->ParameterIndex(KCommAddrId() ) ) ); } } } // leave if we didn't complete going through the results properly if(err != KSqlAtEnd) { User::Leave(err); } CleanupStack::PopAndDestroy(&stmnt); }
/** Returns an array of contact item IDs for all the contact items which may contain the specified telephone number in a telephone, fax or SMS type field. This is improved version of MatchPhoneNumberL method. The number is compared starting from the right side of the number and the method returns an array of candidate matches. Punctuation (e.g. spaces) and other alphabetic characters are ignored when comparing. Leading zeros are removed. Digits are compared up to the lenght of shorter number. @param aNumber Phone number string. @return Array of contact IDs which are candidate matches. */ CContactIdArray* CPplCommAddrTable::BestMatchingPhoneNumberL(const TDesC& aNumber) { const TInt KUpperMaxLength = KMaxPhoneMatchLength - KLowerSevenDigits; CContactIdArray* phoneMatchArray = CContactIdArray::NewLC(); TMatch phoneDigits = CreatePaddedPhoneDigitsL(aNumber, KLowerSevenDigits, KUpperMaxLength); if (phoneDigits.iNumLowerDigits + phoneDigits.iNumUpperDigits > 0) { // build statement RSqlStatement stmnt; CleanupClosePushL(stmnt); stmnt.PrepareL(iDatabase, iMatchSelectStmnt->SqlStringL()); const TInt KValueParamIndex(KFirstParam); // first parameter in query... const TInt KTypeParamIndex(KValueParamIndex + 1); // ...and the second. User::LeaveIfError(stmnt.BindInt(KValueParamIndex, phoneDigits.iLowerSevenDigits)); User::LeaveIfError(stmnt.BindInt(KTypeParamIndex, EPhoneNumber)); // fetch the list of any matching contact ids TInt err(KErrNone); const TInt KContactIdIdx(iMatchSelectStmnt->ParameterIndex( KCommAddrContactId())); const TInt KExtraValueIdx(iMatchSelectStmnt->ParameterIndex(KCommAddrExtraValue())); while ((err = stmnt.Next()) == KSqlAtRow) { // Check the upper digits... TInt32 number = phoneDigits.iUpperDigits; TPtrC extValString = stmnt.ColumnTextL(KExtraValueIdx); TInt32 storedUpperDigits; User::LeaveIfError(TLex(extValString).Val(storedUpperDigits)); TInt32 stored = storedUpperDigits; TBool nonZeroInStoredFound = EFalse; TBool nonZeroInNumberFound = EFalse; while ((number != 0) && (stored != 0)) { nonZeroInNumberFound |= (number % 10 != 0); nonZeroInStoredFound |= (stored % 10 != 0); if (nonZeroInStoredFound && nonZeroInNumberFound) { break; } number /= 10; stored /= 10; } if ( (phoneDigits.iUpperDigits == 0) || (storedUpperDigits == 0) || (number == stored) ) { phoneMatchArray->AddL(stmnt.ColumnInt(KContactIdIdx)); } } // leave if we didn't complete going through the results properly if (err != KSqlAtEnd) { User::Leave(err); } CleanupStack::PopAndDestroy(&stmnt); } CleanupStack::Pop(phoneMatchArray); return phoneMatchArray; }
/** Returns an array of contact item IDs for all the contact items which may contain the specified telephone number in a telephone, fax or SMS type field. The comparison method used is not exact. The number is compared starting from the right side of the number and the method returns an array of candidate matches. Punctuation (e.g. spaces) and other alphabetic characters are ignored when comparing. Additionally, if the Contacts Model Phone Parser (CNTPHONE.DLL) is available, then any DTMF digits are also excluded from the comparision. Note that due to the way numbers are stored in the database, it is recommended that at least 7 match digits are specified even when matching a number containing fewer digits. Failure to follow this guideline may (depending on the database contents) mean that the function will not return the expected Contact IDs. @param aNumber Phone number string. @param aMatchLengthFromRight Number of digits from the right of the phone number to use. Up to 15 digits can be specified, and it is recommended that at least 7 match digits are specified. @param aDatabase The database. @return Array of contact IDs which are candidate matches. */ CContactIdArray* CPplCommAddrTable::MatchPhoneNumberL(const TDesC& aNumber, const TInt aMatchLengthFromRight) { CContactIdArray* phoneMatchArray = CContactIdArray::NewLC(); TInt numLowerDigits = aMatchLengthFromRight; TInt numUpperDigits = 0; if(numLowerDigits > KLowerSevenDigits) { // New style matching. numLowerDigits = KLowerSevenDigits; numUpperDigits = aMatchLengthFromRight - KLowerSevenDigits; } TMatch phoneDigits = CreatePaddedPhoneDigitsL(aNumber, numLowerDigits, numUpperDigits); if (phoneDigits.iNumLowerDigits + phoneDigits.iNumUpperDigits > 0) { // build statement RSqlStatement stmnt; CleanupClosePushL(stmnt); stmnt.PrepareL(iDatabase, iMatchSelectStmnt->SqlStringL() ); const TInt KValueParamIndex(KFirstParam); // first parameter in query... const TInt KTypeParamIndex(KValueParamIndex + 1); // ...and the second. User::LeaveIfError(stmnt.BindInt(KValueParamIndex, phoneDigits.iLowerSevenDigits )); User::LeaveIfError(stmnt.BindInt(KTypeParamIndex, EPhoneNumber )); // fetch the list of any matching contact ids TInt err(KErrNone); const TInt KContactIdIdx(iMatchSelectStmnt->ParameterIndex(KCommAddrContactId() ) ); const TInt KExtraValueIdx(iMatchSelectStmnt->ParameterIndex(KCommAddrExtraValue() ) ); while ((err = stmnt.Next() ) == KSqlAtRow) { if (aMatchLengthFromRight <= KLowerSevenDigits) { // Matching 7 or less digits...we've already matched. phoneMatchArray->AddL(stmnt.ColumnInt(KContactIdIdx) ); } else { // Check the upper digits... TInt32 storedUpperDigits(0); TPtrC extValString = stmnt.ColumnTextL(KExtraValueIdx); User::LeaveIfError(TLex(extValString).Val(storedUpperDigits) ); const TInt KDigitsToRemove = KMaxPhoneMatchLength - KLowerSevenDigits - phoneDigits.iNumUpperDigits; for(TInt i = 0; i < KDigitsToRemove; ++i) { // repeatedly divide by 10 to lop off the appropriate number of digits from the right storedUpperDigits /= 10; } storedUpperDigits = TMatch::PadOutPhoneMatchNumber(storedUpperDigits, KDigitsToRemove); if (phoneDigits.iUpperDigits == storedUpperDigits) { phoneMatchArray->AddL(stmnt.ColumnInt(KContactIdIdx) ); } } } // leave if we didn't complete going through the results properly if(err != KSqlAtEnd) { User::Leave(err); } CleanupStack::PopAndDestroy(&stmnt); } CleanupStack::Pop(phoneMatchArray); return phoneMatchArray; }