Re: [gnome-db] How to use GdaDataModelQuery



I've created a PATCH , it compiles at libgda stage, but when I try to
test it using a program, gcc  says the new
'GDA_DATA_MODEL_QUERY_OPTION_DONT_USE_AUTOINCREMENT_PK_FIELDS' at
GdaDataModelQuery Option is undeclared

Please HELP!!!!!

2008/1/9, Daniel Espinosa <esodan gmail com>:
> I want to add a new row using a GdaDataModelQuery, but it sends
> "invalid parameter '+0'" error, but whay if the GDA object sets all
> parameters before ejecute the query
>
> model = gda_data_model_query_new (query);
>
> gda_data_model_query_compute_modification_queries
> (GDA_DATA_MODEL_QUERY
> (model),NULL,GDA_DATA_MODEL_QUERY_OPTION_USE_ALL_FIELDS_IF_NO_PK,NULL);
>
>
> row = gda_data_model_append_row (model, &error);
>
> if (row < 0)
>    g_printf ("ERROR APPENDING: %s\n", error->message);
>
> Even if I use a GdaDataProxy, append the row and then apply all
> changes, the program sends the same message.
>
> --
> Trabajar, la mejor arma para tu superación
> "de grano en grano, se hace la arena" (R) (entrámite, pero para los
> cuates: LIBRE)
>


-- 
Trabajar, la mejor arma para tu superación
"de grano en grano, se hace la arena" (R) (entrámite, pero para los
cuates: LIBRE)
Index: doc/C/libgda-3.0-docs.sgml
===================================================================
--- doc/C/libgda-3.0-docs.sgml	(revisión: 3038)
+++ doc/C/libgda-3.0-docs.sgml	(copia de trabajo)
@@ -201,6 +201,16 @@
 	<contrib>libgda, gda-postgres, gda-mysql
         </contrib>
       </author>
+      <author>
+        <firstname>Daniel</firstname>
+        <surname>Espinosa Ortiz</surname>
+        <affiliation>
+          <orgname></orgname>
+          <address><email>esodan gmail com</email></address>
+        </affiliation>
+	<contrib>libgda, gda-postgres, gda-mysql
+        </contrib>
+      </author>
     </authorgroup>
     <date>1999 February</date>
     <copyright>
Index: doc/C/tmpl/gda-data-model-bdb.sgml
===================================================================
--- doc/C/tmpl/gda-data-model-bdb.sgml	(revisión: 3038)
+++ doc/C/tmpl/gda-data-model-bdb.sgml	(copia de trabajo)
@@ -29,17 +29,9 @@
 
 </para>
 
+ object: 
+ priv: 
 
-<!-- ##### ARG GdaDataModelBdb:db-name ##### -->
-<para>
-
-</para>
-
-<!-- ##### ARG GdaDataModelBdb:filename ##### -->
-<para>
-
-</para>
-
 <!-- ##### STRUCT GdaDataModelBdbClass ##### -->
 <para>
 
Index: providers/postgres/gda-postgres-provider.c
===================================================================
--- providers/postgres/gda-postgres-provider.c	(revisión: 3038)
+++ providers/postgres/gda-postgres-provider.c	(copia de trabajo)
@@ -1561,6 +1561,74 @@
 	return retval;
 }
 
+static GdaParameter *
+gda_postgres_provider_get_last_inserted_id (GdaServerProvider *provider,
+					  GdaConnection *cnc,
+					  GdaDataModel *recset)
+{
+	Oid oid;
+	PGresult *pgres;
+	GdaPostgresConnectionData *priv_data;
+	GdaPostgresProvider *pg_prv = (GdaPostgresProvider *) provider;
+
+	g_return_val_if_fail (GDA_IS_POSTGRES_PROVIDER (pg_prv), NULL);
+	g_return_val_if_fail (GDA_IS_CONNECTION (cnc), NULL);
+
+	priv_data = g_object_get_data (G_OBJECT (cnc), OBJECT_DATA_POSTGRES_HANDLE);
+	if (!priv_data) {
+		gda_connection_add_event_string (cnc, _("Invalid PostgreSQL handle"));
+		return NULL;
+	}
+
+	if (recset) {
+		g_return_val_if_fail (GDA_IS_POSTGRES_RECORDSET (recset), NULL);
+		/* get the PQresult from the recordset */
+		pgres = gda_postgres_recordset_get_pgresult (GDA_POSTGRES_RECORDSET (recset));
+		if (pgres) {
+			oid = PQoidValue (pgres);
+			if (oid != InvalidOid)
+			{
+				GdaParameter *param;
+				GValue *value;
+				
+				g_value_init (value, G_TYPE_INT);
+				g_value_set_int (value, oid);
+				
+				gda_parameter_new (G_TYPE_INT);
+				
+				gda_parameter_set_value (param, value);
+				
+				return param;
+			}
+		}
+		return NULL;
+	}
+
+	/* get the last inserted OID kept */
+	if (priv_data->last_insert_id)
+	{
+		GdaParameter *param;
+		GValue *value;
+		GValue *str;
+		gint *oid;
+		
+		g_value_init (str, G_TYPE_STRING);
+		g_value_set (str, priv_data->last_insert_id);
+		
+		g_value_init (value, G_TYPE_INT);
+		
+		g_value_transform (str, value);
+		
+		gda_parameter_new (G_TYPE_INT);
+		
+		gda_parameter_set_value (param, value);
+		
+		return param;
+	}
+	else
+		return NULL;
+}
+
 static gchar *
 gda_postgres_provider_get_last_insert_id (GdaServerProvider *provider,
 					  GdaConnection *cnc,
@@ -1599,6 +1667,7 @@
 		return NULL;
 }
 
