Misc cleanup

This commit is contained in:
Timothy Warren 2012-03-28 11:23:08 -04:00
parent d86fa948a9
commit bde8f0f179
9 changed files with 220 additions and 205 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
*/
// --------------------------------------------------------------------------
@ -55,7 +55,7 @@ register_shutdown_function('log_fatal');
// --------------------------------------------------------------------------
// Make sure php-gtk works
if ( ! class_exists('gtk'))
if ( ! class_exists('gtk'))
{
trigger_error("PHP-gtk not found. Please load the php-gtk2 extension in your php.ini", E_USER_ERROR);
die();
@ -70,7 +70,12 @@ if( ! class_exists('pdo'))
// --------------------------------------------------------------------------
// Bulk loading wrapper workaround for PHP < 5.4
/**
* Alias for require_once for array_map
*
* @param string $path
* @return void
*/
function do_include($path)
{
require_once($path);
@ -80,8 +85,8 @@ function do_include($path)
{
array_map('do_include', glob(BASE_DIR . "/common/*.php"));
array_map('do_include', glob(BASE_DIR . "/db/*.php"));
array_map('do_include', glob(BASE_DIR . "/windows/widgets/*.php"));
array_map('do_include', glob(BASE_DIR . "/windows/*.php"));
array_map('do_include', glob(BASE_DIR . "/windows/widgets/*.php"));
array_map('do_include', glob(BASE_DIR . "/windows/*.php"));
}
// --------------------------------------------------------------------------
@ -98,7 +103,7 @@ foreach(pdo_drivers() as $d)
}
$file = "{$path}{$d}.php";
if(is_file($file))
{
require_once("{$path}{$d}.php");
@ -113,6 +118,8 @@ if(function_exists('fbird_connect'))
require_once("{$path}firebird_sql.php");
}
// --------------------------------------------------------------------------
// ! Global Functions
// --------------------------------------------------------------------------
/**

View File

@ -7,14 +7,14 @@
* @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
*/
// --------------------------------------------------------------------------
/**
* Class for manipulating datbase connections, and program settings
*
*
* Use JSON for compatibility
*/
class Settings {
@ -32,14 +32,14 @@ class Settings {
return self::$instance;
}
/**
* Load the settings file - private so it can't be loaded
* directly - the settings should be safe!
*/
private function __construct()
{
$path = SETTINGS_DIR.'/settings.json';
$path = SETTINGS_DIR.'/settings.json';
if( ! is_file($path))
{
@ -74,14 +74,14 @@ class Settings {
/**
* Magic method to simplify isset checking for config options
*
*
* @param string $key
* @return $mixed
* @return mixed
*/
public function __get($key)
{
return (isset($this->current->{$key}) && $key != "dbs")
? $this->current->{$key}
return (isset($this->current->{$key}) && $key != "dbs")
? $this->current->{$key}
: NULL;
}
@ -89,9 +89,9 @@ class Settings {
/**
* Magic method to simplify setting config options
*
*
* @param string $key
* @param mixed $val
* @param mixed
*/
public function __set($key, $val)
{
@ -101,7 +101,7 @@ class Settings {
return FALSE;
}
$this->current->{$key} = $val;
return $this->current->{$key} = $val;
}
// --------------------------------------------------------------------------
@ -155,7 +155,7 @@ class Settings {
/**
* Remove a database connection
*
*
* @param string $name
*/
public function remove_db($name)
@ -173,11 +173,11 @@ class Settings {
}
// --------------------------------------------------------------------------
/**
* Retreive all db connections
*
* @return array
*
* @return array
*/
public function get_dbs()
{
@ -194,7 +194,9 @@ class Settings {
*/
public function get_db($name)
{
return (isset($this->current->dbs->{$name})) ? $this->current->dbs->{$name} : FALSE;
return (isset($this->current->dbs->{$name}))
? $this->current->dbs->{$name}
: FALSE;
}
}

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
*/
// --------------------------------------------------------------------------
@ -16,6 +16,8 @@
* Base Database class
*
* Extends PDO to simplify cross-database issues
*
* @abstract
*/
abstract class DB_PDO extends PDO {
@ -29,9 +31,9 @@ abstract class DB_PDO extends PDO {
{
parent::__construct($dsn, $username, $password, $driver_options);
}
// -------------------------------------------------------------------------
/**
* Simplifies prepared statements for database queries
*
@ -43,23 +45,23 @@ abstract class DB_PDO extends PDO {
{
// Prepare the sql
$query = $this->prepare($sql);
if( ! (is_object($query) || is_resource($query)))
{
$this->get_last_error();
return FALSE;
}
// Set the statement in the class variable for easy later access
$this->statement =& $query;
if( ! (is_array($data) || is_object($data)))
{
trigger_error("Invalid data argument");
return FALSE;
}
// Bind the parameters
foreach($data as $k => $value)
{
@ -67,18 +69,18 @@ abstract class DB_PDO extends PDO {
{
$k++;
}
$res = $query->bindValue($k, $value);
if( ! $res)
{
trigger_error("Parameter not successfully bound");
return FALSE;
}
}
return $query;
return $query;
}
// -------------------------------------------------------------------------
@ -91,7 +93,7 @@ abstract class DB_PDO extends PDO {
* @return PDOStatement
*/
public function prepare_execute($sql, $params)
{
{
$this->statement = $this->prepare_query($sql, $params);
$this->statement->execute();
@ -128,19 +130,19 @@ abstract class DB_PDO extends PDO {
public function affected_rows($statement='')
{
if ( ! empty($statement))
{
{
$this->statement = $statement;
}
// Execute the query
//$this->statement->execute();
// Return number of rows affected
return $this->statement->rowCount();
}
// --------------------------------------------------------------------------
/**
* Return the last error for the current database connection
*
@ -149,7 +151,7 @@ abstract class DB_PDO extends PDO {
public function get_last_error()
{
$info = $this->errorInfo();
echo "Error: <pre>{$info[0]}:{$info[1]}\n{$info[2]}</pre>";
}
@ -195,45 +197,45 @@ abstract class DB_PDO extends PDO {
/**
* Abstract public functions to override in child classes
*/
/**
* Return list of tables for the current database
*
*
* @return array
*/
abstract public function get_tables();
/**
* Empty the passed table
*
*
* @param string $table
*
*
* @return void
*/
abstract public function truncate($table);
/**
* Return the number of rows for the last SELECT query
*
*
* @return int
*/
abstract public function num_rows();
/**
* Retreives an array of non-user-created tables for
* Retreives an array of non-user-created tables for
* the connection/database
*
*
* @return array
*/
abstract public function get_system_tables();
/**
* Return an SQL file with the database table structure
*
* @return string
*/
abstract public function backup_structure();
/**
* Return an SQL file with the database data as insert statements
*
@ -248,21 +250,23 @@ abstract class DB_PDO extends PDO {
* Abstract parent for database manipulation subclasses
*/
abstract class DB_SQL {
/**
* Get database-specific sql to create a new table
*
* @param string $name
* @param array $columns
* @param array $constraints
* @param array $indexes
*
* @abstract
* @param string $name
* @param array $columns
* @param array $constraints
* @param array $indexes
* @return string
*/
abstract public function create_table($name, $columns, array $constraints=array(), array $indexes=array());
/**
* Get database-specific sql to drop a table
*
*
* @abstract
* @param string $name
* @return string
*/
@ -271,16 +275,18 @@ abstract class DB_SQL {
/**
* Get database specific sql for limit clause
*
* @abstract
* @param string $sql
* @param int $limiit
* @param int $offset
* @return string
*/
abstract public function limit($sql, $limit, $offset=FALSE);
/**
* Get the sql for random ordering
*
* @abstract
* @return string
*/
abstract public function random();

View File

@ -7,25 +7,25 @@
* @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
*/
// --------------------------------------------------------------------------
/**
* Firebird Database class
*
*
* PDO-firebird isn't stable, so this is a wrapper of the fbird_ public functions.
*/
class firebird extends DB_PDO {
protected $statement, $statement_link, $trans, $count, $result, $conn;
/**
* Open the link to the database
*
*
* @param string $db
* @param string $user
* @param string $user
* @param string $pass
*/
public function __construct($dbpath, $user='sysdba', $pass='masterkey')
@ -38,27 +38,27 @@ class firebird extends DB_PDO {
throw new PDOException(fbird_errmsg());
die();
}
$class = __CLASS__."_sql";
$this->sql = new $class;
}
// --------------------------------------------------------------------------
/**
* Close the link to the database
* Close the link to the database and any existing results
*/
public function __destruct()
{
@fbird_close();
@fbird_free_result($this->statement);
}
// --------------------------------------------------------------------------
/**
* Empty a database table
*
*
* @param string $table
*/
public function truncate($table)
@ -67,9 +67,9 @@ class firebird extends DB_PDO {
$sql = 'DELETE FROM "'.$table.'"';
$this->statement = $this->query($sql);
}
// --------------------------------------------------------------------------
/**
* Wrapper public function to better match PDO
*
@ -80,7 +80,7 @@ class firebird extends DB_PDO {
public function query($sql)
{
$this->count = 0;
if (isset($this->trans))
{
$this->statement_link = @fbird_query($this->trans, $sql);
@ -95,12 +95,12 @@ class firebird extends DB_PDO {
{
throw new PDOException(fbird_errmsg());
}
return new FireBird_Result($this->statement_link);
}
// --------------------------------------------------------------------------
/**
@ -121,39 +121,39 @@ class firebird extends DB_PDO {
return new FireBird_Result($this->statement_link);
}
// --------------------------------------------------------------------------
/**
* List tables for the current database
*
*
* @return array
*/
public function get_tables()
{
{
$sql = <<<SQL
SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS"
SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS"
WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%'
AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%'
SQL;
$this->statement = $this->query($sql);
$tables = array();
while($row = $this->statement->fetch(PDO::FETCH_ASSOC))
{
$tables[] = $row['RDB$RELATION_NAME'];
}
return $tables;
}
// --------------------------------------------------------------------------
/**
* List system tables for the current database
*
*
* @return array
*/
public function get_system_tables()
@ -172,15 +172,15 @@ SQL;
{
$tables[] = $row['RDB$RELATION_NAME'];
}
return $tables;
}
// --------------------------------------------------------------------------
/**
* Return the number of rows returned for a SELECT query
*
*
* @return int
*/
public function num_rows()
@ -196,12 +196,12 @@ SQL;
return count($this->result);
}
// --------------------------------------------------------------------------
/**
* Start a database transaction
*
*
* @return bool
*/
public function beginTransaction()
@ -213,35 +213,35 @@ SQL;
return FALSE;
}
// --------------------------------------------------------------------------
/**
* Commit a database transaction
*
*
* @return bool
*/
public function commit()
{
return fbird_commit($this->trans);
}
// --------------------------------------------------------------------------
/**
* Rollback a transaction
*
*
* @return bool
*/
public function rollBack()
{
return fbird_rollback($this->trans);
}
// --------------------------------------------------------------------------
/**
* Prepare and execute a query
*
@ -252,10 +252,10 @@ SQL;
public function prepare_execute($sql, $args)
{
$query = $this->prepare($sql);
// Set the statement in the class variable for easy later access
$this->statement =& $query;
return $query->execute($args);
}
@ -263,7 +263,7 @@ SQL;
/**
* Method to emulate PDO->quote
*
*
* @param string $str
* @return string
*/
@ -291,9 +291,9 @@ SQL;
return array(0, $code, $msg);
}
// --------------------------------------------------------------------------
/**
* Bind a prepared query with arguments for executing
*
@ -307,9 +307,9 @@ SQL;
// the firebird database
return FALSE;
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
@ -318,11 +318,11 @@ SQL;
public function backup_structure()
{
// @todo Implement Backup function
return '';
return '';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
@ -341,53 +341,53 @@ SQL;
{
$tables = $this->get_tables();
}
// Filter out the tables you don't want
if( ! empty($exclude))
{
$tables = array_diff($tables, $exclude);
}
$output_sql = '';
// Get the data for each object
foreach($tables as $t)
{
$sql = 'SELECT * FROM "'.trim($t).'"';
$res = $this->query($sql);
$obj_res = $this->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
if(stripos($t, 'RDB$') === FALSE)
{
$row = array_map(array(&$this, 'quote'), $row);
$row = array_map('trim', $row);
}
$row_string = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');';
unset($row);
$insert_rows[] = $row_string;
}
unset($obj_res);
$output_sql .= "\n\nSET TRANSACTION;\n".implode("\n", $insert_rows)."\nCOMMIT;";
}
return $output_sql;
}
}
@ -400,7 +400,7 @@ SQL;
class Firebird_Result {
private $statement;
/**
* Create the object by passing the resource for
* the query
@ -411,12 +411,12 @@ class Firebird_Result {
{
$this->statement = $link;
}
// --------------------------------------------------------------------------
/**
* Emulate PDO fetch public function
*
*
* @param int $fetch_style
* @return mixed
*/
@ -426,7 +426,7 @@ class Firebird_Result {
{
$this->statement = $statement;
}
switch($fetch_style)
{
case PDO::FETCH_OBJ:
@ -442,12 +442,12 @@ class Firebird_Result {
break;
}
}
// --------------------------------------------------------------------------
/**
* Emulate PDO fetchAll public function
*
*
* @param int $fetch_style
* @return mixed
*/
@ -459,17 +459,17 @@ class Firebird_Result {
{
$all[] = $row;
}
$this->result = $all;
return $all;
}
// --------------------------------------------------------------------------
/**
* Run a prepared statement query
*
*
* @param array $args
* @return bool
*/
@ -477,26 +477,26 @@ class Firebird_Result {
{
//Add the prepared statement as the first parameter
array_unshift($args, $this->statement);
// Let php do all the hard stuff in converting
// Let php do all the hard stuff in converting
// the array of arguments into a list of arguments
$this->__construct(call_user_func_array('fbird_execute', $args));
return $this;
}
// --------------------------------------------------------------------------
/**
* Return the number of rows affected by the previous query
*
*
* @return int
*/
public function rowCount($statement="")
{
return fbird_affected_rows();
}
// --------------------------------------------------------------------------
/**

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
*/
// --------------------------------------------------------------------------
@ -28,25 +28,25 @@ class ODBC extends DB_PDO {
$class = __CLASS__.'_sql';
$this->sql = new $class;
}
// --------------------------------------------------------------------------
/**
* List tables for the current database
*
*
* @return mixed
*/
public function get_tables()
{
{
//Not possible reliably with this driver
return FALSE;
}
// --------------------------------------------------------------------------
/**
* List system tables for the current database/connection
*
*
* @return array
*/
public function get_system_tables()
@ -54,12 +54,12 @@ class ODBC extends DB_PDO {
//No way of determining for ODBC
return array();
}
// --------------------------------------------------------------------------
/**
* Empty the current database
*
*
* @return void
*/
public function truncate($table)
@ -67,21 +67,21 @@ class ODBC extends DB_PDO {
$sql = "DELETE FROM {$table}";
$this->query($sql);
}
// --------------------------------------------------------------------------
/**
* Return the number of rows returned for a SELECT query
*
*
* @return int
*/
public function num_rows()
{
// TODO: Implement
// @TODO: Implement
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
@ -90,11 +90,11 @@ class ODBC extends DB_PDO {
public function backup_structure()
{
// Not applicable to ODBC
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
*/
// --------------------------------------------------------------------------
/**
* Window controlling addtion of database connections
* Window controlling addition of database connections
*/
class Add_DB extends GtkWindow {

36
sys/windows/edit_db.php Normal file
View File

@ -0,0 +1,36 @@
<?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
*/
// --------------------------------------------------------------------------
/**
* Window controlling modifications of database connections
*/
class Edit_DB extends GtkWindow {
public function __construct()
{
parent::__construct();
$this->set_position(Gtk::WIN_POS_CENTER);
$this->set_title("Edit Database Connection");
// Create the layout table
$connection_form = new DB_Info_Widget();
// Add the Vbox, and show the window
$this->add($connection_form);
$this->show_all();
}
}
// End of edit_db.php

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
*/
// --------------------------------------------------------------------------
@ -29,7 +29,7 @@ class Main extends GtkWindow {
parent::__construct();
$this->settings =& Settings::get_instance();
if ( ! is_null($this->settings->width) && ! is_null($this->settings->height))
{
@ -79,6 +79,8 @@ class Main extends GtkWindow {
/**
* Display About menu with version information
*
* @return void
*/
public function about()
{
@ -111,7 +113,7 @@ class Main extends GtkWindow {
// --------------------------------------------------------------------------
/**
/**
* Quits the GTK loop
*/
public function quit()
@ -123,13 +125,14 @@ class Main extends GtkWindow {
/**
* Layout the main interface
*
* Create menus, hboxes, vboxs and other widgets
*
* @return void
*/
private function _main_layout()
{
$this->set_title('OpenSQLManager');
// Quit when this window is closed
$this->connect_simple('destroy', array('gtk', 'main_quit'));
@ -183,7 +186,7 @@ class Main extends GtkWindow {
$help_menu = new GtkMenu();
$top_help_menu->set_submenu($help_menu);
//File Menu
{
//Set up the open item
@ -210,9 +213,8 @@ class Main extends GtkWindow {
$menu_bar->append($top_help_menu);
}
return $menu_bar;
}
}
return $menu_bar;
}
}
// End of main.php

View File

@ -1,38 +0,0 @@
<?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
*/
// --------------------------------------------------------------------------
/**
* Class for controlling database views
*/
class DataGrid extends GtkTreeView{
protected $model, $settings;
function __construct()
{
$this->settings =& Settings::get_instance();
$this->model = new GtkTreeStore(GObject::TYPE_PHP_VALUE, GObject::TYPE_STRING);
parent::__construct($this->model);
}
function __get($key)
{
}
function __set($key, $val)
{
}
}