From dec1a1714fd9ce0202b46f451fad54e14dedd48c Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 28 Feb 2012 16:02:37 -0500 Subject: [PATCH] added backup_structure and backup_data methods to db classes --- common/db_pdo.php | 15 ++++++++- databases/firebird.php | 58 ++++++++++++++++++++++++++++++++++ databases/mysql.php | 36 +++++++++++++++++++++ databases/odbc.php | 34 ++++++++++++++++++++ databases/pgsql.php | 40 +++++++++++++++++++++++ databases/sqlite.php | 38 ++++++++++++++++++++++ databases/sqlite_manip.php | 2 +- tests/core.php | 1 - tests/databases/mysql.php | 3 +- tests/databases/sqlite.php | 10 ++---- tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes tests/test_dbs/test_sqlite.db | Bin 4096 -> 2048 bytes 12 files changed, 224 insertions(+), 13 deletions(-) diff --git a/common/db_pdo.php b/common/db_pdo.php index ea6c102..168eb1a 100644 --- a/common/db_pdo.php +++ b/common/db_pdo.php @@ -183,7 +183,20 @@ abstract class DB_PDO extends PDO { * @return array */ abstract public function get_system_tables(); - + + /** + * Return an SQL file with the database table structure + * + * @return string + */ + abstract public function backup_structure(); + + /** + * Return an SQL file with the database data as insert statements + * + * @return string + */ + abstract public function backup_data(); } // ------------------------------------------------------------------------- diff --git a/databases/firebird.php b/databases/firebird.php index 0a46cae..250bb2a 100644 --- a/databases/firebird.php +++ b/databases/firebird.php @@ -35,6 +35,8 @@ class firebird extends DB_PDO { $class = __CLASS__."_manip"; $this->manip = new $class; } + + // -------------------------------------------------------------------------- /** * Close the link to the database @@ -44,6 +46,8 @@ class firebird extends DB_PDO { @ibase_close($this->conn); @ibase_free_result($this->statement); } + + // -------------------------------------------------------------------------- /** * Empty a database table @@ -57,6 +61,8 @@ class firebird extends DB_PDO { $this->query($sql); } + // -------------------------------------------------------------------------- + /** * Wrapper public function to better match PDO * @@ -70,6 +76,8 @@ class firebird extends DB_PDO { $this->statement = ibase_query($this->conn, $sql); return $this->statement; } + + // -------------------------------------------------------------------------- /** * Emulate PDO fetch public function @@ -94,6 +102,8 @@ class firebird extends DB_PDO { break; } } + + // -------------------------------------------------------------------------- /** * Emulate PDO fetchAll public function @@ -114,6 +124,8 @@ class firebird extends DB_PDO { return $all; } + + // -------------------------------------------------------------------------- /** * Emulate PDO prepare @@ -126,6 +138,8 @@ class firebird extends DB_PDO { $this->statement = ibase_prepare($this->conn, $query); return $this->statement; } + + // -------------------------------------------------------------------------- /** * List tables for the current database @@ -151,6 +165,8 @@ SQL; return $tables; } + + // -------------------------------------------------------------------------- /** * List system tables for the current database @@ -176,6 +192,8 @@ SQL; return $tables; } + + // -------------------------------------------------------------------------- /** * Return the number of rows affected by the previous query @@ -186,6 +204,8 @@ SQL; { return ibase_affected_rows($this->conn); } + + // -------------------------------------------------------------------------- /** * Return the number of rows returned for a SELECT query @@ -206,6 +226,8 @@ SQL; return count($this->result); } + // -------------------------------------------------------------------------- + /** * Start a database transaction * @@ -221,6 +243,8 @@ SQL; return FALSE; } + // -------------------------------------------------------------------------- + /** * Commit a database transaction * @@ -231,6 +255,8 @@ SQL; return ibase_commit($this->trans); } + // -------------------------------------------------------------------------- + /** * Rollback a transaction * @@ -241,6 +267,8 @@ SQL; return ibase_rollback($this->trans); } + // -------------------------------------------------------------------------- + /** * Run a prepared statement query * @@ -257,6 +285,8 @@ SQL; return call_user_func_array('ibase_execute', $args); } + // -------------------------------------------------------------------------- + /** * Prepare and execute a query * @@ -274,6 +304,8 @@ SQL; return $this->execute($args); } + // -------------------------------------------------------------------------- + /** * Bind a prepared query with arguments for executing * @@ -287,5 +319,31 @@ SQL; // the firebird database return FALSE; } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's structure + * + * @return string + */ + public function backup_structure() + { + // @todo Implement Backup function + return ''; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's data + * + * @return string + */ + public function backup_data() + { + // @todo Implement Backup function + return ''; + } } // End of firebird.php \ No newline at end of file diff --git a/databases/mysql.php b/databases/mysql.php index 6b84570..6ce88e7 100644 --- a/databases/mysql.php +++ b/databases/mysql.php @@ -39,6 +39,8 @@ class MySQL extends DB_PDO { $class = __CLASS__.'_manip'; $this->manip = new $class; } + + // -------------------------------------------------------------------------- /** * Empty a table @@ -50,6 +52,8 @@ class MySQL extends DB_PDO { $this->query("TRUNCATE `{$table}`"); } + // -------------------------------------------------------------------------- + /** * Get databases for the current connection * @@ -60,6 +64,8 @@ class MySQL extends DB_PDO { $res = $this->query("SHOW DATABASES"); return $this->fetchAll(PDO::FETCH_ASSOC); } + + // -------------------------------------------------------------------------- /** * Returns the tables available in the current database @@ -71,6 +77,8 @@ class MySQL extends DB_PDO { $res = $this->query("SHOW TABLES"); return $res->fetchAll(PDO::FETCH_ASSOC); } + + // -------------------------------------------------------------------------- /** * Returns system tables for the current database @@ -82,6 +90,8 @@ class MySQL extends DB_PDO { //MySQL doesn't have system tables return array(); } + + // -------------------------------------------------------------------------- /** * Return the number of rows returned for a SELECT query @@ -92,5 +102,31 @@ class MySQL extends DB_PDO { { return isset($this->statement) ? $this->statement->rowCount() : FALSE; } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's structure + * + * @return string + */ + public function backup_structure() + { + // @todo Implement Backup function + return ''; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's data + * + * @return string + */ + public function backup_data() + { + // @todo Implement Backup function + return ''; + } } //End of mysql.php \ No newline at end of file diff --git a/databases/odbc.php b/databases/odbc.php index 0a635f0..2bf58e0 100644 --- a/databases/odbc.php +++ b/databases/odbc.php @@ -28,6 +28,8 @@ class ODBC extends DB_PDO { $class = __CLASS__.'_manip'; $this->manip = new $class; } + + // -------------------------------------------------------------------------- /** * List tables for the current database @@ -39,6 +41,8 @@ class ODBC extends DB_PDO { //Not possible reliably with this driver return FALSE; } + + // -------------------------------------------------------------------------- /** * List system tables for the current database/connection @@ -50,6 +54,8 @@ class ODBC extends DB_PDO { //No way of determining for ODBC return array(); } + + // -------------------------------------------------------------------------- /** * Empty the current database @@ -61,6 +67,8 @@ class ODBC extends DB_PDO { $sql = "DELETE FROM {$table}"; $this->query($sql); } + + // -------------------------------------------------------------------------- /** * Return the number of rows returned for a SELECT query @@ -71,5 +79,31 @@ class ODBC extends DB_PDO { { // TODO: Implement } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's structure + * + * @return string + */ + public function backup_structure() + { + // Not applicable to ODBC + return ''; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's data + * + * @return string + */ + public function backup_data() + { + // Not applicable to ODBC + return ''; + } } // End of odbc.php \ No newline at end of file diff --git a/databases/pgsql.php b/databases/pgsql.php index ec2b96f..ba5eacc 100644 --- a/databases/pgsql.php +++ b/databases/pgsql.php @@ -35,6 +35,8 @@ class pgSQL extends DB_PDO { $class = __CLASS__.'_manip'; $this->manip = new $class; } + + // -------------------------------------------------------------------------- /** * Empty a table @@ -46,6 +48,8 @@ class pgSQL extends DB_PDO { $sql = 'TRUNCATE "' . $table . '"'; $this->query($sql); } + + // -------------------------------------------------------------------------- /** * Get list of databases for the current connection @@ -66,6 +70,8 @@ SQL; return $dbs; } + + // -------------------------------------------------------------------------- /** * Get the list of tables for the current db @@ -86,6 +92,8 @@ SQL; return $tables; } + + // -------------------------------------------------------------------------- /** * Get the list of system tables @@ -107,6 +115,8 @@ SQL; return $tables; } + + // -------------------------------------------------------------------------- /** * Get a list of schemas, either for the current connection, or @@ -136,6 +146,8 @@ SQL; return $schemas; } + + // -------------------------------------------------------------------------- /** * Get a list of views for the current db @@ -155,6 +167,8 @@ SQL; return $views; } + + // -------------------------------------------------------------------------- /** * Return the number of rows returned for a SELECT query @@ -165,5 +179,31 @@ SQL; { return (isset($this->statement)) ? $this->statement->rowCount : FALSE; } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's structure + * + * @return string + */ + public function backup_structure() + { + // @todo Implement Backup function + return ''; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's data + * + * @return string + */ + public function backup_data() + { + // @todo Implement Backup function + return ''; + } } //End of pgsql.php \ No newline at end of file diff --git a/databases/sqlite.php b/databases/sqlite.php index 49cb114..3762e0f 100644 --- a/databases/sqlite.php +++ b/databases/sqlite.php @@ -34,6 +34,8 @@ class SQLite extends DB_PDO { $class = __CLASS__."_manip"; $this->manip = new $class; } + + // -------------------------------------------------------------------------- /** * Empty a table @@ -52,6 +54,8 @@ class SQLite extends DB_PDO { $this->statement->execute(); } + + // -------------------------------------------------------------------------- /** * List tables for the current database @@ -77,6 +81,8 @@ SQL; return $tables; } + + // -------------------------------------------------------------------------- /** * List system tables for the current database @@ -89,6 +95,8 @@ SQL; // that is of any importance. return array('sqlite_master'); } + + // -------------------------------------------------------------------------- /** * Load a database for the current connection @@ -101,6 +109,8 @@ SQL; $sql = "ATTACH DATABASE '{$db}' AS \"{$name}\""; $this->query($sql); } + + // -------------------------------------------------------------------------- /** * Unload a database from the current connection @@ -117,6 +127,8 @@ SQL; $this->statement->execute(); } + + // -------------------------------------------------------------------------- /** * Return the number of rows returned for a SELECT query @@ -127,5 +139,31 @@ SQL; { return (isset($this->statement)) ? $this->statement->rowCount : FALSE; } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's structure + * + * @return string + */ + public function backup_structure() + { + // @todo Implement Backup function + return ''; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's data + * + * @return string + */ + public function backup_data() + { + // @todo Implement Backup function + return ''; + } } //End of sqlite.php \ No newline at end of file diff --git a/databases/sqlite_manip.php b/databases/sqlite_manip.php index b3cedd3..d8f622f 100644 --- a/databases/sqlite_manip.php +++ b/databases/sqlite_manip.php @@ -67,7 +67,7 @@ class SQLite_manip extends db_manip { } // Generate the sql for the creation of the table - $sql = "CREATE TABLE \"{$name}\" ("; + $sql = "CREATE TABLE IF NOT EXISTS \"{$name}\" ("; $sql .= implode(", ", $columns); $sql .= ")"; diff --git a/tests/core.php b/tests/core.php index 146278b..af31b1d 100644 --- a/tests/core.php +++ b/tests/core.php @@ -38,7 +38,6 @@ class CoreTest extends UnitTestCase { function TestPHPVersion() { $this->assertTrue(version_compare(PHP_VERSION, "5.2", "ge")); - $this->assertTrue(version_compare(PHP_VERSION, "5.4", "<")); } /** diff --git a/tests/databases/mysql.php b/tests/databases/mysql.php index c88e564..28e592c 100644 --- a/tests/databases/mysql.php +++ b/tests/databases/mysql.php @@ -29,8 +29,7 @@ class MySQLTest extends UnitTestCase { function __construct() { parent::__construct(); - - //$this->db = new MySQL(); + } } diff --git a/tests/databases/sqlite.php b/tests/databases/sqlite.php index 4c417d6..f683e04 100644 --- a/tests/databases/sqlite.php +++ b/tests/databases/sqlite.php @@ -19,12 +19,6 @@ */ class SQLiteTest extends UnitTestCase { - /** - * __construct function. - * - * @access public - * @return void - */ function __construct() { parent::__construct(); @@ -36,11 +30,11 @@ class SQLiteTest extends UnitTestCase { { $this->assertIsA($this->db, 'SQLite'); } - + function TestGetTables() { $tables = $this->db->get_tables(); - $this->assertTrue( ! empty($tables)); + $this->assertTrue(is_array($tables)); } function TestGetSystemTables() diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index 111bfce6e91db904e6d787ccd44d7d7ffe8d6650..f7adb982ffeef746212b4334b7e56678204a8122 100755 GIT binary patch delta 4270 zcmc&%e{3699sfT2-q}9?Op`QWseLXqHEojB=lrmZ3w2pM2^3D)x`9Y(mlm~D=~8;zy3s{~wG9#lsYsMxskBa;P#6hp;%7R(@7}$; z_`=_C(aYWEz4!fmKKFjz*Y&zquWO?O^67`Y;9Rj_T|joPcmN*20^lLf{sv$ydn2{e z!~iky;9re30>M)8%&ebyKKkvL6yD7+Ugxn)HL4vmAB_EO8#z8hD_``JxufH|y2#wj zv%B`aN%TgwT|e?=f6!20RgOd(weq0~IaKi+s(3F|Bx#zQK1S(R+tc15SoC}trm;+V z!Z3lQe(bw6MN0bby>wGS%fFSZLmjR{5$#n_H^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+ybWs(2-n1viiqj=0r!^_UZg__oaJ_<)-o;Ku?S!&K7X2S=B zy5J3&;n&!Dv-K&$8TJ)BVML8+F1a@_J776 z)h<$h?-lBgL-bn}SH0>iEjQV$cRL_&f;!Q#;H^?l7-su|_xlx^f^25k@T~`|&$Ho- z&kSeE)>r7UY_eWN(^{D}1V>7a$01xRFMN}eW-_fUzDrjH+t6;_FB{_{a8<}YgQ0^i z4EXT)LVy2A=(5<{(CU@mE>XFu?QQJ*ElTNMWW6*M0zAHnwX2}6ap{mt>n>ewYfV&D@jf`h$-*mJczsuRRaNu85qV0TB~fEZ)L0TVmPCyu zw+s6Ni%>+qi8qLzDjxAWt-m+&h#0Hl4Q^|LFn64L&pmggS>~_Iq6X&J!+&! zjr6FIerrd1r?o`yvfA`6YeZP|PHTyN!$pr8=}{v+YNSVv^uO*%@3fZaT~?dkWwq&@ z))M{CE_&2Rj~eMwBRy)Qzq=#7(^{f;S#5fk)uwk^i}Xp^&~uJfLyh#PksdYDqegnY z&}#6%OE#A-tX9*7)oS2_R{MXu+kCyAPpb4j!J>T1{3Oa>fLl4R6ys`Chs(7%bNS}R`3^G2|q%k%v-B)-UVNPG5no}0Jx{?_zAIiU5t diff --git a/tests/test_dbs/test_sqlite.db b/tests/test_dbs/test_sqlite.db index af74dbce18c6e150dd73e6cbb4c782953927d824..460d01c4ea39febad89b22e95daa60dd7c555dcb 100755 GIT binary patch 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 delta 182 zcmZn=Xi%6SE%=gwfq?~xnSq!Ih#ybXF&2Kwp!-OP7sz8|Udh0`lG&PBZDZkfCdTH8 zjv|bC6AQCqLmAn{MMW7Kc}o(La#BlDi%US5$vMc?F~n6N#L>yeRRJWTppl(gsSx5C z5u&3|mY7qT3T0=eD0upXxVpOrDFg(0`Z@+hDtNm_YJ%)!VA(9la+Y~wg9$4moVQtl JWgk!|696dsFCPE^