Implemented backup data methods in mysql and pgsql drivers

This commit is contained in:
Timothy Warren 2012-04-19 08:59:10 -04:00
parent 5d5588c909
commit 698837dd02
3 changed files with 97 additions and 5 deletions

View File

@ -128,8 +128,51 @@ class MySQL_Util extends DB_Util {
{ {
$tables = $this->get_tables(); $tables = $this->get_tables();
// @todo Implement Backup function // Filter out the tables you don't want
return ''; if( ! empty($exclude))
{
$tables = array_diff($tables, $exclude);
}
$output_sql = '';
// Select the rows from each Table
foreach($tables as &$t)
{
$sql = "SELECT * FROM `{$t}`";
$res = $this->query($sql);
$rows = $res->fetchAll(PDO::FETCH_ASSOC);
$res = NULL;
// Skip empty tables
if (count($rows) < 1) continue;
// Nab the column names by getting the keys of the first row
$columns = @array_keys($rows[0]);
$insert_rows = array();
// Create the insert statements
foreach($rows as &$row)
{
$row = array_values($row);
$row = array_map(array(&$this, 'quote'), $row);
$row = array_map('trim', $row);
$row_string = 'INSERT INTO `'.trim($t).'` (`'.implode('`,`', $columns).'`) VALUES ('.implode(',', $row).');';
$row = NULL;
$insert_rows[] = $row_string;
}
$obj_res = NULL;
$output_sql .= "\n\n".implode("\n", $insert_rows)."\n";
}
return $output_sql;
} }
} }
// End of mysql_util.php // End of mysql_util.php

View File

@ -110,12 +110,61 @@ class PgSQL_Util extends DB_Util {
/** /**
* Create an SQL backup file for the current database's data * Create an SQL backup file for the current database's data
* *
* @param array $exclude
* @return string * @return string
*/ */
public function backup_data() public function backup_data($exclude=array())
{ {
// @todo Implement Backup function $tables = $this->get_tables();
return '';
// 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 = $res->fetchAll(PDO::FETCH_ASSOC);
// Don't add to the file if the table is empty
if (count($obj_res) < 1) continue;
$res = NULL;
// 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
$row = array_map(array(&$this, 'quote'), $row);
$row = array_map('trim', $row);
$row_string = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');';
$row = NULL;
$insert_rows[] = $row_string;
}
$obj_res = NULL;
$output_sql .= "\n\n".implode("\n", $insert_rows)."\n";
}
return $output_sql;
} }
} }
// End of pgsql_util.php // End of pgsql_util.php

Binary file not shown.