Show views on connection, clean up metadata-fetching methods

This commit is contained in:
Timothy Warren 2012-04-04 10:47:14 -04:00
parent 8c35ae3e96
commit 9c1ac2bd17
9 changed files with 114 additions and 44 deletions

View File

@ -26,6 +26,9 @@ error_reporting(-1 & ~(E_STRICT));
// Set the stupid timezone so PHP shuts up. // Set the stupid timezone so PHP shuts up.
date_default_timezone_set('GMT'); date_default_timezone_set('GMT');
// Don't set an arbitary memory limit!
ini_set('memory_limit', -1);
// Set the current directory as the base for included files // Set the current directory as the base for included files
define('BASE_DIR', dirname(__FILE__).'/sys'); define('BASE_DIR', dirname(__FILE__).'/sys');
define('SETTINGS_DIR', dirname(__FILE__)); define('SETTINGS_DIR', dirname(__FILE__));

View File

@ -143,4 +143,23 @@ function about()
$dlg->destroy(); $dlg->destroy();
} }
/**
* Filter out db rows into one array
*
* @param array $array
* @param mixed $index
* @return array
*/
function db_filter($array, $index)
{
$new_array = array();
foreach($array as $a)
{
$new_array[] = $a[$index];
}
return $new_array;
}
// End of functions.php // End of functions.php

View File

@ -212,6 +212,13 @@ abstract class DB_PDO extends PDO {
*/ */
abstract public function get_dbs(); abstract public function get_dbs();
/**
* Return list of views for the current database
*
* @return array
*/
abstract public function get_views();
/** /**
* Empty the passed table * Empty the passed table
* *

View File

@ -151,6 +151,19 @@ SQL;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Get list of views for the current database
*
* @return array
*/
public function get_views()
{
// @todo Implement
return FALSE;
}
// --------------------------------------------------------------------------
/** /**
* Not applicable to firebird * Not applicable to firebird
* *

View File

@ -56,17 +56,8 @@ class MySQL extends DB_PDO {
*/ */
public function get_dbs() public function get_dbs()
{ {
$res = $this->query("SHOW DATABASES"); $res = $this->query("SHOW DATABASES WHERE `Database` !='information_schema'");
$vals = array_values($res->fetchAll(PDO::FETCH_ASSOC)); return db_filter(array_values($res->fetchAll(PDO::FETCH_ASSOC)), 'Database');
$return = array();
foreach($vals as $v)
{
$return[] = $v['Database'];
}
return $return;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -78,17 +69,21 @@ class MySQL extends DB_PDO {
*/ */
public function get_tables() public function get_tables()
{ {
$res = $this->query("SHOW TABLES"); $res = $this->query('SHOW TABLES');
return db_filter($res->fetchAll(PDO::FETCH_NUM), 0);
$tables = array();
$rows = $res->fetchAll(PDO::FETCH_NUM);
foreach($rows as $r)
{
$tables[] = $r[0];
} }
return $tables; // --------------------------------------------------------------------------
/**
* Get list of views for the current database
*
* @return array
*/
public function get_views()
{
$res = $this->query('SELECT `table_name` FROM `information_schema`.`views`');
return db_filter($res->fetchAll(PDO::FETCH_NUM), 'table_name');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -45,7 +45,7 @@ class ODBC extends DB_PDO {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Not applicable to firebird * Not applicable to ODBC
* *
* @return FALSE * @return FALSE
*/ */
@ -56,6 +56,18 @@ class ODBC extends DB_PDO {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Not applicable to ODBC
*
* @return FALSE
*/
public function get_views()
{
return FALSE;
}
// --------------------------------------------------------------------------
/** /**
* List system tables for the current database/connection * List system tables for the current database/connection
* *

View File

@ -66,9 +66,7 @@ SQL;
$res = $this->query($sql); $res = $this->query($sql);
$dbs = $res->fetchAll(PDO::FETCH_ASSOC); return db_filter($res->fetchAll(PDO::FETCH_ASSOC), 'datname');
return $dbs;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -88,16 +86,8 @@ SQL;
$res = $this->query($sql); $res = $this->query($sql);
$tables = $res->fetchAll(PDO::FETCH_ASSOC); return db_filter($res->fetchAll(PDO::FETCH_ASSOC), 'tablename');
$good_tables = array();
foreach($tables as $t)
{
$good_tables[] = $t['tablename'];
}
return $good_tables;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -117,10 +107,7 @@ SQL;
$res = $this->query($sql); $res = $this->query($sql);
$tables = $res->fetchAll(PDO::FETCH_ASSOC); return db_filter($res->fetchAll(PDO::FETCH_ASSOC), 'tablename');
return $tables;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -170,9 +157,8 @@ SQL;
$res = $this->query($sql); $res = $this->query($sql);
$views = $res->fetchAll(PDO::FETCH_ASSOC);
return $views; return db_filter($res->fetchAll(PDO::FETCH_ASSOC), 'viewname');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -83,7 +83,7 @@ SQL;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Not applicable to firebird * Not applicable to SQLite
* *
* @return FALSE * @return FALSE
*/ */
@ -94,6 +94,19 @@ SQL;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Get list of views for the current database
*
* @return array
*/
public function get_views()
{
// @todo Implement
return FALSE;
}
// --------------------------------------------------------------------------
/** /**
* List system tables for the current database * List system tables for the current database
* *

View File

@ -89,7 +89,7 @@ class DB_tabs extends GTKNotebook {
$db_model = $dbs->get_model(); $db_model = $dbs->get_model();
$db_data = $conn->get_dbs(); $db_data = $conn->get_dbs();
if($db_data) if($db_data !== FALSE)
{ {
foreach($db_data as $d) foreach($db_data as $d)
{ {
@ -115,7 +115,6 @@ class DB_tabs extends GTKNotebook {
foreach($table_data as $t) foreach($table_data as $t)
{ {
$table_model->append(null, array($t)); $table_model->append(null, array($t));
//$table_model->set($iter, 0, $t);
} }
$cell_renderer = new GtkCellRendererText(); $cell_renderer = new GtkCellRendererText();
@ -127,7 +126,22 @@ class DB_tabs extends GTKNotebook {
// 'Views' Tab // 'Views' Tab
{ {
$views = new Data_grid();
$view_model = $views->get_model();
$view_data = $conn->get_views();
if ($view_data !== FALSE)
{
foreach($view_data as $v)
{
$view_model->append(null, array($v));
}
$cell_renderer = new GtkCellRendererText();
$views->insert_column_with_data_func(0, 'View Name', $cell_renderer, array(self::$instance, 'add_data_col'));
self::$instance->add_tab('Views', $views);
}
} }
@ -149,8 +163,16 @@ class DB_tabs extends GTKNotebook {
*/ */
public function add_data_col($col, $cell, $model, $iter, $i=0) public function add_data_col($col, $cell, $model, $iter, $i=0)
{ {
$col->set_visible(TRUE);
$data = $model->get_value($iter, $i); $data = $model->get_value($iter, $i);
if (empty($data))
{
return;
}
print_r($data);
$col->set_visible(TRUE);
$cell->set_property('text', $data); $cell->set_property('text', $data);
} }