diff --git a/.gitignore b/.gitignore index a233cb0..e042d7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ # Ignore the stupid .DS_Store files *.DS_Store +settings.json +errors.txt diff --git a/src/common/db_pdo.php b/src/common/db_pdo.php new file mode 100644 index 0000000..484ae49 --- /dev/null +++ b/src/common/db_pdo.php @@ -0,0 +1,111 @@ +prepare($sql); + + if( ! is_like_array($query)) + { + $this->get_last_error(); + return FALSE; + } + + // Set the statement in the class variable for easy later access + $this->statement =& $query; + + + if( ! is_like_array($data)) + { + trigger_error("Invalid data argument"); + return FALSE; + } + + // Bind the parameters + foreach($data as $k => $value) + { + $res = $query->bindValue($k, $value); + + if( ! $res) + { + trigger_error("Parameter not successfully bound"); + return FALSE; + } + } + + return $query; + + } + + // ------------------------------------------------------------------------- + + /** + * Retreives the data from a select query + * + * @param PDOStatement $statement + * @return array + */ + function get_query_data($statement) + { + // Execute the query + $statement->execute(); + + // Return the data array fetched + return $statement->fetchAll(PDO::FETCH_ASSOC); + } + + // ------------------------------------------------------------------------- + + /** + * Returns number of rows affected by an INSERT, UPDATE, DELETE type query + * + * @param PDOStatement $statement + * @return int + */ + function get_rows_changed($statement) + { + // Execute the query + $statement->execute(); + + // Return number of rows affected + return $statement->rowCount(); + } + +} + +// End of db_pdo.php \ No newline at end of file diff --git a/src/common/settings.php b/src/common/settings.php index d8e66a7..03bfdcd 100644 --- a/src/common/settings.php +++ b/src/common/settings.php @@ -39,6 +39,12 @@ class Settings { $this->current = json_decode(file_get_contents($path)); } + // Add the DB object under the settings if it doesn't already exist + if( ! isset($this->current->dbs)) + { + $this->current->dbs = new stdClass(); + } + } // -------------------------------------------------------------------------- @@ -48,22 +54,78 @@ class Settings { */ function __destruct() { - file_put_contents(json_encode($this->current), BASE_DIR.'/settings.json'); + file_put_contents(BASE_DIR.'/settings.json', json_encode($this->current)); + } + + // -------------------------------------------------------------------------- + + /** + * Magic method to simplify isset checking for config options + * + * @param string $key + * @return $mixed + */ + function __get($key) + { + return (isset($this->current->{$key})) ? $this->current->{$key} : NULL; + } + + // -------------------------------------------------------------------------- + + /** + * Magic method to simplify setting config options + * + * @param string $key + * @param mixed $val + */ + function __set($key, $val) + { + //Don't allow direct db config changes + if($key == "dbs") + { + return FALSE; + } + + $this->current->{$key} = $val; } // -------------------------------------------------------------------------- /** * Add a database connection - * - * @param string $type - * @param string $host - * @param string $user - * @param string $pass + * + * @param string $name + * @param array $params */ - function add_db($type, $host, $user, $pass) + function add_db($name, $params) { - + if(empty($this->current->dbs->{$name})) + { + $this->current->dbs->{$name} = $params; + } + else + { + return FALSE; + } } + + // -------------------------------------------------------------------------- + + /** + * Remove a database connection + * + * @param string $name + */ + function remove_db($name) + { + if( ! isset($this->current->dbs->{$name})) + { + return FALSE; + } + + // Remove the db name from the object + unset($this->current->dbs->{$name}); + } + } // End of settings.php \ No newline at end of file diff --git a/src/databases/firebird.php b/src/databases/firebird.php index b7b4912..7767a04 100644 --- a/src/databases/firebird.php +++ b/src/databases/firebird.php @@ -12,12 +12,6 @@ // -------------------------------------------------------------------------- -// Test for support -if( ! function_exists('ibase_connect')) -{ - return FALSE; -} - /** * Firebird Database class * diff --git a/src/databases/mysql.php b/src/databases/mysql.php index 5460a34..e974850 100644 --- a/src/databases/mysql.php +++ b/src/databases/mysql.php @@ -12,12 +12,6 @@ // -------------------------------------------------------------------------- -// Test for support -if( ! in_array('mysql', pdo_drivers())) -{ - return FALSE; -} - /** * MySQL specific class * @@ -27,7 +21,9 @@ class MySQL extends DB_PDO { function __construct($dsn, $username=null, $password=null, $options=array()) { - parent::__construct($dsn, $username, $password, $options); + parent::__construct("mysql:$dsn", $username, $password, $options); + + } /** diff --git a/src/databases/odbc.php b/src/databases/odbc.php index b9dfdec..6849020 100644 --- a/src/databases/odbc.php +++ b/src/databases/odbc.php @@ -12,12 +12,6 @@ // -------------------------------------------------------------------------- -// Test for support -if( ! in_array('odbc', pdo_drivers())) -{ - return FALSE; -} - /** * ODBC Database Driver * @@ -29,7 +23,7 @@ class ODBC extends DB_PDO { function __construct($dsn, $username=null, $password=null, $options=array()) { - parent::__construct($dsn, $username, $password, $options); + parent::__construct("odbc:$dsn", $username, $password, $options); } } diff --git a/src/databases/pgsql.php b/src/databases/pgsql.php index 5a313bd..1644d1c 100644 --- a/src/databases/pgsql.php +++ b/src/databases/pgsql.php @@ -12,12 +12,6 @@ // -------------------------------------------------------------------------- -// Test for support -if( ! in_array('pgsql', pdo_drivers())) -{ - return FALSE; -} - /** * PostgreSQL specifc class * @@ -27,7 +21,7 @@ class pgSQL extends DB_PDO { function __construct($dsn, $username=null, $password=null, $options=array()) { - parent::__construct($dsn, $username, $password, $options); + parent::__construct("pgsql:$dsn", $username, $password, $options); } /** diff --git a/src/databases/sqlite.php b/src/databases/sqlite.php index b59381a..c76c162 100644 --- a/src/databases/sqlite.php +++ b/src/databases/sqlite.php @@ -12,12 +12,6 @@ // -------------------------------------------------------------------------- -// Test for support -if( ! in_array('sqlite', pdo_drivers())) -{ - return FALSE; -} - /** * SQLite specific class * @@ -25,9 +19,25 @@ if( ! in_array('sqlite', pdo_drivers())) */ class SQLite extends DB_PDO { + /** + * Static function to simply creating dsn for the current database driver + * + * @return SQLite object + */ + static function connect() + { + + } + + /** + * Open SQLite Database + * + * @param string $dsn + */ function __construct($dsn) { - parent::__construct($dsn); + // DSN is simply `sqlite:/path/to/db` + parent::__construct("sqlite:{$dsn}"); } /** @@ -37,7 +47,23 @@ class SQLite extends DB_PDO { */ function truncate($table) { - + // SQLite has a TRUNCATE optimization, + // but no support for the actual command. + $sql = "DELETE FROM {$table}"; + $this->query($sql); } + /** + * Create an sqlite database file + * + * @param $path + */ + function create_db($path) + { + // Create the file if it doesn't exist + if( ! file_exists($path)) + { + touch($path); + } + } } \ No newline at end of file diff --git a/src/index.php b/src/index.php index 556df0b..9fdfa72 100644 --- a/src/index.php +++ b/src/index.php @@ -10,18 +10,27 @@ * @license http://philsturgeon.co.uk/code/dbad-license */ - // -------------------------------------------------------------------------- +// -------------------------------------------------------------------------- - /** - * Bootstrap file - * - * Initializes parent window and starts the GTK event loop - */ +/** + * Bootstrap file + * + * Initializes parent window and starts the GTK event loop + */ // -------------------------------------------------------------------------- +// Suppress errors that php-gtk puts out error_reporting(-1 & ~(E_STRICT | E_DEPRECATED)); +// Set the stupid timezone so PHP shuts up. +date_default_timezone_set('GMT'); + +// Set the current directory as the base for included files +define('BASE_DIR', dirname(__FILE__)); + +// -------------------------------------------------------------------------- + /** * Log fatal errors */ @@ -42,14 +51,24 @@ function log_fatal() register_shutdown_function('log_fatal'); +// -------------------------------------------------------------------------- + // Make sure php-gtk works if ( ! class_exists('gtk')) { - die("Please load the php-gtk2 module in your php.ini\r\n"); + trigger_error("PHP-gtk not found. Please load the php-gtk2 module in your php.ini", E_USER_ERROR); + + die(); } -// Set the stupid timezone so PHP shuts up. -date_default_timezone_set('GMT'); +// Make sure pdo exists +if( ! class_exists('pdo')) +{ + trigger_error("PHP support for PDO is required.", E_USER_ERROR); + die(); +} + +// -------------------------------------------------------------------------- // Bulk loading wrapper workaround for PHP < 5.4 function do_include($path) @@ -57,16 +76,35 @@ function do_include($path) require_once($path); } -define('BASE_DIR', dirname(__FILE__)); - -// Load modules // Load everything so that we don't have to do requires later { array_map('do_include', glob(BASE_DIR . "/common/*.php")); - array_map('do_include', glob(BASE_DIR . "/databases/*.php")); array_map('do_include', glob(BASE_DIR . "/windows/*.php")); } +// -------------------------------------------------------------------------- + +// Load db classes based on capability +$path = BASE_DIR . "/databases/"; + +foreach(pdo_drivers() as $d) +{ + $file = "{$path}{$d}.php"; + + if(is_file($file)) + { + require_once($file); + } +} + +// Load Firebird if there is support +if(function_exists('ibase_connect')) +{ + require_once("{$path}firebird.php"); +} + +// -------------------------------------------------------------------------- + // Create the main window $wnd = new Main(); diff --git a/src/windows/add_db.php b/src/windows/add_db.php index 4f98074..d54e731 100644 --- a/src/windows/add_db.php +++ b/src/windows/add_db.php @@ -17,13 +17,15 @@ */ class Add_DB extends GtkWindow { - var $conn, $dbtype, $host, $user, $password, $database; + var $conn, $dbtype, $host, $user, $password, $database, $settings; function __construct() { parent::__construct(); - $this->set_title("OpenSQLManager - Add Database Connection"); + $this->settings = new Settings(); + + $this->set_title("Add Database Connection"); // Add the Vbox, and show the window $this->add($this->_layout()); @@ -114,6 +116,11 @@ class Add_DB extends GtkWindow { continue; } + // Replace default capitalization with something that looks better. + $d = str_replace("sql", "SQL", $d); + $d = str_ireplace("pg", "Postgre", $d); + $d = ucfirst($d); + $drivers[] = $d; }