MOBILE-2319 db: Use REPLACE in inserts

main
Dani Palou 2018-03-20 15:44:26 +01:00
parent 8dc255ea20
commit a6ea996778
16 changed files with 23 additions and 52 deletions

View File

@ -514,7 +514,7 @@ export class AddonCalendarProvider {
notificationtime: e.notificationtime || -1 notificationtime: e.notificationtime || -1
}; };
return db.insertOrUpdateRecord(this.EVENTS_TABLE, eventRecord, { id: eventRecord.id }); return db.insertRecord(this.EVENTS_TABLE, eventRecord);
})); }));
}); });
@ -539,7 +539,7 @@ export class AddonCalendarProvider {
event.notificationtime = time; event.notificationtime = time;
return site.getDb().insertOrUpdateRecord(this.EVENTS_TABLE, event, { id: event.id }).then(() => { return site.getDb().insertRecord(this.EVENTS_TABLE, event).then(() => {
return this.scheduleEventNotification(event, time); return this.scheduleEventNotification(event, time);
}); });
}); });

View File

@ -148,11 +148,7 @@ export class AddonMessagesOfflineProvider {
deviceoffline: this.appProvider.isOnline() ? 0 : 1 deviceoffline: this.appProvider.isOnline() ? 0 : 1
}; };
return site.getDb().insertOrUpdateRecord(this.MESSAGES_TABLE, entry, { return site.getDb().insertRecord(this.MESSAGES_TABLE, entry).then(() => {
touserid: toUserId,
smallmessage: message,
timecreated: entry.timecreated
}).then(() => {
return entry; return entry;
}); });
}); });
@ -173,11 +169,7 @@ export class AddonMessagesOfflineProvider {
data = { deviceoffline: value ? 1 : 0 }; data = { deviceoffline: value ? 1 : 0 };
messages.forEach((message) => { messages.forEach((message) => {
promises.push(db.insertOrUpdateRecord(this.MESSAGES_TABLE, data, { promises.push(db.insertRecord(this.MESSAGES_TABLE, data));
touserid: message.touserid,
smallmessage: message.smallmessage,
timecreated: message.timecreated
}));
}); });
return Promise.all(promises); return Promise.all(promises);

View File

@ -218,13 +218,8 @@ export class AddonNotesOfflineProvider {
created: now, created: now,
lastmodified: now lastmodified: now
}; };
const conditions = {
userid: userId,
content: content,
created: now
};
return site.getDb().insertOrUpdateRecord(this.NOTES_TABLE, data, conditions).then(() => { return site.getDb().insertRecord(this.NOTES_TABLE, data).then(() => {
return data; return data;
}); });
}); });

View File

@ -402,7 +402,7 @@ export class AddonPushNotificationsProvider {
number: value number: value
}; };
return this.appDB.insertOrUpdateRecord(this.BADGE_TABLE, entry, {siteid: siteId, addon: addon}).then(() => { return this.appDB.insertRecord(this.BADGE_TABLE, entry).then(() => {
return value; return value;
}); });
} }

View File

@ -780,7 +780,7 @@ export class CoreSite {
entry.key = preSets.cacheKey; entry.key = preSets.cacheKey;
} }
return this.db.insertOrUpdateRecord(this.WS_CACHE_TABLE, entry, { id: id }); return this.db.insertRecord(this.WS_CACHE_TABLE, entry);
}); });
} }

View File

