/**
 * gda_server_provider_perform_operation_default:
 * @provider: a #GdaServerProvider object
 * @cnc: (allow-none): a #GdaConnection object which will be used to perform an action, or %NULL
 * @op: a #GdaServerOperation object
 * @error: a place to store an error, or %NULL
 *
 * Performs the operation described by @op, using the SQL from the rendering of the operation
 *
 * Returns: %TRUE if no error occurred
 */
gboolean
gda_server_provider_perform_operation_default (GdaServerProvider *provider, GdaConnection *cnc,
					       GdaServerOperation *op, GError **error)
{
	gchar *sql;
	GdaBatch *batch;
	const GSList *list;
	gboolean retval = TRUE;

	sql = gda_server_provider_render_operation (provider, cnc, op, error);
	if (!sql)
		return FALSE;

	GdaSqlParser *parser;
	parser = gda_server_provider_internal_get_parser (provider); /* no ref held! */
	batch = gda_sql_parser_parse_string_as_batch (parser, sql, NULL, error);
	g_free (sql);
	if (!batch)
		return FALSE;

	for (list = gda_batch_get_statements (batch); list; list = list->next) {
		if (gda_connection_statement_execute_non_select (cnc, GDA_STATEMENT (list->data), NULL, NULL, error) == -1) {
			retval = FALSE;
			break;
		}
	}
	g_object_unref (batch);

	return retval;
}
Beispiel #2
0
static void
sql_indent_clicked_cb (G_GNUC_UNUSED GtkButton *button, QueryConsolePage *tconsole)
{
	gchar *sql;
	GdaBatch *batch;

	if (!tconsole->priv->parser)
		tconsole->priv->parser = t_connection_create_parser (tconsole->priv->tcnc);

	sql = query_editor_get_all_text (tconsole->priv->editor);
	batch = gda_sql_parser_parse_string_as_batch (tconsole->priv->parser, sql, NULL, NULL);
	g_free (sql);
	if (batch) {
		GString *string;
		const GSList *stmt_list, *list;
		stmt_list = gda_batch_get_statements (batch);
		string = g_string_new ("");
		for (list = stmt_list; list; list = list->next) {
			sql = t_connection_render_pretty_sql (tconsole->priv->tcnc,
							      GDA_STATEMENT (list->data));
			if (!sql)
				sql = gda_statement_to_sql (GDA_STATEMENT (list->data), NULL, NULL);
			if (list != stmt_list)
				g_string_append (string, "\n\n");
			g_string_append_printf (string, "%s;\n", sql);
			g_free (sql);

		}
		g_object_unref (batch);

		query_editor_set_text (tconsole->priv->editor, string->str);
		g_string_free (string, TRUE);
	}
}
Beispiel #3
0
/**
 * gda_tools_favorites_get_actions
 * @bfav: a #ToolsFavorites
 * @bcnc: a #BrowserConnection
 * @set: a #GdaSet
 *
 * Get a list of #ToolsFavoriteAction which can be executed with the data in @set.
 *
 * Returns: a new list of #ToolsFavoriteAction, free list with gda_tools_favorites_free_actions()
 */
