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; 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 // ! Abstract public functions to override in child classes
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
@ -281,20 +303,6 @@ abstract class DB_PDO extends PDO {
*/ */
abstract public function get_system_tables(); 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 * Connect to a different database
* *

View File

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

View File

@ -413,88 +413,5 @@ SQL;
// the firebird database // the firebird database
return FALSE; 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 // 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 { class Firebird_Result {

View File

@ -126,5 +126,88 @@ class Firebird_SQL extends DB_SQL {
{ {
return FALSE; 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 //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 * Surrounds the string with the databases identifier escape characters
* *

View File

@ -129,5 +129,31 @@ class MySQL_SQL extends DB_SQL{
{ {
return ' RAND()'; 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 //End of mysql_sql.php

View File

@ -161,31 +161,5 @@ class ODBC extends DB_PDO {
{ {
// @TODO: Implement // @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 // End of odbc_driver.php

View File

@ -62,5 +62,31 @@ class ODBC_SQL extends DB_SQL {
{ {
return FALSE; 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 // End of odbc_sql.php

View File

@ -254,31 +254,5 @@ SQL;
{ {
return (isset($this->statement)) ? $this->statement->rowCount : FALSE; 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 //End of pgsql_driver.php

View File

@ -106,5 +106,31 @@ class pgSQL_SQL extends DB_SQL {
return ' RANDOM()'; 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 //End of pgsql_manip.php

View File

@ -220,96 +220,5 @@ SQL;
{ {
return (isset($this->statement)) ? $this->statement->rowCount : FALSE; 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 //End of sqlite_driver.php

View File

@ -118,5 +118,96 @@ class SQLite_SQL extends DB_SQL {
{ {
return ' RANDOM()'; 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 //End of sqlite_sql.php

View File

@ -80,7 +80,12 @@ class Query_Builder {
switch($dbtype) switch($dbtype)
{ {
default: 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)) if ( ! empty($params->port))
{ {
@ -1079,8 +1084,6 @@ class Query_Builder {
break; break;
} }
// echo $sql.'<br />';
return $sql; return $sql;
} }
} }

View File

@ -284,6 +284,18 @@ class DB_tabs extends GTKNotebook {
// Get the selected database // Get the selected database
$new_db = $view->get(0); $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
} }