+
 static gboolean 
 gda_postgres_provider_begin_transaction (GdaServerProvider *provider,
 				         GdaConnection *cnc,
Index: libgda/gda-data-model-query.c
===================================================================
--- libgda/gda-data-model-query.c	(revisión: 3038)
+++ libgda/gda-data-model-query.c	(copia de trabajo)
@@ -1519,7 +1519,7 @@
 static GSList   *auto_compute_make_mod_query_fields (GdaDataModelQuery *model, GdaQueryTarget *modify_target,
 						     GError **error);
 static void      auto_compute_add_mod_fields_to_query (GdaDataModelQuery *model, GdaQueryTarget *modify_target,
-						       GSList *mod_query_fields, GdaQuery *query);
+						       GSList *mod_query_fields, gboolean dont_use_autoincrement_pk_fields, GdaQuery *query);
 static void      auto_compute_add_where_cond_to_query (GdaDataModelQuery *model, GSList *mod_query_fields, 
 						       GdaQuery *query);
 /**
@@ -1573,7 +1573,8 @@
 	if (!cond_query_fields)
 		return FALSE;
 
-	mod_query_fields = auto_compute_make_mod_query_fields (model, modify_target, error);
+	mod_query_fields = auto_compute_make_mod_query_fields (model, modify_target,
+								error);
 	if (!mod_query_fields) {
 		g_slist_free (cond_query_fields);
 		return FALSE;
@@ -1582,7 +1583,7 @@
 	/* compute UPDATE query */
 	query = gda_query_new (gda_object_get_dict (GDA_OBJECT (model->priv->queries[SEL_QUERY])));
 	gda_query_set_query_type (query, GDA_QUERY_TYPE_UPDATE);
-	auto_compute_add_mod_fields_to_query (model, modify_target, mod_query_fields, query);
+	auto_compute_add_mod_fields_to_query (model, modify_target,mod_query_fields, FALSE, query);
 	auto_compute_add_where_cond_to_query (model, cond_query_fields, query);
 	g_object_set (G_OBJECT (model), "update_query", query, NULL);
 	g_object_unref (query);
@@ -1590,7 +1591,8 @@
 	/* compute INSERT query */
 	query = gda_query_new (gda_object_get_dict (GDA_OBJECT (model->priv->queries[SEL_QUERY])));
 	gda_query_set_query_type (query, GDA_QUERY_TYPE_INSERT);
-	auto_compute_add_mod_fields_to_query (model, modify_target, mod_query_fields, query);
+	auto_compute_add_mod_fields_to_query (model, modify_target, mod_query_fields,
+						options & GDA_DATA_MODEL_QUERY_OPTION_DONT_USE_AUTOINCREMENT_PK_FIELDS, query);
 	g_object_set (G_OBJECT (model), "insert_query", query, NULL);
 	g_object_unref (query);
 
@@ -1766,11 +1768,13 @@
 }
 
 static GSList *
-auto_compute_make_mod_query_fields (GdaDataModelQuery *model, GdaQueryTarget *modify_target, GError **error)
+auto_compute_make_mod_query_fields (GdaDataModelQuery *model, GdaQueryTarget *modify_target,
+					GError **error)
 {
 	GSList *mod_query_fields = NULL;
 	GSList *mod_dict_fields = NULL;
 	GSList *target_fields, *list;
+	GSList *pk_ai_fields; /* List of PrimaryKeys and Autoincrement or serial fields */
 	gboolean duplicate = FALSE;
 
 	/* make sure there are no duplicates in the fields referenced by modify_target */
@@ -1800,6 +1804,7 @@
 		}
 	}
 	
+	
 	g_slist_free (target_fields);
 	g_slist_free (mod_dict_fields);
 
