GdaMetaStruct

GdaMetaStruct — In memory representation of some database objects

Synopsis




                    GdaMetaStruct;
enum                GdaMetaStructFeature;
enum                GdaMetaStructError;
enum                GdaMetaDbObjectType;
#define             GDA_META_DB_OBJECT                  (x)
#define             GDA_META_DB_OBJECT_GET_TABLE        (dbobj)
#define             GDA_META_DB_OBJECT_GET_VIEW         (dbobj)
                    GdaMetaTable;
                    GdaMetaView;
                    GdaMetaTableColumn;
#define             GDA_META_TABLE_COLUMN               (x)
                    GdaMetaTableForeignKey;
#define             GDA_META_TABLE_FOREIGN_KEY          (x)
#define             GDA_META_STRUCT_ERROR
GdaMetaStruct*      gda_meta_struct_new                 (GdaMetaStructFeature features);
GdaMetaDbObject*    gda_meta_struct_complement          (GdaMetaStruct *mstruct,
                                                         GdaMetaStore *store,
                                                         GdaMetaDbObjectType type,
                                                         const GValue *catalog,
                                                         const GValue *schema,
                                                         const GValue *name,
                                                         GError **error);
enum                GdaMetaSortType;
gboolean            gda_meta_struct_sort_db_objects     (GdaMetaStruct *mstruct,
                                                         GdaMetaSortType sort_type,
                                                         GError **error);
GdaMetaDbObject*    gda_meta_struct_get_db_object       (GdaMetaStruct *mstruct,
                                                         const GValue *catalog,
                                                         const GValue *schema,
                                                         const GValue *name);
GdaMetaTableColumn* gda_meta_struct_get_table_column    (GdaMetaStruct *mstruct,
                                                         GdaMetaTable *table,
                                                         const GValue *col_name);
enum                GdaMetaGraphInfo;
gchar*              gda_meta_struct_dump_as_graph       (GdaMetaStruct *mstruct,
                                                         GdaMetaGraphInfo info,
                                                         GError **error);

Object Hierarchy


  GObject
   +----GdaMetaStruct

Properties


  "features"                 guint                 : Read / Write / Construct Only

Description

The GdaMetaStruct object reads data from a GdaMetaStore object and creates an easy to use in memory representation for some database objects. For example one can easily analyse the columns of a table (or its foreign keys) using a GdaMetaStruct.

The GdaMetaStruct object exports a single GSList list of database objects as GdaMetaDbObject structures, which should not be modified. The list is the db_objects member of the structure. Use gda_meta_struct_complement() to request that the GdaMetaStruct object add some information about some database object.

In the following code sample, one prints the columns names and types of a table:

GdaMetaStruct *mstruct;
GdaMetaDbObject *dbo;
GValue *catalog, *schema, *name;

/* Define name (and optionnally catalog and schema) */
[...]

mstruct = gda_meta_struct_new ();
gda_meta_struct_complement (mstruct, store, GDA_META_DB_TABLE, catalog, schema, name, NULL);
dbo = gda_meta_struct_get_db_object (mstruct, catalog, schema, name);
if (!dbo)
        g_print ("Table not found\n");
else {
        GSList *list;
        for (list = GDA_META_DB_OBJECT_GET_TABLE (dbo)->columns; list; list = list->next) {
                GdaMetaTableColumn *tcol = GDA_META_TABLE_COLUMN (list->data);
                g_print ("COLUMN: %s (%s)\n", tcol->column_name, tcol->column_type);
        }
}
gda_meta_struct_free (mstruct);
  

If now the database object type is not known, one can use the following code:

GdaMetaStruct *mstruct;
GdaMetaDbObject *dbo;
GValue *catalog, *schema, *name;

/* Define name (and optionnally catalog and schema) */
[...]

mstruct = gda_meta_struct_new ();
gda_meta_struct_complement (mstruct, store, GDA_META_DB_UNKNOWN, catalog, schema, name, NULL);
dbo = gda_meta_struct_get_db_object (mstruct, catalog, schema, name);
if (!dbo)
        g_print ("Object not found\n");
