Exemplo n.º 1
0
static void
__initialize_statement_delete_query_parameters (MidgardDBObjectClass *klass, const gchar *query_string, gboolean add_workspace)
{
	GdaSqlParser *parser = gda_sql_parser_new ();
	GdaStatement *stmt;
	GError *error = NULL;
	stmt = gda_sql_parser_parse_string (parser, query_string, NULL, &error);

	if (!stmt) {
		g_error ("Couldn't create %s class DELETE(UPDATE) prepared statement. %s", 
				G_OBJECT_CLASS_NAME (klass), error && error->message ? error->message : "Unknown reason");
		return;
	}

	GdaSet *params; 
	if (!gda_statement_get_parameters (stmt, &params, &error)) {
		g_error ("Failed to create DELETE(UPDATE) GdaSet for %s class. %s", 
				G_OBJECT_CLASS_NAME (klass), error && error->message ? error->message : "Unknown reason");
	}
	
	if (add_workspace) {
		klass->dbpriv->_workspace_statement_delete = stmt;
		klass->dbpriv->_workspace_statement_delete_params = params;
		return;
	}
	
	klass->dbpriv->_statement_delete = stmt;
	klass->dbpriv->_statement_delete_params = params;

	return;
}
Exemplo n.º 2
0
gboolean
wmud_db_load_exits(GSList **exits, GError **err)
{
    GdaStatement     *sth = NULL;
    GdaDataModel     *res = NULL;
    GdaDataModelIter *iter;

    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading exits");
    if (dbh == NULL) {
        if (err) {
            g_set_error(err,
                        WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
                        "Database backend not initialized");
        }

        return FALSE;
    }

    sth = gda_sql_parser_parse_string(
        parser,
        "SELECT room_id, direction, other_side FROM room_exits",
        NULL,
        NULL);
    res  = gda_connection_statement_execute_select(dbh, sth, NULL, NULL);
    iter = gda_data_model_create_iter(res);
    gda_data_model_iter_move_next(iter);

    while (gda_data_model_iter_is_valid(iter)) {
        const GValue *val;
        wmudExit     *room_exit;

        room_exit = g_new0(wmudExit, 1);

        val                       = gda_data_model_iter_get_value_at(iter, 0);
        room_exit->source_room_id = g_value_get_int(val);

        val                     = gda_data_model_iter_get_value_at(iter, 1);
        room_exit->direction_id = g_value_get_int(val);

        val                            = gda_data_model_iter_get_value_at(iter, 2);
        room_exit->destination_room_id = g_value_get_int(val);

        g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
              "Loaded exit %d =%d=> %d",
              room_exit->source_room_id,
              room_exit->direction_id,
              room_exit->destination_room_id);

        *exits = g_slist_prepend(*exits, room_exit);

        gda_data_model_iter_move_next(iter);
    }

    g_object_unref(iter);
    g_object_unref(sth);

    return TRUE;
}
Exemplo n.º 3
0
static GdaStatement *
create_statement (const gchar *sql)
{
	GdaSqlParser *parser;
	GdaStatement *stmt;
	parser = gda_sql_parser_new ();
	stmt = gda_sql_parser_parse_string (parser,  sql, NULL, NULL);
	g_object_unref (parser);
	return stmt;
}
Exemplo n.º 4
0
gboolean
wmud_db_load_planet_planes(GSList **planet_planes, GError **err)
{
    GdaStatement     *sth = NULL;
    GdaDataModel     *res = NULL;
    GdaDataModelIter *iter;

    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
          "Loading planet<->plane associations");
    if (dbh == NULL) {
        if (err) {
            g_set_error(err,
                        WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
                        "Database backend not initialized");
        }

        return FALSE;
    }

    sth = gda_sql_parser_parse_string(
        parser,
        "SELECT planet_id, plane_id FROM planet_planes",
        NULL,
        NULL);
    res  = gda_connection_statement_execute_select(dbh, sth, NULL, NULL);
    iter = gda_data_model_create_iter(res);
    gda_data_model_iter_move_next(iter);

    while (gda_data_model_iter_is_valid(iter)) {
        const GValue         *val;
        wmudPlanetPlaneAssoc *planet_plane;

        planet_plane = g_new0(wmudPlanetPlaneAssoc, 1);

        val                     = gda_data_model_iter_get_value_at(iter, 0);
        planet_plane->planet_id = g_value_get_int(val);

        val                    = gda_data_model_iter_get_value_at(iter, 1);
        planet_plane->plane_id = g_value_get_int(val);

        g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
              "Loaded planet-plane association %d <> %d",
              planet_plane->planet_id,
              planet_plane->plane_id);

        *planet_planes = g_slist_prepend(*planet_planes, planet_plane);

        gda_data_model_iter_move_next(iter);
    }

    g_object_unref(iter);
    g_object_unref(sth);

    return TRUE;
}
Exemplo n.º 5
0
gboolean
wmud_db_load_areas(GSList **areas, GError **err)
{
    GdaStatement     *sth = NULL;
    GdaDataModel     *res = NULL;
    GdaDataModelIter *iter;

    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading areas");
    if (dbh == NULL) {
        if (err) {
            g_set_error(err,
                        WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
                        "Database backend not initialized");
        }

        return FALSE;
    }

    sth = gda_sql_parser_parse_string(
        parser,
        "SELECT id, name FROM areas",
        NULL,
        NULL);
    res  = gda_connection_statement_execute_select(dbh, sth, NULL, NULL);
    iter = gda_data_model_create_iter(res);
    gda_data_model_iter_move_next(iter);

    while (gda_data_model_iter_is_valid(iter)) {
        const GValue *val;
        wmudArea     *area;

        area = g_new0(wmudArea, 1);

        val      = gda_data_model_iter_get_value_at(iter, 0);
        area->id = g_value_get_int(val);

        val        = gda_data_model_iter_get_value_at(iter, 1);
        area->name = g_strdup(g_value_get_string(val));

        g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded area _%s_", area->name);

        *areas = g_slist_prepend(*areas, area);

        gda_data_model_iter_move_next(iter);
    }

    g_object_unref(iter);
    g_object_unref(sth);

    return TRUE;
}
Exemplo n.º 6
0
/*
 * get_parameters()
 */
