[evolution-data-server/openismus-work] EBookBackendSqliteDB: Added e_book_backend_new_contact[s]() APIs



commit c90b625c7c1173c6143981a12b49f3c3e8cfc036
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Fri Jan 18 15:50:04 2013 +0900

    EBookBackendSqliteDB: Added e_book_backend_new_contact[s]() APIs
    
    The new API allows control on whether or not to overwrite existing contacts
    with the same UID.
    
    Deprecated the older e_book_backend_add_contact[s]() apis which include
    the unused 'partial_contact' parameter and have no control on whether
    or not to overwrite existing contacts.
    
    Additionally this patch exposes the E_BOOK_SDB_ERROR domain and a couple
    of error codes to distinguish if an error occurred because of a constraint
    or other errors (UID conflicts at contact addition time are constraint
    errors).

 .../libedata-book/e-book-backend-sqlitedb.c        |  118 ++++++++++++++------
 .../libedata-book/e-book-backend-sqlitedb.h        |   42 ++++++-
 2 files changed, 123 insertions(+), 37 deletions(-)
---
diff --git a/addressbook/libedata-book/e-book-backend-sqlitedb.c b/addressbook/libedata-book/e-book-backend-sqlitedb.c
index efbc60c..199cae7 100644
--- a/addressbook/libedata-book/e-book-backend-sqlitedb.c
+++ b/addressbook/libedata-book/e-book-backend-sqlitedb.c
@@ -82,8 +82,6 @@ struct _EBookBackendSqliteDBPrivate {
 
 G_DEFINE_TYPE (EBookBackendSqliteDB, e_book_backend_sqlitedb, G_TYPE_OBJECT)
 
-#define E_BOOK_SDB_ERROR \
-	(e_book_backend_sqlitedb_error_quark ())
 
 static GHashTable *db_connections = NULL;
 static GStaticMutex dbcon_lock = G_STATIC_MUTEX_INIT;
@@ -161,7 +159,7 @@ typedef struct {
 	GSList *list;
 } StoreVCardData;
 
-static GQuark
+GQuark
 e_book_backend_sqlitedb_error_quark (void)
 {
 	static GQuark quark = 0;
@@ -304,7 +302,7 @@ book_backend_sql_exec_real (sqlite3 *db,
 	}
 
 	if (ret != SQLITE_OK) {
-		d (g_print ("Error in SQL EXEC statement: %s [%s].\n", stmt, errmsg));
+		d(g_print ("Error in SQL EXEC statement: %s [%s] RETURN CODE: %d.\n", stmt, errmsg, ret));
 		g_set_error (
 			error, E_BOOK_SDB_ERROR,
 			0, "%s", errmsg);
@@ -1561,13 +1559,15 @@ static gchar *
 insert_stmt_from_contact (EBookBackendSqliteDB *ebsdb,
                           EContact *contact,
                           const gchar *folderid,
-                          gboolean store_vcard)
+                          gboolean store_vcard,
+			  gboolean replace_existing)
 {
 	GString *string;
 	gchar   *str, *vcard_str;
 	gint     i;
 
-	str    = sqlite3_mprintf ("INSERT or REPLACE INTO %Q VALUES (", folderid);
+	str    = sqlite3_mprintf ("INSERT or %s INTO %Q VALUES (",
+				  replace_existing ? "REPLACE" : "FAIL", folderid);
 	string = g_string_new (str);
 	sqlite3_free (str);
 
@@ -1644,6 +1644,7 @@ static gboolean
 insert_contact (EBookBackendSqliteDB *ebsdb,
 		EContact *contact,
 		const gchar *folderid,
+		gboolean replace_existing,
 		GError **error)
 {
 	EBookBackendSqliteDBPrivate *priv;
@@ -1653,7 +1654,7 @@ insert_contact (EBookBackendSqliteDB *ebsdb,
 	priv = ebsdb->priv;
 
 	/* Update main summary table */
-	stmt = insert_stmt_from_contact (ebsdb, contact, folderid, priv->store_vcard);
+	stmt = insert_stmt_from_contact (ebsdb, contact, folderid, priv->store_vcard, replace_existing);
 	success = book_backend_sql_exec (priv->db, stmt, NULL, NULL, error);
 	g_free (stmt);
 
@@ -1719,28 +1720,26 @@ insert_contact (EBookBackendSqliteDB *ebsdb,
 	return success;
 }
 
-
 /**
- * e_book_backend_sqlitedb_add_contact
- * @ebsdb:
+ * e_book_backend_sqlitedb_new_contact
+ * @ebsdb: An #EBookBackendSqliteDB
  * @folderid: folder id
  * @contact: EContact to be added
- * @partial_content: contact does not contain full information. Used when
- * the backend cache's partial information for auto-completion.
- * @error:
+ * @replace_existing: Whether this contact should replace another contact with the same UID.
+ * @error: A location to store any error that may have occurred.
  *
- * This is a convenience wrapper for e_book_backend_sqlitedb_add_contacts,
- * which is the preferred means to add multiple contacts when possible.
+ * This is a convenience wrapper for e_book_backend_sqlitedb_new_contacts,
+ * which is the preferred means to add or modify multiple contacts when possible.
  *
  * Returns: TRUE on success.
  *
- * Since: 3.2
+ * Since: 3.8
  **/
 gboolean
-e_book_backend_sqlitedb_add_contact (EBookBackendSqliteDB *ebsdb,
+e_book_backend_sqlitedb_new_contact (EBookBackendSqliteDB *ebsdb,
                                      const gchar *folderid,
                                      EContact *contact,
-                                     gboolean partial_content,
+                                     gboolean replace_existing,
                                      GError **error)
 {
 	GSList l;
@@ -1752,35 +1751,36 @@ e_book_backend_sqlitedb_add_contact (EBookBackendSqliteDB *ebsdb,
 	l.data = contact;
 	l.next = NULL;
 
-	return e_book_backend_sqlitedb_add_contacts (
+	return e_book_backend_sqlitedb_new_contacts (
 		ebsdb, folderid, &l,
-		partial_content, error);
+		replace_existing, error);
 }
 
 /**
- * e_book_backend_sqlitedb_add_contacts
- * @ebsdb:
+ * e_book_backend_sqlitedb_new_contacts
+ * @ebsdb: An #EBookBackendSqliteDB
  * @folderid: folder id
  * @contacts: list of EContacts
- * @partial_content: contact does not contain full information. Used when
- * the backend cache's partial information for auto-completion.
- * @error:
+ * @replace_existing: Whether this contact should replace another contact with the same UID.
+ * @error: A location to store any error that may have occurred.
  *
+ * Adds or replaces contacts in @ebsdb. If @replace_existing is specified then existing
+ * contacts with the same UID will be replaced, otherwise adding an existing contact
+ * will return an error.
  *
  * Returns: TRUE on success.
  *
- * Since: 3.2
+ * Since: 3.8
  **/
 gboolean
-e_book_backend_sqlitedb_add_contacts (EBookBackendSqliteDB *ebsdb,
+e_book_backend_sqlitedb_new_contacts (EBookBackendSqliteDB *ebsdb,
                                       const gchar *folderid,
                                       GSList *contacts,
-                                      gboolean partial_content,
+                                      gboolean replace_existing,
                                       GError **error)
 {
 	GSList *l;
 	GError *err = NULL;
-	gboolean ret = TRUE;
 
 	g_return_val_if_fail (E_IS_BOOK_BACKEND_SQLITEDB (ebsdb), FALSE);
 	g_return_val_if_fail (folderid != NULL, FALSE);
@@ -1791,7 +1791,7 @@ e_book_backend_sqlitedb_add_contacts (EBookBackendSqliteDB *ebsdb,
 	for (l = contacts; !err && l != NULL; l = g_slist_next (l)) {
 		EContact *contact = (EContact *) l->data;
 
-		insert_contact (ebsdb, contact, folderid, &err);
+		insert_contact (ebsdb, contact, folderid, replace_existing, &err);
 	}
 
 	book_backend_sqlitedb_end_transaction (ebsdb, !err, err ? NULL : &err);
@@ -1799,7 +1799,61 @@ e_book_backend_sqlitedb_add_contacts (EBookBackendSqliteDB *ebsdb,
 	if (err)
 		g_propagate_error (error, err);
 
-	return ret && !err;
+	return err == NULL;
+}
+
+/**
+ * e_book_backend_sqlitedb_add_contact
+ * @ebsdb:
+ * @folderid: folder id
+ * @contact: EContact to be added
+ * @partial_content: contact does not contain full information. Used when
+ * the backend cache's partial information for auto-completion.
+ * @error:
+ *
+ * This is a convenience wrapper for e_book_backend_sqlitedb_add_contacts,
+ * which is the preferred means to add multiple contacts when possible.
+ *
+ * Returns: TRUE on success.
+ *
+ * Since: 3.2
+ *
+ * Deprecated: 3.8: Use e_book_backend_sqlitedb_new_contact() instead.
+ **/
+gboolean
+e_book_backend_sqlitedb_add_contact (EBookBackendSqliteDB *ebsdb,
+                                     const gchar *folderid,
+                                     EContact *contact,
+                                     gboolean partial_content,
+                                     GError **error)
+{
+	return e_book_backend_sqlitedb_new_contact (ebsdb, folderid, contact, TRUE, error);
+}
+
+/**
+ * e_book_backend_sqlitedb_add_contacts
+ * @ebsdb:
+ * @folderid: folder id
+ * @contacts: list of EContacts
+ * @partial_content: contact does not contain full information. Used when
+ * the backend cache's partial information for auto-completion.
+ * @error:
+ *
+ *
+ * Returns: TRUE on success.
+ *
+ * Since: 3.2
+ *
+ * Deprecated: 3.8: Use e_book_backend_sqlitedb_new_contacts() instead.
+ **/
+gboolean
+e_book_backend_sqlitedb_add_contacts (EBookBackendSqliteDB *ebsdb,
+                                      const gchar *folderid,
+                                      GSList *contacts,
+                                      gboolean partial_content,
+                                      GError **error)
+{
+	return e_book_backend_sqlitedb_new_contacts (ebsdb, folderid, contacts, TRUE, error);
 }
 
 /**
@@ -4107,7 +4161,7 @@ validate_county_code (EBookBackendSqliteDB  *ebsdb,
 			continue;
 
 		if (update_e164_params (E_VCARD (contact), ebsdb->priv->country_code))
-			success = insert_contact (ebsdb, contact, folderid, error);
+			success = insert_contact (ebsdb, contact, folderid, TRUE, error);
 
 		g_object_unref (contact);
 	}
diff --git a/addressbook/libedata-book/e-book-backend-sqlitedb.h b/addressbook/libedata-book/e-book-backend-sqlitedb.h
index f4156d2..71f15bc 100644
--- a/addressbook/libedata-book/e-book-backend-sqlitedb.h
+++ b/addressbook/libedata-book/e-book-backend-sqlitedb.h
@@ -47,6 +47,15 @@
 	(G_TYPE_INSTANCE_GET_CLASS \
 	((obj), E_TYPE_BOOK_BACKEND_SQLITEDB, EBookBackendSqliteDBClass))
 
+/**
+ * E_BOOK_SDB_ERROR:
+ *
+ * Error domain for #EBookBackendSqliteDB operations.
+ *
+ * Since: 3.8
+ **/
+#define E_BOOK_SDB_ERROR (e_book_backend_sqlitedb_error_quark ())
+
 G_BEGIN_DECLS
 
 typedef struct _EBookBackendSqliteDB EBookBackendSqliteDB;
@@ -54,6 +63,18 @@ typedef struct _EBookBackendSqliteDBClass EBookBackendSqliteDBClass;
 typedef struct _EBookBackendSqliteDBPrivate EBookBackendSqliteDBPrivate;
 
 /**
+ * EBookSDBError:
+ * @E_BOOK_SDB_ERROR_CONSTRAINT: The error occurred due to an explicit constraint
+ * @E_BOOK_SDB_ERROR_OTHER: Another error occurred
+ *
+ * Defines the types of possible errors reported by the #EBookBackendSqliteDB
+ */
+typedef enum {
+	E_BOOK_SDB_ERROR_CONSTRAINT,
+	E_BOOK_SDB_ERROR_OTHER
+} EBookSDBError;
+
+/**
  * EBookBackendSqliteDB:
  *
  * Contains only private data that should be read and manipulated using the
@@ -86,6 +107,7 @@ typedef struct {
 } EbSdbSearchData;
 
 GType		e_book_backend_sqlitedb_get_type	(void);
+GQuark          e_book_backend_sqlitedb_error_quark     (void);
 
 EBookBackendSqliteDB *
 		e_book_backend_sqlitedb_new		(const gchar *path,
@@ -109,16 +131,15 @@ gboolean	e_book_backend_sqlitedb_lock_updates	(EBookBackendSqliteDB *ebsdb,
 gboolean	e_book_backend_sqlitedb_unlock_updates	(EBookBackendSqliteDB *ebsdb,
 							 gboolean do_commit,
 							 GError **error);
-
-gboolean	e_book_backend_sqlitedb_add_contact	(EBookBackendSqliteDB *ebsdb,
+gboolean	e_book_backend_sqlitedb_new_contact	(EBookBackendSqliteDB *ebsdb,
 							 const gchar *folderid,
 							 EContact *contact,
-							 gboolean partial_content,
+							 gboolean replace_existing,
 							 GError **error);
-gboolean	e_book_backend_sqlitedb_add_contacts	(EBookBackendSqliteDB *ebsdb,
+gboolean	e_book_backend_sqlitedb_new_contacts	(EBookBackendSqliteDB *ebsdb,
 							 const gchar *folderid,
 							 GSList *contacts,
-							 gboolean partial_content,
+							 gboolean replace_existing,
 							 GError **error);
 gboolean	e_book_backend_sqlitedb_remove_contact	(EBookBackendSqliteDB *ebsdb,
 							 const gchar *folderid,
@@ -254,6 +275,17 @@ gboolean	e_book_backend_sqlitedb_is_summary_query
 						(const gchar *query);
 gboolean	e_book_backend_sqlitedb_is_summary_fields
 						(GHashTable *fields_of_interest);
+
+gboolean	e_book_backend_sqlitedb_add_contact	(EBookBackendSqliteDB *ebsdb,
+							 const gchar *folderid,
+							 EContact *contact,
+							 gboolean partial_content,
+							 GError **error);
+gboolean	e_book_backend_sqlitedb_add_contacts	(EBookBackendSqliteDB *ebsdb,
+							 const gchar *folderid,
+							 GSList *contacts,
+							 gboolean partial_content,
+							 GError **error);
 #endif
 
 G_END_DECLS



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]