else {
        if ((dbo->obj_type == GDA_META_DB_TABLE) || (dbo->obj_type == GDA_META_DB_VIEW)) {
                if (dbo->obj_type == GDA_META_DB_TABLE)
                        g_print ("Is a table\n");
                else if (dbo->obj_type == GDA_META_DB_VIEW) {
                        g_print ("Is a view, definition is:\n");
                        g_print ("%s\n", GDA_META_DB_OBJECT_GET_VIEW (dbo)->view_def);
                }

                GSList *list;
                for (list = GDA_META_DB_OBJECT_GET_TABLE (dbo)->columns; list; list = list->next) {
                        GdaMetaTableColumn *tcol = GDA_META_TABLE_COLUMN (list->data);
                        g_print ("COLUMN: %s (%s)\n", tcol->column_name, tcol->column_type);
                }
        }
        else 
                g_print ("Not a table or a view\n");
}
gda_meta_struct_free (mstruct);
  

Details

GdaMetaStruct

typedef struct _GdaMetaStruct GdaMetaStruct;


enum GdaMetaStructFeature

typedef enum {
	GDA_META_STRUCT_FEATURE_NONE              = 0,
	GDA_META_STRUCT_FEATURE_FOREIGN_KEYS      = 1 << 0,
	GDA_META_STRUCT_FEATURE_VIEW_DEPENDENCIES = 1 << 1,

	GDA_META_STRUCT_FEATURE_ALL               = GDA_META_STRUCT_FEATURE_FOREIGN_KEYS |
	                                            GDA_META_STRUCT_FEATURE_VIEW_DEPENDENCIES
} GdaMetaStructFeature;


enum GdaMetaStructError

typedef enum {
	GDA_META_STRUCT_UNKNOWN_OBJECT_ERROR,
        GDA_META_STRUCT_DUPLICATE_OBJECT_ERROR,
        GDA_META_STRUCT_INCOHERENCE_ERROR
} GdaMetaStructError;


enum GdaMetaDbObjectType

typedef enum {
	GDA_META_DB_UNKNOWN,
	GDA_META_DB_TABLE,
	GDA_META_DB_VIEW
} GdaMetaDbObjectType;


GDA_META_DB_OBJECT()

#define GDA_META_DB_OBJECT(x) ((GdaMetaDbObject*)(x))

x :

GDA_META_DB_OBJECT_GET_TABLE()

#define GDA_META_DB_OBJECT_GET_TABLE(dbobj) (&((dbobj)->extra.meta_table))

dbobj :

GDA_META_DB_OBJECT_GET_VIEW()

#define GDA_META_DB_OBJECT_GET_VIEW(dbobj) (&((dbobj)->extra.meta_view))

dbobj :

GdaMetaTable

typedef struct {
	GSList       *columns; /* list of GdaMetaTableColumn */
	
	/* PK fields index */
	gint         *pk_cols_array;
	gint          pk_cols_nb;

	/* Foreign keys */
	GSList       *reverse_fk_list; /* list of GdaMetaTableForeignKey where @depend_on == this GdaMetaDbObject */
	GSList       *fk_list; /* list of GdaMetaTableForeignKey where @gda_meta_table == this GdaMetaDbObject */
} GdaMetaTable;


GdaMetaView

typedef struct {
	GdaMetaTable  table;
	gchar        *view_def;
	gboolean      is_updatable;
} GdaMetaView;


GdaMetaTableColumn

typedef struct {
	gchar        *column_name;
	gchar        *column_type;
	GType         gtype;
	gboolean      pkey;
        gboolean      nullok;
	gchar        *default_value;
} GdaMetaTableColumn;


GDA_META_TABLE_COLUMN()

#define GDA_META_TABLE_COLUMN(x) ((GdaMetaTableColumn*)(x))

x :

GdaMetaTableForeignKey

typedef struct {
	GdaMetaDbObject  *meta_table;
	GdaMetaDbObject  *depend_on;

	gint              cols_nb;	
	gint             *fk_cols_array; /* FK fields index */
	gchar           **fk_names_array; /* FK fields names */
	gint             *ref_pk_cols_array; /* Ref PK fields index */
	gchar           **ref_pk_names_array; /* Ref PK fields names */
} GdaMetaTableForeignKey;


GDA_META_TABLE_FOREIGN_KEY()

#define GDA_META_TABLE_FOREIGN_KEY(x) ((GdaMetaTableForeignKey*)(x))

x :

GDA_META_STRUCT_ERROR

#define GDA_META_STRUCT_ERROR gda_meta_struct_error_quark ()


gda_meta_struct_new ()