static gboolean
test1 (GError **error)
{
	GdaSqlParser *parser;
	guint i;

	parser = gda_sql_parser_new ();
	
	for (i = 0; i < sizeof (test1_data) / sizeof (ATest); i++) {
		ATest *test = &(test1_data[i]);
		GdaStatement *stmt;
		GError *lerror = NULL;

		stmt = gda_sql_parser_parse_string (parser, test->sql, NULL, &lerror);
		if (!stmt) {
			if (test->result) {
				if (lerror) 
					g_propagate_error (error, lerror);
				return FALSE;
			}
		}
		else { 
			GdaSet *set;
			if (!gda_statement_get_parameters (stmt, &set, &lerror)) {
				if (test->result) {
					if (lerror) 
						g_propagate_error (error, lerror);
					return FALSE;
				}
			}
			else if (set) {
				if (!tests_common_check_set (data, test->id, set, &lerror)) {
					if (lerror) 
						g_propagate_error (error, lerror);
					return FALSE;
				}
				g_object_unref (set);
			}
			g_object_unref (stmt);
		}

		if (lerror)
			g_error_free (lerror);
	}

	g_object_unref (parser);

	return TRUE;
}
Exemplo n.º 7
0
GSList *
create_queries (GdaConnection *cnc)
{
	GdaSqlParser *parser;
	GdaStatement *stmt;
	GSList *list = NULL;

	parser = gda_connection_create_parser (cnc);

	stmt = gda_sql_parser_parse_string (parser, "SELECT * FROM customers", NULL, NULL);
	g_object_set_data ((GObject*) stmt, "name", "customers");
	list = g_slist_prepend (list, stmt);

	
	stmt = gda_sql_parser_parse_string (parser, "SELECT s.* FROM salesrep s "
					"INNER JOIN customers c ON (s.id=c.default_served_by) "
					    "WHERE c.id=## /* name:\"customers/id\" type:gint */", NULL, NULL);
	g_object_set_data ((GObject*) stmt, "name", "salesrep_for_customer");
	list = g_slist_prepend (list, stmt);

	g_object_unref (parser);

	return list;
}
Exemplo n.º 8
0
static gboolean
clear_blobs (GdaConnection *cnc, GError **error)
{
	GdaStatement *stmt;
	gboolean retval;
#define SQL_DELETE "DELETE FROM blobs"
	show_header ("Clear blobs");
	stmt = gda_sql_parser_parse_string (parser, SQL_DELETE, NULL, error);
	if (!stmt)
		return FALSE;
	
	retval = exec_statement (cnc, stmt, NULL, error);
	g_object_unref (stmt);

	return retval;
}
Exemplo n.º 9
0
static gboolean
update_blob (GdaConnection *cnc, gint id, const gchar *data, glong binary_length, GError **error)
{
	GdaStatement *stmt;
	GdaSet *plist;
	GdaHolder *param;
	GValue *value;
	gchar *str;
	gboolean retval;
	const gchar* SQL_UPDATE = "UPDATE blobs set name = ##/*name:'name' type:gchararray*/, data = ##/*name:'theblob' type:'GdaBlob'*/ WHERE id= ##/*name:'id' type:gint*/";

	show_header ("Update a blob");
	stmt = gda_sql_parser_parse_string (parser, SQL_UPDATE, NULL, error);
	if (!stmt)
		return FALSE;
	if (!gda_statement_get_parameters (stmt, &plist, NULL))
		return FALSE;

	/* blob id */
	param = gda_set_get_holder (plist, "id");
	str = g_strdup_printf ("%d", id);
	if (! gda_holder_set_value_str (param, NULL, str, error))
		return FALSE;
	g_free (str);

	/* blob name */
	param = gda_set_get_holder (plist, "name");
	str = g_strdup_printf ("BLOB_%d", id);
	if (! gda_holder_set_value_str (param, NULL, str, error))
		return FALSE;
	g_free (str);

	/* blob data */
	param = gda_set_get_holder (plist, "theblob");
	value = gda_value_new_blob ((guchar*) data, binary_length);
	if (! gda_holder_set_value (param, value, error))
		return FALSE;
	gda_value_free (value);

	gda_connection_clear_events_list (cnc);
	retval = exec_statement (cnc, stmt, plist, error);
	g_object_unref (stmt);
	g_object_unref (plist);

	return retval;
}
Exemplo n.º 10
0
static void
gda_capi_provider_init (GdaCapiProvider *capi_prv, G_GNUC_UNUSED GdaCapiProviderClass *klass)
{
	g_mutex_lock (&init_mutex);

	if (!internal_stmt) {
		InternalStatementItem i;
		GdaSqlParser *parser;

		parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) capi_prv);
		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
		for (i = INTERNAL_STMT1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
			internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
			if (!internal_stmt[i])
				g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
		}
	}
Exemplo n.º 11
0
static GdaDataModel *
execute_select (GdaConnection *cnc, GdaPostgresReuseable *rdata, const gchar *sql)
{
	GdaSqlParser *parser;
	GdaStatement *stmt;
	GdaDataModel *model;
	parser = _gda_postgres_reuseable_create_parser ((GdaProviderReuseable*) rdata);
	
	stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
	g_object_unref (parser);
	g_assert (stmt);

	model = gda_connection_statement_execute_select (cnc, stmt, NULL, NULL);
	g_object_unref (stmt);

	return model;
}
Exemplo n.º 12
0
/* 
 * display the contents of the 'products' table 
 */
void
display_customers (GdaConnection *cnc)
{
	GdaDataModel *data_model;
	GdaSqlParser *parser;
	GdaStatement *stmt;
	gchar *sql = "SELECT id, name FROM customers ORDER BY id";
	GError *error = NULL;

	parser = g_object_get_data (G_OBJECT (cnc), "parser");
	stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
	data_model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
	g_object_unref (stmt);
        if (!data_model) 
                g_error ("Could not get the contents of the 'products' table: %s\n",
                         error && error->message ? error->message : "No detail");
	gda_data_model_dump (data_model, stdout);
	g_object_unref (data_model);
}
Exemplo n.º 13
0
static gboolean
display_blobs (GdaConnection *cnc, GError **error)
{
	GdaStatement *stmt;
	gboolean retval;
	GdaDataModel *model;

	stmt = gda_sql_parser_parse_string (parser, "SELECT * FROM blobs", NULL, error);
	if (!stmt)
		return FALSE;

	model = gda_connection_statement_execute_select (cnc, stmt, NULL, error);

	retval = model ? TRUE : FALSE;
	if (model) {
		gda_data_model_dump (model, stdout);
		g_object_unref (model);
	}
	return retval;
}
Exemplo n.º 14
0
GdaDataModel *
run_sql_select_cursor (GdaConnection *cnc, const gchar *sql)
{
	GdaStatement *stmt;
        GError *error = NULL;
        GdaDataModel *res;
        GdaSqlParser *parser;

        parser = gda_connection_create_parser (cnc);
        stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
        g_object_unref (parser);

        res = gda_connection_statement_execute_select_full (cnc, stmt, NULL, GDA_STATEMENT_MODEL_CURSOR_FORWARD, 
							    NULL, &error);
        g_object_unref (stmt);
        if (!res)
                g_print ("Could not execute query in cursor mode: %s\n",
                         error && error->message ? error->message : "no detail");
        return res;
}
Exemplo n.º 15
0
/*
 * Returns: the number of failures
 */
