QList<QContactLocalId> CntFilterDefault::contacts( const QContactFilter &filter, const QList<QContactSortOrder> &sortOrders, bool &filterSupportedflag, QContactManager::Error* error) { Q_UNUSED(sortOrders); Q_UNUSED(filterSupportedflag); //Check if any invalid filter is passed if(!filterSupported(filter)) { *error = QContactManager::NotSupportedError; return QList<QContactLocalId>(); } QList<QContactLocalId> idList; //Create the query QString sqlQuery; createSelectQuery( filter,sqlQuery,error); QString sortQuery = m_dbInfo.getSortQuery(sortOrders, sqlQuery, error); //fetch the contacts if(*error == QContactManager::NoError) { idList = m_srvConnection.searchContacts(sortQuery, error); } return idList; }
void CntFilterIntersection::createSelectQuery(const QContactFilter& filter, QString& selectquery, QContactManager::Error* error) { const QContactIntersectionFilter& intersectionfilter = static_cast<const QContactIntersectionFilter&>(filter); if (!filterSupported(filter)) { *error = QContactManager::NotSupportedError; return; } QList<QContactFilter> individualFilters = intersectionfilter.filters(); //QString selectquery; int fltrcnt = individualFilters.count(); if (fltrcnt) getSelectQueryforFilter(individualFilters[0],selectquery,error); for (int i=1; i< fltrcnt ; i++) { QString query; getSelectQueryforFilter(individualFilters[i],query,error); if (*error == QContactManager::NoError ) { selectquery.append(" INTERSECT "); selectquery += query; } } }
void CntFilterRelationship::createSelectQuery(const QContactFilter& filter, QString& sqlQuery, QContactManager::Error* error) { if (!filterSupported(filter)) { *error = QContactManager::NotSupportedError; return; } QContactRelationshipFilter relationfilter(filter); QContactId id_to_search = relationfilter.relatedContactId(); if(relationfilter.relatedContactRole() == QContactRelationship::First ) { sqlQuery = QString("SELECT DISTINCT contact_group_member_id FROM groups WHERE contact_group_id = %1").arg(id_to_search.localId()); } else if(relationfilter.relatedContactRole() == QContactRelationship::Second ) { sqlQuery = QString("SELECT DISTINCT contact_group_id FROM groups WHERE contact_group_member_id = %1").arg(id_to_search.localId()); } else if(relationfilter.relatedContactRole() == QContactRelationship::Either ) { sqlQuery = QString("SELECT contact_group_member_id FROM groups WHERE contact_group_id = %1").arg(id_to_search.localId()); + " union " + QString("SELECT DISTINCT contact_group_id FROM groups WHERE contact_group_id = %1").arg(id_to_search.localId()); } }
QList<QContactLocalId> CntFilterDetail::contacts( const QContactFilter &filter, const QList<QContactSortOrder> &sortOrders, bool &filterSupportedflag, QContactManager::Error* error) { //Check if any invalid filter is passed if (!filterSupported(filter) ) { *error = QContactManager::NotSupportedError; filterSupportedflag = false; return QList<QContactLocalId>(); } QList<QContactLocalId> idList; QContactDetailFilter detailFilter(filter); QString sqlQuery; //Check for phonenumber. Special handling needed if ( (detailFilter.detailDefinitionName() == QContactPhoneNumber::DefinitionName ) && (detailFilter.detailFieldName() != QContactPhoneNumber::FieldSubTypes)) { //Handle phonenumber ... if(detailFilter.detailFieldName().isEmpty()){ fetchAllPhoneNumbers(sqlQuery); } else if(bestMatchingEnabled()) { bestMatchPhoneNumberQuery(filter,sqlQuery,error); } else { createMatchPhoneNumberQuery(filter,sqlQuery,error); } if (*error == QContactManager::NoError) { //fetch the contacts idList = m_srvConnection.searchContacts(sqlQuery,error); } } else if (detailFilter.matchFlags() == QContactFilter::MatchKeypadCollation) { //predictive search filter idList = HandlePredictiveSearchFilter(filter,error); } // handle other cases else { createSelectQuery(filter,sqlQuery,error); QString sortQuery = m_dbInfo.getSortQuery(sortOrders, sqlQuery, error); if (*error == QContactManager::NoError) { //fetch the contacts idList = m_srvConnection.searchContacts(sortQuery, error); } } return idList; }
void CntFilterDefault::createSelectQuery(const QContactFilter& filter, QString& sqlQuery, QContactManager::Error* error) { //Check if any invalid filter is passed if(!filterSupported(filter)) { *error = QContactManager::NotSupportedError; } //For default filter, just return the below query sqlQuery = "SELECT DISTINCT contact_id FROM contact WHERE (type_flags>>24)<=1 OR (type_flags>>24)=3"; }
void CntFilterDetail::createSelectQuery(const QContactFilter& filter, QString& sqlQuery, QContactManager::Error* error) { if (!filterSupported(filter)) { *error = QContactManager::NotSupportedError; return; } QContactDetailFilter detailFilter(filter); //type if (detailFilter.detailDefinitionName() == QContactType::DefinitionName) { if (detailFilter.value().toString() == QContactType::TypeContact) sqlQuery = "SELECT contact_id FROM contact WHERE (type_flags>>24)<=1"; else if (detailFilter.value().toString() == QContactType::TypeGroup) sqlQuery = "SELECT contact_id FROM contact WHERE (type_flags>>24)=3"; } else if (detailFilter.detailDefinitionName() == QContactGuid::DefinitionName) { if (detailFilter.detailFieldName() == QContactGuid::FieldGuid) { QStringList fullGuidValue = detailFilter.value().toString().split('-'); if (fullGuidValue.count() == 3) { QString localGuidValue = fullGuidValue.at(1); sqlQuery = "SELECT contact_id FROM contact WHERE guid_string = '" + localGuidValue + '\''; } } } else if (detailFilter.detailDefinitionName() == QContactEmailAddress::DefinitionName && (detailFilter.detailFieldName().isEmpty())) { QString type = QString(" type = %1").arg(CntDbInfo::EEmailAddress); QString whereClause = " WHERE" + type; sqlQuery = "SELECT DISTINCT contact_id FROM comm_addr" + whereClause; } else if (detailFilter.detailDefinitionName() == QContactOnlineAccount::DefinitionName && (detailFilter.detailFieldName().isEmpty())) { QString type = QString(" type = %1").arg(CntDbInfo::ESipAddress); QString whereClause = " WHERE" + type; sqlQuery = "SELECT DISTINCT contact_id FROM comm_addr" + whereClause; } //everything else else { QString tableName; QString sqlWhereClause; getTableNameWhereClause(detailFilter,tableName,sqlWhereClause,error); //Create the sql query sqlQuery += "SELECT DISTINCT contact_id FROM " + tableName + " WHERE " + sqlWhereClause; } }
void CntFilterDetail::createSelectQuery(const QContactFilter& filter, QString& sqlQuery, QContactManager::Error* error) { if (!filterSupported(filter)) { *error = QContactManager::NotSupportedError; return; } QContactDetailFilter detailFilter(filter); //display label if (detailFilter.detailDefinitionName() == QContactDisplayLabel::DefinitionName) { CntFilterDetailDisplayLabel displayLabelFilter; displayLabelFilter.createSelectQuery(filter, sqlQuery, error); } //type else if (detailFilter.detailDefinitionName() == QContactType::DefinitionName) { if (detailFilter.value().toString() == QContactType::TypeContact) sqlQuery = "SELECT contact_id FROM contact WHERE (type_flags>>24)=0"; else if (detailFilter.value().toString() == QContactType::TypeGroup) sqlQuery = "SELECT contact_id FROM contact WHERE (type_flags>>24)=3"; } else if (detailFilter.detailDefinitionName() == QContactGuid::DefinitionName) { if (detailFilter.detailFieldName() == QContactGuid::FieldGuid) { QStringList fullGuidValue = detailFilter.value().toString().split('-'); if (fullGuidValue.count() == 3) { QString localGuidValue = fullGuidValue.at(1); sqlQuery = "SELECT contact_id FROM contact WHERE guid_string = '" + localGuidValue + '\''; } } } //everything else else { QString tableName; QString sqlWhereClause; getTableNameWhereClause(detailFilter,tableName,sqlWhereClause,error); //Create the sql query sqlQuery += "SELECT DISTINCT contact_id FROM " + tableName + " WHERE " + sqlWhereClause; } }
/* * Creates an sql query to fetch 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. * * 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. */ void CntFilterDetail::createMatchPhoneNumberQuery( const QContactFilter& filter, QString& sqlQuery, QContactManager::Error* error) { if (!filterSupported(filter) ) { *error = QContactManager::NotSupportedError; return; } QContactDetailFilter detailFilter(filter); QString number((detailFilter.value()).toString()); TPtrC numberPtr(reinterpret_cast<const TUint16*>(number.utf16())); TInt matchLengthFromRight(KDefaultMatchLength); // no need to propagate error, we can use the default match length TRAP_IGNORE(getMatchLengthL(matchLengthFromRight)); TInt numLowerDigits = matchLengthFromRight; TInt numUpperDigits = 0; if (numLowerDigits > KLowerSevenDigits) { numLowerDigits = KLowerSevenDigits; numUpperDigits = matchLengthFromRight - KLowerSevenDigits; } else if (numLowerDigits == 0) { // best match phonenumbers numLowerDigits = KLowerSevenDigits; } TMatch phoneDigits = createPaddedPhoneDigits( numberPtr, numLowerDigits, numUpperDigits, error); if (*error == QContactManager::NoError) { // select fields for contacts that match phone lookup // SELECT contact_id FROM comm_addr // WHERE value = [value string] AND type = [type value]; // QString type = QString(" type = %1").arg(CntDbInfo::EPhoneNumber); QString value = QString(" value = %1").arg(phoneDigits.iLowerSevenDigits); QString whereClause = " WHERE" + value + " AND" + type; if (matchLengthFromRight <= KLowerSevenDigits) { // Matching 7 or less digits... sqlQuery = "SELECT contact_id FROM comm_addr" + whereClause; } else { // Checking the upper digits... // select fields for contacts that match phone lookup // SELECT contact_id, extra_value FROM comm_addr // WHERE value = [value string] AND type = [type value]; // QString type = QString(" type = %1").arg(CntDbInfo::EPhoneNumber); QString value = QString(" value = %1").arg(phoneDigits.iLowerSevenDigits); QString whereClause = " WHERE" + value + " AND" + type; sqlQuery = "SELECT contact_id, extra_value FROM comm_addr" + whereClause; QList<QPair<QContactLocalId, QString> > contactMatches = m_srvConnection.searchPhoneNumbers(sqlQuery, error); // Check if search query was successful if (*error != QContactManager::NoError) { return; } QStringList list; for (int i=0; i<contactMatches.count(); ++i) { // Check the upper digits... TInt32 storedUpperDigits(0); QString extraValue = contactMatches.at(i).second; TPtrC extValString(reinterpret_cast<const TUint16*>(extraValue.utf16())); if (TLex(extValString).Val(storedUpperDigits) == KErrNone) { const TInt KDigitsToRemove = KMaxPhoneMatchLength - KLowerSevenDigits - phoneDigits.iNumUpperDigits; for (TInt j = 0; j < KDigitsToRemove; ++j) { // 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) { list.append(QString("%1").arg(contactMatches.at(i).first)); } } else { *error = QContactManager::UnspecifiedError; } } // Recreate query to fetch all match ids // SELECT DISTINCT contact_id FROM contact WHERE contact_id in ( // .. // ) QString ids = list.join(" ,"); sqlQuery = "SELECT DISTINCT contact_id FROM contact WHERE contact_id in ("; sqlQuery += ids; sqlQuery += ')'; } // refine search if (bestMatchingEnabled()) { QList<QContactLocalId> list = m_srvConnection.searchContacts(sqlQuery,error); QList<QContactLocalId> bestMatchingIds; if (*error == QContactManager::NoError) { TRAP_IGNORE( bestMatchingIds = getBestMatchPhoneNumbersL(number, list, error); ) if (bestMatchingIds.count()>0) { // recreate query QString selectQuery = " SELECT contact_id FROM comm_addr WHERE contact_id in ("; QString ids = QString("%1").arg(bestMatchingIds.at(0)); for(int i=1; i<bestMatchingIds.count(); ++i) { ids += QString(" ,%1").arg(bestMatchingIds.at(i)); } selectQuery += ids + ')'; sqlQuery = selectQuery; } else { // empty list QString selectQuery = " SELECT contact_id FROM comm_addr WHERE contact_id in (null)"; sqlQuery = selectQuery; } }
/* * Returns an sql query to fetch 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 createMatchPhoneNumberQuery 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 length of shorter number. */ void CntFilterDetail::bestMatchPhoneNumberQuery(const QContactFilter& filter, QString& sqlQuery, QContactManager::Error* error) { if (!filterSupported(filter) ) { *error = QContactManager::NotSupportedError; return; } QContactDetailFilter detailFilter(filter); QString number((detailFilter.value()).toString()); TPtrC numberPtr(reinterpret_cast<const TUint16*>(number.utf16())); const TInt KUpperMaxLength = KMaxPhoneMatchLength - KLowerSevenDigits; TMatch phoneDigits = createPaddedPhoneDigits(numberPtr, KLowerSevenDigits, KUpperMaxLength, error); if (*error == QContactManager::NoError) { // select fields for contacts that match phone lookup // SELECT contact_id, extra_value FROM comm_addr // WHERE value = [value string] AND type = [type value]; // QString type = QString(" type = %1").arg(CntDbInfo::EPhoneNumber); QString value = QString(" value = %1").arg(phoneDigits.iLowerSevenDigits); QString whereClause = " WHERE" + value + " AND" + type; sqlQuery = "SELECT contact_id, extra_value FROM comm_addr" + whereClause; QList<QPair<QContactLocalId, QString> > contactMatches = m_srvConnection.searchPhoneNumbers(sqlQuery, error); // Check if search query was successful if (*error != QContactManager::NoError) { return; } QStringList list; for (int i=0; i<contactMatches.count(); ++i) { // Check the upper digits... TInt32 number = phoneDigits.iUpperDigits; QString extraValue = contactMatches.at(i).second; TPtrC extValString(reinterpret_cast<const TUint16*>(extraValue.utf16())); TInt32 storedUpperDigits(0); if (TLex(extValString).Val(storedUpperDigits) == KErrNone) { 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)) { list.append(QString("%1").arg(contactMatches.at(i).first)); } } else { *error = QContactManager::UnspecifiedError; return; } } // Recreate query to fetch all match ids // SELECT DISTINCT contact_id FROM contact WHERE contact_id in ( // .. // ) QString ids = list.join(" ,"); sqlQuery = "SELECT DISTINCT contact_id FROM contact WHERE contact_id in ("; sqlQuery += ids; sqlQuery += ')'; } }