From 105a08660367fdff793307ddee354d88ec0af322 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 28 Feb 2012 20:44:14 -0500 Subject: [PATCH] Implemented quote and backup_data methods in firebird driver --- databases/firebird.php | 62 ++++++++++++++++++++++++++++++++-- databases/sqlite.php | 11 ++++-- tests/databases/firebird.php | 2 ++ tests/test_dbs/test_sqlite.db | Bin 2048 -> 2048 bytes 4 files changed, 71 insertions(+), 4 deletions(-) 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() { + ?>
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 460d01c4ea39febad89b22e95daa60dd7c555dcb..0f5588473109f3c6305ce13b21ab967f8ea495f5 100755 GIT binary patch delta 58 zcmZn=Xb_knEvUl4z`z8>j6hmwqK+}Y3WM$=C0-zpfrWvAdD3P<7IkLEJRo(Jd1Av{ E0O6Mjxc~qF literal 2048 zcmWFz^vNtqRY=P(%1ta$FlJz3U}R))P*7lCV2}V}CMaeE(mX&6qG14;4HHABB^Y!c z$@79lSr{0YC!tG^@<&5pGz2h0AWvDDu~EJxF)1fCxhOTUBsIPywYUU{$>bd5>KNjx z5aQ_MJ5aj9W7!;}C?HZ}0keyno5aJpUqM)NtmY4%! zYQpShe#d+d#vY|dLtr!nhC>MOF|ja+i7PbyBFmb G7Xtt^H#F%0