static gint
do_test (const xmlChar *id, const xmlChar *sql, gboolean valid_expected) 
{
	static GdaSqlParser *parser = NULL;
	GdaStatement *stmt;
	gboolean is_valid;
	GError *error = NULL;

	if (!parser) {
		parser = gda_connection_create_parser (cnc);
		if (!parser)
			parser = gda_sql_parser_new ();
	}

#ifdef GDA_DEBUG
	g_print ("===== TEST %s SQL: @%s@\n", id, sql);
#endif

	stmt = gda_sql_parser_parse_string (parser, (const gchar*) sql, NULL, NULL);
	if (!stmt) {
		g_print ("ERROR for test '%s': could not parse statement\n", id);
		return FALSE;
	}
	is_valid = gda_statement_check_validity (stmt, cnc, &error);
	if (is_valid && !valid_expected) {
		g_print ("ERROR for test '%s': statement is valid but test expected it invalid\n", id);
		g_object_unref (stmt);
		return FALSE;
	}
	if (!is_valid && valid_expected) {
		g_print ("ERROR for test '%s': statement is invalid but test expected it valid: %s\n", id,
			 error && error->message ? error->message : "No detail");
		g_object_unref (stmt);
		return FALSE;
	}
	/*g_print ("EXP %d, got %d\n", valid_expected, is_valid);*/
	/*g_print ("PARSED: %s\n", gda_statement_serialize (stmt));*/

	g_object_unref (stmt);
	return TRUE;
}
Exemplo n.º 16
0
Arquivo: ddl.c Projeto: UIKit0/libgda
/* 
 * display the contents of the 'products' table 
 */
void
display_products_contents (GdaConnection *cnc)
{
	GdaDataModel *data_model;
	GdaStatement *stmt;
	gchar *sql = "SELECT * FROM products";
	GError *error = NULL;
	GdaSqlParser *parser;

	parser = gda_connection_create_parser (cnc);
        if (!parser) /* @cnc doe snot provide its own parser => use default one */
                parser = gda_sql_parser_new ();

	stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
	data_model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
	g_object_unref (stmt);
        if (!data_model) 
                g_error ("Could not get the contents of the 'products' table: %s\n",
                         error && error->message ? error->message : "No detail");
	gda_data_model_dump (data_model, stdout);
	g_object_unref (data_model);
}
Exemplo n.º 17
0
GdaDataModel *
run_sql_select (GdaConnection *cnc, const gchar *sql)
{
        GdaStatement *stmt;
        GError *error = NULL;
        GdaDataModel *res;
        GdaSqlParser *parser;

        parser = gda_connection_create_parser (cnc);
	if (!parser)
		parser = gda_sql_parser_new ();

        stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
        g_object_unref (parser);

        res = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
        g_object_unref (stmt);
        if (!res)
                g_print ("Could not execute query: %s\n",
                         error && error->message ? error->message : "no detail");
        return res;
}
Exemplo n.º 18
0
gboolean
run_sql_non_select (GdaConnection *cnc, const gchar *sql)
{
        GdaStatement *stmt;
        GError *error = NULL;
        gint nrows;
        GdaSqlParser *parser;

        parser = gda_connection_create_parser (cnc);
	if (!parser)
		parser = gda_sql_parser_new ();
        stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
        g_object_unref (parser);

        nrows = gda_connection_statement_execute_non_select (cnc, stmt, NULL, NULL, &error);
        g_object_unref (stmt);
        if (nrows == -1) {
                g_print ("NON SELECT error: %s\n", error && error->message ? error->message : "no detail");
		if (error)
			g_error_free (error);
		return FALSE;
	}
	return TRUE;
}
Exemplo n.º 19
0
GtkWidget *
do_form_model_change (GtkWidget *do_widget)
{  
	if (!window) {
                GdaStatement *stmt;
		GtkWidget *vbox;
		GtkWidget *label;
		GdaDataModel *models [3];
		
		window = gtk_dialog_new_with_buttons ("Changing data in a GdauiForm",
						      GTK_WINDOW (do_widget),
						      0,
						      "Close", GTK_RESPONSE_NONE,
						      NULL);
		
		g_signal_connect (window, "response",
				  G_CALLBACK (gtk_widget_destroy), NULL);
		g_signal_connect (window, "destroy",
				  G_CALLBACK (gtk_widget_destroyed), &window);
		
		vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
		gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))),
				    vbox, TRUE, TRUE, 0);
		gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
		
		label = gtk_label_new ("The data in the same GdauiForm widget can be change don the fly.");
		gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

		/* creating data models */
		stmt = gda_sql_parser_parse_string (demo_parser, "SELECT * FROM products ORDER BY ref, category LIMIT 15", NULL, NULL);
		models[0] = gda_connection_statement_execute_select (demo_cnc, stmt, NULL, NULL);
		gda_data_select_compute_modification_statements (GDA_DATA_SELECT (models[0]), NULL);
		g_object_unref (stmt);

		stmt = gda_sql_parser_parse_string (demo_parser, "SELECT * FROM products WHERE price > 20.2 ORDER BY ref, category LIMIT 10", NULL, NULL);
		models[1] = gda_connection_statement_execute_select (demo_cnc, stmt, NULL, NULL);
		gda_data_select_compute_modification_statements (GDA_DATA_SELECT (models[1]), NULL);
		g_object_unref (stmt);

		stmt = gda_sql_parser_parse_string (demo_parser, "SELECT name, price, ref, category FROM products WHERE price > 20.2 ORDER BY name LIMIT 30", NULL, NULL);
		models[2] = gda_connection_statement_execute_select (demo_cnc, stmt, NULL, NULL);
		gda_data_select_compute_modification_statements (GDA_DATA_SELECT (models[2]), NULL);
		g_object_unref (stmt);

		
		/* allow choosing which data model to display */
		label = gtk_label_new ("");
		gtk_widget_set_halign (label, GTK_ALIGN_START);
                gtk_label_set_markup (GTK_LABEL (label), "<b>Choose which data model to display:</b>");
		gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

		GtkWidget *layout, *rb;
		GSList *group = NULL;
		gint i;
		layout = gtk_grid_new ();
		gtk_box_pack_start (GTK_BOX (vbox), layout, FALSE, FALSE, 0);
		
		for (i = 0; i < 3; i++) {
			gchar *str;
			str = g_strdup_printf ("%d columns x %d rows", gda_data_model_get_n_columns (models[i]),
					       gda_data_model_get_n_rows (models[i]));
			rb = gtk_radio_button_new_with_label (group, str);
			g_free (str);
			gtk_grid_attach (GTK_GRID (layout), rb, i, 0, 1, 1);
			g_signal_connect (rb, "toggled", G_CALLBACK (model_toggled_cb), models[i]);
			g_object_set_data_full (G_OBJECT (rb), "model", models[i], g_object_unref);
			group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (rb));
		}

		/* Create the form widget */
		label = gtk_label_new ("");
		gtk_widget_set_halign (label, GTK_ALIGN_START);
                gtk_label_set_markup (GTK_LABEL (label), "<b>GdauiForm:</b>");
		gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

		form = gdaui_form_new (models[0]);
		g_object_set (G_OBJECT (form), "info-flags",
                              GDAUI_DATA_PROXY_INFO_CURRENT_ROW |
                              GDAUI_DATA_PROXY_INFO_ROW_MOVE_BUTTONS |
                              GDAUI_DATA_PROXY_INFO_ROW_MODIFY_BUTTONS, NULL);
		gtk_box_pack_start (GTK_BOX (vbox), form, TRUE, TRUE, 0);

		GdaDataProxy *proxy;
		proxy = gdaui_data_proxy_get_proxy (GDAUI_DATA_PROXY (form));
		g_object_set (proxy, "cache-changes", TRUE, NULL);
	}

	gboolean visible;
	g_object_get (G_OBJECT (window), "visible", &visible, NULL);
	if (!visible)
		gtk_widget_show_all (window);
	else {
		gtk_widget_destroy (window);
		window = NULL;
	}

	return window;
}
Exemplo n.º 20
0
GtkWidget *
do_grid_data_layout (GtkWidget *do_widget)
{  
	if (!window) {
                GdaStatement *stmt;
		GtkWidget *vbox;
		GtkWidget *label;
		GdaDataModel *model;
		GtkWidget *grid;
		GdauiRawGrid *raw_grid;
		
		window = gtk_dialog_new_with_buttons ("Grid with custom data layout",
						      GTK_WINDOW (do_widget),
						      0,
						      "Close", GTK_RESPONSE_NONE,
						      NULL);
		
		g_signal_connect (window, "response",
				  G_CALLBACK (gtk_widget_destroy), NULL);
		g_signal_connect (window, "destroy",
				  G_CALLBACK (gtk_widget_destroyed), &window);
		
		vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
		gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))),
				    vbox, TRUE, TRUE, 0);
		gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
		
		label = gtk_label_new ("The following GdauiGrid widget displays information about customers,\n"
				       "using a picture of the customer.\n");
		gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
		
		/* Create the demo widget: select all the customers and computes the total
		 * amount of orders they have spent */
		stmt = gda_sql_parser_parse_string (demo_parser, 
						    "select c.id, c.name, c.country, c.city, c.photo, c.comments, sum (od.quantity * (1 - od.discount/100) * p.price) as total_orders from customers c left join orders o on (c.id=o.customer) left join order_contents od on (od.order_id=o.id) left join products p on (p.ref = od.product_ref) group by c.id order by total_orders desc",
						    NULL, NULL);

                model = gda_connection_statement_execute_select (demo_cnc, stmt, NULL, NULL);
                g_object_unref (stmt);
 		grid = gdaui_grid_new (model);
		g_object_unref (model);

		/* request custom layout:
		   <gdaui_grid name="customers">
		     <gdaui_entry name="name"/>
		     <gdaui_entry name="total_orders" label="Total ordered" plugin="number:NB_DECIMALS=2;CURRENCY=€"/>
		     <gdaui_entry name="photo" plugin="picture"/>
		   </gdaui_grid>
		 */
		g_object_get (G_OBJECT (grid), "raw-grid", &raw_grid, NULL);
		gchar *filename;
		filename = demo_find_file ("custom_layout.xml", NULL);
		gdaui_raw_grid_set_layout_from_file (GDAUI_RAW_GRID (raw_grid), filename, "customers");
		g_free (filename);

		gtk_box_pack_start (GTK_BOX (vbox), grid, TRUE, TRUE, 0);

		gtk_widget_set_size_request (window, 500, 500);
	}

	gboolean visible;
	g_object_get (G_OBJECT (window), "visible", &visible, NULL);
	if (!visible)
		gtk_widget_show_all (window);
	else {
		gtk_widget_destroy (window);
		window = NULL;
	}

	return window;
}
Exemplo n.º 21
0
/**
 * wmud_db_load_players:
 * @err: a GError to put error messages in it
 *
 * Loads all player records from the database
 */
