Re: [gnome-db] GdaQuery type insert... How to?



On 9/21/06, Daniel Espinosa <esodan gmail com> wrote:
Could you helpme in a schematic code to how create a GdaQuery that make an
INSERT command?

For now I'm doing this:

query = gda_query_new()
target = gda_query_target_new(query, "table name")  // I'm using the name in
an Dict
gda_query_add_target(query, target)

You need to add gda_query_set_query_type(query, GDA_QUERY_TYPE_INSERT)
to specify the type of query you want.

Right, so far your query would be "INSERT INTO table_name", is pretty
useless: you need to add some fields to it. It's up to you to decide
which field you want to add, that is what "table_name.field_name" you
want to specify in your INSERT query.

For example if you want to specify a value for each field in the
"table_name" table, then you need to:
1- get a pointer to the GdaDictTable named "table_name" in your dictionary
2- get a list of fields in that table, and for each field do (please
see at the end for explanations about GdaEntity, etc objects:
   // Create a GdaQueryFieldValue
   GdaQueryField *qf;
   qf = gda_query_field_field_new (query, "the table_name.field_name);
   gda_entity_add_field (query, qf);
   g_object_unref (qf);

   // Create a GdaQueryFieldValue which will be the actual value to
give to qf (part of the "VALUES (...)" of the INSERT query
   // this object can be manipulated in many ways depending on what
you want to do.
   GdaQueryField *qv;
   qv = gda_query_field_value_new (query, a GType);
   gda_query_field_value_set_value (qv, a GValue);
   gda_query_field_set_visible (qv, FALSE);
   gda_entity_add_field (query, qv);
   g_object_unref (qv);

   // Specify that qf will get its value from qv
   g_object_set (qf, "value-provider", qv, NULL);


Now how do I get the actual fields in the table "table name"? Can do this?

table_fields = gda_entity_get_all_fields(GDA_ENTITY(query))

while(list_fields) {
   GdaEntity *field = (GdaEntity*) list_fields->data

   // And Now? Do I need Entities or QueryEntities or QueryField or
QueryFieldField

}

Could you talk about the Diferences betwean a GdaEntity, QueryEntities,
QueryField and QueryFieldField?

A GdaEntity is an interface for objects which are composed of a list
of fields (each implementing the GdaEntityField interface). Examples
of GdaEntity are a table (a GdaDictTable) which has a list of fields
(GdaDictField objects), or a query (a GdaQuery) which has a list of
fields (GdaQueryField objects). The table example is easy to
unserstand, for the query, think about the SELECT query which is like
a view which is like a table.

A table has a given set of fields (each a GdaDictField object) which
have attributes you can query using the gda_dict_field_*() and
gda_entity_field_*() functions. The same schema applies to queries,
except that you must use gda_query_field*() instead of
gda_dict_field_*().


Could you explain how a Query Object works and what it need to work?

Please have a look at the "Data manipulation (DML) queries" section in
the libgda's doc which is quite a lenghty introduction to queries.

Cheers,

Vivien



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