@ -625,7 +625,7 @@ export class SQLiteDB {
questionMarks = ',?'.repeat(keys.length).substr(1); questionMarks = ',?'.repeat(keys.length).substr(1);
return [ return [
`INSERT INTO ${table} (${fields}) VALUES (${questionMarks})`, `INSERT OR REPLACE INTO ${table} (${fields}) VALUES (${questionMarks})`,
keys.map((key) => data[key]) keys.map((key) => data[key])
]; ];
} }
@ -644,24 +644,6 @@ export class SQLiteDB {
}); });
} }
/**
* Insert or update a record.
*
* @param {string} table The database table.
* @param {object} data An object with the fields to insert/update: fieldname=>fieldvalue.
* @param {object} conditions The conditions to check if the record already exists (and to update it).
* @return {Promise<any>} Promise resolved with done.
*/
insertOrUpdateRecord(table: string, data: object, conditions: object): Promise<any> {
return this.getRecord(table, conditions).then(() => {
// It exists, update it.
return this.updateRecords(table, data, conditions);
}).catch(() => {
// Doesn't exist, insert it.
return this.insertRecord(table, data);
});
}
/** /**
* Insert a record into a table and return the "rowId" field. * Insert a record into a table and return the "rowId" field.
* *

View File

@ -699,7 +699,7 @@ export class CoreCourseProvider {
previousDownloadTime: previousDownloadTime previousDownloadTime: previousDownloadTime
}; };
return site.getDb().insertOrUpdateRecord(this.COURSE_STATUS_TABLE, data, { id: courseId }); return site.getDb().insertRecord(this.COURSE_STATUS_TABLE, data);
} }
}).then(() => { }).then(() => {
// Success inserting, trigger event. // Success inserting, trigger event.

View File

@ -417,7 +417,9 @@ export class CoreCourseModulePrefetchDelegate extends CoreDelegate {
courseId: courseId, courseId: courseId,
time: this.timeUtils.timestamp() time: this.timeUtils.timestamp()
}; };
site.getDb().insertOrUpdateRecord(this.CHECK_UPDATES_TIMES_TABLE, entry, { courseId: courseId }); site.getDb().insertRecord(this.CHECK_UPDATES_TIMES_TABLE, entry).catch(() => {
// Ignore errors.
});
return this.treatCheckUpdatesResult(data.toCheck, response, result); return this.treatCheckUpdatesResult(data.toCheck, response, result);
}).catch((error) => { }).catch((error) => {

View File

@ -671,7 +671,7 @@ export class LocalNotificationsMock extends LocalNotifications {
notification = Object.assign({}, notification); // Clone the object. notification = Object.assign({}, notification); // Clone the object.
notification.triggered = !!triggered; notification.triggered = !!triggered;
return this.appDB.insertOrUpdateRecord(this.DESKTOP_NOTIFS_TABLE, notification, { id: notification.id }); return this.appDB.insertRecord(this.DESKTOP_NOTIFS_TABLE, notification);
} }
/** /**

View File

@ -383,7 +383,7 @@ export class CoreUserProvider {
profileimageurl: avatar profileimageurl: avatar
}; };
return site.getDb().insertOrUpdateRecord(this.USERS_TABLE, userRecord, { id: userId }); return site.getDb().insertRecord(this.USERS_TABLE, userRecord);
}); });
} }

View File

@ -81,6 +81,6 @@ export class CoreConfigProvider {
* @return {Promise<any>} Promise resolved when done. * @return {Promise<any>} Promise resolved when done.
*/ */
set(name: string, value: boolean | number | string): Promise<any> { set(name: string, value: boolean | number | string): Promise<any> {
return this.appDB.insertOrUpdateRecord(this.TABLE_NAME, { name: name, value: value }, { name: name }); return this.appDB.insertRecord(this.TABLE_NAME, { name: name, value: value });
} }
} }

View File

@ -459,7 +459,7 @@ export class CoreCronDelegate {
value: time value: time
}; };
return this.appDB.insertOrUpdateRecord(this.CRON_TABLE, entry, { id: id }); return this.appDB.insertRecord(this.CRON_TABLE, entry);
} }
/** /**

View File

@ -477,7 +477,7 @@ export class CoreFilepoolProvider {
componentId: componentId || '' componentId: componentId || ''
}; };
return db.insertOrUpdateRecord(this.LINKS_TABLE, newEntry, { fileId: fileId }); return db.insertRecord(this.LINKS_TABLE, newEntry);
}); });
} }
@ -544,7 +544,7 @@ export class CoreFilepoolProvider {
values.fileId = fileId; values.fileId = fileId;
return this.sitesProvider.getSiteDb(siteId).then((db) => { return this.sitesProvider.getSiteDb(siteId).then((db) => {
return db.insertOrUpdateRecord(this.FILES_TABLE, values, { fileId: fileId }); return db.insertRecord(this.FILES_TABLE, values);
}); });
} }
@ -2766,7 +2766,7 @@ export class CoreFilepoolProvider {
// The package already has this status, no need to change it. // The package already has this status, no need to change it.
promise = Promise.resolve(); promise = Promise.resolve();
} else { } else {
promise = site.getDb().insertOrUpdateRecord(this.PACKAGES_TABLE, packageEntry, { id: packageId }); promise = site.getDb().insertRecord(this.PACKAGES_TABLE, packageEntry);
} }
return promise.then(() => { return promise.then(() => {

View File

@ -500,6 +500,6 @@ export class CoreLocalNotificationsProvider {
at: parseInt(notification.at, 10) at: parseInt(notification.at, 10)
}; };
return this.appDB.insertOrUpdateRecord(this.TRIGGERED_TABLE, entry, { id: notification.id }); return this.appDB.insertRecord(this.TRIGGERED_TABLE, entry);
} }
} }

View File

@ -623,7 +623,7 @@ export class CoreSitesProvider {
loggedOut: 0 loggedOut: 0
}; };
return this.appDB.insertOrUpdateRecord(this.SITES_TABLE, entry, { id: id }); return this.appDB.insertRecord(this.SITES_TABLE, entry);
} }
/** /**
@ -895,7 +895,7 @@ export class CoreSitesProvider {
siteId: siteId siteId: siteId
}; };
return this.appDB.insertOrUpdateRecord(this.CURRENT_SITE_TABLE, entry, { id: 1 }).then(() => { return this.appDB.insertRecord(this.CURRENT_SITE_TABLE, entry).then(() => {
this.eventsProvider.trigger(CoreEventsProvider.LOGIN, {}, siteId); this.eventsProvider.trigger(CoreEventsProvider.LOGIN, {}, siteId);
}); });
} }

View File

@ -142,7 +142,7 @@ export class CoreSyncProvider {
data.component = component; data.component = component;
data.id = id; data.id = id;
return db.insertOrUpdateRecord(this.SYNC_TABLE, data, { component: component, id: id }); return db.insertRecord(this.SYNC_TABLE, data);
}); });
} }