[gom] gom: Use new to/from bytes functions from GomResource



commit 108ef88c2dabbc724ee9623806c2edefd5642285
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Apr 16 17:26:52 2014 +0200

    gom: Use new to/from bytes functions from GomResource

 gom/gom-command-builder.c |   37 +++++++++++++++++++++++++++++++++----
 gom/gom-command.c         |   23 +++++++++++++++++++++++
 gom/gom-cursor.c          |   13 +++++++++++++
 gom/gom-resource-group.c  |   22 ++++++++++++++++++----
 4 files changed, 87 insertions(+), 8 deletions(-)
---
diff --git a/gom/gom-command-builder.c b/gom/gom-command-builder.c
index db704f3..399b32f 100644
--- a/gom/gom-command-builder.c
+++ b/gom/gom-command-builder.c
@@ -67,6 +67,8 @@ sql_type_for_column (GParamSpec *pspec)
    case G_TYPE_DOUBLE:
       return "FLOAT";
    default:
+      if (g_param_spec_get_qdata(pspec, GOM_RESOURCE_FROM_BYTES_FUNC) != NULL)
+        return "BLOB";
       return NULL;
    }
 }
@@ -544,6 +546,35 @@ do_prop_on_insert (GParamSpec       *pspec,
 #undef BELONGS_TO_TYPE
 }
 
