From fdc37f88199f17389a14b6e2ecceb211d3ed7d68 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 2 Apr 2012 10:23:27 -0400 Subject: [PATCH] Normalize database table listing --- sys/db/drivers/pgsql.php | 64 ++++++++-------- sys/db/drivers/sqlite.php | 86 +++++++++++----------- sys/db/query_builder.php | 2 +- sys/windows/widgets/connection_sidebar.php | 19 +++++ tests/databases/sqlite.php | 61 +++++++-------- 5 files changed, 126 insertions(+), 106 deletions(-) diff --git a/sys/db/drivers/pgsql.php b/sys/db/drivers/pgsql.php index 78e7b9e..c7f1ae3 100644 --- a/sys/db/drivers/pgsql.php +++ b/sys/db/drivers/pgsql.php @@ -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 = <<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 = <<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 * diff --git a/sys/db/drivers/sqlite.php b/sys/db/drivers/sqlite.php index ed58c1e..298e636 100644 --- a/sys/db/drivers/sqlite.php +++ b/sys/db/drivers/sqlite.php @@ -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 = <<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; } } diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index b1a8af2..7192623 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -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)) { diff --git a/sys/windows/widgets/connection_sidebar.php b/sys/windows/widgets/connection_sidebar.php index 8827726..c25256c 100644 --- a/sys/windows/widgets/connection_sidebar.php +++ b/sys/windows/widgets/connection_sidebar.php @@ -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 * diff --git a/tests/databases/sqlite.php b/tests/databases/sqlite.php index 6f090d6..48c45bf 100644 --- a/tests/databases/sqlite.php +++ b/tests/databases/sqlite.php @@ -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 = <<db->prepare_query($sql, array(1,"boogers", "Gross")); - + $statement->execute(); } - + function TestPrepareExecute() { $sql = <<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)); }*/ } \ No newline at end of file