## Processing queries

## Executing sentences

### Building commands

Before invoking a query you have to build the structure containing the command and you can do this with gda_command_new ().

The command type we most commonly use is GDA_COMMAND_TYPE_SQL because we will only focus on SQL queries[3]

              typedef enum {
                      GDA_COMMAND_OPTION_IGNORE_ERRORS  = 1,
                      GDA_COMMAND_OPTION_STOP_ON_ERRORS = 1 << 1,
                      GDA_COMMAND_OPTION_BAD_OPTION     = 1 << 2
              } GdaCommandOptions;

| Ignores all errors and executes all sentences returning data models. For failed sentences, it returns an empty data model.
| Stops when finding and error and doesn't return data models.

Here you see an example of creating a command:

              gint
              execute_sql_non_query (GdaConnection *connection, const gchar * buffer)
              {
                GdaCommand *command;
                gint number;

                command = gda_command_new (buffer, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
                number  = gda_connection_execute_non_query (connection, command, NULL);

                gda_command_free (command);

                return (number);
              }

| Our function. You can give it several comma-separated sentences.
| We will see it later.
| It is a good practice to free the commands.

### Making non queries

Non queries are queries that does not return data, only the number of rows affected, as a DELETE or an UPDATE. We use gda_connection_execute_non_query()

            gint
            execute_sql_non_query (GdaConnection *connection, const gchar * buffer)
            {
              GdaCommand *command;
              gint number;

              command = gda_command_new (buffer, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
              number  = gda_connection_execute_non_query (connection, command, NULL);

              gda_command_free (command);

              return (number);
            }

### Making normal queries

Normal queries are queries that return data (data models). You have two ways to do this:

- gda_data_model_execute_single_command()

- gda_data_model_execute_command()

You can use the first way when you want to invoke only a single command. Second way is used to execute several comma-separated sentences. It is recommended to use gda_connection_execute_single_command (). Here you see an example:

              gboolean
              execute_sql_command (GdaConnection *connection, const gchar * buffer)
              {
                GdaCommand *command;
                GList *list;
                GList *node;
                gboolean errors=FALSE;

                GdaDataModel *dm;

                command = gda_command_new (buffer, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
                list = gda_connection_execute_command (connection, command, NULL);
                if (list!=NULL)
                  for (node=g_list_first(list); node != NULL; node=g_list_next(node))
                    {
                      dm=(GdaDataModel *) node->data;
                      if (dm == NULL)
                        {
                          errors=TRUE;
                        }
                      else
                        {
                          show_table (dm);
                          g_object_unref(dm);
                        }
                    }
                else
                  {
                    errors=TRUE;
                  }
                gda_command_free (command);

                return (errors);
              }

| Executes the query and obtains a list of data models
| Loop for moving through the list of data models. If you use gda_connection_execute_single_command(), you should not need to use a loop, because this function would return a data model.

[3] There are other command types, as XML and so on.