From c85f791de5103ba65073013fd7fd5a23a41e045f Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 2 Feb 2012 17:43:09 -0500 Subject: [PATCH] Lots of updates Added mention of Mariadb to read me, made db_pdo an abstract class, created manip_subclasses for database drivers, created start of unit tests. --- README.md | 4 +-- src/databases/db_pdo.php | 10 +++---- src/databases/firebird.php | 35 +++++++++++++++++++++++ src/databases/mysql.php | 30 +++++++++++++++++++- src/databases/odbc.php | 2 ++ src/databases/pgsql.php | 58 +++++++++++++++++++++++++++++++++++++- src/databases/sqlite.php | 53 +++++++++++++++++++++++++++------- tests/index.php | 19 +++++++++++++ 8 files changed, 192 insertions(+), 19 deletions(-) create mode 100644 tests/index.php diff --git a/README.md b/README.md index f6fa548..9023228 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ OpenSQLManager is an attempt to create an alternative to Navicat that is free and open. It is build with PHP-GTK, so I'm looking for a way to create normal binaries. ### Included pre-configured version of php for windows -Because php-gtk is such a pain to compile on Windows, I've put together this package from the latest php-gtk windows package in `php-gtk2-win.7z`. +Because php-gtk is such a pain to compile on Windows, I've put together this package from the latest php-gtk windows package. It's available in the downloads section. ## PHP Requirements * Version 5.2 - 5.3.* @@ -21,7 +21,7 @@ Because php-gtk is such a pain to compile on Windows, I've put together this pac The databases currently slated to be supported are: * [PostgreSQL](http://www.postgresql.org) -* [MySQL](http://www.mysql.com/) +* [MySQL](http://www.mysql.com/) / [MariaDB](http://mariadb.org/) * [SQLite](http://sqlite.org/) Plan to implement, not support: diff --git a/src/databases/db_pdo.php b/src/databases/db_pdo.php index 6677526..a689786 100644 --- a/src/databases/db_pdo.php +++ b/src/databases/db_pdo.php @@ -17,7 +17,7 @@ * * Extends PDO to simplify cross-database issues */ -class DB_PDO extends PDO { +abstract class DB_PDO extends PDO { protected $statement; @@ -108,10 +108,10 @@ class DB_PDO extends PDO { // ------------------------------------------------------------------------- - abstract function create_database($name){} - - + /** + * Abstract functions to override in child classes + */ + abstract function get_dbs(){} } - // End of db_pdo.php \ No newline at end of file diff --git a/src/databases/firebird.php b/src/databases/firebird.php index 5809bd3..444e61a 100644 --- a/src/databases/firebird.php +++ b/src/databases/firebird.php @@ -20,6 +20,7 @@ class firebird { protected $conn, $statement; + private $esc_char = "''"; /** * Open the link to the database @@ -40,6 +41,18 @@ class firebird { { ibase_close($this->conn); } + + /** + * Empty a database table + * + * @param string $table + */ + function truncate($table) + { + // Firebird lacka a truncate command + $sql = "DELETE FROM {$table}"; + $this->query($sql); + } /** * Wrapper function to better match PDO @@ -52,6 +65,28 @@ class firebird { $this->statement = ibase_query($this->conn, $sql); return $this->statement; } + + /** + * Gets all the databases for the current connection + * + * @return mixed + */ + function get_dbs() + { + // I don't think this is possible with Firebird + return FALSE; + } +} + +class firebird_manip extends firebird { + + function __construct($db, $user="sysdba", $pass="masterkey") + { + parent::__construct($db, $user, $pass); + } + + + } // End of firebird.php \ No newline at end of file diff --git a/src/databases/mysql.php b/src/databases/mysql.php index 10aa5aa..0b295e9 100644 --- a/src/databases/mysql.php +++ b/src/databases/mysql.php @@ -19,6 +19,14 @@ */ class MySQL extends DB_PDO { + /** + * Connect to MySQL Database + * + * @param string $dsn + * @param string $username=null + * @param string $password=null + * @param array $options=array() + */ function __construct($dsn, $username=null, $password=null, $options=array()) { $options = array_merge(array( @@ -37,8 +45,28 @@ class MySQL extends DB_PDO { function truncate($table) { $sql = "TRUNCATE `{$table}`"; - $this->query($sql); } + /** + * Returns the datbases available for the current connection + * + * @return array + */ + function get_dbs() + { + $sql = "SHOW TABLES"; + $res = $this->query($sql); + + return $res->fetchAll(PDO::FETCH_ASSOC); + } + +} + +class MySQL_manip extends MySQL { + + function __construct($dsn, $user=null, $pass=null, $opt=array()) + { + parent::__construct($dsn, $user, $pass, $opt); + } } \ No newline at end of file diff --git a/src/databases/odbc.php b/src/databases/odbc.php index 6849020..7996aa0 100644 --- a/src/databases/odbc.php +++ b/src/databases/odbc.php @@ -26,6 +26,8 @@ class ODBC extends DB_PDO { parent::__construct("odbc:$dsn", $username, $password, $options); } + function get_dbs(){} + } // End of odbc.php \ No newline at end of file diff --git a/src/databases/pgsql.php b/src/databases/pgsql.php index 1644d1c..bfdcf61 100644 --- a/src/databases/pgsql.php +++ b/src/databases/pgsql.php @@ -19,6 +19,14 @@ */ class pgSQL extends DB_PDO { + /** + * Connect to a PosgreSQL database + * + * @param string $dsn + * @param string $username=null + * @param string $password=null + * @param array $options=array() + */ function __construct($dsn, $username=null, $password=null, $options=array()) { parent::__construct("pgsql:$dsn", $username, $password, $options); @@ -31,7 +39,55 @@ class pgSQL extends DB_PDO { */ function truncate($table) { - + $sql = 'TRUNCATE "' . $table . '"'; + $this->query($sql); + } + + /** + * Get the list of databases for the current db connection + * + * @return array + */ + function get_dbs() + { + $sql = 'SELECT "tablename" FROM "pg_tables" + WHERE "tablename" NOT LIKE pg\_% + AND "tablename" NOT LIKE sql\%'; + + $res = $this->query($sql); + + $dbs = $res->fetchAll(PDO::FETCH_ASSOC); + + return $dbs; + } + + /** + * Get a list of views for the current db connection + * + * @return array + */ + function get_views() + { + $sql = 'SELECT "viewname" FROM "pg_views" + WHERE viewname NOT LIKE pg\_%'; + + $res = $this->query($sql); + + $views = $res->fetchAll(PDO::FETCH_ASSOC); + + return $views; + } + +} + +/** + * PostgreSQL DB Structure manipulation class + */ +class pgSQL_manip extends pgSQL { + + function __construct($dsn, $username=null, $password=null, $options=array()) + { + parent::__construct($dsn, $username, $password, $options); } } \ No newline at end of file diff --git a/src/databases/sqlite.php b/src/databases/sqlite.php index c76c162..ca7ba83 100644 --- a/src/databases/sqlite.php +++ b/src/databases/sqlite.php @@ -19,16 +19,6 @@ */ 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 * @@ -53,6 +43,49 @@ class SQLite extends DB_PDO { $this->query($sql); } + /** + * List databases for the current connection + * + * @return mixed + */ + function get_dbs() + { + // SQLite doesn't have a way of doing this + return FALSE; + } +} + +class SQLite_manip extends SQLite { + + function __construct($dsn) + { + parent::__construct($dsn); + } + + /** + * Convenience function to create a new table + * + * @param string $name //Name of the table + * @param array $columns //columns as straight array and/or column => type pairs + * @param array $constraints // column => constraint pairs + * @param array $indexes // column => index pairs + * @return srtring + */ + function create_table($name, $columns, $constraints, $indexes) + { + $sql = "CREATE TABLE {$name} ("; + + foreach($columns as $colname => $type) + { + if(is_numeric($colname)) + { + $colname = $type; + } + } + + + } + /** * Create an sqlite database file * diff --git a/tests/index.php b/tests/index.php new file mode 100644 index 0000000..21b0a2d --- /dev/null +++ b/tests/index.php @@ -0,0 +1,19 @@ +