GSList *
gda_tools_favorites_get_actions (ToolsFavorites *bfav, BrowserConnection *bcnc, GdaSet *set)
{
	GSList *fav_list, *list, *retlist = NULL;
	g_return_val_if_fail (GDA_TOOLS_IS_FAVORITES (bfav), NULL);
	g_return_val_if_fail (BROWSER_IS_CONNECTION (bcnc), NULL);
	g_return_val_if_fail (!set || GDA_IS_SET (set), NULL);

	fav_list = gda_tools_favorites_list (bfav, 0, GDA_TOOLS_FAVORITES_ACTIONS, -1, NULL);
	if (! fav_list)
		return NULL;

	for (list = fav_list; list; list = list->next) {
		ToolsFavoritesAttributes *fa = (ToolsFavoritesAttributes*) list->data;
		ToolsFavoritesAttributes qfa;
		if (! g_str_has_prefix (fa->contents, "QUERY")) {
			g_warning ("Malformed action contents '%s', please report error to "
				   "http://bugzilla.gnome.org/ for the \"libgda\" product",
				   fa->contents);
			continue;
		}
		if (gda_tools_favorites_get (bfav, atoi (fa->contents + 5), &qfa, NULL)) {
			GdaSet *params;
			GSList *plist;
			GdaBatch *batch;
			GdaStatement *stmt = NULL;
			GdaSqlParser *parser;
			const gchar *remain;
			const GSList *stmt_list;
			gint nb_bound = 0;

			parser = browser_connection_create_parser (bcnc);
			batch = gda_sql_parser_parse_string_as_batch (parser, qfa.contents, &remain, NULL);
			g_object_unref (parser);
			if (!batch) {
				gda_tools_favorites_reset_attributes (&qfa);
				continue;
			}
			stmt_list = gda_batch_get_statements (batch);
			for (plist = (GSList*) stmt_list; plist; plist = plist->next) {
				if (! gda_statement_is_useless (GDA_STATEMENT (plist->data))) {
					if (stmt)
						break;
					else
						stmt = g_object_ref (GDA_STATEMENT (plist->data));
				}
			}
			g_object_unref (batch);
			if (!stmt || plist) {
				gda_tools_favorites_reset_attributes (&qfa);
				continue;
			}
			
			if (! gda_statement_get_parameters (stmt, &params, NULL) || !params) {
				g_object_unref (stmt);
				gda_tools_favorites_reset_attributes (&qfa);
				continue;
			}
			browser_connection_define_ui_plugins_for_stmt (bcnc, stmt, params);
			
			for (plist = params->holders; plist; plist = plist->next) {
				/* try to find holder in @set */
				GdaHolder *req_holder, *in_holder;
				req_holder = GDA_HOLDER (plist->data);
				in_holder = gda_set_get_holder (set, gda_holder_get_id (req_holder));
				if (in_holder && gda_holder_set_bind (req_holder, in_holder, NULL)) {
					/* bound this holder to the oune found */
					nb_bound++;
				}
			}

			if (nb_bound > 0) {
				/* at least 1 holder was found=> keep the action */
				ToolsFavoriteAction *act;
				act = g_new0 (ToolsFavoriteAction, 1);
				retlist = g_slist_insert_sorted (retlist, act,
								 (GCompareFunc) actions_sort_func);
				act->params = g_object_ref (params);
				act->id = fa->id;
				act->name = g_strdup (fa->name);
				act->stmt = g_object_ref (stmt);
				act->nb_bound = nb_bound;

				/*g_print ("Action identified: ID=%d Bound=%d name=[%s] SQL=[%s]\n",
				  act->id, act->nb_bound,
				  act->name, qfa.contents);*/
			}

			g_object_unref (stmt);
			g_object_unref (params);
			gda_tools_favorites_reset_attributes (&qfa);
		}
	}
	gda_tools_favorites_free_list (fav_list);

	return retlist;
}
Beispiel #4
0
static void
actually_execute (QueryConsolePage *tconsole, const gchar *sql, GdaSet *params,
		  gboolean add_editor_history)
{
	GdaBatch *batch;
	GError *error = NULL;
	const gchar *remain;

	/* if a query is being executed, then show an error */
	if (tconsole->priv->currently_executing) {
		ui_show_error (GTK_WINDOW (gtk_widget_get_toplevel ((GtkWidget*) tconsole)),
			       _("A query is already being executed, "
				 "to execute another query, open a new connection."));
		return;
	}
	tconsole->priv->currently_executing = TRUE;

	if (!tconsole->priv->parser)
		tconsole->priv->parser = t_connection_create_parser (tconsole->priv->tcnc);

	batch = gda_sql_parser_parse_string_as_batch (tconsole->priv->parser, sql, &remain, &error);
	if (!batch) {
		ui_show_error (GTK_WINDOW (gtk_widget_get_toplevel ((GtkWidget*) tconsole)),
			       _("Error while parsing code: %s"),
			       error && error->message ? error->message : _("No detail"));
		g_clear_error (&error);
		return;
	}

	if (add_editor_history) {
		/* mark the current SQL to be kept by the editor as an internal history */
		query_editor_keep_current_state (tconsole->priv->editor);
	}

	/* actual Execution */
	const GSList *stmt_list, *list;
	GTimeVal start_time;
	g_get_current_time (&start_time);

	QueryEditorHistoryBatch *hist_batch;
	hist_batch = query_editor_history_batch_new (start_time, params);
	query_editor_start_history_batch (tconsole->priv->history, hist_batch);
	query_editor_history_batch_unref (hist_batch);

	gboolean within_transaction;
	stmt_list = gda_batch_get_statements (batch);
	for (list = stmt_list; list; list = list->next) {
		GdaStatement *stmt;
		GObject *result;
		GError *lerror = NULL;
		within_transaction = t_connection_get_transaction_status (tconsole->priv->tcnc) ? TRUE : FALSE;
		stmt = GDA_STATEMENT (list->data);
		result = t_connection_execute_statement (tconsole->priv->tcnc, stmt, params,
							 GDA_STATEMENT_MODEL_RANDOM_ACCESS,
							 NULL, &lerror);
		if (result) {
			QueryEditorHistoryItem *history;
			GdaSqlStatement *sqlst;
			g_object_get (G_OBJECT (stmt), "structure", &sqlst, NULL);
			if (!sqlst->sql) {
				gchar *sql;
				sql = gda_statement_to_sql (stmt, NULL, NULL);
				history = query_editor_history_item_new (sql, result, lerror);
				g_free (sql);
			}
			else
				history = query_editor_history_item_new (sqlst->sql, result, lerror);
			g_object_unref (result);
			gda_sql_statement_free (sqlst);
			
			history->within_transaction = within_transaction;

			/* display a message if a transaction has been started */
			if (! history->within_transaction &&
			    t_connection_get_transaction_status (tconsole->priv->tcnc) &&
			    gda_statement_get_statement_type (stmt) != GDA_SQL_STATEMENT_BEGIN) {
				browser_window_show_notice_printf (BROWSER_WINDOW (gtk_widget_get_toplevel ((GtkWidget*) tconsole)),
								   GTK_MESSAGE_INFO,
								   "QueryExecTransactionStarted",
								   "%s", _("A transaction has automatically been started\n"
									   "during this statement's execution, this usually\n"
									   "happens when blobs are selected (and the transaction\n"
									   "will have to remain opened while the blobs are still\n"
									   "accessible, clear the corresponding history item before\n"
									   "closing the transaction)."));
			}

			query_editor_add_history_item (tconsole->priv->history, history);
			query_editor_history_item_unref (history);

			browser_window_push_status (BROWSER_WINDOW (gtk_widget_get_toplevel ((GtkWidget*) tconsole)),
						    "QueryConsolePage", _("Statement executed"), TRUE);			
		}
		else {
			ui_show_error (GTK_WINDOW (gtk_widget_get_toplevel ((GtkWidget*) tconsole)),
				       _("Error executing query:\n%s"),
				       lerror && lerror->message ? lerror->message : _("No detail"));
			g_clear_error (&lerror);
			break;
		}
	}
	g_object_unref (batch);
	tconsole->priv->currently_executing = FALSE;
}
Beispiel #5
0
static gboolean
compute_params (QueryConsolePage *tconsole)
{
	gchar *sql;
	GdaBatch *batch;

	if (tconsole->priv->params) {
		t_connection_keep_variables (tconsole->priv->tcnc, tconsole->priv->params);
		g_object_unref (tconsole->priv->params);
	}
	tconsole->priv->params = NULL;

	if (tconsole->priv->params_form) {
		gtk_widget_destroy (tconsole->priv->params_form);
		tconsole->priv->params_form = NULL;		
	}

	if (!tconsole->priv->parser)
		tconsole->priv->parser = t_connection_create_parser (tconsole->priv->tcnc);

	sql = query_editor_get_all_text (tconsole->priv->editor);
	batch = gda_sql_parser_parse_string_as_batch (tconsole->priv->parser, sql, NULL, NULL);
	g_free (sql);
	if (batch) {
		GError *error = NULL;
		gboolean show_variables = FALSE;

		if (gda_batch_get_parameters (batch, &(tconsole->priv->params), &error)) {
			if (tconsole->priv->params) {
				t_connection_define_ui_plugins_for_batch (tconsole->priv->tcnc,
									  batch,
									  tconsole->priv->params);
				show_variables = TRUE;
				tconsole->priv->params_form = gdaui_basic_form_new (tconsole->priv->params);
				g_object_set ((GObject*) tconsole->priv->params_form,
					      "show-actions", TRUE, NULL);
				g_signal_connect (tconsole->priv->params_form, "activated",
						  G_CALLBACK (params_form_activated_cb), tconsole);
			}
			else {
				tconsole->priv->params_form = gtk_label_new ("");
				gtk_label_set_markup (GTK_LABEL (tconsole->priv->params_form), VARIABLES_HELP);
			}
		}
		else {
			show_variables = TRUE;
			tconsole->priv->params_form = gtk_label_new ("");
			gtk_label_set_markup (GTK_LABEL (tconsole->priv->params_form), VARIABLES_HELP);
		}
		gtk_container_add (GTK_CONTAINER (tconsole->priv->params_form_box), tconsole->priv->params_form);
		gtk_widget_show (tconsole->priv->params_form);
		g_object_unref (batch);

		t_connection_load_variables (tconsole->priv->tcnc, tconsole->priv->params);
		if (tconsole->priv->params && show_variables &&
		    gda_set_is_valid (tconsole->priv->params, NULL))
			show_variables = FALSE;
		if (show_variables && !gtk_toggle_button_get_active (tconsole->priv->params_toggle))
			gtk_toggle_button_set_active (tconsole->priv->params_toggle, TRUE);
	}
	else {
		tconsole->priv->params_form = gtk_label_new ("");
		gtk_label_set_markup (GTK_LABEL (tconsole->priv->params_form), VARIABLES_HELP);
		gtk_container_add (GTK_CONTAINER (tconsole->priv->params_form_box), tconsole->priv->params_form);
		gtk_widget_show (tconsole->priv->params_form);
	}
	
	/* remove timeout */
	tconsole->priv->params_compute_id = 0;
	return FALSE;
}
Beispiel #6
0
/*
 * Returns: the number of failures
 */