@@ -1813,7 +1818,8 @@
 
 static void
 auto_compute_add_mod_fields_to_query (GdaDataModelQuery *model, GdaQueryTarget *modify_target, 
-				      GSList *mod_query_fields, GdaQuery *query)
+				      GSList *mod_query_fields, 
+				      gboolean dont_use_autoincrement_pk_fields, GdaQuery *query)
 {
 	GdaQueryTarget *target;
 	GdaDictTable *mod_table;
@@ -1828,32 +1834,42 @@
 		GdaQueryFieldField *qfield;
 		GdaQueryFieldValue *qvalue;
 		GdaEntityField *efield;
+		GdaDictFieldAttribute attr;
 		gchar *str;
 
 		efield = gda_query_field_field_get_ref_field (GDA_QUERY_FIELD_FIELD (list->data));
-		qfield = (GdaQueryFieldField *) g_object_new (GDA_TYPE_QUERY_FIELD_FIELD,
-							      "dict", gda_object_get_dict (GDA_OBJECT (query)),
-							      "query", query,
-							      "target", target,
-							      "field", efield, NULL);
-		gda_entity_add_field (GDA_ENTITY (query), GDA_ENTITY_FIELD (qfield));
-		g_object_unref (qfield);
+		
+		attr = gda_dict_field_get_attributes (GDA_QUERY_FIELD (efield));
+		
+		if (!(dont_use_autoincrement_pk_fields
+			&& gda_dict_field_is_pkey_part (GDA_QUERY_FIELD (efield))
+			&& (attr & FIELD_AUTO_INCREMENT)))
+		{
+			qfield = (GdaQueryFieldField *) g_object_new (GDA_TYPE_QUERY_FIELD_FIELD,
+								      "dict", gda_object_get_dict (GDA_OBJECT (query)),
+								      "query", query,
+								      "target", target,
+								      "field", efield, NULL);
+			gda_entity_add_field (GDA_ENTITY (query), GDA_ENTITY_FIELD (qfield));
+			g_object_unref (qfield);
+		
 
-		qvalue = (GdaQueryFieldValue *) gda_query_field_value_new (query, 
-									   gda_entity_field_get_g_type (efield));
-		gda_entity_add_field (GDA_ENTITY (query), GDA_ENTITY_FIELD (qvalue));
-		gda_query_field_value_set_is_parameter (qvalue, TRUE);
-		gda_query_field_set_visible (GDA_QUERY_FIELD (qvalue), FALSE);
-		str = g_strdup_printf ("+%d", 
-				       gda_entity_get_field_index (GDA_ENTITY (model->priv->queries[SEL_QUERY]),
-								   GDA_ENTITY_FIELD (list->data)));
-		gda_object_set_name (GDA_OBJECT (qvalue), str);
-		g_free (str);
-		if (gda_dict_field_is_null_allowed (GDA_DICT_FIELD (efield)))
-			gda_query_field_value_set_not_null (qvalue, FALSE);
-		g_object_unref (qvalue);
+			qvalue = (GdaQueryFieldValue *) gda_query_field_value_new (query, 
+										   gda_entity_field_get_g_type (efield));
+			gda_entity_add_field (GDA_ENTITY (query), GDA_ENTITY_FIELD (qvalue));
+			gda_query_field_value_set_is_parameter (qvalue, TRUE);
+			gda_query_field_set_visible (GDA_QUERY_FIELD (qvalue), FALSE);
+			str = g_strdup_printf ("+%d", 
+					       gda_entity_get_field_index (GDA_ENTITY (model->priv->queries[SEL_QUERY]),
+									   GDA_ENTITY_FIELD (list->data)));
+			gda_object_set_name (GDA_OBJECT (qvalue), str);
+			g_free (str);
+			if (gda_dict_field_is_null_allowed (GDA_DICT_FIELD (efield)))
+				gda_query_field_value_set_not_null (qvalue, FALSE);
+			g_object_unref (qvalue);
 
-		g_object_set (G_OBJECT (qfield), "value-provider", qvalue, NULL);
+			g_object_set (G_OBJECT (qfield), "value-provider", qvalue, NULL);
+		}
 	}
 	
 }
