Show Database table data in a popup
This commit is contained in:
Timothy Warren 2012-04-11 11:16:44 -04:00
parent cb6cc58655
commit 91edf09ff7
3 changed files with 169 additions and 31 deletions

View File

@ -43,6 +43,11 @@ class Data_Grid extends GtkTreeView {
*/
public function get($pos = 0)
{
if ( ! is_numeric($pos))
{
return parent::get($pos);
}
// Get the selection object of the row
$sel = $this->get_selection();
@ -75,5 +80,30 @@ class Data_Grid extends GtkTreeView {
$this->remove_column($c);
}
}
// --------------------------------------------------------------------------
/**
* Adds a column of data to the model
*
* @param GtkTreeViewColumn $col
* @param GtkCellRenderer $cell
* @param GtkTreeModel $model
* @param GtkTreeIter $iter
* @param int $i
* @return void
*/
public function add_data_col($col, $cell, $model, $iter, $i=0)
{
$data = $model->get_value($iter, $i);
if (empty($data))
{
return;
}
$col->set_visible(TRUE);
$cell->set_property('text', $data);
}
}
// End of data_grid.php

View File

@ -96,7 +96,13 @@ class DB_tabs extends GTKNotebook {
// 'Tables' Tab
{
self::_add_tab($conn, 'Tables', 'Table Name', 'get_tables');
self::_add_tab($conn, 'Tables', 'Table Name', 'get_tables', array(
array(
'row-activated',
array(self::$instance, 'show_table_data'),
$conn
)
));
}
// 'System Tables' Tab
@ -106,7 +112,13 @@ class DB_tabs extends GTKNotebook {
// 'Views' Tab
{
self::_add_tab($conn, 'Views', 'View Name', 'get_views');
self::_add_tab($conn, 'Views', 'View Name', 'get_views', array(
array(
'row-activated',
array(self::$instance, 'show_table_data'),
$conn
)
));
}
// 'Sequences' Tab
@ -136,31 +148,6 @@ class DB_tabs extends GTKNotebook {
// --------------------------------------------------------------------------
/**
* Adds a column of data to the model
*
* @param GtkTreeViewColumn $col
* @param GtkCellRenderer $cell
* @param GtkTreeModel $model
* @param GtkTreeIter $iter
* @param int $i
* @return void
*/
public function add_data_col($col, $cell, $model, $iter, $i=0)
{
$data = $model->get_value($iter, $i);
if (empty($data))
{
return;
}
$col->set_visible(TRUE);
$cell->set_property('text', $data);
}
// --------------------------------------------------------------------------
/**
* Remove current tabs
*/
@ -217,13 +204,13 @@ class DB_tabs extends GTKNotebook {
$cell_renderer = new GtkCellRendererText();
$cell_renderer->set_property('editable', FALSE);
$tab->insert_column_with_data_func(0, $col_name, $cell_renderer, array(self::$instance, 'add_data_col'));
$tab->insert_column_with_data_func(0, $col_name, $cell_renderer, array($tab, 'add_data_col'));
if ( ! empty($events))
{
foreach($events as $name => $method)
foreach($events as $method)
{
$tab->connect($name, $method);
call_user_func_array(array($tab, 'connect'), $method);
}
}
@ -298,7 +285,7 @@ class DB_tabs extends GTKNotebook {
{
$renderer = new GtkCellRendererText();
$renderer->set_property('editable', TRUE);
$tab->insert_column_with_data_func($i, $c, $renderer, array(self::$instance, 'add_data_col'), $i);
$tab->insert_column_with_data_func($i, $c, $renderer, array($tab, 'add_data_col'), $i);
}
self::$instance->add_tab($tab_name, $tab);
@ -306,5 +293,26 @@ class DB_tabs extends GTKNotebook {
return;
}
// --------------------------------------------------------------------------
/**
* Create popup window with table data
*
* @param GTKTreeView $view
* @param array $path
* @param GtkTreeviewColumn $col
* @param Query_Builder $conn
* @return void
*/
public function show_table_data($view, $path, $col, &$conn)
{
$table = $view->get(0);
$query = $conn->get($table);
$data = $query->fetchAll(PDO::FETCH_ASSOC);
return new DB_Table_data($data);
}
}
// End of db_tabs.php

View File

@ -0,0 +1,100 @@
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Popup window to display database table data
*/
class DB_Table_Data extends GTKWindow {
protected $win;
public function __construct($data)
{
parent::__construct();
$this->set_title("Table Data");
$this->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
$this->set_destroy_with_parent(TRUE);
$this->win = new GTKScrolledWindow();
$this->win->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
$this->add($this->win);
$this->_render($data);
// Resize to a sane size
$this->set_size_request(640, 480);
}
/**
* Layout the window
*
* @param array $data
* @return void
*/
protected function _render($data)
{
if ( ! is_array($data))
{
return;
}
$model = new StdClass();
$cols = ( ! empty($data)) ? array_keys($data[0]) : array();
// Add columns to model
$col_num = (count($cols) > 0) ? count($cols) : 1;
$model_args = array_fill(0, $col_num, Gobject::TYPE_PHP_VALUE);
$eval_string = '$model = new GTKTreeStore('.implode(',', $model_args).');';
// Shame, shame, but how else?
eval($eval_string);
$view = new Data_Grid($model);
$view->set_headers_clickable(TRUE);
// Set the data in the model
for($i=0, $c = count($data); $i < $c; $i++)
{
// Add a row
$row = $model->insert($i);
$j = -1;
$vals = array($row);
foreach($data[$i] as $v)
{
$vals[] = ++$j;
$vals[] = trim($v);
}
call_user_func_array(array($model, 'set'), $vals);
}
// Add columns to view
foreach($cols as $i => $c)
{
$renderer = new GtkCellRendererText();
$renderer->set_property('editable', TRUE);
$view->insert_column_with_data_func($i, $c, $renderer, array($view, 'add_data_col'), $i);
}
// Add the grid to the window
$this->win->add_with_viewport($view);
$this->show_all();
}
}