Various driver cleanup

This commit is contained in:
Timothy Warren 2012-04-09 16:11:14 -04:00
parent a43f04efd1
commit bdd78328a8
16 changed files with 365 additions and 325 deletions

View File

@ -204,6 +204,28 @@ abstract class DB_PDO extends PDO {
return FALSE;
}
// -------------------------------------------------------------------------
/**
* Method to simplify retreiving db results for meta-data queries
*
* @param string $sql
* @param string $filtered_index
*/
protected function driver_query($sql, $filtered_index="")
{
$res = $this->query($sql);
$all = $res->fetchAll(PDO::FETCH_ASSOC);
if ( ! empty($filtered_index))
{
return db_filter($all, $filtered_index);
}
return $all;
}
// -------------------------------------------------------------------------
// ! Abstract public functions to override in child classes
// -------------------------------------------------------------------------
@ -281,20 +303,6 @@ abstract class DB_PDO extends PDO {
*/
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
*
* @return string
*/
abstract public function backup_data();
/**
* Connect to a different database
*

View File

@ -56,5 +56,19 @@ abstract class DB_SQL {
* @return string
*/
abstract public function random();
/**
* 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
*
* @return string
*/
abstract public function backup_data();
}
// End of db_sql.php

View File

@ -413,88 +413,5 @@ SQL;
// the firebird database
return FALSE;
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// @todo Implement Backup function
return '';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
* @param array $exclude
* @param bool $system_tables
* @return string
*/
public function backup_data($exclude=array(), $system_tables=FALSE)
{
// Determine which tables to use
if($system_tables == TRUE)
{
$tables = array_merge($this->get_system_tables(), $this->get_tables());
}
else
{
$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;
}
}
// End of firebird_driver.php

View File

