From 698837dd02dac9992a8600fbc57237844ab394ca Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 19 Apr 2012 08:59:10 -0400 Subject: [PATCH] Implemented backup data methods in mysql and pgsql drivers --- drivers/mysql/mysql_util.php | 47 +++++++++++++++++++++++++++-- drivers/pgsql/pgsql_util.php | 55 ++++++++++++++++++++++++++++++++-- tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 3 files changed, 97 insertions(+), 5 deletions(-) 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 83122eda73c023987676d6b16780adf71c9d6e82..5fdda5c57444e082ad62edbf8c6c9a33ab6b653b 100644 GIT binary patch delta 1081 zcmZ9KO=uHQ5Xa}`YqNQjzM=)a*wi#^j|qz6%@{9IP!Cl+Y3WTAf`+h^D7(7!Eh-*F zWDqYN#IyL(Uer^)DT3g|Q;q(aH8_9 z2)mb(emYiB3?~5Y(+?mep8WurJ^s~sa=;`gNYOnv6oQ`N#GA>v*Q0lpRzjD?{Ec z!*lyE%-YkiKVO6uy9nDB>HG-XvP0O21U9!q>l>SH8+uR8Bb2z^$ zj|mr_uF4q-KcldK)sC#!T-xMEN=$(OaR`8QI-zNGxZaHOfi=GC$aA8J*{-ZpIMb!x zO?)_z4!-ZoDbd0Xt<<=VIjl3>C+^FfMuO8Y913g1Y2d##;xr=86n@!~j<|u3)@6N? z>zKouISu@~E8NciHpWWu44{s-W+S@cnOYYI3Bio#PN)Y-X0V0 z3{9+Y9dlSShxKTDcljE`FzOW4y|{cg_A>Y;DOSl*F?#vy;i31^h#2i`NIT7|*VmWXDWz>E*UO{~zo2 B@0=-w0x z!XVzHh~7joy{I=YqUgnorzCBv6#H>d!IMt%c5K*P_6>jLi`7_dTV_l=vEyit7ZmP ztXa5d_brBWJ`Mw#ifEJm#=Yah<3xbKDrm)f_Tw#BuS@8gbknX9hoROGli;<#kz_;ymWC zW{!)$*5w;~+>+z?zAFz4z`tEt#^Y@{hW>_}8)?MI4NY#~*f%0KG`TTAzTWF_0^PRE z6*-yPV9n!X&A(TV!Ri`T$9LZeR@dXK4~+Ag9w+B9hc&m!nmK-q<7;byNO8 zQw#lZ7CDbOteL}lIJ(Aaj8oN|D&~F=r;5*L)@1tEc3^|9?GlPDS`W_SIk9FgYZY;L zO?eGHyt_qf=;0TtHo{)+-qZbgxU?+~Q4nuU`F|vH=J)OrZ|ulSw90z19lgxiC%f|O EKb1r5lmGw#