Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright 2010 Colomban Wendling <ban(at)herbesfolles(dot)org>
- *
- * 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 2 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, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
- using Geany;
- using Gtk;
- using Postgres;
- /* Geany sets these variables at plugin registration time */
- public Plugin geany_plugin;
- public Data geany_data;
- public Functions geany_functions;
- //public Database plugin_db_connection;
- /*----------------- Plugin-global variables (prefix with G_) -----------------*/
- private GLib.SList<unowned Gtk.Widget> G_destroy_widget_stack;
- /*---------------------------- Begin plugin code ----------------------------*/
- public class PluginQueryWindow : Gtk.Window {
- private TreeView results;
- private TextView text_view;
- private int col_count;
- private bool data_loaded;
- private Database pgconn;
- private Result pgresult;
- public PluginQueryWindow () {
- // setup some basic window properties
- this.title = "Query Window";
- this.position = WindowPosition.CENTER;
- this.set_default_size (800, 500);
- this.position = WindowPosition.CENTER;
- this.data_loaded = false;
- // build the toolbar buttons
- var open_button = new ToolButton.from_stock (Gtk.Stock.OPEN);
- open_button.label = "Open Query";
- open_button.clicked.connect(()=>{this.on_open_clicked();});
- var connect_button = new ToolButton.from_stock (Gtk.Stock.CONNECT);
- connect_button.label = "Connect to DB";
- connect_button.clicked.connect(
- ()=>{
- this.db_connect("localhost","webuser","webuser","fulcrum");
- }
- );
- // TODO: make execute button ONLY execute selected query text
- var execute_button = new ToolButton.from_stock (Gtk.Stock.EXECUTE);
- execute_button.label = "Execute Query";
- execute_button.clicked.connect(()=>{this.perform_query();});
- var close_button = new ToolButton.from_stock (Gtk.Stock.CLOSE);
- close_button.label = "Close Window";
- close_button.clicked.connect(()=>{this.destroy();});
- // create the toolbar and add the buttons
- var toolbar = new Toolbar ();
- toolbar.set_style(ToolbarStyle.BOTH);
- toolbar.add (open_button);
- toolbar.add (connect_button);
- toolbar.add (execute_button);
- toolbar.add (close_button);
- // query entry area
- this.text_view = new TextView ();
- // TODO: set initial query to selected text from current document
- this.text_view.buffer.text = "select * from projects;";
- var query_scroll = new ScrolledWindow (null, null);
- query_scroll.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
- query_scroll.add (this.text_view);
- // query results area
- this.results = new TreeView ();
- var result_scroll = new ScrolledWindow (null, null);
- result_scroll.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
- result_scroll.add (this.results);
- //setup_treeview (results);
- // now pack it all
- var vbox = new VBox(false, 0);
- vbox.pack_start (toolbar, false, true, 0);
- vbox.pack_start (query_scroll, true, true, 0);
- vbox.pack_start (result_scroll, true, true, 0);
- this.add (vbox);
- // TODO: read db config from the project
- this.db_connect("localhost","webuser","webuser","fulcrum");
- this.destroy.connect (()=>{this.plugin_db_disconnect();});
- this.show_all ();
- }
- private void on_open_clicked () {
- var file_chooser = new FileChooserDialog ("Open File", this,
- FileChooserAction.OPEN,
- Stock.CANCEL, ResponseType.CANCEL,
- Stock.OPEN, ResponseType.ACCEPT);
- if (file_chooser.run () == ResponseType.ACCEPT) {
- open_file (file_chooser.get_filename ());
- }
- file_chooser.destroy ();
- }
- private void open_file (string filename) {
- try {
- string text;
- FileUtils.get_contents (filename, out text);
- this.text_view.buffer.text = text;
- } catch (Error e) {
- stderr.printf ("Error: %s\n", e.message);
- }
- }
- private void perform_query()
- {
- //this.pgresult = this.pgconn.exec ("select * from pg_database");
- string query = this.text_view.buffer.text;
- Msgwin.status_add("trying to execute query: "+query);
- if(query != "")
- {
- // TODO: capture query errors
- this.pgresult = this.pgconn.exec(query);
- this.update_results();
- }
- else
- {
- // TODO: replace errors with real popups
- var window = new Window ();
- window.title = "Error";
- window.set_default_size (300, 50);
- window.position = WindowPosition.CENTER;
- var button = new Button.with_label ("Please enter a query");
- window.add (button);
- window.show_all ();
- }
- }
- private void update_results()
- {
- if(this.data_loaded)
- {
- for (int i = this.col_count; i >=0; i--)
- {
- this.results.remove_column(this.results.get_column(i));
- }
- }
- int nFields = this.pgresult.get_n_fields ();
- Type[] cols = new Type[nFields];
- for (int i = 0; i < nFields; i++)
- {
- cols[i] = typeof (string);
- }
- var listmodel = new ListStore.newv(cols);
- this.results.set_model (listmodel);
- for (int i = 0; i < nFields; i++)
- {
- this.results.insert_column_with_attributes (
- -1,
- this.pgresult.get_field_name(i),
- new CellRendererText (),
- "text",
- 0
- );
- var column = this.results.get_column(i);
- column.set_resizable(true);
- Msgwin.status_add("Setting "+column.get_title()+" to resizable");
- }
- TreeIter iter;
- for (int i = 0; i < this.pgresult.get_n_tuples(); i++) {
- listmodel.append (out iter);
- for (int j = 0; j < nFields; j++) {
- // BUG: Fails to insert actual result value into TreeView
- //listmodel.set_value(iter,j,"Mike");
- // ^^ This works! 'Mike' shows up in every col
- //Msgwin.status_add("Result:"+(string)this.pgresult.get_value (i, j));
- // ^^ This works! the value from the db is printed to the status window
- string myval = (string)this.pgresult.get_value (i, j);
- listmodel.set_value(iter,j,myval);
- // ^^ this does NOT work. The column number is put into every column, rather than the value
- //listmodel.set_value(iter,j,("mike"+(string)this.pgresult.get_value (i, j)));
- // ^^ this does NOT work. The string 'mike' plus the column number (mike1,mike2) is put into every column
- }
- stdout.printf ("\n");
- }
- this.data_loaded = true;
- this.col_count = nFields;
- }
- private bool db_connect(string host,string user,string password, string dbname)
- {
- string conninfo = "";
- if(host != "")
- conninfo += " host="+host;
- if(user != "")
- conninfo += " user="+user;
- if(password != "")
- conninfo += " password="+password;
- if(dbname != "")
- conninfo += " dbname="+dbname;
- this.pgconn = Postgres.connect_db (conninfo);
- if (pgconn.get_status () != ConnectionStatus.OK) {
- stderr.printf ("Connection to database failed: %s", this.pgconn.get_error_message ());
- return false;
- }
- return true;
- }
- private int plugin_db_disconnect()
- {
- // TODO: make disconnect actually work
- return 1;
- }
- }
- /*------------------------- Plugin registration & co -------------------------*/
- public int plugin_version_check (int abi_version)
- {
- return Plugin.version_check (abi_version, 185);
- }
- public void plugin_set_info (Plugin.Info info)
- {
- info.set (
- "Database Plugin",
- "A plugin for Geany to view databases",
- "0.1",
- "Mike Thorn"
- );
- }
- public void plugin_init (Geany.Data data)
- {
- Gtk.MenuItem item;
- unowned Gtk.MenuShell? menu = Ui.lookup_widget (geany_data.main_widgets.window,
- "menu_document1_menu") as Gtk.MenuShell;
- if (menu == null) {
- menu = geany_data.main_widgets.tools_menu;
- } else {
- item = new Gtk.SeparatorMenuItem ();
- menu.append (item);
- G_destroy_widget_stack.append (item);
- item.show ();
- }
- item = new Gtk.MenuItem.with_label ("Query Window");
- item.activate.connect (() => {
- //Document.editor.insert_text_block("mike test",0,0,0,false);
- new PluginQueryWindow();
- });
- menu.append (item);
- G_destroy_widget_stack.append (item);
- item.show ();
- // TODO: Add keybindings
- }
- public void plugin_cleanup ()
- {
- foreach (var w in G_destroy_widget_stack) {
- w.destroy ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement