From 1532b8505673eb730e9b10cf5d5408ef0a519240 Mon Sep 17 00:00:00 2001 From: Edward Lee Date: Fri, 9 Apr 2010 10:20:58 -0700 Subject: [PATCH] Bug 558264 - Form data fails to sync when there's nothing to upload [r=mconnor] Wrap the createStatement calls to guard against missing guid columns and lazily add it. --HG-- extra : rebase_source : c59a692c6ad08321835c75023b54f02e8d1964da --- services/sync/modules/engines/forms.js | 48 ++++++++++++++------------ 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/services/sync/modules/engines/forms.js b/services/sync/modules/engines/forms.js index 13e08c22fab8..2a38090c5f5e 100644 --- a/services/sync/modules/engines/forms.js +++ b/services/sync/modules/engines/forms.js @@ -50,7 +50,7 @@ Cu.import("resource://weave/type_records/forms.js"); let FormWrapper = { getAllEntries: function getAllEntries() { let entries = []; - let query = Svc.Form.DBConnection.createStatement( + let query = this.createStatement( "SELECT fieldname, value FROM moz_formhistory"); while (query.executeStep()) { entries.push({ @@ -62,7 +62,7 @@ let FormWrapper = { }, getEntry: function getEntry(guid) { - let query = Svc.Form.DBConnection.createStatement( + let query = this.createStatement( "SELECT fieldname, value FROM moz_formhistory WHERE guid = :guid"); query.params.guid = guid; if (!query.executeStep()) @@ -75,24 +75,10 @@ let FormWrapper = { }, getGUID: function getGUID(name, value) { - let getQuery = "SELECT guid FROM moz_formhistory " + - "WHERE fieldname = :name AND value = :value"; - try { - getQuery = Svc.Form.DBConnection.createStatement(getQuery); - } - catch(ex) { - // The column must not exist yet, so add it with an index - Svc.Form.DBConnection.executeSimpleSQL( - "ALTER TABLE moz_formhistory ADD COLUMN guid TEXT"); - Svc.Form.DBConnection.executeSimpleSQL( - "CREATE INDEX IF NOT EXISTS moz_formhistory_guid_index " + - "ON moz_formhistory (guid)"); - - // Try creating the query now that the column exists - getQuery = Svc.Form.DBConnection.createStatement(getQuery); - } - // Query for the provided entry + let getQuery = this.createStatement( + "SELECT guid FROM moz_formhistory " + + "WHERE fieldname = :name AND value = :value"); getQuery.params.name = name; getQuery.params.value = value; getQuery.executeStep(); @@ -102,7 +88,7 @@ let FormWrapper = { return getQuery.row.guid; // We need to create a guid for this entry - let setQuery = Svc.Form.DBConnection.createStatement( + let setQuery = this.createStatement( "UPDATE moz_formhistory SET guid = :guid " + "WHERE fieldname = :name AND value = :value"); let guid = Utils.makeGUID(); @@ -115,18 +101,36 @@ let FormWrapper = { }, hasGUID: function hasGUID(guid) { - let query = Svc.Form.DBConnection.createStatement( + let query = this.createStatement( "SELECT 1 FROM moz_formhistory WHERE guid = :guid"); query.params.guid = guid; return query.executeStep(); }, replaceGUID: function replaceGUID(oldGUID, newGUID) { - let query = Svc.Form.DBConnection.createStatement( + let query = this.createStatement( "UPDATE moz_formhistory SET guid = :newGUID WHERE guid = :oldGUID"); query.params.oldGUID = oldGUID; query.params.newGUID = newGUID; query.execute(); + }, + + createStatement: function createStatement(query) { + try { + // Just return the statement right away if it's okay + return Svc.Form.DBConnection.createStatement(query); + } + catch(ex) { + // Assume guid column must not exist yet, so add it with an index + Svc.Form.DBConnection.executeSimpleSQL( + "ALTER TABLE moz_formhistory ADD COLUMN guid TEXT"); + Svc.Form.DBConnection.executeSimpleSQL( + "CREATE INDEX IF NOT EXISTS moz_formhistory_guid_index " + + "ON moz_formhistory (guid)"); + + // Try creating the query now that the column exists + return Svc.Form.DBConnection.createStatement(query); + } } };