From 451d883242d705c2c50510e78fd159b999a3b8fb Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 28 Feb 2012 17:44:34 -0500 Subject: [PATCH] Implemented backup_data method for sqlite driver --- databases/sqlite.php | 49 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/databases/sqlite.php b/databases/sqlite.php index aee62ac..285c3fa 100644 --- a/databases/sqlite.php +++ b/databases/sqlite.php @@ -175,8 +175,53 @@ SQL; */ public function backup_data() { - // @todo Implement Backup function - return ''; + // Get a list of all the objects + $sql = 'SELECT "name" FROM "sqlite_master"'; + $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 = implode("\n", $insert_rows); + } + + return $output_sql; } } //End of sqlite.php \ No newline at end of file