@ -13,7 +13,10 @@
// --------------------------------------------------------------------------
/**
* Firebird result class to emulate PDOStatement Class
* Firebird result class to emulate PDOStatement Class - only implements
* data-fetching methods
*
* @todo Implement more of the PDOStatement Class
*/
class Firebird_Result {

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
*/
// --------------------------------------------------------------------------
@ -19,18 +19,18 @@ class Firebird_SQL extends DB_SQL {
/**
* Convienience public function to generate sql for creating a db table
*
* @param string $name
*
* @param string $name
* @param array $fields
* @param array $constraints=array()
* @param array $indexes=array()
*
*
* @return string
*/
public function create_table($name, $fields, array $constraints=array(), array $indexes=array())
{
$column_array = array();
// Reorganize into an array indexed with column information
// Eg $column_array[$colname] = array(
// 'type' => ...,
@ -56,7 +56,7 @@ class Firebird_SQL extends DB_SQL {
}
}
// Join column definitons together
// Join column definitons together
$columns = array();
foreach($column_array as $n => $props)
{
@ -74,12 +74,12 @@ class Firebird_SQL extends DB_SQL {
return $sql;
}
// --------------------------------------------------------------------------
/**
* Drop the selected table
*
*
* @param string $name
* @return string
*/
@ -87,7 +87,7 @@ class Firebird_SQL extends DB_SQL {
{
return 'DROP TABLE "'.$name.'"';
}
// --------------------------------------------------------------------------
/**
@ -102,21 +102,21 @@ class Firebird_SQL extends DB_SQL {
{
// Keep the current sql string safe for a moment
$orig_sql = $sql;
$sql = 'FIRST '. (int) $limit;
if ($offset > 0)
{
$sql .= ' SKIP '. (int) $offset;
}
$sql = preg_replace("`SELECT`i", "SELECT {$sql}", $orig_sql);
return $sql;
}
}
// --------------------------------------------------------------------------
/**
* Random ordering keyword
*
@ -126,5 +126,88 @@ class Firebird_SQL extends DB_SQL {
{
return FALSE;
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// @todo Implement Backup function
return '';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
* @param array $exclude
* @param bool $system_tables
* @return string
*/
public function backup_data($exclude=array(), $system_tables=FALSE)
{
// Determine which tables to use
if($system_tables == TRUE)
{
$tables = array_merge($this->get_system_tables(), $this->get_tables());
}
else
{
$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;
}
}
//End of firebird_sql.php

View File

@ -176,32 +176,6 @@ class MySQL extends DB_PDO {
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// @todo Implement Backup function
return '';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
* @return string
*/
public function backup_data()
{
// @todo Implement Backup function
return '';
}
// --------------------------------------------------------------------------
/**
* Surrounds the string with the databases identifier escape characters
*

View File

@ -7,11 +7,11 @@
* @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
*/
// --------------------------------------------------------------------------
/**
* MySQL specifc SQL
*/
@ -19,18 +19,18 @@ class MySQL_SQL extends DB_SQL{
/**
* Convienience public function for creating a new MySQL table
*
*
* @param [type] $name [description]
* @param [type] $columns [description]
* @param array $constraints=array() [description]
* @param array $indexes=array() [description]
*
*
* @return [type]
*/
public function create_table($name, $columns, array $constraints=array(), array $indexes=array())
{
$column_array = array();
// Reorganize into an array indexed with column information
// Eg $column_array[$colname] = array(
// 'type' => ...,
@ -56,25 +56,25 @@ class MySQL_SQL extends DB_SQL{
}
}
// Join column definitons together
// Join column definitons together
$columns = array();
foreach($column_array as $n => $props)
{
$n = trim($n, '`');
$str = "`{$n}` ";
$str .= (isset($props['type'])) ? "{$props['type']} " : "";
$columns[] = $str;
}
// Add constraints
foreach($column_array as $n => $props)
{
{
if (isset($props['constraint']))
{
$columns[] = $props['constraint'];
}
}
}
// Generate the sql for the creation of the table
@ -84,12 +84,12 @@ class MySQL_SQL extends DB_SQL{
return $sql;
}
// --------------------------------------------------------------------------
/**
* Convience public function for droping a MySQL table
*
*
* @param string $name
* @return string
*/
@ -97,7 +97,7 @@ class MySQL_SQL extends DB_SQL{
{
return "DROP TABLE `{$name}`";
}
// --------------------------------------------------------------------------
/**
@ -117,9 +117,9 @@ class MySQL_SQL extends DB_SQL{
return $sql." LIMIT {$offset}, {$limit}";
}
// --------------------------------------------------------------------------
/**
* Random ordering keyword
*
@ -128,6 +128,32 @@ class MySQL_SQL extends DB_SQL{
public function random()
{
return ' RAND()';
}
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// @todo Implement Backup function
return '';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
* @return string
*/
public function backup_data()
{
// @todo Implement Backup function
return '';
}
}
//End of mysql_sql.php

View File

@ -161,31 +161,5 @@ class ODBC extends DB_PDO {
{
// @TODO: Implement
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// Not applicable to ODBC
return '';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
* @return string
*/
public function backup_data()
{
// Not applicable to ODBC
return '';
}
}
// End of odbc_driver.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
*/
// --------------------------------------------------------------------------
@ -35,7 +35,7 @@ class ODBC_SQL extends DB_SQL {
{
return "DROP TABLE {$name}";
}
// --------------------------------------------------------------------------
/**
@ -50,9 +50,9 @@ class ODBC_SQL extends DB_SQL {
{
return $sql;
}
// --------------------------------------------------------------------------
/**
* Random ordering keyword
*
@ -62,5 +62,31 @@ class ODBC_SQL extends DB_SQL {
{
return FALSE;
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// Not applicable to ODBC
return '';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
* @return string
*/
public function backup_data()
{
// Not applicable to ODBC
return '';
}
}
// End of odbc_sql.php

View File

@ -254,31 +254,5 @@ SQL;
{
return (isset($this->statement)) ? $this->statement->rowCount : FALSE;
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// @todo Implement Backup function
return '';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
* @return string
*/
public function backup_data()
{
// @todo Implement Backup function
return '';
}
}
//End of pgsql_driver.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
*/
// --------------------------------------------------------------------------
@ -16,11 +16,11 @@
* PostgreSQL specifc SQL
*/
class pgSQL_SQL extends DB_SQL {
public function create_table($name, $columns, array $constraints=array(), array $indexes=array())
{
$column_array = array();
// Reorganize into an array indexed with column information
// Eg $column_array[$colname] = array(
// 'type' => ...,
@ -46,7 +46,7 @@ class pgSQL_SQL extends DB_SQL {
}
}
// Join column definitons together
// Join column definitons together
$columns = array();
foreach($column_array as $n => $props)
{
@ -64,14 +64,14 @@ class pgSQL_SQL extends DB_SQL {
return $sql;
}
// --------------------------------------------------------------------------
public function delete_table($name)
{
return 'DROP TABLE "'.$name.'"';
}
// --------------------------------------------------------------------------
/**
@ -93,9 +93,9 @@ class pgSQL_SQL extends DB_SQL {
return $sql;
}
// --------------------------------------------------------------------------
/**
* Random ordering keyword
*
@ -106,5 +106,31 @@ class pgSQL_SQL extends DB_SQL {
return ' RANDOM()';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// @todo Implement Backup function
return '';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
* @return string
*/
public function backup_data()
{
// @todo Implement Backup function
return '';
}
}
//End of pgsql_manip.php

View File

@ -220,96 +220,5 @@ SQL;
{
return (isset($this->statement)) ? $this->statement->rowCount : FALSE;
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// Fairly easy for SQLite...just query the master table
$sql = 'SELECT "sql" FROM "sqlite_master"';
$res = $this->query($sql);
$result = $res->fetchAll(PDO::FETCH_ASSOC);
$sql_array = array();
foreach($result as $r)
{
$sql_array[] = $r['sql'];
}
$sql_structure = implode("\n\n", $sql_array);
return $sql_structure;
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
* @param array $excluded
* @return string
*/
public function backup_data($excluded=array())
{
// Get a list of all the objects
$sql = 'SELECT "name" FROM "sqlite_master"';
if( ! empty($excluded))
{
$sql .= ' WHERE NOT IN("'.implode('","', $excluded).'")';
}
$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;
}
}
//End of sqlite_driver.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
*/
// --------------------------------------------------------------------------
@ -19,7 +19,7 @@ class SQLite_SQL extends DB_SQL {
/**
* Convenience public 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
@ -29,7 +29,7 @@ class SQLite_SQL extends DB_SQL {
public function create_table($name, $columns, array $constraints=array(), array $indexes=array())
{
$column_array = array();
// Reorganize into an array indexed with column information
// Eg $column_array[$colname] = array(
// 'type' => ...,
@ -55,7 +55,7 @@ class SQLite_SQL extends DB_SQL {
}
}
// Join column definitons together
// Join column definitons together
$columns = array();
foreach($column_array as $n => $props)
{
@ -73,12 +73,12 @@ class SQLite_SQL extends DB_SQL {
return $sql;
}
// --------------------------------------------------------------------------
/**
* SQL to drop the specified table
*
*
* @param string $name
* @return string
*/
@ -86,7 +86,7 @@ class SQLite_SQL extends DB_SQL {
{
return 'DROP TABLE IF EXISTS "'.$name.'"';
}
// --------------------------------------------------------------------------
/**
@ -106,9 +106,9 @@ class SQLite_SQL extends DB_SQL {
return $sql." LIMIT {$offset}, {$limit}";
}
// --------------------------------------------------------------------------
/**
* Random ordering keyword
*
@ -118,5 +118,96 @@ class SQLite_SQL extends DB_SQL {
{
return ' RANDOM()';
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's data
*
* @param array $excluded
* @return string
*/
public function backup_data($excluded=array())
{
// Get a list of all the objects
$sql = 'SELECT "name" FROM "sqlite_master"';
if( ! empty($excluded))
{
$sql .= ' WHERE NOT IN("'.implode('","', $excluded).'")';
}
$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;
}
// --------------------------------------------------------------------------
/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// Fairly easy for SQLite...just query the master table
$sql = 'SELECT "sql" FROM "sqlite_master"';
$res = $this->query($sql);
$result = $res->fetchAll(PDO::FETCH_ASSOC);
$sql_array = array();
foreach($result as $r)
{
$sql_array[] = $r['sql'];
}
$sql_structure = implode("\n\n", $sql_array);
return $sql_structure;
}
}
//End of sqlite_sql.php

View File

@ -80,7 +80,12 @@ class Query_Builder {
switch($dbtype)
{
default:
$dsn = "host={$params->host};dbname={$params->conn_db}";
$dsn = "dbname={$params->conn_db}";
if ( ! empty($params->host))
{
$dsn .= ";host={$params->host}";
}
if ( ! empty($params->port))
{
@ -1079,8 +1084,6 @@ class Query_Builder {
break;
}
// echo $sql.'<br />';
return $sql;
}
}

View File

@ -284,7 +284,19 @@ class DB_tabs extends GTKNotebook {
// Get the selected database
$new_db = $view->get(0);
// Get existing connections
$conns = DB_REG::get_connections();
// Get connection info for existing connections
$conn_info = array();
foreach($conns as $c)
{
$conn_info[$c] = Settings::get_instance()->get_db($c);
}
// @todo figure out how to single out the current db connection
}
}