+static void
+resource_get_property(GObject     *object,
+                      const gchar *property_name,
+                      GValue      *value)
+{
+   GValue real_value = { 0, };
+   GomResourceToBytesFunc to_bytes;
+   GParamSpec *pspec;
+   GBytes *bytes;
+
+   pspec = g_object_class_find_property(G_OBJECT_GET_CLASS(object), property_name);
+   g_assert(pspec);
+
+   g_value_init(&real_value, pspec->value_type);
+   g_object_get_property(object, property_name, &real_value);
+
+   to_bytes = g_param_spec_get_qdata(pspec, GOM_RESOURCE_TO_BYTES_FUNC);
+   if (!to_bytes) {
+      *value = real_value;
+      return;
+   }
+
+   bytes = (* to_bytes) (&real_value);
+   g_value_init(value, G_TYPE_BYTES);
+   g_value_take_boxed(value, bytes);
+
+   g_value_unset(&real_value);
+}
+
 /**
  * gom_command_builder_build_insert:
  * @builder: (in): A #GomCommandBuilder.
@@ -613,8 +644,7 @@ gom_command_builder_build_insert (GomCommandBuilder *builder,
       if (do_prop_on_insert(pspecs[i], klass, priv->resource_type)) {
          GValue value = { 0 };
 
-         g_value_init(&value, pspecs[i]->value_type);
-         g_object_get_property(G_OBJECT(resource), pspecs[i]->name, &value);
+         resource_get_property(G_OBJECT(resource), pspecs[i]->name, &value);
          gom_command_set_param(command, idx++, &value);
          g_value_unset(&value);
       }
@@ -685,8 +715,7 @@ gom_command_builder_build_update (GomCommandBuilder *builder,
       if (do_prop_on_insert(pspecs[i], klass, priv->resource_type)) {
          GValue value = { 0 };
 
-         g_value_init(&value, pspecs[i]->value_type);
-         g_object_get_property(G_OBJECT(resource), pspecs[i]->name, &value);
+         resource_get_property(G_OBJECT(resource), pspecs[i]->name, &value);
          gom_command_set_param(command, idx++, &value);
          g_value_unset(&value);
       }
diff --git a/gom/gom-command.c b/gom/gom-command.c
index 8996c24..98d8d16 100644
--- a/gom/gom-command.c
+++ b/gom/gom-command.c
@@ -31,6 +31,8 @@ struct _GomCommandPrivate
    gchar        *sql;
    sqlite3_stmt *stmt;
    GHashTable   *params;
+
+   GPtrArray    *blobs;
 };
 
 enum
@@ -114,6 +116,19 @@ gom_command_bind_param (GomCommand   *command,
       } else if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_ENUM)) {
          sqlite3_bind_int(priv->stmt, param, g_value_get_enum(value));
          break;
+      } else if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_BYTES)) {
+         GBytes *bytes;
+
+         bytes = g_value_get_boxed(value);
+         if (!bytes) {
+            sqlite3_bind_blob(priv->stmt, param,
+                              NULL, 0, NULL);
+         } else {
+            sqlite3_bind_blob(priv->stmt, param,
+                              g_bytes_get_data(bytes, NULL), g_bytes_get_size(bytes), NULL);
+            g_ptr_array_add(priv->blobs, g_bytes_ref(bytes));
+         }
+         break;
       }
       g_warning("Failed to bind gtype %s.", g_type_name(G_VALUE_TYPE(value)));
       g_assert_not_reached();
@@ -332,6 +347,9 @@ gom_command_reset (GomCommand *command)
    if (priv->stmt) {
       sqlite3_clear_bindings(priv->stmt);
       sqlite3_reset(priv->stmt);
+
+      g_ptr_array_unref(priv->blobs);
+      priv->blobs = g_ptr_array_new_with_free_func ((GDestroyNotify) g_bytes_unref);
    }
 }
 
@@ -385,6 +403,10 @@ gom_command_finalize (GObject *object)
       g_hash_table_destroy(priv->params);
    }
 
+   if (priv->blobs) {
+      g_ptr_array_unref(priv->blobs);
+   }
+
    G_OBJECT_CLASS(gom_command_parent_class)->finalize(object);
 }
 
@@ -492,6 +514,7 @@ gom_command_init (GomCommand *command)
       G_TYPE_INSTANCE_GET_PRIVATE(command,
                                   GOM_TYPE_COMMAND,
                                   GomCommandPrivate);
+   command->priv->blobs = g_ptr_array_new_with_free_func ((GDestroyNotify) g_bytes_unref);
 }
 
 GQuark
diff --git a/gom/gom-cursor.c b/gom/gom-cursor.c
index d82ef9f..70564e4 100644
--- a/gom/gom-cursor.c
+++ b/gom/gom-cursor.c
@@ -130,6 +130,19 @@ gom_cursor_get_column (GomCursor *cursor,
          g_value_set_string(value,
                             (gchar *)sqlite3_column_text(priv->stmt, column));
          break;
+      case SQLITE_BLOB: {
+         gconstpointer data;
+         gpointer data_copy;
+         gsize size;
+
+         g_value_init(value, G_TYPE_BYTES);
+         data = sqlite3_column_blob(priv->stmt, column);
+         size = sqlite3_column_bytes(priv->stmt, column);
+
+         data_copy = g_memdup(data, size);
+         g_value_take_boxed(value, g_bytes_new_take(data_copy, size));
+         }
+         break;
       default:
          g_assert_not_reached();
       }
diff --git a/gom/gom-resource-group.c b/gom/gom-resource-group.c
index a469541..e24ca31 100644
--- a/gom/gom-resource-group.c
+++ b/gom/gom-resource-group.c
@@ -202,10 +202,24 @@ set_props (GomResource *resource,
    for (i = 0; i < n_cols; i++) {
       name = gom_cursor_get_column_name(cursor, i);
       if ((pspec = g_object_class_find_property(klass, name))) {
-         g_value_init(&value, pspec->value_type);
-         gom_cursor_get_column(cursor, i, &value);
-         g_object_set_property(G_OBJECT(resource), name, &value);
-         g_value_unset(&value);
+         GomResourceFromBytesFunc from_bytes;
+
+         from_bytes = g_param_spec_get_qdata(pspec, GOM_RESOURCE_FROM_BYTES_FUNC);
+         if (from_bytes) {
+            GValue converted = { 0, };
+
+            g_value_init(&value, G_TYPE_BYTES);
+            gom_cursor_get_column(cursor, i, &value);
+            (*from_bytes) (g_value_get_boxed(&value), &converted);
+            g_value_unset(&value);
+            g_object_set_property(G_OBJECT(resource), name, &converted);
+            g_value_unset(&converted);
+         } else {
+            g_value_init(&value, pspec->value_type);
+            gom_cursor_get_column(cursor, i, &value);
+            g_object_set_property(G_OBJECT(resource), name, &value);
+            g_value_unset(&value);
+         }
       }
    }
 }


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