Get information about a table's columns

Libgda supports reporting meta data about a database (for which there is an opened connection). The meta data are stored in a database (usually an in-memory database) which structure is close to the information schema SQL standard (ISO/IEC 9075), and adapted (form information, this database is managed by a GdaMetaStore object). As databases don't notify the changes made to their objects, it is necessary to update (or synchronize) the meta data if the database's schema has been changed, or if the meta data has not yet been extracted: call gda_connection_update_meta_store() for this purpose.

One then needs to find the data requested among the (rather large) quantity of meta data available, there are two possibilities:

  • Knowing the information schema's structure (tables and views) used, run some SELECT commands on the GdaMetaStore object's internal connection: this solution is the most powerful but requires some knowledge of the information schema's structure, and the data is not easy to use.

  • Use a GdaMetaStruct object which exports the meta data as a dynamic tree of pre-defined data structures, easy to use.

The following code shows how to list all the colums of the "customers" table of a connection (the connection is assumed to already be opened):

GdaMetaStruct *mstruct;
GdaMetaDbObject *dbo;
GValue *table_name;
if (!gda_connection_update_meta_store (cnc, NULL, &error)) {
    /* there was an error */
mstruct = gda_meta_struct_new (store, GDA_META_STRUCT_FEATURE_NONE);
table_name = gda_value_new (G_TYPE_STRING);
g_value_set_string (value, "customers");
dbo = gda_meta_struct_complement (mstruct, GDA_META_DB_TABLE,
                                  NULL, NULL, 
                                  table_name, &error);
gda_value_free (table_name);
if (dbo) {
    /* the "customers" table has been found, its details are in dbo */
    GdaMetaTable *table = GDA_META_TABLE (dbo);
    GSList *list;
    for (list = table->columns: list; list = list->next) 
        g_print ("Column: %s\n", ((GdaMetaTableColumn*) list->data)->column_name);
    g_print ("Table not found\n");
g_object_unref (mstruct);