Execute a DDL command

DDL commands (commands to modify the database schema such as create tables and views, change users' access rights, etc) are treated in the same way as non select commands (refer to the HOWTO about executing non SELECT commands). However Libgda offers a better and more portable way of executing such commands: using a GdaServerOperation object.

Executing a DDL command involves the following steps:

  1. Request a new GdaServerOperation object from the database provider for a specific operation using the gda_server_provider_create_operation() function.

  2. Specify the GdaServerOperation object's behaviour by setting some pre-defined parameters; for example when creating a table, the parameters to be set include the tables name, the names of the columns, the constraints, etc. The list of parameters to set is listed by the gda-list-server-op program.

  3. Ask the server provider to execute the operation based on the GdaServerOperation object

The following code in part illustrates how to create a view:

GdaServerOperation *op;

op = gda_connection_create_operation (cnc, GDA_SERVER_OPERATION_CREATE_VIEW, NULL, &error);
if (!op)
    /* there was an error while creating the GdaServerOperation object */
else {
    /* define the view to create */
    if (!gda_server_operation_set_value_at (op, "myview", &error, 
                                            "/VIEW_DEF_P/VIEW_NAME") ||
        !gda_server_operation_set_value_at (op, "SELECT * FROM customers", &error, 
        /* there was an error */
    else {
        if (! gda_connection_perform_operation (cnc, op, &error))
            g_print ("Error\n");
            g_print ("View created\n");
    g_object_unref (op);

Please also note that Libgda provides some convenience functions to wrap this process, see the Convenience functions section for more information.