static gboolean sql_execute_command (GdaConnection *con, gchar *command) { GdaCommand *cmd; GList *list; GError *error = NULL; cmd = gda_command_new (command, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); list = gda_connection_execute_command (con, cmd, NULL, &error); gda_command_free (cmd); while (list) { if (list->data) { g_object_unref (list->data); } list = list->next; } g_list_free (list); if(error != NULL) { g_error_free (error); return FALSE; } else { return TRUE; } }
static GdaDataModel * sql_execute_query (GdaConnection *con, gchar *query) { GdaCommand *cmd; GdaDataModel *result; cmd = gda_command_new (query, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); result = gda_connection_execute_select_command (con, cmd, NULL, NULL); gda_command_free (cmd); return result; }
/** * oseaserver_command_execute_single_query: * @cmd_string: printf posix format string sql command. * @Varargs: Arguments for cmd_string * * Execute a sql query which gets data such as SELECT sentence. This function can be used as follow: * * oseaserver_command_execute_single_query ("SELECT * FROM anywhere"); * * But also, as follow: * * oseaserver_command_execute_single_query ("SELECT * FROM anywhere WHERE attr = '%s'", value); * Return value: a dataset which contains the query data or NULL if something fails. **/ CoyoteDataSet * oseaserver_command_execute_single_query (gchar * cmd_string, ...) { gchar * cmd_result = NULL; GdaCommand * command = NULL; GdaDataModel * datamodel = NULL; CoyoteDataSet * dataset = NULL; va_list args; g_return_val_if_fail (cmd_string, FALSE); // Create a database connection if (!connection) { connection = oseaserver_database_new_connection (); if (!connection) { g_log (LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Unable to create a database connection"); return NULL; } } // Create sql query to database va_start (args, cmd_string); cmd_result = g_strdup_vprintf (cmd_string, args); va_end (args); command = gda_command_new ((const gchar *) cmd_result, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); datamodel = gda_connection_execute_single_command (connection, command, NULL); gda_command_free (command); if (!datamodel) { g_log (LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Unable to execute: \n%s\n", cmd_result); g_free (cmd_result); return NULL; } dataset = oseaserver_command_get_dataset (datamodel); g_free (cmd_result); g_object_unref (datamodel); return dataset; }
static const GValue *_select_guid_at_offset(const gchar *table, guint offset) { GdaCommand *command; GdaDataModel *model; GString *sql = g_string_new("SELECT guid FROM "); g_string_append_printf(sql, "%s LIMIT 1 OFFSET %d", table, offset); command = gda_command_new (sql->str, GDA_COMMAND_TYPE_SQL, 0); model = gda_connection_execute_select_command (mgd->priv->connection, command, NULL, NULL); gda_command_free (command); g_string_free(sql, TRUE); if (!model) return NULL; gda_data_model_dump (model, stdout); return gda_data_model_get_value_at_col_name(model, "guid", 0); }
/** * oseaserver_command_execute_non_query: * @cmd_string: printf posix format string sql command. * @Varargs: Arguments for cmd_string * * Execute a sql query which doesn't get any data such as UPDATE or * DELETE sentences. This function can be used as follow: * * oseaserver_command_execute_non_query (connection, "DELETE * FROM anywhere"); * * But also, as follow: * * oseaserver_command_execute_non_query (connection, "DELETE * FROM anywhere WHERE attr = '%s'", value); * * Return value: TRUE if query was succesfully executed, otherwise return FALSE. **/ gboolean oseaserver_command_execute_non_query (gchar * cmd_string, ...) { gchar * cmd_result = NULL; GdaCommand * command = NULL; gboolean result = TRUE; gint error_code; va_list args; g_return_val_if_fail (cmd_string, FALSE); // Create a database connection if (!connection) { connection = oseaserver_database_new_connection (); if (!connection) { g_log (LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Unable to create a database connection"); return FALSE; } } // Create sql query to database va_start (args, cmd_string); cmd_result = g_strdup_vprintf (cmd_string, args); va_end (args); command = gda_command_new ((const gchar *) cmd_result, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); error_code = gda_connection_execute_non_query (connection, command, NULL); if (error_code == -1) { oseaserver_log_write ("Unable to execute: \n%s\n", cmd_result); result = FALSE; } gda_command_free (command); g_free (cmd_result); return result; }