[gom] gom: Add support for UNIQUE constraint on columns



commit 9d2d340c26351b8b3ab5432502825b0e07ff887f
Author: Mathieu Bridon <bochecha fedoraproject org>
Date:   Wed May 7 15:25:57 2014 +0800

    gom: Add support for UNIQUE constraint on columns
    
    https://bugzilla.gnome.org/show_bug.cgi?id=729722

 gom/gom-command-builder.c |   15 +++++++++++++++
 gom/gom-resource.c        |   26 ++++++++++++++++++++++++++
 gom/gom-resource.h        |    4 ++++
 3 files changed, 45 insertions(+), 0 deletions(-)
---
diff --git a/gom/gom-command-builder.c b/gom/gom-command-builder.c
index ece6fbc..b8c1ce2 100644
--- a/gom/gom-command-builder.c
+++ b/gom/gom-command-builder.c
@@ -206,6 +206,19 @@ add_reference (GString           *str,
 }
 
 static void
+add_unique (GString           *str,
+            GParamSpec        *pspec)
+{
+   gboolean unique;
+   unique = GPOINTER_TO_UINT(g_param_spec_get_qdata(pspec, GOM_RESOURCE_UNIQUE));
+
+   if (!unique)
+       return;
+
+   g_string_append_printf(str, " UNIQUE ");
+}
+
+static void
 add_joins (GString          *str,
            GomResourceClass *klass)
 {
@@ -404,6 +417,7 @@ gom_command_builder_build_create (GomCommandBuilder *builder,
                                    pspecs[i]->name,
                                    sql_type_for_column (pspecs[i]));
             add_reference(str, pspecs[i]);
+            add_unique(str, pspecs[i]);
          }
       }
       g_string_append(str, ")");
@@ -428,6 +442,7 @@ gom_command_builder_build_create (GomCommandBuilder *builder,
        g_string_append_printf(str, "'%s' %s",
                               pspecs[i]->name,
                               sql_type_for_column (pspecs[i]));
+       add_unique(str, pspecs[i]);
        add_reference(str, pspecs[i]);
 
        command = g_object_new(GOM_TYPE_COMMAND,
diff --git a/gom/gom-resource.c b/gom/gom-resource.c
index 5288e1c..6da9ae5 100644
--- a/gom/gom-resource.c
+++ b/gom/gom-resource.c
@@ -209,6 +209,26 @@ gom_resource_class_set_table (GomResourceClass *resource_class,
               "%s", table);
 }
 
+void
+gom_resource_class_set_unique (GomResourceClass *resource_class,
+                               const gchar      *property_name)
+{
+   GParamSpec *pspec;
+
+   g_return_if_fail(GOM_IS_RESOURCE_CLASS(resource_class));
+   g_return_if_fail(property_name != NULL);
+
+   pspec = g_object_class_find_property(G_OBJECT_CLASS(resource_class), property_name);
+   if (!pspec) {
+      g_warning("Unique property '%s' isn't declared yet. Are you running gom_resource_class_set_unique() 
too early?",
+                property_name);
+      return;
+   }
+
+   g_param_spec_set_qdata_full(pspec, GOM_RESOURCE_UNIQUE,
+                               GUINT_TO_POINTER (TRUE), NULL);
+}
+
 GomRepository *
 gom_resource_get_repository (GomResource *resource)
 {
@@ -983,3 +1003,9 @@ gom_resource_ref_property_name (void)
 {
    return g_quark_from_static_string("gom_resource_ref_property_name");
 }
+
+GQuark
+gom_resource_unique (void)
+{
+   return g_quark_from_static_string("gom_resource_unique");
+}
diff --git a/gom/gom-resource.h b/gom/gom-resource.h
index 2e4228a..e08a876 100644
--- a/gom/gom-resource.h
+++ b/gom/gom-resource.h
@@ -38,6 +38,7 @@ G_BEGIN_DECLS
 #define GOM_RESOURCE_FROM_BYTES_FUNC (gom_resource_from_bytes_func_quark())
 #define GOM_RESOURCE_REF_TABLE_CLASS (gom_resource_ref_table_class())
 #define GOM_RESOURCE_REF_PROPERTY_NAME (gom_resource_ref_property_name())
+#define GOM_RESOURCE_UNIQUE          (gom_resource_unique())
 
 typedef struct _GomResource        GomResource;
 typedef struct _GomResourceClass   GomResourceClass;
@@ -90,6 +91,8 @@ void              gom_resource_class_set_reference               (GomResourceCla
                                                                   const gchar              *property_name,
                                                                   const gchar              *ref_table_name,
                                                                   const gchar              
*ref_property_name);
+void              gom_resource_class_set_unique                  (GomResourceClass         *resource_class,
+                                                                  const gchar              *property_name);
 
 void              gom_resource_delete_async          (GomResource          *resource,
                                                       GAsyncReadyCallback   callback,
@@ -105,6 +108,7 @@ GQuark            gom_resource_to_bytes_func_quark   (void) G_GNUC_CONST;
 GQuark            gom_resource_from_bytes_func_quark (void) G_GNUC_CONST;
 GQuark            gom_resource_ref_table_class       (void) G_GNUC_CONST;
 GQuark            gom_resource_ref_property_name     (void) G_GNUC_CONST;
+GQuark            gom_resource_unique                (void) G_GNUC_CONST;
 GType             gom_resource_get_type              (void) G_GNUC_CONST;
 void              gom_resource_save_async            (GomResource          *resource,
                                                       GAsyncReadyCallback   callback,


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