Load DB classes based on support

Reorganized db class loading, improvements to settings class.
This commit is contained in:
Timothy Warren 2012-02-01 16:36:55 -05:00
parent 9707d708cb
commit 120b526293
10 changed files with 282 additions and 58 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
# Ignore the stupid .DS_Store files
*.DS_Store
settings.json
errors.txt

111
src/common/db_pdo.php Normal file
View File

@ -0,0 +1,111 @@
<?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
*/
// --------------------------------------------------------------------------
/**
* Base Database class
*
* Extends PDO to simplify cross-database issues
*/
class DB_PDO extends PDO {
protected $statement;
function __construct($dsn, $username=NULL, $password=NULL, $driver_options=array())
{
parent::__construct($dsn, $username, $password, $driver_options);
}
// -------------------------------------------------------------------------
/**
* Simplifies prepared statements for database queries
*
* @param string $sql
* @param array $data
* @return mixed PDOStatement / FALSE
*/
function prepare_query($sql, $data)
{
// Prepare the sql
$query = $this->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

View File

@ -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

View File

@ -12,12 +12,6 @@
// --------------------------------------------------------------------------
// Test for support
if( ! function_exists('ibase_connect'))
{
return FALSE;
}
/**
* Firebird Database class
*

View File

@ -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);
}
/**

View File

@ -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);
}
}

View File

@ -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);
}
/**

View File

@ -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);
}
}
}

View File

@ -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();

View File

@ -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;
}