## GdaVconnectionDataModel GdaVconnectionDataModel — Virtual connection based on a list of GdaDataModel |

## Stability Level

Stable, unless otherwise indicated

## Synopsis

    struct              GdaVconnectionDataModel;
    void                (*GdaVconnectionDataModelFunc)      (GdaDataModel *Param1,
                                                             const gchar *Param2,
                                                             gpointer Param3);
    struct              GdaVconnectionDataModelSpec;
    struct              GdaVconnectionDataModelFilter;
    GList *             (*GdaVconnectionDataModelCreateColumnsFunc)
                                                            (GdaVconnectionDataModelSpec *Param1,
                                                             GError **Param2);
    GdaDataModel *      (*GdaVconnectionDataModelCreateModelFunc)
                                                            (GdaVconnectionDataModelSpec *Param1);
    void                (*GdaVconnectionDataModelParseFilterFunc)
                                                            (GdaVconnectionDataModelSpec *Param1,
                                                             GdaVconnectionDataModelFilter *Param2);
    GdaDataModel *      (*GdaVconnectionDataModelCreateFModelFunc)
                                                            (GdaVconnectionDataModelSpec *Param1,
                                                             int Param2,
                                                             const char *Param3,
                                                             int Param4,
                                                             GValue **Param5);
    gboolean            gda_vconnection_data_model_add      (GdaVconnectionDataModel *cnc,
                                                             GdaVconnectionDataModelSpec *spec,
                                                             GDestroyNotify spec_free_func,
                                                             const gchar *table_name,
                                                             GError **error);
    gboolean            gda_vconnection_data_model_add_model
                                                            (GdaVconnectionDataModel *cnc,
                                                             GdaDataModel *model,
                                                             const gchar *table_name,
                                                             GError **error);
    gboolean            gda_vconnection_data_model_remove   (GdaVconnectionDataModel *cnc,
                                                             const gchar *table_name,
                                                             GError **error);
    const gchar *       gda_vconnection_data_model_get_table_name
                                                            (GdaVconnectionDataModel *cnc,
                                                             GdaDataModel *model);
    GdaDataModel *      gda_vconnection_data_model_get_model
                                                            (GdaVconnectionDataModel *cnc,
                                                             const gchar *table_name);
    GdaVconnectionDataModelSpec * gda_vconnection_data_model_get
                                                            (GdaVconnectionDataModel *cnc,
                                                             const gchar *table_name);
    void                gda_vconnection_data_model_foreach  (GdaVconnectionDataModel *cnc,
                                                             GdaVconnectionDataModelFunc func,
                                                             gpointer data);

## Object Hierarchy

      GObject
       +----GdaConnection
             +----GdaVirtualConnection
                   +----GdaVconnectionDataModel
                         +----GdaLdapConnection
                         +----GdaVconnectionHub

## Implemented Interfaces

GdaVconnectionDataModel implements GdaLockable.

## Signals

      "vtable-created"                                 : Run Last
      "vtable-dropped"                                 : Run Last

## Description

The GdaVconnectionDataModel is a virtual connection in which GdaDataModel data models can be added or removed, each representing a table in the connection, the `gda_vconnection_data_model_add_model()` and `gda_vconnection_data_model_remove()` methods.

Furthermore it is possible to declare tables without any associated data model yet, the real data model being automatically created when the table's data is needed. This allows more dynamic table's contents and filtering optimizations. See the `gda_vconnection_data_model_add()` and `gda_vconnection_data_model_remove()` methods. The filtering optimizations are based on SQLite's virtual table optimisations see SQLite's documentation for some background information.

## Details

### struct GdaVconnectionDataModel

    struct GdaVconnectionDataModel;

### GdaVconnectionDataModelFunc ()

    void                (*GdaVconnectionDataModelFunc)      (GdaDataModel *Param1,
                                                             const gchar *Param2,
                                                             gpointer Param3);

This function is called for every GdaDataModel representing a table in a GdaVconnectionDataModel connection, when using the `gda_vconnection_data_model_foreach()` method.

