diff --git a/databases/firebird.php b/databases/firebird.php index 250bb2a..7892ebf 100644 --- a/databases/firebird.php +++ b/databases/firebird.php @@ -303,6 +303,24 @@ SQL; return $this->execute($args); } + + // -------------------------------------------------------------------------- + + /** + * Method to emulate PDO->quote + * + * @param string $str + * @return string + */ + public function quote($str) + { + if(is_numeric($str)) + { + return $str; + } + + return "'".str_replace("'", "''", $str)."'"; + } // -------------------------------------------------------------------------- @@ -342,8 +360,48 @@ SQL; */ public function backup_data() { - // @todo Implement Backup function - return ''; + $tables = $this->get_tables(); + + $output_sql = ''; + + // Get the data for each object + foreach($table 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 firebird.php \ No newline at end of file diff --git a/databases/sqlite.php b/databases/sqlite.php index 285c3fa..6cf4b92 100644 --- a/databases/sqlite.php +++ b/databases/sqlite.php @@ -171,12 +171,19 @@ SQL; /** * Create an SQL backup file for the current database's data * + * @param array $excluded * @return string */ - public function backup_data() + 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); @@ -218,7 +225,7 @@ SQL; unset($obj_res); - $output_sql = implode("\n", $insert_rows); + $output_sql .= "\n\n".implode("\n", $insert_rows); } return $output_sql; diff --git a/tests/databases/firebird.php b/tests/databases/firebird.php index 6d9cb5d..16f082c 100644 --- a/tests/databases/firebird.php +++ b/tests/databases/firebird.php @@ -118,6 +118,8 @@ SQL; function TestDeleteTable() { + ?>
= $this->db->backup_data(); ?>db->manip->delete_table('create_test'); $this->db->query($sql); diff --git a/tests/test_dbs/test_sqlite.db b/tests/test_dbs/test_sqlite.db index 460d01c..0f55884 100755 Binary files a/tests/test_dbs/test_sqlite.db and b/tests/test_dbs/test_sqlite.db differ