gboolean
wmud_db_load_players(GError **err)
{
    GdaStatement     *sth = NULL;
    GdaDataModel     *res;
    GdaDataModelIter *iter;
    GError           *local_err = NULL;

    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading players");
    if (dbh == NULL) {
        if (err) {
            g_set_error(err,
                        WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
                        "Database backend not initialized");
        }

        return FALSE;
    }

    sth = gda_sql_parser_parse_string(
        parser,
        "SELECT id, login, password, email FROM players",
        NULL,
        NULL);

    /* TODO: error checking! */
    if ((res = gda_connection_statement_execute_select(
             dbh,
             sth,
             NULL,
             &local_err)) == NULL) {
        g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
              "Unable to load players: %s",
              local_err->message);
        g_set_error(err,
                    WMUD_DB_ERROR, WMUD_DB_ERROR_SELECT_ERROR,
                    "SELECT error: %s",
                    local_err->message);

        return FALSE;
    }

    iter = gda_data_model_create_iter(res);
    gda_data_model_iter_move_next(iter);

    while (gda_data_model_iter_is_valid(iter)) {
        const GValue *val;
        WmudPlayer   *player;

        player = wmud_player_new();

        val = gda_data_model_iter_get_value_at(iter, 0);
        wmud_player_set_id(player, g_value_get_int(val));

        val = gda_data_model_iter_get_value_at(iter, 1);
        wmud_player_set_player_name(player, g_value_get_string(val));

        val = gda_data_model_iter_get_value_at(iter, 2);
        wmud_player_set_cpassword(player, g_value_get_string(val));

        val = gda_data_model_iter_get_value_at(iter, 3);
        wmud_player_set_email(player, g_value_get_string(val));

        g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
              "Loaded player _%s_",
              wmud_player_get_player_name(player));

        players = g_slist_prepend(players, player);

        gda_data_model_iter_move_next(iter);
    }

    g_object_unref(iter);
    g_object_unref(sth);
    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Finished loading players");

    return TRUE;
}
Exemplo n.º 22
0
static GSList *
gda_tree_mgr_columns_update_children (GdaTreeManager *manager, GdaTreeNode *node, G_GNUC_UNUSED const GSList *children_nodes,
				      gboolean *out_error, GError **error)
{
	GdaTreeMgrColumns *mgr = GDA_TREE_MGR_COLUMNS (manager);
	GdaMetaStore *store;
	GdaDataModel *model;
	GSList *list = NULL;
	GdaConnection *scnc;
	GdaTreeMgrColumnsPrivate *priv = gda_tree_mgr_columns_get_instance_private (mgr);

	if (!priv->cnc && !priv->mstore) {
		g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
			     "%s", _("No connection and no GdaMetaStore specified"));
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}
	else if (priv->mstore)
		store = priv->mstore;
	else
		store = gda_connection_get_meta_store (priv->cnc);

	scnc = gda_meta_store_get_internal_connection (store);

	/* create statements if necessary */
	if (!priv->stmt) {
		GdaSqlParser *parser;
		GdaStatement *stmt;

		parser = gda_connection_create_parser (scnc);
		if (! parser)
			parser = gda_sql_parser_new ();

		stmt = gda_sql_parser_parse_string (parser,
						    "SELECT column_name FROM _columns WHERE "
						    "table_schema= ##schema::string AND "
						    "table_name= ##table_name::string "
						    "ORDER BY ordinal_position", NULL, error);
		g_object_unref (parser);
		if (!stmt) {
			if (out_error)
				*out_error = TRUE;
			return NULL;
		}

		if (!gda_statement_get_parameters (stmt, &(priv->params), error)) {
			if (out_error)
				*out_error = TRUE;
			g_object_unref (stmt);
			return NULL;
		}
		priv->stmt = stmt;
	}


	gboolean schema_specified = FALSE;
	gboolean table_specified = FALSE;
	if (priv->schema) {
		schema_specified = TRUE;
		g_assert (gda_set_set_holder_value (priv->params, NULL, "schema",
						    priv->schema));
	}
	if (priv->table_name) {
		table_specified = TRUE;
		g_assert (gda_set_set_holder_value (priv->params, NULL, "table_name",
						    priv->table_name));
	}
	if (!schema_specified && node) {
		/* looking for a schema in @node's attributes */
		const GValue *cvalue;
		cvalue = gda_tree_node_fetch_attribute (node, "schema");
		if (cvalue) {
			GdaHolder *h = gda_set_get_holder (priv->params, "schema");
			if (!gda_holder_set_value (h, cvalue, error)) {
				if (out_error)
					*out_error = TRUE;
				return NULL;
			}
			schema_specified = TRUE;
		}
	}
	if (!table_specified && node) {
		/* looking for a table in @node's attributes */
		const GValue *cvalue;
		cvalue = gda_tree_node_fetch_attribute (node, "table_name");
		if (cvalue) {
			GdaHolder *h = gda_set_get_holder (priv->params, "table_name");
			if (!gda_holder_set_value (h, cvalue, error)) {
				if (out_error)
					*out_error = TRUE;
				return NULL;
			}
			table_specified = TRUE;
		}
	}

	if (!schema_specified) {
		g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
			     "%s", _("No schema specified"));
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}

	if (!table_specified) {
		g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
			     "%s", _("No table specified"));
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}

	model = gda_connection_statement_execute_select (scnc, priv->stmt, priv->params, error);

	if (!model) {
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}

	GdaDataModelIter *iter;
	iter = gda_data_model_create_iter (model);
	for (; iter && gda_data_model_iter_move_next (iter);) {
		GdaTreeNode* snode;
		const GValue *cvalue;

		cvalue = gda_data_model_iter_get_value_at (iter, 0);
		if (!cvalue) {
			if (list) {
				g_slist_free_full (list, (GDestroyNotify) g_object_unref);
			}
			if (out_error)
				*out_error = TRUE;
			g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
				     "%s", _("Unable to get column name"));
			return NULL;
		}

		snode = gda_tree_manager_create_node (manager, node, g_value_get_string (cvalue));
		gda_tree_node_set_node_attribute (snode, "column_name", cvalue, NULL);
		list = g_slist_prepend (list, snode);
	}
	if (iter)
		g_object_unref (iter);
	g_object_unref (model);

	return list;
}
Exemplo n.º 23
0
GtkWidget *
do_form_rw (GtkWidget *do_widget)
{  
	if (!window) {
		GdaStatement *stmt;
		GtkWidget *vbox;
		GtkWidget *label;
		GdaDataModel *model;
		GtkWidget *form;
		
		window = gtk_dialog_new_with_buttons ("GdauiForm (RW)",
						      GTK_WINDOW (do_widget),
						      0,
						      "Close", GTK_RESPONSE_NONE,
						      NULL);
		
		g_signal_connect (window, "response",
				  G_CALLBACK (gtk_widget_destroy), NULL);
		g_signal_connect (window, "destroy",
				  G_CALLBACK (gtk_widget_destroyed), &window);
		
		vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
		gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))),
				    vbox, TRUE, TRUE, 0);
		gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
		
		label = gtk_label_new ("The following GdauiForm widget displays data from the 'products' table.\n\n"
				       "As modification queries are provided, the data is read-write\n(except for the 'price' "
				       "field as these queries voluntarily omit that field).");
		gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
		
		/* Create the demo widget */
		stmt = gda_sql_parser_parse_string (demo_parser, 
						    "SELECT ref, category, name, price, wh_stored FROM products", 
						    NULL, NULL);
		model = gda_connection_statement_execute_select (demo_cnc, stmt, NULL, NULL);
		g_object_unref (stmt);
		GError *error = NULL;
		if (!gda_data_select_compute_modification_statements (GDA_DATA_SELECT (model), &error)) {
			g_print ("===> %s\n", error && error->message ? error->message : "No detail");
		}
		form = gdaui_form_new (model);
		g_object_unref (model);
		g_object_set (G_OBJECT (form), "info-flags",
			      GDAUI_DATA_PROXY_INFO_CURRENT_ROW |
			      GDAUI_DATA_PROXY_INFO_ROW_MOVE_BUTTONS |
			      GDAUI_DATA_PROXY_INFO_ROW_MODIFY_BUTTONS, NULL);

		gtk_box_pack_start (GTK_BOX (vbox), form, TRUE, TRUE, 0);
	}

	gboolean visible;
	g_object_get (G_OBJECT (window), "visible", &visible, NULL);
	if (!visible)
		gtk_widget_show_all (window);
	else {
		gtk_widget_destroy (window);
		window = NULL;
	}

	return window;
}
Exemplo n.º 24
0
/*
 * @prov may be NULL
 */