`Param1` : | a pointer to a GdaDataModel. [allow-none]
`Param2` : | the name of the table represented by `Param1`
`Param3` : | a data pointer, passed as last ergument to `gda_vconnection_data_model_foreach()`

### struct GdaVconnectionDataModelSpec

    struct GdaVconnectionDataModelSpec {
    	GdaDataModel                             *data_model;
    	GdaVconnectionDataModelCreateColumnsFunc  create_columns_func;
    	GdaVconnectionDataModelCreateModelFunc    create_model_func;

    	GdaVconnectionDataModelParseFilterFunc    create_filter_func;
    	GdaVconnectionDataModelCreateFModelFunc   create_filtered_model_func;
    };

This structure holds all the information supplied to declare a virtual table using `gda_vconnection_data_model_add()`. You don't need to provider pointers for all the functions and for `data_model`, but the following rules have to be respected:

- `data_model` is not `NULL` and all the function pointers are `NULL`: this is the situation when the virtual table's contents is defined once by `data_model`

- `data_model` is `NULL` and `create_columns_func` is not `NULL`:

  - `create_filtered_model_func` is not `NULL`: this is the situation where the virtual table's associated data model handles filter optimizations. `create_model_func` is ignored in this case.

  - `create_model_func` is not `NULL`: this is the situation where the virtual table's associated data model does not handle filter optimizations

Note that if specifying a `create_filtered_model_func`, you should also specifiy a `create_filter_func` function which is actually responsible for analysing the optimization.

GdaDataModel *`data_model`; | a GdaDataModel, or `NULL`. [allow-none]
GdaVconnectionDataModelCreateColumnsFunc `create_columns_func`; | a pointer to a GdaVconnectionDataModelCreateColumnsFunc function, or `NULL`. [allow-none]
GdaVconnectionDataModelCreateModelFunc `create_model_func`; | a pointer to a GdaVconnectionDataModelCreateModelFunc function, or `NULL`. [allow-none]
GdaVconnectionDataModelParseFilterFunc `create_filter_func`; | a pointer to a GdaVconnectionDataModelParseFilterFunc function, or `NULL`. [allow-none]
GdaVconnectionDataModelCreateFModelFunc `create_filtered_model_func`; | a pointer to a GdaVconnectionDataModelCreateFModelFunc function, or `NULL`. [allow-none]

### struct GdaVconnectionDataModelFilter

    struct GdaVconnectionDataModelFilter {
    	/* Inputs */
    	int nConstraint; /* Number of entries in aConstraint */
    	struct GdaVirtualConstraint {
    		int iColumn;       /* Column on left-hand side of constraint */
    		GdaSqlOperatorType op;  /* Constraint operator, among _EQ, _LT, _LEQ, _GT, _GEQ, _REGEXP */
    	} *aConstraint;            /* Table of WHERE clause constraints */
    	int nOrderBy;              /* Number of terms in the ORDER BY clause */
    	struct GdaVirtualOrderby {
    		int iColumn;       /* Column number */
    		gboolean desc;     /* TRUE for DESC.  FALSE for ASC. */
    	} *aOrderBy;               /* The ORDER BY clause */

    	/* Outputs */
    	struct GdaVirtualConstraintUsage {
    		int argvIndex;     /* if >0, constraint is part of argv to xFilter */
    		gboolean omit;     /* Do not code a test for this constraint if TRUE */
    	} *aConstraintUsage;
    	int idxNum;                /* Number used to identify the index */
    	gpointer idxPointer;       /* Pointer used to identify the index */
    	gboolean orderByConsumed;  /* TRUE if output is already ordered */
    	double estimatedCost;      /* Estimated cost of using this index */
    };

