Normalize database table listing

This commit is contained in:
Timothy Warren 2012-04-02 10:23:27 -04:00
parent 59e167c064
commit fdc37f8819
5 changed files with 126 additions and 106 deletions

View File

@ -7,7 +7,7 @@
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
@ -21,7 +21,7 @@ class pgSQL extends DB_PDO {
/**
* Connect to a PosgreSQL database
*
*
* @param string $dsn
* @param string $username=null
* @param string $password=null
@ -35,7 +35,7 @@ class pgSQL extends DB_PDO {
$class = __CLASS__.'_sql';
$this->sql = new $class;
}
// --------------------------------------------------------------------------
/**
@ -46,21 +46,21 @@ class pgSQL extends DB_PDO {
public function truncate($table)
{
$sql = 'TRUNCATE "' . $table . '"';
$this->query($sql);
$this->query($sql);
}
// --------------------------------------------------------------------------
/**
* Get list of databases for the current connection
*
*
* @return array
*/
public function get_dbs()
{
$sql = <<<SQL
SELECT "datname" FROM "pg_database"
WHERE "datname" NOT IN ('template0','template1')
SELECT "datname" FROM "pg_database"
WHERE "datname" NOT IN ('template0','template1')
ORDER BY 1
SQL;
@ -70,28 +70,28 @@ SQL;
return $dbs;
}
// --------------------------------------------------------------------------
/**
* Get the list of tables for the current db
*
*
* @return array
*/
public function get_tables()
{
$sql = <<<SQL
SELECT "tablename" FROM "pg_tables"
WHERE "tablename" NOT LIKE 'pg\_%'
AND "tablename" NOT LIKE 'sql\%'
SELECT "tablename" FROM "pg_tables"
WHERE "tablename" NOT LIKE 'pg_%'
AND "tablename" NOT LIKE 'sql_%'
SQL;
$res = $this->query($sql);
$tables = $res->fetchAll(PDO::FETCH_ASSOC);
$good_tables = array();
foreach($tables as $t)
{
$good_tables[] = $t['tablename'];
@ -99,12 +99,12 @@ SQL;
return $good_tables;
}
// --------------------------------------------------------------------------
/**
* Get the list of system tables
*
*
* @return array
*/
public function get_system_tables()
@ -114,21 +114,21 @@ SQL;
WHERE "tablename" LIKE 'pg\_%'
OR "tablename" LIKE 'sql\%'
SQL;
$res = $this->query($sql);
$tables = $res->fetchAll(PDO::FETCH_ASSOC);
return $tables;
}
// --------------------------------------------------------------------------
/**
* Get a list of schemas, either for the current connection, or
* for the current datbase, if specified.
*
*
* @param string $database=""
* @return array
*/
@ -137,7 +137,7 @@ SQL;
if($database === "")
{
$sql = <<<SQL
SELECT DISTINCT "schemaname" FROM "pg_tables"
SELECT DISTINCT "schemaname" FROM "pg_tables"
WHERE "schemaname" NOT LIKE 'pg\_%'
SQL;
@ -153,18 +153,18 @@ SQL;
return $schemas;
}
// --------------------------------------------------------------------------
/**
* Get a list of views for the current db
*
*
* @return array
*/
public function get_views()
{
$sql = <<<SQL
SELECT "viewname" FROM "pg_views"
SELECT "viewname" FROM "pg_views"
WHERE "viewname" NOT LIKE 'pg\_%';
SQL;
@ -174,21 +174,21 @@ SQL;
return $views;
}
// --------------------------------------------------------------------------
/**
* Return the number of rows returned for a SELECT query
*
*
* @return int
*/
public function num_rows()
{
return (isset($this->statement)) ? $this->statement->rowCount : FALSE;
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
@ -197,11 +197,11 @@ SQL;
public function backup_structure()
{
// @todo Implement Backup function
return '';
return '';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*

View File

@ -7,13 +7,13 @@
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* SQLite specific class
* SQLite specific class
*
* @extends DB_PDO
*/
@ -23,8 +23,8 @@ class SQLite extends DB_PDO {
/**
* Open SQLite Database
*
* @param string $dsn
*
* @param string $dsn
*/
public function __construct($dsn, $user=NULL, $pass=NULL)
{
@ -34,7 +34,7 @@ class SQLite extends DB_PDO {
$class = __CLASS__."_sql";
$this->sql = new $class;
}
// --------------------------------------------------------------------------
/**
@ -49,42 +49,42 @@ class SQLite extends DB_PDO {
$sql = 'DELETE FROM "'.$table.'"';
$this->statement = $this->query($sql);
return $this->statement;
}
// --------------------------------------------------------------------------
/**
* List tables for the current database
*
*
* @return mixed
*/
public function get_tables()
{
{
$tables = array();
$sql = <<<SQL
SELECT "name", "sql"
FROM "sqlite_master"
SELECT "name", "sql"
FROM "sqlite_master"
WHERE "type"='table'
SQL;
$res = $this->query($sql);
$result = $res->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $r)
{
$tables[$r['name']] = $r['sql'];
$tables[] = $r['name'];
}
return $tables;
}
// --------------------------------------------------------------------------
/**
* List system tables for the current database
*
*
* @return array
*/
public function get_system_tables()
@ -93,53 +93,53 @@ SQL;
// that is of any importance.
return array('sqlite_master');
}
// --------------------------------------------------------------------------
/**
* Load a database for the current connection
*
*
* @param string $db
* @param string $name
* @param string $name
*/
public function load_database($db, $name)
{
$sql = 'ATTACH DATABASE "'.$db.'" AS "'.$name.'"';
$this->query($sql);
}
// --------------------------------------------------------------------------
/**
* Unload a database from the current connection
*
*
* @param string $name
*/
public function unload_database($name)
{
$sql = 'DETACH DATABASE ":name"';
$this->prepare_query($sql, array(
':name' => $name,
));
$this->statement->execute();
}
// --------------------------------------------------------------------------
/**
* Return the number of rows returned for a SELECT query
*
*
* @return int
*/
public function num_rows()
{
return (isset($this->statement)) ? $this->statement->rowCount : FALSE;
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
@ -160,12 +160,12 @@ SQL;
}
$sql_structure = implode("\n\n", $sql_array);
return $sql_structure;
return $sql_structure;
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
@ -184,48 +184,48 @@ SQL;
$res = $this->query($sql);
$result = $res->fetchAll(PDO::FETCH_ASSOC);
unset($res);
$output_sql = '';
// Get the data for each object
foreach($result as $r)
{
$sql = 'SELECT * FROM "'.$r['name'].'"';
$res = $this->query($sql);
$obj_res = $res->fetchAll(PDO::FETCH_ASSOC);
unset($res);
// Nab the column names by getting the keys of the first row
$columns = array_keys($obj_res[0]);
$insert_rows = array();
// Create the insert statements
foreach($obj_res as $row)
{
$row = array_values($row);
// Quote values as needed by type
for($i=0, $icount=count($row); $i<$icount; $i++)
{
$row[$i] = (is_numeric($row[$i])) ? $row[$i] : $this->quote($row[$i]);
}
$row_string = 'INSERT INTO "'.$r['name'].'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');';
unset($row);
$insert_rows[] = $row_string;
}
unset($obj_res);
$output_sql .= "\n\n".implode("\n", $insert_rows);
}
return $output_sql;
}
}

View File

@ -80,7 +80,7 @@ class Query_Builder {
switch($dbtype)
{
default:
$dsn = "host={$params->host};dbname={$params->database}";
$dsn = "host={$params->host};dbname={$params->conn_db}";
if ( ! empty($params->port))
{

View File

@ -116,6 +116,10 @@ class Connection_Sidebar extends GtkVBox {
// Label column
$cell_renderer = new GtkCellRendererText();
$this->treeview->insert_column_with_data_func(1, 'Connection name', $cell_renderer, array($this, 'set_label'));
// Status column
$cell_renderer = new GtkCellRendererPixbuf();
$this->treeview->insert_column_with_data_func(2, 'Status', $cell_renderer, array($this, 'set_status_icon'));
}
// --------------------------------------------------------------------------
@ -166,6 +170,21 @@ class Connection_Sidebar extends GtkVBox {
// --------------------------------------------------------------------------
/**
* Sets the status icon of the current db connection
*
* @param GtkTreeViewColumn $col
* @param GtkCellRenderer $cell
* @param GtkTreeModel $model
* @param GtkTreeIter $iter
*/
public function set_status_icon($col, $cell, $model, $iter)
{
}
// --------------------------------------------------------------------------
/**
* Returns window for creating a new database connection
*

View File

@ -7,29 +7,29 @@
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* SQLiteTest class.
*
*
* @extends UnitTestCase
*/
class SQLiteTest extends UnitTestCase {
function __construct()
{
parent::__construct();
}
function setUp()
{
$path = TEST_DIR.DS.'test_dbs'.DS.'test_sqlite.db';
$this->db = new SQLite($path);
}
function tearDown()
{
unset($this->db);
@ -39,20 +39,20 @@ class SQLiteTest extends UnitTestCase {
{
$this->assertIsA($this->db, 'SQLite');
}
function TestGetTables()
{
$tables = $this->db->get_tables();
$this->assertTrue(is_array($tables));
}
function TestGetSystemTables()
{
$tables = $this->db->get_system_tables();
$this->assertTrue(is_array($tables));
}
function TestCreateTransaction()
{
$res = $this->db->beginTransaction();
@ -62,25 +62,25 @@ class SQLiteTest extends UnitTestCase {
function TestCreateTable()
{
//Attempt to create the table
$sql = $this->db->sql->create_table('create_test',
$sql = $this->db->sql->create_table('create_test',
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
),
),
array(
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
//Attempt to create the table
$sql = $this->db->sql->create_table('create_join',
$sql = $this->db->sql->create_table('create_join',
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
),
),
array(
'id' => 'PRIMARY KEY'
)
@ -89,61 +89,62 @@ class SQLiteTest extends UnitTestCase {
//Check
$dbs = $this->db->get_tables();
$this->assertEqual($dbs['create_test'], 'CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT )');
$this->assertTrue(in_array('create_test', $dbs));
}
function TestTruncate()
{
$this->db->truncate('create_test');
$this->assertIsA($this->db->affected_rows(), 'int');
}
function TestPreparedStatements()
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement->execute();
}
function TestPrepareExecute()
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare_execute($sql, array(
2, "works", 'also?'
));
}
function TestCommitTransaction()
{
$res = $this->db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
$this->db->query($sql);
$res = $this->db->commit();
$this->assertTrue($res);
}
function TestRollbackTransaction()
{
$res = $this->db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
$this->db->query($sql);
$res = $this->db->rollback();
$this->assertTrue($res);
}
// This is really time intensive ! Run only when needed
/*function TestDeleteTable()
{
@ -157,7 +158,7 @@ SQL;
//Check
$dbs = $this->db->get_tables();
$this->assertFalse(in_array('create_test', $dbs));
$this->assertFalse(in_array('create_test', $dbs));
}*/
}