GdaMetaStruct*      gda_meta_struct_new                 (GdaMetaStructFeature features);

features : the kind of information the new GdaMetaStruct object will compute (the more features, the more time it takes to run)
Returns : the newly created GdaMetaStruct object

gda_meta_struct_complement ()

GdaMetaDbObject*    gda_meta_struct_complement          (GdaMetaStruct *mstruct,
                                                         GdaMetaStore *store,
                                                         GdaMetaDbObjectType type,
                                                         const GValue *catalog,
                                                         const GValue *schema,
                                                         const GValue *name,
                                                         GError **error);

Creates a new GdaMetaDbObject structure in mstruct to represent the database object (of type type) which can be uniquely identified as catalog.schema.name.

If catalog is NULL and schema is not NULL, then the database object will be the one which is "visible" by default (that is which can be accessed only by its short name name).

If both catalog and schema are NULL, then the database object will be the one which can be accessed by its schema.name name.

mstruct : a GdaMetaStruct object
store : the GdaMetaStore to use
type : the type of object to add (which can be GDA_META_DB_UNKNOWN)
catalog : the catalog the object belongs to (as a G_TYPE_STRING GValue), or NULL
schema : the schema the object belongs to (as a G_TYPE_STRING GValue), or NULL
name : the object's name (as a G_TYPE_STRING GValue), not NULL
error : a place to store errors, or NULL
Returns : the GdaMetaDbObject corresponding to the database object if no error occurred, or NULL

enum GdaMetaSortType

typedef enum {
	GDA_META_SORT_ALHAPETICAL,
	GDA_META_SORT_DEPENDENCIES
} GdaMetaSortType;

GDA_META_SORT_ALHAPETICAL sort by alphabetical order on the schema name and on the object name
GDA_META_SORT_DEPENDENCIES sort such as that for any given GdaMetaDbObject in the list, all its dependencies are _before_ it in the list

gda_meta_struct_sort_db_objects ()

gboolean            gda_meta_struct_sort_db_objects     (GdaMetaStruct *mstruct,
                                                         GdaMetaSortType sort_type,
                                                         GError **error);

Reorders the list of database objects within mstruct in a way specified by sort_type.

mstruct : a GdaMetaStruct object
sort_type : the kind of sorting requested
error : a place to store errors, or NULL
Returns : TRUE if no error occurred

gda_meta_struct_get_db_object ()

GdaMetaDbObject*    gda_meta_struct_get_db_object       (GdaMetaStruct *mstruct,
                                                         const GValue *catalog,
                                                         const GValue *schema,
                                                         const GValue *name);

Tries to locate the GdaMetaDbObject structure representing the database object named after catalog, schema and name.

If one or both of catalog and schema are NULL, and more than one database object matches the name, then the return value is also NULL.

mstruct : a GdaMetaStruct object
catalog : the catalog the object belongs to (as a G_TYPE_STRING GValue), or NULL
schema : the schema the object belongs to (as a G_TYPE_STRING GValue), or NULL
name : the object's name (as a G_TYPE_STRING GValue), not NULL
Returns : the GdaMetaDbObject or NULL if not found

gda_meta_struct_get_table_column ()

GdaMetaTableColumn* gda_meta_struct_get_table_column    (GdaMetaStruct *mstruct,
                                                         GdaMetaTable *table,
                                                         const GValue *col_name);

Tries to find the GdaMetaTableColumn representing the column named col_name in table.

mstruct : a GdaMetaStruct object
table : the GdaMetaTable structure to find the column for
col_name : the name of the column to find (as a G_TYPE_STRING GValue)
Returns : the GdaMetaTableColumn or NULL if not found

enum GdaMetaGraphInfo

typedef enum {
	GDA_META_GRAPH_COLUMNS = 1 << 0
} GdaMetaGraphInfo;


gda_meta_struct_dump_as_graph ()

gchar*              gda_meta_struct_dump_as_graph       (GdaMetaStruct *mstruct,
                                                         GdaMetaGraphInfo info,
                                                         GError **error);

Creates a new graph (in the GraphViz syntax) representation of mstruct.

mstruct : a GdaMetaStruct object
info : informs what kind of information to show in the resulting graph
error : a place to store errors, or NULL
Returns : a new string, or NULL if an error occurred.

Property Details

The "features" property

  "features"                 guint                 : Read / Write / Construct Only

Allowed values: <= G_MAXINT

Default value: 3