static guint
do_a_test (GdaServerProvider *prov, GdaSqlParser *parser)
{
	guint nfailed = 0;
	GdaStatement *stmt;
	GdaSet *params;
	gchar *sql;
	GError *error = NULL;

	/* SQL parsed as an INSERT statement */
	sql = "INSERT INTO tstest VALUES (##ts::timestamp, ##time::time)";
	stmt = gda_sql_parser_parse_string (parser, sql, NULL, &error);
	if (!stmt) {
		g_print ("Failed to parse [%s]: %s\n", sql, error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		nfailed ++;
		goto endtest;
	}

	g_assert (gda_statement_get_statement_type (stmt) == GDA_SQL_STATEMENT_INSERT);
	if (! gda_statement_get_parameters (stmt, &params, &error)) {
		g_print ("Failed to obtain parameters: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		nfailed ++;
		goto endtest;
	}

	if (! gda_set_set_holder_value (params, &error, "ts", &ts)) {
		g_print ("Failed to bind 'ts' parameter: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}

	if (! gda_set_set_holder_value (params, &error, "time", &gt)) {
		g_print ("Failed to bind 'time' parameter: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}

	GdaConnection *cnc = NULL;
	if (prov) {
		cnc = gda_connection_new_from_string (gda_server_provider_get_name (prov), "DB_NAME=dummy;HOST=dummy", NULL,
						      GDA_CONNECTION_OPTIONS_NONE, &error);
		if (!cnc) {
			g_print ("Failed to create GdaConnection object: %s\n",
				 error && error->message ? error->message : "No detail");
			g_clear_error (&error);
			g_object_unref (stmt);
			g_object_unref (params);
			nfailed ++;
			goto endtest;
		}
	}
	gchar *expected;
	expected = "('@@@@@@@@@@ 17:10:23+2', '16:09:22-3')";
	if (cnc)
		sql = gda_connection_statement_to_sql (cnc, stmt, params, 0, NULL, &error);
	else
		sql = gda_statement_to_sql_extended (stmt, NULL, params, 0, NULL, &error);
	if (!sql) {
		g_print ("Failed to render as SQL: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}
	if (!string_equal_to_template (sql, expected)) {
		g_print ("Wrong rendered SQL: [%s] instead of [%s]\n", sql, expected);
		g_object_unref (stmt);
		g_object_unref (params);
		g_free (sql);
		nfailed ++;
		goto endtest;
	}
	g_free (sql);

	expected = "('@@@@@@@@@@ 15:10:23', '19:09:22')";
	if (cnc)
		sql = gda_connection_statement_to_sql (cnc, stmt, params, GDA_STATEMENT_SQL_TIMEZONE_TO_GMT, NULL, &error);
	else
		sql = gda_statement_to_sql_extended (stmt, NULL, params, GDA_STATEMENT_SQL_TIMEZONE_TO_GMT, NULL, &error);
	if (!sql) {
		g_print ("Failed to render as SQL: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}
	if (!string_equal_to_template (sql, expected)) {
		g_print ("Wrong rendered SQL for GMT timezone: [%s] instead of [%s]\n", sql, expected);
		g_object_unref (stmt);
		g_object_unref (params);
		g_free (sql);
		nfailed ++;
		goto endtest;
	}
	g_free (sql);

	/* SQL not parsed as a valid statement */
	sql = "AAAA (##ts::timestamp, ##time::time)";
	stmt = gda_sql_parser_parse_string (parser, sql, NULL, &error);
	if (!stmt) {
		g_print ("Failed to parse [%s]: %s\n", sql, error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		nfailed ++;
		goto endtest;
	}

	g_assert (gda_statement_get_statement_type (stmt) == GDA_SQL_STATEMENT_UNKNOWN);
	if (! gda_statement_get_parameters (stmt, &params, &error)) {
		g_print ("Failed to obtain parameters: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		nfailed ++;
		goto endtest;
	}

	if (! gda_set_set_holder_value (params, &error, "ts", &ts)) {
		g_print ("Failed to bind 'ts' parameter: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}

	if (! gda_set_set_holder_value (params, &error, "time", &gt)) {
		g_print ("Failed to bind 'time' parameter: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}

	expected = "('@@@@@@@@@@ 17:10:23+2', '16:09:22-3')";
	if (cnc)
		sql = gda_connection_statement_to_sql (cnc, stmt, params, 0, NULL, &error);
	else
		sql = gda_statement_to_sql_extended (stmt, NULL, params, 0, NULL, &error);
	if (!sql) {
		g_print ("Failed to render as SQL: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}
	if (!string_equal_to_template (sql, expected)) {
		g_print ("Wrong rendered SQL: [%s] instead of [%s]\n", sql, expected);
		g_object_unref (stmt);
		g_object_unref (params);
		g_free (sql);
		nfailed ++;
		goto endtest;
	}
	g_free (sql);

	expected = "('@@@@@@@@@@ 15:10:23', '19:09:22')";
	if (cnc)
		sql = gda_connection_statement_to_sql (cnc, stmt, params, GDA_STATEMENT_SQL_TIMEZONE_TO_GMT, NULL, &error);
	else
		sql = gda_statement_to_sql_extended (stmt, NULL, params, GDA_STATEMENT_SQL_TIMEZONE_TO_GMT, NULL, &error);
	if (!sql) {
		g_print ("Failed to render as SQL: %s\n", error && error->message ? error->message : "No detail");
		g_clear_error (&error);
		g_object_unref (stmt);
		g_object_unref (params);
		nfailed ++;
		goto endtest;
	}
	if (!string_equal_to_template (sql, expected)) {
		g_print ("Wrong rendered SQL for GMT timezone: [%s] instead of [%s]\n", sql, expected);
		g_object_unref (stmt);
		g_object_unref (params);
		g_free (sql);
		nfailed ++;
		goto endtest;
	}
	g_free (sql);

 endtest:
	return nfailed;
}
Exemplo n.º 25
0
static GSList *
gda_tree_mgr_schemas_update_children (GdaTreeManager *manager, GdaTreeNode *node,
				      G_GNUC_UNUSED const GSList *children_nodes, gboolean *out_error,
				      GError **error)
{
	GdaTreeMgrSchemas *mgr = GDA_TREE_MGR_SCHEMAS (manager);
	GdaMetaStore *store;
	GdaDataModel *model;
	GSList *list = NULL;
	GdaConnection *scnc;

	if (!mgr->priv->cnc && !mgr->priv->mstore) {
		g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
			     "%s", _("No connection and no GdaMetaStore specified"));
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}
	else if (mgr->priv->mstore)
		store = mgr->priv->mstore;
	else
		store = gda_connection_get_meta_store (mgr->priv->cnc);

	scnc = gda_meta_store_get_internal_connection (store);

	if (!mgr->priv->stmt) {
		GdaSqlParser *parser;
		GdaStatement *stmt;

		parser = gda_connection_create_parser (scnc);
		if (! parser)
			parser = gda_sql_parser_new ();

		stmt = gda_sql_parser_parse_string (parser,
						    "SELECT schema_name FROM _schemata "
						    "WHERE schema_internal = FALSE OR schema_name LIKE 'info%' "
						    "ORDER BY schema_name DESC", NULL, error);
		g_object_unref (parser);
		if (!stmt) {
			if (out_error)
				*out_error = TRUE;
			return NULL;
		}
		mgr->priv->stmt = stmt;
	}

	model = gda_connection_statement_execute_select (scnc, mgr->priv->stmt, NULL, error);
	if (!model) {
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}

	GdaDataModelIter *iter;
	iter = gda_data_model_create_iter (model);
	for (; iter && gda_data_model_iter_move_next (iter);) {
		GdaTreeNode* snode;
		const GValue *cvalue;

		cvalue = gda_data_model_iter_get_value_at (iter, 0);
		if (!cvalue) {
			if (list) {
				g_slist_foreach (list, (GFunc) g_object_unref, NULL);
				g_slist_free (list);
			}
			if (out_error)
				*out_error = TRUE;
			g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
				     "%s", _("Unable to get schema name"));
			return NULL;
		}

		snode = gda_tree_manager_create_node (manager, node, g_value_get_string (cvalue));
		gda_tree_node_set_node_attribute (snode, "schema", cvalue, NULL);
		list = g_slist_prepend (list, snode);
	}
	if (iter)
		g_object_unref (iter);
	g_object_unref (model);

	return list;
}
Exemplo n.º 26
0
static gboolean
do_test (ATest *test)
{
	GdaSqlParser *parser;
	GdaStatement *stmt;
	GError *error = NULL;
	gchar *tmp;

	g_print ("** test %s\n", test->id);
	parser = gda_sql_parser_new ();

	GdaSet *params;
	GValue *nv;
	GdaHolder *holder;
	stmt = gda_sql_parser_parse_string (parser, test->sql, NULL, &error);
	g_object_unref (parser);
	if (!stmt) {
		g_print ("Parsing error: %s\n", error && error->message ? error->message : "No detail");
		return FALSE;
	}

	if (! gda_statement_get_parameters (stmt, &params, &error)) {
		g_print ("Error: %s\n", error && error->message ? error->message : "No detail");
		return FALSE;
	}
	g_assert (gda_set_set_holder_value (params, NULL, "name", "zzz"));
	nv = gda_value_new_null ();
	holder = gda_set_get_holder (params, "id");
	g_assert (gda_holder_set_value (holder, nv, NULL));
	gda_value_free (nv);

	GdaSqlStatement *sqlst;
	g_object_get (stmt, "structure", &sqlst, NULL);

	sqlst = gda_rewrite_sql_statement_for_null_parameters (sqlst, params, NULL, &error);
	if (!sqlst) {
		g_print ("Rewrite error: %s\n", error && error->message ? error->message : "No detail");
		return FALSE;
	}
	g_object_set (stmt, "structure", sqlst, NULL);

	/* SQL rendering */
	tmp = gda_statement_to_sql_extended (stmt, NULL, NULL, GDA_STATEMENT_SQL_PARAMS_SHORT,
					     NULL, &error);
	if (!tmp) {
		g_print ("Rendering error: %s\n", error && error->message ? error->message : "No detail");
		return FALSE;
	}
	/*g_print ("SQL after mod: [%s]\n", tmp);*/
	g_free (tmp);

	tmp = gda_sql_statement_serialize (sqlst);
	if (!tmp) {
		g_print ("Error: gda_sql_statement_serialize() failed\n");
		return FALSE;
	}
	else if (strcmp (test->result, tmp)) {
		g_print ("Exp: [%s]\nGot: [%s]\n", test->result, tmp);
		return FALSE;
	}

	g_free (tmp);
	gda_sql_statement_free (sqlst);

	g_object_unref (stmt);
	return TRUE;
}
Exemplo n.º 27
0
/*
 * evaluate_expression
 *
 * Evaluates the @expr expression, which must be a valid SQLite expression
 *
 * Returns: a new GValue if no error occurred
 */
static GValue *
evaluate_expression (GdaReportEngine *engine, RunContext *context, const gchar *expr, GError **error)
{
	GdaStatement *stmt;
	GdaSet *plist;
	GdaDataModel *model;
	GValue *retval;
	gchar *sql;
	static GMutex init_mutex;
	GdaConnection *vcnc = NULL;
	GdaSqlParser *parser;

	/* create a virtual connection to execute the expression, if it's the first time */
	g_mutex_lock (&init_mutex);
	if (!vcnc) {
		static GdaVirtualProvider *provider = NULL;

		if (!provider)
			provider = gda_vprovider_data_model_new ();
		vcnc = gda_virtual_connection_open (provider, GDA_CONNECTION_OPTIONS_NONE, error);
		if (! vcnc) {
			g_mutex_unlock (&init_mutex);
			return NULL;
		}
	}
	g_mutex_unlock (&init_mutex);

	/* parser */
	parser = g_object_get_data (G_OBJECT (context->cnc), "__gda_parser");
	if (!parser) {
		parser = gda_connection_create_parser (context->cnc);
		g_object_set_data_full (G_OBJECT (context->cnc), "__gda_parser", 
					parser, g_object_unref);
	}
	
	/* create the stmt 
	 * REM: SQL injection is prevented because only the first statement is kept by GdaStatement 
	 */
	sql = g_strdup_printf ("SELECT %s", expr);
	stmt = gda_sql_parser_parse_string (parser, sql, NULL, error);
	if (!stmt)
		return NULL;

	GdaStatement *lstmt;
	lstmt = rewrite_statement (engine, context, stmt, error);
	g_object_unref (stmt);
	if (!lstmt)
		return NULL;
	
	stmt = lstmt;
	if (!gda_statement_get_parameters (stmt, &plist, error)) {
		g_object_unref (stmt);
		return NULL;
	}

	if (plist) {
		if (!assign_parameters_values (engine, context, plist, error)) {
			g_object_unref (stmt);
			g_object_unref (plist);
			return NULL;
		}
	}

	model = gda_connection_statement_execute_select (vcnc, stmt, plist, error);
	if (plist)
		g_object_unref (plist);
	g_object_unref (stmt);
	if (!model) 
		return NULL;

	if (gda_data_model_get_n_rows (model) != 1) {
		g_set_error (error, 0, 0,
			     _("Expression '%s' should return exactly one value"), expr);
		return NULL;
	}
	retval = (GValue *) gda_data_model_get_value_at (model, 0, 0, error);
	if (retval)
		retval = gda_value_copy (retval);
	g_object_unref (model);
	return retval;
}
Exemplo n.º 28
0
GtkWidget *
do_form_pict (GtkWidget *do_widget)
{  
	if (!window) {
		GdaStatement *stmt;
		GtkWidget *vbox;
		GtkWidget *label;
		GdaDataModel *model;
		GtkWidget *form;
		GdaSet *data_set;
		GdaHolder *param;
		GValue *value;
		
		window = gtk_dialog_new_with_buttons ("Form with the 'picture' plugin",
						      GTK_WINDOW (do_widget),
						      0,
						      GTK_STOCK_CLOSE,
						      GTK_RESPONSE_NONE,
						      NULL);
		
		g_signal_connect (window, "response",
				  G_CALLBACK (gtk_widget_destroy), NULL);
		g_signal_connect (window, "destroy",
				  G_CALLBACK (gtk_widget_destroyed), &window);
		
		vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
		gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))),
				    vbox, TRUE, TRUE, 0);
		gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
		
		label = gtk_label_new ("The following GdauiForm widget displays data from the 'pictures' table.\n\n"
				       "The pictures are stored as BLOB inside the database and\n"
				       "are displayed using the 'picture' plugin (right click to \n"
				       "open a menu, or double click to load an image).");
		gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
		
		/* Create the demo widget */
		stmt = gda_sql_parser_parse_string (demo_parser, "SELECT id, pict FROM pictures", NULL, NULL);
		model = gda_connection_statement_execute_select (demo_cnc, stmt, NULL, NULL);
		g_object_unref (stmt);
		gda_data_select_compute_modification_statements (GDA_DATA_SELECT (model), NULL);
		form = gdaui_form_new (model);
		g_object_unref (model);
		g_object_set (G_OBJECT (form), "info-flags",
			      GDAUI_DATA_PROXY_INFO_CURRENT_ROW |
			      GDAUI_DATA_PROXY_INFO_ROW_MOVE_BUTTONS |
			      GDAUI_DATA_PROXY_INFO_ROW_MODIFY_BUTTONS, NULL);

		/* specify that we want to use the 'picture' plugin */
		data_set = GDA_SET (gdaui_data_selector_get_data_set (GDAUI_DATA_SELECTOR (form)));
		param = gda_set_get_holder (data_set, "pict");
		value = gda_value_new_from_string ("picture", G_TYPE_STRING);
		gda_holder_set_attribute_static (param, GDAUI_ATTRIBUTE_PLUGIN, value);
		gda_value_free (value);

		gtk_box_pack_start (GTK_BOX (vbox), form, TRUE, TRUE, 0);
	}

	gboolean visible;
	g_object_get (G_OBJECT (window), "visible", &visible, NULL);
	if (!visible)
		gtk_widget_show_all (window);
	else {
		gtk_widget_destroy (window);
		window = NULL;
	}

	return window;
}
Exemplo n.º 29
0
/*
 * COMMAND: <gda_report_section>
 *
 * creates a new RunContext, then executes its children, and destroys the RunContext
 *
 * uses node's contents: yes
 * requested attributes: "query_name"
 *
 * REM: either "query_name" or a <gda_report_query> sub node must be provided to create a data model.
 */
static gboolean
command_gda_report_section_run (GdaReportEngine *engine, xmlNodePtr node, GSList **created_nodes,
				RunContext *context, GError **error)
{	
	xmlChar *qname;
	RunContext *ctx = NULL;
	
	/* compute stmt to push a RunContext */
	qname = xmlGetProp (node, BAD_CAST "query_name");
	if (qname) {
		GdaStatement *stmt;
		GdaConnection *cnc = NULL;
		xmlChar *prop;

		/* find statement */
		stmt = run_context_find_stmt (engine, context, qname);
		if (!stmt) {
			g_set_error (error, 0, 0,
				     _("Unknown query '%s'"), qname);
			xmlFree (qname);
			return FALSE;
		}

		/* find which connection to use */
		prop = xmlGetProp (node, BAD_CAST "cnc_name");
		cnc = run_context_find_connection (engine, context, prop);
		if (!cnc) {
			if (prop) {
				g_set_error (error, 0, 0,
					     _("No connection named '%s' found"), prop);
				xmlFree (prop);
			}
			else
				g_set_error (error, 0, 0, "%s", 
					     _("No connection specified"));
			return FALSE;
		}

		ctx = run_context_push_with_stmt (engine, context, cnc, stmt, (gchar *) qname, error);
		xmlFree (qname);
	}
	else {
		xmlNodePtr child;
		for (child = node->children; child; child = child->next) {
			if (!strcmp ((gchar *) child->name, "gda_report_query")) {
				GdaStatement *stmt;
				xmlChar *prop;
				GdaConnection *cnc = NULL;
				GdaSqlParser *parser = NULL;

				/* find which connection to use */
				prop = xmlGetProp (child, BAD_CAST "cnc_name");
				cnc = run_context_find_connection (engine, context, prop);
				if (!cnc) {
					if (prop) {
						g_set_error (error, 0, 0,
							     _("No connection named '%s' found"), prop);
						xmlFree (prop);
					}
					else
						g_set_error (error, 0, 0, "%s", 
							     _("No connection specified"));
					return FALSE;
				}
				
				/* parser object */
				parser = g_object_get_data (G_OBJECT (cnc), "__gda_parser");
				if (!parser) {
					parser = gda_connection_create_parser (cnc);
					g_object_set_data_full (G_OBJECT (cnc), "__gda_parser", 
								parser, g_object_unref);
				}
				
				/* statement */
				stmt = gda_sql_parser_parse_string (parser, (gchar *) xmlNodeGetContent (child), 
								    NULL, error);
				if (!stmt)
					return FALSE;

				qname = xmlGetProp (child, BAD_CAST "query_name");
				if (qname) {
					ctx = run_context_push_with_stmt (engine, context, cnc,
									  stmt, (gchar *) qname, error);
					xmlFree (qname);
				}
				else
					ctx = run_context_push_with_stmt (engine, context, cnc,
									  stmt, "context", error);
				if (ctx)
					ctx->stmt = stmt;
				else {
					g_object_unref (stmt);
					return FALSE;
				}
				break;
			}
		}
	}

	if (!ctx) {
		g_set_error (error, 0, 0, "%s", 
			     _("Query is not specified (not named and not defined)"));
		return FALSE;
	}
	
	/* go for the children while iterating */
	if (!real_run_at_node (engine, node->children, ctx, error)) {
		run_context_pop (engine, ctx);
		return FALSE;
	}
	else {
		xmlNodePtr child;
		for (child = node->children; child; child = node->children) {
			xmlUnlinkNode (child);
			*created_nodes = g_slist_prepend (*created_nodes, child);
		}
	}

	*created_nodes = g_slist_reverse (*created_nodes);
	run_context_pop (engine, ctx);

	return TRUE;
}
Exemplo n.º 30
0
gboolean
wmud_db_load_menu(GSList **menu_items, GError **err)
{
    GdaStatement     *sth = NULL;
    GdaDataModel     *res = NULL;
    GdaDataModelIter *iter;

    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading menu items");
    if (dbh == NULL) {
        if (err) {
            g_set_error(err,
                        WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
                        "Database backend not initialized");
        }

        return FALSE;
    }

    sth = gda_sql_parser_parse_string(
        parser,
        "SELECT id, menuchar, need_active_char, placement, display_text, fnctn FROM menu ORDER BY placement",
        NULL,
        NULL);
    res  = gda_connection_statement_execute_select(dbh, sth, NULL, NULL);
    iter = gda_data_model_create_iter(res);
    gda_data_model_iter_move_next(iter);

    while (gda_data_model_iter_is_valid(iter)) {
        const GValue *val;
        wmudMenu     *menu_item;

        menu_item = g_new0(wmudMenu, 1);

        val           = gda_data_model_iter_get_value_at(iter, 0);
        menu_item->id = g_value_get_int(val);

        val                 = gda_data_model_iter_get_value_at(iter, 1);
        menu_item->menuchar = *(g_value_get_string(val));

        val                         = gda_data_model_iter_get_value_at(iter, 2);
        menu_item->need_active_char = g_value_get_boolean(val);

        val                  = gda_data_model_iter_get_value_at(iter, 3);
        menu_item->placement = g_value_get_int(val);

        val             = gda_data_model_iter_get_value_at(iter, 4);
        menu_item->text = g_strdup(g_value_get_string(val));

        val             = gda_data_model_iter_get_value_at(iter, 5);
        menu_item->func = g_strdup(g_value_get_string(val));

        g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
              "Loaded menu item %d: %s(%c)",
              menu_item->id, menu_item->text,
              menu_item->menuchar);

        *menu_items = g_slist_prepend(*menu_items, menu_item);

        gda_data_model_iter_move_next(iter);
    }

    g_object_unref(iter);
    g_object_unref(sth);

    return TRUE;
}