static gint
do_test (GdaSqlParser *parser, const xmlChar *id, const xmlChar *sql, const xmlChar *expected,
         const xmlChar *error_line, const xmlChar *error_col)
{
    gboolean failures = 0;
#ifdef GDA_DEBUG
    GdaSqlParserMode mode;
    g_object_get (G_OBJECT (parser), "mode", &mode, NULL);
    g_print ("===== TEST %s (%s mode) SQL: @%s@\n", id,
             mode == GDA_SQL_PARSER_MODE_PARSE ? "PARSE" : "DELIMIT", sql);
#endif
    if (expected) {
        GdaBatch *batch;
        GError *error = NULL;

        batch = gda_sql_parser_parse_string_as_batch (parser, (gchar *) sql, NULL, &error);
        if (batch) {
            gchar *ser;
            ser = gda_batch_serialize (batch);
            if (strcmp (ser, (gchar *) expected)) {
                g_print ("ERROR for test '%s':\n   *exp: %s\n   *got: %s\n",
                         id, expected, ser);
                failures ++;
            }
            g_free (ser);
            g_object_unref (batch);
        }
        else {
            if (error && (error->domain == GDA_SQL_PARSER_ERROR) &&
                    (error->code == GDA_SQL_PARSER_EMPTY_SQL_ERROR) &&
                    (*expected == 0))
                /* OK */;
            else {
                g_print ("ERROR for test '%s':\n   *got error: %s\n", id,
                         error && error->message ? error->message: "No detail");
                if (error)
                    g_error_free (error);
                failures ++;
            }
        }
    }
    else if (error_line && error_col) {
        GdaBatch *batch;
        GError *error = NULL;

        batch = gda_sql_parser_parse_string_as_batch (parser, (gchar *) sql, NULL, &error);
        if (batch) {
            gchar *ser;
            ser = gda_batch_serialize (batch);
            g_print ("ERROR for test '%s':\n   *got: %s\n",
                     id, ser);
            g_free (ser);
            g_object_unref (batch);
            failures ++;
        }
        else {
            /* FIXME: test error line and col */
            gint line, col;
            g_object_get (parser, "line-error", &line, "column-error", &col, NULL);
            if ((atoi ((gchar*) error_line) != line) || (atoi ((gchar *) error_col) != col)) {
                g_print ("ERROR for test '%s':\n   *exp line=%s, col=%s\n   *got line=%d, col=%d\n",
                         id, error_line, error_col, line, col);
                failures ++;
            }
            if (error)
                g_error_free (error);
        }
    }
    else
        g_print ("Missing or incomplete expected result for test '%s'!\n", id);

    return failures;
}