Index: libgda/gda-data-model-query.h
===================================================================
--- libgda/gda-data-model-query.h	(revisión: 3038)
+++ libgda/gda-data-model-query.h	(copia de trabajo)
@@ -48,7 +48,8 @@
 } GdaDataModelQueryError;
 
 typedef enum {
-	GDA_DATA_MODEL_QUERY_OPTION_USE_ALL_FIELDS_IF_NO_PK = 1 << 0
+	GDA_DATA_MODEL_QUERY_OPTION_USE_ALL_FIELDS_IF_NO_PK          = 1 << 0,
+	GDA_DATA_MODEL_QUERY_OPTION_DONT_USE_AUTOINCREMENT_PK_FIELDS = 1 << 1
 } GdaDataModelQueryOptions;
 
 struct _GdaDataModelQuery {
Index: libgda/gda-data-proxy.c
===================================================================
--- libgda/gda-data-proxy.c	(revisión: 3038)
+++ libgda/gda-data-proxy.c	(copia de trabajo)
@@ -110,7 +110,7 @@
 {
 	PROP_0,
 	PROP_MODEL,
-	PROP_ADD_NULL_ENTRY,
+	PROP_PREPEND_NULL_ENTRY,
 	PROP_DEFER_SYNC,
 	PROP_SAMPLE_SIZE
 };
@@ -533,6 +533,32 @@
 
 	parent_class = g_type_class_peek_parent (class);
 
+	/* virtual functions */
+#ifdef GDA_DEBUG
+	GDA_OBJECT_CLASS (class)->dump = (void (*)(GdaObject *, guint)) gda_data_proxy_dump;
+#endif
+
+	object_class->dispose = gda_data_proxy_dispose;
+	object_class->finalize = gda_data_proxy_finalize;
+
+	/* Properties */
+	object_class->set_property = gda_data_proxy_set_property;
+	object_class->get_property = gda_data_proxy_get_property;
+
+	g_object_class_install_property (object_class, PROP_MODEL,
+					 g_param_spec_object ("model", _("Data model"), NULL,
+                                                               GDA_TYPE_DATA_MODEL,
+							       (G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT)));
+	g_object_class_install_property (object_class, PROP_PREPEND_NULL_ENTRY,
+					 g_param_spec_boolean ("prepend-null-entry", NULL, NULL, FALSE,
+							       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
+	g_object_class_install_property (object_class, PROP_DEFER_SYNC,
+					 g_param_spec_boolean ("defer-sync", NULL, NULL, TRUE,
+							       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
+	g_object_class_install_property (object_class, PROP_SAMPLE_SIZE,
+					 g_param_spec_int ("sample-size", NULL, NULL, 0, G_MAXINT - 1, 300,
+							   (G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT)));
+
 	/* signals */
 	gda_data_proxy_signals [ROW_DELETE_CHANGED] =
 		g_signal_new ("row_delete_changed",
@@ -583,31 +609,6 @@
 	class->pre_changes_applied = m_pre_changes_applied;
 	class->post_changes_applied = NULL;
 
-	/* virtual functions */
-#ifdef GDA_DEBUG
-	GDA_OBJECT_CLASS (class)->dump = (void (*)(GdaObject *, guint)) gda_data_proxy_dump;
-#endif
-
-	object_class->dispose = gda_data_proxy_dispose;
-	object_class->finalize = gda_data_proxy_finalize;
-
-	/* Properties */
-	object_class->set_property = gda_data_proxy_set_property;
-	object_class->get_property = gda_data_proxy_get_property;
-
-	g_object_class_install_property (object_class, PROP_MODEL,
-					 g_param_spec_object ("model", _("Data model"), NULL,
-                                                               GDA_TYPE_DATA_MODEL,
-							       (G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT)));
-	g_object_class_install_property (object_class, PROP_ADD_NULL_ENTRY,
-					 g_param_spec_boolean ("prepend_null_entry", NULL, NULL, FALSE,
-							       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
-	g_object_class_install_property (object_class, PROP_DEFER_SYNC,
-					 g_param_spec_boolean ("defer_sync", NULL, NULL, TRUE,
-							       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
-	g_object_class_install_property (object_class, PROP_SAMPLE_SIZE,
-					 g_param_spec_int ("sample_size", NULL, NULL, 0, G_MAXINT - 1, 300,
-							   (G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT)));
 }
 
 static void
@@ -870,7 +871,7 @@
 				proxy->priv->chunk = NULL;
 			}
 			break;
-		case PROP_ADD_NULL_ENTRY:
+		case PROP_PREPEND_NULL_ENTRY:
 			if (proxy->priv->add_null_entry != g_value_get_boolean (value)) {
 				proxy->priv->add_null_entry = g_value_get_boolean (value);
 
@@ -915,7 +916,10 @@
 	proxy = GDA_DATA_PROXY (object);
 	if (proxy->priv) {
 		switch (param_id) {
-		case PROP_ADD_NULL_ENTRY:
+		case PROP_MODEL:
+			g_value_set_object (value, proxy->priv->model);
+			break;
+		case PROP_PREPEND_NULL_ENTRY:
 			g_value_set_boolean (value, proxy->priv->add_null_entry);
 			break;
 		case PROP_DEFER_SYNC:


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