From ace22357f673da6300327e76b8645c9c3bf83d7e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 10 Apr 2012 15:42:12 -0400 Subject: [PATCH] Fix #8 --- sys/db/query_builder.php | 5 +++ sys/windows/widgets/connection_sidebar.php | 29 ++++++++++++++ sys/windows/widgets/db_tabs.php | 46 +++++++++++++++++++--- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index 7f1d8d3..c74c5db 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -53,6 +53,9 @@ class Query_Builder { // ) private $query_map; + // Convenience property for connection management + public $conn_name; + /** * Constructor * @@ -111,6 +114,8 @@ class Query_Builder { { $this->db = new $dbtype($dsn); } + + $this->conn_name = $params->name; // Make things just slightly shorter $this->sql =& $this->db->sql; diff --git a/sys/windows/widgets/connection_sidebar.php b/sys/windows/widgets/connection_sidebar.php index 5e7630e..f37874d 100644 --- a/sys/windows/widgets/connection_sidebar.php +++ b/sys/windows/widgets/connection_sidebar.php @@ -19,6 +19,7 @@ class Connection_Sidebar extends GtkVBox { protected $settings, $menu, $treeview; private static $instance; + private $conn_name; /** * Return the current instance of the class @@ -120,6 +121,9 @@ class Connection_Sidebar extends GtkVBox { // Status column $cell_renderer = new GtkCellRendererPixbuf(); $this->treeview->insert_column_with_data_func(2, 'Status', $cell_renderer, array($this, 'set_status_icon')); + + // Connect event to change database tabs + $this->treeview->connect('cursor-changed', array($this, 'switch_tab')); } // -------------------------------------------------------------------------- @@ -361,6 +365,7 @@ class Connection_Sidebar extends GtkVBox { try { $conn =& DB_Reg::get_db($data->name); + $this->conn_name = $data->name; } catch(PDOException $e) { @@ -385,5 +390,29 @@ class Connection_Sidebar extends GtkVBox { $this->refresh(); } + + // -------------------------------------------------------------------------- + + /** + * Change tabs based on db connection selected + * + * @param type $view + */ + public function switch_tab($view) + { + $data = $view->get(0); + $conns = DB_Reg::get_connections(); + + // Don't reset if you are over the same database + if ($data->name === $this->conn_name) + { + return; + } + + if (in_array($data->name, $conns)) + { + $this->db_connect(); + } + } } // End of connection_sidebar.php diff --git a/sys/windows/widgets/db_tabs.php b/sys/windows/widgets/db_tabs.php index 00ab433..8260c17 100644 --- a/sys/windows/widgets/db_tabs.php +++ b/sys/windows/widgets/db_tabs.php @@ -22,6 +22,7 @@ class DB_tabs extends GTKNotebook { * @var DB_Tabs */ private static $instance; + private $data; /** * Return the db tabs object if it exists, or create and return @@ -46,6 +47,7 @@ class DB_tabs extends GTKNotebook { public function __construct() { parent::__construct(); + $this->data = new StdClass(); } // -------------------------------------------------------------------------- @@ -80,9 +82,11 @@ class DB_tabs extends GTKNotebook { // Empty the tabs self::reset(); + self::$instance->hide_all(); + // 'Databases' Tab { - self::_add_tab($conn, 'Databases', 'Db Name', 'get_dbs', array(), array( + self::_add_tab($conn, 'Databases', 'Db Name', 'get_dbs', array( 'row-activated' => array(self::$instance, '_switch_db'), )); } @@ -164,10 +168,14 @@ class DB_tabs extends GTKNotebook { */ public static function reset() { + self::$instance->hide_all(); + for($i=self::$instance->get_n_pages(); $i >= 0; $i--) { self::$instance->remove_page($i); } + + self::$instance->show_all(); } // -------------------------------------------------------------------------- @@ -179,14 +187,28 @@ class DB_tabs extends GTKNotebook { * @param string $tab_name * @param string $col_name * @param string $method + * @param array $events * @return void */ - private static function _add_tab(&$conn, $tab_name, $col_name, $method, $params=array(), $events=array()) + private static function _add_tab(&$conn, $tab_name, $col_name, $method, $events=array()) { $tab = new Data_Grid(); $tab_model = $tab->get_model(); - $tab_data = call_user_func_array(array($conn, $method), $params); + $conn_name = $conn->conn_name; + + if ( ! isset(self::$instance->data->{$conn_name})) + { + self::$instance->data->{$conn_name}= array(); + } + + $instance_data =& self::$instance->data->{$conn_name}; + + $tab_data = (empty($instance_data[$tab_name])) + ? call_user_func_array(array($conn, $method), array()) + : $instance_data[$tab_name]; + + $instance_data[$tab_name] = $tab_data; if ($tab_data !== FALSE) { @@ -225,7 +247,21 @@ class DB_tabs extends GTKNotebook { */ private static function _add_row_tab(&$conn, $tab_name, $method) { - $tab_data = call_user_func_array(array($conn, $method), array()); + + $conn_name = $conn->conn_name; + + if ( ! isset(self::$instance->data->{$conn_name})) + { + self::$instance->data->{$conn_name}= array(); + } + + $instance_data =& self::$instance->data->{$conn_name}; + + $tab_data = (empty($instance_data[$tab_name])) + ? call_user_func_array(array($conn, $method), array()) + : $instance_data[$tab_name]; + + $instance_data[$tab_name] = $tab_data; if ( ! empty($tab_data)) { @@ -296,8 +332,6 @@ class DB_tabs extends GTKNotebook { } // @todo figure out how to single out the current db connection - - } } // End of db_tabs.php