/* * Copyright (C) 2012 - Juan Ferrer Toribio * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "vn-consulter.h" #include "stdlib.h" G_DEFINE_TYPE (VnConsulter, vn_consulter, VN_TYPE_FORM); #define FILE_KW "sql://" //+++++++++++++++++++++++++++++++++++++++++++++++++++ Private void vn_consulter_clean_clicked (GtkButton * button, VnConsulter * obj) { gtk_list_store_clear (obj->model); } static gboolean idle_sender (VnConsulter * obj) { static int a = 0; if (a < 100) { db_model_refresh (vn_grid_get_model (obj->grid)); a++; return TRUE; } a = 0; obj->source = 0; return FALSE; } void vn_consulter_multisend_clicked (GtkButton * button, VnConsulter * obj) { if (vn_grid_get_model (obj->grid)) { if (!obj->source) obj->source = g_timeout_add (100, (GSourceFunc) idle_sender, obj); else if (g_source_remove (obj->source)) obj->source = 0; } } void vn_consulter_start_clicked (GtkButton * button, VnConsulter * obj) { db_conn_start_transaction (VN_FORM (obj)->conn); } void vn_consulter_on_stop_clicked (GtkButton * button, VnConsulter * obj) { db_conn_kill_query (VN_FORM (obj)->conn); } void vn_consulter_commit_clicked (GtkButton * button, VnConsulter * obj) { db_conn_commit (VN_FORM (obj)->conn); } void vn_consulter_rollback_clicked (GtkButton * button, VnConsulter * obj) { db_conn_rollback (VN_FORM (obj)->conn); } static void vn_consulter_set_message (VnConsulter * obj, const gchar * msg) { GtkTreePath * path; GtkTreeIter iter; gtk_list_store_append (obj->model, &iter); gtk_list_store_set (obj->model, &iter, 0, msg, -1); path = gtk_tree_model_get_path (GTK_TREE_MODEL (obj->model), &iter); gtk_tree_view_scroll_to_cell (obj->tv, path, NULL, FALSE, 0, 0); gtk_tree_path_free (path); } static void vn_consulter_model_status_changed (DbModel * model, DbModelStatus status, VnConsulter * obj) { GList * n; GtkTreeView * tv = GTK_TREE_VIEW (obj->grid); GList * cols = gtk_tree_view_get_columns (tv); for (n = cols; n; n = n->next) { GtkTreeViewColumn * col = n->data; gtk_tree_view_remove_column (tv, col); } g_list_free (cols); switch (status) { case DB_MODEL_STATUS_READY: { gint i; for (i = 0; i < db_model_get_ncols (model); i++) { VnColumn * column; GType col_type, type = db_model_get_column_type (model, i); if (type == G_TYPE_BYTES) col_type = VN_TYPE_COLUMN_IMAGE; else if (type == G_TYPE_BOOLEAN) col_type = VN_TYPE_COLUMN_CHECK; else col_type = VN_TYPE_COLUMN_ENTRY; if (i == 7) col_type = VN_TYPE_COLUMN_IMAGE; column = vn_grid_append_column (obj->grid, i ,db_model_get_column_name (model, i) ,col_type ,TRUE, FALSE ); if (i == 7) g_object_set (column, "host", "www.verdnatura.es", "path", "image/cache/catalog/icon", "tooltip-path", "../full", NULL); if (type == G_TYPE_FLOAT || type == G_TYPE_DOUBLE) g_object_set (column, "digits", 3, NULL); } break; } case DB_MODEL_STATUS_ERROR: vn_consulter_set_message (obj, "Error"); break; default: break; } gtk_widget_set_sensitive (GTK_WIDGET (obj->mode), TRUE); } void vn_consulter_mode_toggled (GtkToggleButton * mode, VnConsulter * obj) { DbIterator * iterator = vn_handler_get_iterator (obj->handler); db_iterator_set_mode (iterator ,(gtk_toggle_button_get_active (mode)) ? DB_ITERATOR_MODE_ON_CHANGE: DB_ITERATOR_MODE_ON_DEMAND ); } void vn_consulter_send (GtkButton * button, VnConsulter * obj) { DbIterator * iterator; DbModel * model; gchar * sql; sql = gtk_combo_box_text_get_active_text (obj->combo); vn_consulter_set_message (obj, sql); if (g_str_has_prefix (sql, FILE_KW)) model = db_model_new_with_file (VN_FORM (obj)->conn, sql + g_utf8_strlen (FILE_KW, -1)); else model = db_model_new_with_sql (VN_FORM (obj)->conn, sql); g_signal_connect (model, "status-changed", G_CALLBACK (vn_consulter_model_status_changed), obj); iterator = db_iterator_new (model); iterator->mode = DB_ITERATOR_MODE_ON_DEMAND; vn_handler_set_iterator (obj->handler, iterator); vn_grid_set_iterator (obj->grid, iterator); vn_consulter_mode_toggled (GTK_TOGGLE_BUTTON (obj->mode), obj); g_free (sql); g_object_unref (model); g_object_unref (iterator); } static void vn_consulter_open (VnConsulter * obj) { gint i; VnForm * form = VN_FORM (obj); gchar * queries[] = { //MySQL (kk schema) "/*my*/SELECT id, name, item_id, item_id2, amount, item.price " "FROM movement JOIN item USING (item_id, item_id2)" ,"/*my*/SELECT item_id,item_id2, i.name, m.id, m.amount, m.price " "FROM item i LEFT JOIN movement m USING (item_id, item_id2)" ,"/*my*/SELECT item_id, item_id2, name, price FROM item" //PgSQL (test schema) ,"/*pg*/SELECT pg_sleep(120)" ,"/*pg*/SELECT i.id, i.name, i.color, m.id, m.amount " "FROM item i LEFT JOIN movement m ON i.id = m.item_id" ,"/*pg*/SELECT m.id, amount, item_id, name, color " "FROM movement m JOIN item ON m.item_id = item.id" ,FILE_KW"example/consulter" ,"/*pg*/ SELECT id1, id2, name, ok, date, number, floating, image " "FROM prueben" ,NULL }; obj->model = vn_form_get (form, "model"); obj->combo = vn_form_get (form, "query"); obj->tv = vn_form_get (form, "treeview"); obj->mode = vn_form_get (form, "mode"); obj->grid = vn_form_get (form, "consulter"); obj->handler = vn_form_get (form, "handler"); gtk_entry_set_placeholder_text (vn_form_get (form, "combo-entry"), _("Type or select a query")); gtk_tree_view_insert_column_with_attributes (obj->tv, -1, "Mensaje", gtk_cell_renderer_text_new (), "text", 0, NULL); for (i = 0; i < g_strv_length (queries); i++) gtk_combo_box_text_prepend_text (obj->combo, queries[i]); } //+++++++++++++++++++++++++++++++++++++++++++++++++++ Class static void vn_consulter_init (VnConsulter * obj) { obj->source = 0; } static void vn_consulter_class_init (VnConsulterClass * k) { VN_FORM_CLASS (k)->open = (VnFormOpenFunc) vn_consulter_open; }