This structure contains data which should be analysed to produce a data model (used as data for a virtual table) when a GdaVconnectionDataModelCreateFModelFunc is called. The structure contains an input part (which should not be modified) and and output part (it is closely mapped with SQLite's sqlite3_index_info type).

A pointer to this structure is passed to the GdaVconnectionDataModelParseFilterFunc function and the function has to modify the variables in the output part (marked as *Outputs*).

The `idxNum` and `idxPointer` are passed to the GdaVconnectionDataModelCreateFModelFunc function call and they represent nothing specific except that the GdaVconnectionDataModelParseFilterFunc and GdaVconnectionDataModelCreateFModelFunc functions need to agree on their meaning.

See the gda-vconnection-hub.c file for an usage example.

### GdaVconnectionDataModelCreateColumnsFunc ()

    GList *             (*GdaVconnectionDataModelCreateColumnsFunc)
                                                            (GdaVconnectionDataModelSpec *Param1,
                                                             GError **Param2);

Function called to create the virtual table's columns, as GdaColumn objects.

`Param1` : | a pointer to a GdaVconnectionDataModelSpec structure
`Param2` : | a place to store errors, or `NULL`
Returns : | a new list of GdaColumn objects. [element-type GdaColumn][transfer full]

### GdaVconnectionDataModelCreateModelFunc ()

    GdaDataModel *      (*GdaVconnectionDataModelCreateModelFunc)
                                                            (GdaVconnectionDataModelSpec *Param1);

Function called to create a GdaDataModel object, called when a virtual table's data need to be accessed, and when optimization is not handled.

`Param1` : | a pointer to a GdaVconnectionDataModelSpec structure
Returns : | a new GdaDataModel. [transfer full]

### GdaVconnectionDataModelParseFilterFunc ()

    void                (*GdaVconnectionDataModelParseFilterFunc)
                                                            (GdaVconnectionDataModelSpec *Param1,
                                                             GdaVconnectionDataModelFilter *Param2);

This function actually analyses the proposed optimization and modified `Param2` to tell the database engine how (if applicable) it implements the optimization.

`Param1` : | a pointer to a GdaVconnectionDataModelSpec structure
`Param2` : | a pointer to a GdaVconnectionDataModelFilter structure

### GdaVconnectionDataModelCreateFModelFunc ()

    GdaDataModel *      (*GdaVconnectionDataModelCreateFModelFunc)
                                                            (GdaVconnectionDataModelSpec *Param1,
                                                             int Param2,
                                                             const char *Param3,
                                                             int Param4,
                                                             GValue **Param5);

Function called to create a GdaDataModel object, called when a virtual table's data need to be accessed, and when optimization is handled.

`Param1` : | a pointer to a GdaVconnectionDataModelSpec structure
`Param2` : | the index number chosen to actually execute the optimization (from GdaVconnectionDataModelFilter's idxNum attribute)
`Param3` : | corresponds to the GdaVconnectionDataModelFilter's idxPointer attribute
`Param4` : | size of `Param5`
`Param5` : | an array of GValue, as specified by the GdaVconnectionDataModelFilter's aConstraintUsage's argvIndex value
Returns : | a new GdaDataModel. [transfer full]

### gda_vconnection_data_model_add ()

    gboolean            gda_vconnection_data_model_add      (GdaVconnectionDataModel *cnc,
                                                             GdaVconnectionDataModelSpec *spec,
                                                             GDestroyNotify spec_free_func,
                                                             const gchar *table_name,
                                                             GError **error);

Create a new virtual table named `table_name` in `cnc`. The contents of that new table is dictated by what's in `spec`.

If there is just one GdaDataModel to make appear as a table then the `gda_vconnection_data_model_add_model()` method is easier to use.

The `spec_free_func` can (depending on your code) be used to clean memory allocated for `spec` or `spec->data_model`.

If an error occurs, then the `spec_free_func` function is called using `spec` as argument.

`cnc` : | a GdaVconnectionDataModel connection
`spec` : | a GdaVconnectionDataModelSpec structure, used AS IS (not copied) and can be modified
`spec_free_func` : | function to call when freeing `spec`, or `NULL`. [allow-none]
`table_name` : | the name of the table
`error` : | a place to store errors, or `NULL`
Returns : | `TRUE` if no error occurred

### gda_vconnection_data_model_add_model ()

    gboolean            gda_vconnection_data_model_add_model
                                                            (GdaVconnectionDataModel *cnc,
                                                             GdaDataModel *model,
                                                             const gchar *table_name,
                                                             GError **error);

Make `model` appear as a table named `table_name` in the `cnc` connection (as if a "CREATE TABLE..." statement was executed, except that the data contained within `model` is actually used when `table_name`'s contents is read or written).

For a more general approach, see the `gda_vconnection_data_model_add()` method.

`cnc` : | a GdaVconnectionDataModel connection
`model` : | a GdaDataModel
`table_name` : | the name of the table
`error` : | a place to store errors, or `NULL`
Returns : | `TRUE` if no error occurred

### gda_vconnection_data_model_remove ()

    gboolean            gda_vconnection_data_model_remove   (GdaVconnectionDataModel *cnc,
                                                             const gchar *table_name,
                                                             GError **error);

Remove the table named `table_name` in the `cnc` connection (as if a "DROP TABLE..." statement was executed, except that no data gets destroyed as the associated data model remains the same).

`cnc` : | a GdaVconnectionDataModel connection
`table_name` : | the name of the table to remove from `cnc`
`error` : | a place to store errors, or `NULL`
Returns : | `TRUE` if no error occurred

### gda_vconnection_data_model_get_table_name ()

    const gchar *       gda_vconnection_data_model_get_table_name
                                                            (GdaVconnectionDataModel *cnc,
                                                             GdaDataModel *model);

Find the name of the table associated to `model` in `cnc`

`cnc` : | a GdaVconnectionDataModel connection
`model` : | a GdaDataModel representing a table within `cnc`
Returns : | the table name, or `NULL` if not found. [transfer none][allow-none]

### gda_vconnection_data_model_get_model ()

    GdaDataModel *      gda_vconnection_data_model_get_model
                                                            (GdaVconnectionDataModel *cnc,
                                                             const gchar *table_name);

Find the GdaDataModel object representing the `table_name` table in `cnc`. it can return `NULL` either if no table named `table_name` exists, or if that table actually exists but no GdaDataModel has yet been created. For a more general approach, use the `gda_vconnection_data_model_get()`.

`cnc` : | a GdaVconnectionDataModel connection
`table_name` : | a table name within `cnc`
Returns : | the GdaDataModel, or `NULL`. [transfer none][allow-none]

### gda_vconnection_data_model_get ()

    GdaVconnectionDataModelSpec * gda_vconnection_data_model_get
                                                            (GdaVconnectionDataModel *cnc,
                                                             const gchar *table_name);

Find the GdaVconnectionDataModelSpec specifying how the table named `table_name` is represented in `cnc`.

`cnc` : | a GdaVconnectionDataModel connection
`table_name` : | a table name within `cnc`
Returns : | a GdaVconnectionDataModelSpec pointer, of `NULL` if there is no table named `table_name`. [transfer none][allow-none]

Since 4.2.6

### gda_vconnection_data_model_foreach ()

    void                gda_vconnection_data_model_foreach  (GdaVconnectionDataModel *cnc,
                                                             GdaVconnectionDataModelFunc func,
                                                             gpointer data);

Call `func` for each table in `cnc`.

Warning: `func` will be called for any table present in `cnc` even if no data model represents the contents of the table (which means the 1st argument of `func` may be `NULL`)

`cnc` : | a GdaVconnectionDataModel connection
`func` : | a GdaVconnectionDataModelFunc function pointer
`data` : | data to pass to `func` calls

## Signal Details

### The `"vtable-created"` signal

    void                user_function                      (GdaVconnectionDataModel *cnc,
                                                            gchar                   *spec,
                                                            gpointer                 user_data)      : Run Last

Signal emitted when a new virtual table has been declared

`cnc` : | the GdaVconnectionDataModel connection
`spec` : | the GdaVconnectionDataModelSpec for the new virtual table
`user_data` : | user data set when the signal handler was connected.

### The `"vtable-dropped"` signal

    void                user_function                      (GdaVconnectionDataModel *cnc,
                                                            gchar                   *spec,
                                                            gpointer                 user_data)      : Run Last

Signal emitted when a new virtual table has been undeclared

`cnc` : | the GdaVconnectionDataModel connection
`spec` : | the GdaVconnectionDataModelSpec for the new virtual table
`user_data` : | user data set when the signal handler was connected.

## See Also

The GdaVproviderDataModel provider to use to create such connection objects.