[gom] gom: Add support for NOT NULL constraint on columns



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

    gom: Add support for NOT NULL constraint on columns
    
    https://bugzilla.gnome.org/show_bug.cgi?id=729862

 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 b8c1ce2..3574ccf 100644
--- a/gom/gom-command-builder.c
+++ b/gom/gom-command-builder.c
@@ -219,6 +219,19 @@ add_unique (GString           *str,
 }
 
 static void
+add_notnull (GString           *str,
+             GParamSpec        *pspec)
+{
+   gboolean notnull;
+   notnull = GPOINTER_TO_UINT(g_param_spec_get_qdata(pspec, GOM_RESOURCE_NOTNULL));
+
+   if (!notnull)
+       return;
+
+   g_string_append_printf(str, " NOT NULL ");
+}
+
+static void
 add_joins (GString          *str,
            GomResourceClass *klass)
 {
@@ -418,6 +431,7 @@ gom_command_builder_build_create (GomCommandBuilder *builder,
                                    sql_type_for_column (pspecs[i]));
             add_reference(str, pspecs[i]);
             add_unique(str, pspecs[i]);
+            add_notnull(str, pspecs[i]);
          }
       }
       g_string_append(str, ")");
@@ -443,6 +457,7 @@ gom_command_builder_build_create (GomCommandBuilder *builder,
                               pspecs[i]->name,
                               sql_type_for_column (pspecs[i]));
        add_unique(str, pspecs[i]);
+       add_notnull(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 6da9ae5..ed11173 100644
--- a/gom/gom-resource.c
+++ b/gom/gom-resource.c
@@ -229,6 +229,26 @@ gom_resource_class_set_unique (GomResourceClass *resource_class,
                                GUINT_TO_POINTER (TRUE), NULL);
 }
 
+void
+gom_resource_class_set_notnull (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("NOT NULL property '%s' isn't declared yet. Are you running gom_resource_class_set_notnull() 
too early?",
+                property_name);
+      return;
+   }
+
+   g_param_spec_set_qdata_full(pspec, GOM_RESOURCE_NOTNULL,
+                               GUINT_TO_POINTER (TRUE), NULL);
+}
+
 GomRepository *
 gom_resource_get_repository (GomResource *resource)
 {
@@ -1009,3 +1029,9 @@ gom_resource_unique (void)
 {
    return g_quark_from_static_string("gom_resource_unique");
 }
+
+GQuark
+gom_resource_notnull (void)
+{
+   return g_quark_from_static_string("gom_resource_notnull");
+}
diff --git a/gom/gom-resource.h b/gom/gom-resource.h
index e08a876..c10c35c 100644
--- a/gom/gom-resource.h
+++ b/gom/gom-resource.h
@@ -39,6 +39,7 @@ G_BEGIN_DECLS
 #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())
+#define GOM_RESOURCE_NOTNULL         (gom_resource_notnull())
 
 typedef struct _GomResource        GomResource;
 typedef struct _GomResourceClass   GomResourceClass;
@@ -93,6 +94,8 @@ void              gom_resource_class_set_reference               (GomResourceCla
                                                                   const gchar              
*ref_property_name);
 void              gom_resource_class_set_unique                  (GomResourceClass         *resource_class,
                                                                   const gchar              *property_name);
+void              gom_resource_class_set_notnull                 (GomResourceClass         *resource_class,
+                                                                  const gchar              *property_name);
 
 void              gom_resource_delete_async          (GomResource          *resource,
                                                       GAsyncReadyCallback   callback,
@@ -109,6 +112,7 @@ 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;
+GQuark            gom_resource_notnull               (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]