Skip to content

Commit

Permalink
Merge branch 'jb52969-detail-timestamps' into 'master'
Browse files Browse the repository at this point in the history
[qtcontacts-sqlite] Add 'created' and 'modified' columns to Details table. JB#52969

See merge request mer-core/qtcontacts-sqlite!57
  • Loading branch information
blam committed Feb 22, 2021
2 parents 3d50f95 + 6527885 commit a0d3937
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/engine/contactreader.cpp
Expand Up @@ -689,6 +689,8 @@ static void readDetail(QContact *contact, QSqlQuery &query, quint32 contactId, q
const QVariant modifiableVariant = query.value(col++);
const bool nonexportable = query.value(col++).toBool();
const int changeFlags = query.value(col++).toInt();
const QDateTime created = query.value(col++).toDateTime();
const QDateTime modified = query.value(col++).toDateTime();

// only save the detail to the contact if it hasn't been deleted,
// or if we are part of a sync fetch (i.e. keepChangeFlags is true)
Expand Down Expand Up @@ -739,6 +741,9 @@ static void readDetail(QContact *contact, QSqlQuery &query, quint32 contactId, q
setValue(&detail, QContactDetail__FieldChangeFlags, changeFlags);
}

setValue(&detail, QContactDetail__FieldCreated, created);
setValue(&detail, QContactDetail__FieldModified, modified);

// Constraints should be applied unless generating a partial aggregate; the partial aggregate
// is intended for modification, so adding constraints prevents it from being used correctly.
// Normal aggregate contact details are always immutable.
Expand Down Expand Up @@ -2524,6 +2529,8 @@ QContactManager::Error ContactReader::queryContacts(
"Details.modifiable,"
"COALESCE(Details.nonexportable, 0),"
"Details.changeFlags, "
"Details.created, "
"Details.modified, "
"%1 "
"FROM temp.%2 "
"CROSS JOIN Details ON Details.contactId = temp.%2.contactId " // Cross join ensures we scan the temp table first
Expand All @@ -2545,7 +2552,7 @@ QContactManager::Error ContactReader::queryContacts(
QHash<QString, QPair<ReadDetail, int> > readProperties;

// Skip the Details table fields, and the indexing fields of the first join table
int offset = 11 + 2;
int offset = 13 + 2;

const ContactWriter::DetailList &definitionMask = fetchHint.detailTypesHint();

Expand Down Expand Up @@ -2633,6 +2640,10 @@ QContactManager::Error ContactReader::queryContacts(
contact.saveDetail(&deactivated);
}

// ignore created and modified timestamps, will be saved by readDetail()
col++;
col++;

int contactType = contactQuery.value(col++).toInt();
QContactType typeDetail = contact.detail<QContactType>();
typeDetail.setType(static_cast<QContactType::TypeValues>(contactType));
Expand Down
14 changes: 12 additions & 2 deletions src/engine/contactsdatabase.cpp
Expand Up @@ -337,7 +337,9 @@ static const char *createDetailsTable =
"\n modifiable BOOL,"
"\n nonexportable BOOL,"
"\n changeFlags INTEGER DEFAULT 0,"
"\n unhandledChangeFlags INTEGER DEFAULT 0);";
"\n unhandledChangeFlags INTEGER DEFAULT 0,"
"\n created DATETIME,"
"\n modified DATETIME);";

static const char *createDetailsRemoveIndex =
"\n CREATE INDEX DetailsRemoveIndex ON Details(contactId, detail);";
Expand Down Expand Up @@ -1676,6 +1678,13 @@ static const char *upgradeVersion22[] = {
0 // NULL-terminated
};

static const char *upgradeVersion23[] = {
"\n ALTER TABLE Details ADD COLUMN created DATETIME",
"\n ALTER TABLE Details ADD COLUMN modified DATETIME",
"PRAGMA user_version=24",
0 // NULL-terminated
};

typedef bool (*UpgradeFunction)(QSqlDatabase &database);

struct UpdatePhoneNormalization
Expand Down Expand Up @@ -2197,9 +2206,10 @@ static UpgradeOperation upgradeVersions[] = {
{ 0, upgradeVersion20 },
{ 0, upgradeVersion21 },
{ 0, upgradeVersion22 },
{ 0, upgradeVersion23 },
};

static const int currentSchemaVersion = 23;
static const int currentSchemaVersion = 24;

static bool execute(QSqlDatabase &database, const QString &statement)
{
Expand Down
18 changes: 15 additions & 3 deletions src/engine/contactwriter.cpp
Expand Up @@ -2001,7 +2001,9 @@ quint32 writeCommonDetails(ContactsDatabase &db, quint32 contactId, quint32 deta
" modifiable,"
" nonexportable,"
" changeFlags,"
" unhandledChangeFlags)"
" unhandledChangeFlags,"
" created,"
" modified)"
" VALUES ("
" :contactId,"
" :detail,"
Expand All @@ -2013,7 +2015,10 @@ quint32 writeCommonDetails(ContactsDatabase &db, quint32 contactId, quint32 deta
" :modifiable,"
" :nonexportable,"
" %1,"
" %2)").arg(aggregateContact ? QStringLiteral("0") : QStringLiteral("1")) // ChangeFlags::IsAdded
" %2,"
" :created,"
" :modified)"
).arg(aggregateContact ? QStringLiteral("0") : QStringLiteral("1")) // ChangeFlags::IsAdded
.arg((aggregateContact || !recordUnhandledChangeFlags) ? QStringLiteral("0") : QStringLiteral("1"))
: QStringLiteral(
" UPDATE Details SET"
Expand All @@ -2025,7 +2030,8 @@ quint32 writeCommonDetails(ContactsDatabase &db, quint32 contactId, quint32 deta
" provenance = :provenance,"
" modifiable = :modifiable,"
" nonexportable = :nonexportable"
" %1 %2"
" %1 %2,"
" modified = :modified"
" WHERE contactId = :contactId AND detailId = :detailId")
.arg(aggregateContact ? QString() : QStringLiteral(", ChangeFlags = ChangeFlags | 2")) // ChangeFlags::IsModified
.arg((aggregateContact || !recordUnhandledChangeFlags) ? QString() : QStringLiteral(", UnhandledChangeFlags = UnhandledChangeFlags | 2")));
Expand All @@ -2041,9 +2047,14 @@ quint32 writeCommonDetails(ContactsDatabase &db, quint32 contactId, quint32 deta
? detailValue(detail, QContactDetail__FieldModifiable)
: QVariant());
const QVariant nonexportable = detailValue(detail, QContactDetail__FieldNonexportable);
const QVariant modified = aggregateContact
? detailValue(detail, QContactDetail__FieldModified)
: ContactsDatabase::dateTimeString(QDateTime::currentDateTimeUtc());

if (detailId > 0) {
query.bindValue(":detailId", detailId);
} else {
query.bindValue(":created", modified);
}

query.bindValue(":contactId", contactId);
Expand All @@ -2055,6 +2066,7 @@ quint32 writeCommonDetails(ContactsDatabase &db, quint32 contactId, quint32 deta
query.bindValue(":provenance", provenance);
query.bindValue(":modifiable", modifiable);
query.bindValue(":nonexportable", nonexportable);
query.bindValue(":modified", modified);

if (!ContactsDatabase::execute(query)) {
query.reportError(QStringLiteral("Failed to write common details for %1\ndetailUri: %2, linkedDetailUris: %3")
Expand Down
2 changes: 2 additions & 0 deletions src/extensions/qtcontacts-extensions.h
Expand Up @@ -54,6 +54,8 @@ static const int QContactDetail__FieldNonexportable = (QContactDetail::FieldLink
static const int QContactDetail__FieldChangeFlags = (QContactDetail::FieldLinkedDetailUris+4);
static const int QContactDetail__FieldUnhandledChangeFlags = (QContactDetail::FieldLinkedDetailUris+5);
static const int QContactDetail__FieldDatabaseId = (QContactDetail::FieldLinkedDetailUris+6);
static const int QContactDetail__FieldCreated = (QContactDetail::FieldLinkedDetailUris+7);
static const int QContactDetail__FieldModified = (QContactDetail::FieldLinkedDetailUris+8);

// The following change types can be reported for a detail when fetched via the synchronization plugin fetch API.
static const int QContactDetail__ChangeFlag_IsAdded = 1 << 0;
Expand Down

0 comments on commit a0d3937

Please sign in to comment.