[gnome-db] Question about gda_column_get_table
- From: Juan Manuel Mouriz <jmouriz gmail com>
- To: GNOME-DB <gnome-db-list gnome org>
- Subject: [gnome-db] Question about gda_column_get_table
- Date: Fri, 25 Jul 2008 22:15:52 -0300
Good night list. I write the following program using GDA, but I have a doubt about red painted code:
#include <gtk/gtk.h>
#include <libgda/libgda.h>
#include <stdio.h>
#define DSN "DB_DIR=.;DB_NAME=balance.db"
#define UNKNOWN_ERROR "No hay ningún detalle de este error"
typedef struct _Data {
GtkTreeView *tree_view;
gint position;
const gchar *table;
const gchar *name;
} Data;
/* Muestra un diálogo de error al usuario */
void
show_error_dialog (const gchar *title, const gchar *message, const gchar *error)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new_with_markup (NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
error ? "<span weight=\"heavy\" size=\"larger\">%s</span>" : "%s", message);
if (error)
gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog), "%s", error);
gtk_window_set_title (GTK_WINDOW (dialog), title ? title : "Error");
gdk_beep ();
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
/* Función que se ejecuta cada vez que se edita una celda */
static void
cell_edited (GtkCellRendererText *renderer, const gchar *path, const gchar *value, gpointer data)
{
GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (((Data *) data)->tree_view));
gint column_number = ((Data *) data)->position;
const gchar *table = ((Data *) data)->table;
const gchar *name = ((Data *) data)->name;
g_debug ("update %s set %s = '%s'\n", table, name, value);
GtkTreeIter iter;
gtk_tree_model_get_iter (model, &iter, gtk_tree_path_new_from_string (path));
gtk_list_store_set (GTK_LIST_STORE (model), &iter, column_number, value, -1);
}
/* Exponer los datos en la vista */
void
expose_data_model (const gchar *table, GdaDataModel *data_model, GtkTreeView *tree_view)
{
GList *columns = NULL;
gint column_count = -1;
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
GtkTreeModel *model;
GType *types;
gint column_number;
gint row_count;
gint row_number;
GtkTreeIter iter;
if ((row_count = gda_data_model_get_n_rows (data_model)) < 1)
return;
columns = gtk_tree_view_get_columns (tree_view);
while (g_list_nth (columns, ++column_count))
gtk_tree_view_remove_column (tree_view, g_list_nth_data (columns, column_count));
g_list_free (columns);
if ((column_count = gda_data_model_get_n_columns (data_model)) < 1)
return;
types = g_new0 (GType, column_count * 2);
for (column_number = 0; column_number < column_count; column_number++) {
const gchar *name = gda_data_model_get_column_name (data_model, column_number);
GdaColumn *column_description = gda_data_model_describe_column (data_model, column_number);
g_debug ("table: %s\n", gda_column_get_table (column_description));
Data *data = "" (sizeof (Data));
data->tree_view = tree_view;
data->position = column_number;
data->table = table;
data->name = name;
renderer = gtk_cell_renderer_text_new ();
g_object_set (renderer, "editable", TRUE, NULL);
g_signal_connect (renderer, "edited", G_CALLBACK (cell_edited), data);
column = gtk_tree_view_column_new_with_attributes (name, renderer, "text", column_number, "xalign",
column_count + column_number, NULL);
gtk_tree_view_append_column (tree_view, column);
types[column_number] = G_TYPE_STRING;
types[column_count + column_number] = G_TYPE_FLOAT;
}
model = GTK_TREE_MODEL (gtk_list_store_newv (column_count * 2, types));
gtk_tree_view_set_model (tree_view, model);
for (row_number = 0; row_number < row_count; row_number++) {
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
for (column_number = 0; column_number < column_count; column_number++) {
const GValue *value = gda_data_model_get_value_at (data_model, column_number, row_number);
gtk_list_store_set (GTK_LIST_STORE (model), &iter, column_number, gda_value_stringify (value), -1);
if (!G_VALUE_HOLDS_STRING (value))
gtk_list_store_set (GTK_LIST_STORE (model), &iter, column_count + column_number, 1.0, -1);
}
}
}
/* Función que se ejecuta cuando se cierra la ventana */
gboolean
window_delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
{
gtk_main_quit ();
}
/* Punto de entrada */
int
main (int argc, char *argv[])
{
GdaConnection *connection;
GtkWidget *window;
GtkWidget *scrolled_window;
GtkWidget *tree_view;
GError *error;
gtk_init (&argc, &argv);
gda_init ();
/* Crear la conexión a la base de datos */
error = NULL;
connection = gda_connection_open_from_string ("SQLite", DSN, NULL, 0, &error);
if (!connection) {
show_error_dialog ("Error de conexión", error && error->message ? error->message : UNKNOWN_ERROR, NULL);
return (1);
}
/* Abrir la conexión */
error = NULL;
if (!gda_connection_open (connection, &error)) {
show_error_dialog ("Error de conexión", error && error->message ? error->message : UNKNOWN_ERROR, NULL);
return (1);
}
/* Actualizar el diccionario */
error = NULL;
if (!gda_connection_update_meta_store (connection, NULL, &error)) {
show_error_dialog ("Error de conexión", error && error->message ? error->message : UNKNOWN_ERROR, NULL);
return (1);
}
/* Definición de la GUI */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
tree_view = gtk_tree_view_new ();
/* Cargar datos */
GdaStatement *statement;
GdaDataModel *data_model;
GdaSqlParser *parser = gda_connection_create_parser (connection);
if (!parser)
parser = gda_sql_parser_new ();
if (!parser) {
show_error_dialog ("Error interno", "No se puede crear el analizador sintáctico para interpretar comandos SQL", NULL);
return (1);
}
g_object_set_data_full (G_OBJECT (connection), "parser", parser, g_object_unref);
error = NULL;
statement = gda_sql_parser_parse_string (parser, "
select obras.obra_id || ':' || complejos.complejo_id || ':' || unidades.unidad_id as id, "
"obras.obra, complejos.complejo, unidades.unidad, unidades.cantidad from unidades inner join complejos on complejos.complejo_id = "
"unidades.complejo_id inner join obras on obras.obra_id = complejos.obra_id order by 1", NULL, &error);
if (!statement) {
show_error_dialog ("Error SQL", error && error->message ? error->message : UNKNOWN_ERROR, NULL);
return (1);
}
data_model = gda_data_model_query_new (connection, statement);
g_object_unref (statement);
if (!data_model) {
show_error_dialog ("Error SQL", error && error->message ? error->message : UNKNOWN_ERROR, NULL);
return (1);
}
expose_data_model ("materiales", data_model, GTK_TREE_VIEW (tree_view));
gda_data_model_dump (data_model, stdout);
/* Construir la GUI */
gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
gtk_container_add (GTK_CONTAINER (window), scrolled_window);
/* Conectar todas las señales */
g_signal_connect (window, "delete_event", G_CALLBACK (window_delete_event), NULL);
/* Comenzar la iteración con el usuario */
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
Why I don't get the table name? How to can I get this?
Thanks
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]