diff --git a/drivers/mysql/mysql_util.php b/drivers/mysql/mysql_util.php index 4c197b1..47eb276 100644 --- a/drivers/mysql/mysql_util.php +++ b/drivers/mysql/mysql_util.php @@ -127,9 +127,52 @@ class MySQL_Util extends DB_Util { public function backup_data($exclude=array()) { $tables = $this->get_tables(); + + // Filter out the tables you don't want + 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"; + } - // @todo Implement Backup function - return ''; + return $output_sql; } } // End of mysql_util.php \ No newline at end of file diff --git a/drivers/pgsql/pgsql_util.php b/drivers/pgsql/pgsql_util.php index 6e35960..4facaad 100644 --- a/drivers/pgsql/pgsql_util.php +++ b/drivers/pgsql/pgsql_util.php @@ -110,12 +110,61 @@ class PgSQL_Util extends DB_Util { /** * Create an SQL backup file for the current database's data * + * @param array $exclude * @return string */ - public function backup_data() + public function backup_data($exclude=array()) { - // @todo Implement Backup function - return ''; + $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 = $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 \ No newline at end of file diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index 83122ed..5fdda5c 100644 Binary files a/tests/db_files/FB_TEST_DB.FDB and b/tests/db_files/FB_TEST_DB.FDB differ