summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTom Needham <needham.thomas@gmail.com>2012-03-01 19:41:14 +0000
committerTom Needham <needham.thomas@gmail.com>2012-03-01 19:41:14 +0000
commit33c5b3a2efa8559a3e960e6d8256576aab604869 (patch)
tree2d57180bcee36986865d492e2d1d37ba717f3098 /lib
parent20b96787899906c596d4132fe98955f1639a9ff2 (diff)
downloadnextcloud-server-33c5b3a2efa8559a3e960e6d8256576aab604869.tar.gz
nextcloud-server-33c5b3a2efa8559a3e960e6d8256576aab604869.zip
Added replaceDB method in lib/db.php
Diffstat (limited to 'lib')
-rw-r--r--lib/db.php83
1 files changed, 74 insertions, 9 deletions
diff --git a/lib/db.php b/lib/db.php
index 9d3c20e0145..f43f00a397b 100644
--- a/lib/db.php
+++ b/lib/db.php
@@ -316,8 +316,11 @@ class OC_DB {
// read file
$content = file_get_contents( $file );
- // Make changes and save them to a temporary file
- $file2 = tempnam( get_temp_dir(), 'oc_db_scheme_' );
+ // Make changes and save them to an in-memory file
+ $file2 = 'static://db_scheme';
+ if($file2 == ''){
+ die('could not create tempfile in get_temp_dir() - aborting');
+ }
$content = str_replace( '*dbname*', $CONFIG_DBNAME, $content );
$content = str_replace( '*dbprefix*', $CONFIG_DBTABLEPREFIX, $content );
if( $CONFIG_DBTYPE == 'pgsql' ){ //mysql support it too but sqlite doesn't
@@ -328,7 +331,7 @@ class OC_DB {
// Try to create tables
$definition = self::$schema->parseDatabaseDefinitionFile( $file2 );
- // Delete our temporary file
+ //clean up memory
unlink( $file2 );
// Die in case something went wrong
@@ -368,8 +371,8 @@ class OC_DB {
return false;
}
- // Make changes and save them to a temporary file
- $file2 = tempnam( get_temp_dir(), 'oc_db_scheme_' );
+ // Make changes and save them to an in-memory file
+ $file2 = 'static://db_scheme';
$content = str_replace( '*dbname*', $previousSchema['name'], $content );
$content = str_replace( '*dbprefix*', $CONFIG_DBTABLEPREFIX, $content );
if( $CONFIG_DBTYPE == 'pgsql' ){ //mysql support it too but sqlite doesn't
@@ -378,7 +381,7 @@ class OC_DB {
file_put_contents( $file2, $content );
$op = self::$schema->updateDatabase($file2, $previousSchema, array(), false);
- // Delete our temporary file
+ //clean up memory
unlink( $file2 );
if (PEAR::isError($op)) {
@@ -390,6 +393,27 @@ class OC_DB {
}
/**
+ * @breif replaces the owncloud tables with a new set
+ */
+ public static function replaceDB( $file ){
+
+ // Delete the old tables
+ self::removeDBStructure( '/home/tom/sites/secure.tomneedham.com/public_html/migration/db_structure.xml' );
+
+ $apps = OC_App::getAllApps();
+ foreach($apps as $app){
+ $path = '/apps/'.$app.'/appinfo/database.xml';
+ if(file_exists($path)){
+ self::removeDBStructure( $path );
+ }
+ }
+
+ // Create new tables
+ self::createDBFromStructure( $file );
+
+ }
+
+ /**
* @brief connects to a MDB2 database scheme
* @returns true/false
*
@@ -483,6 +507,27 @@ class OC_DB {
}
/**
+ * @breif replaces the owncloud tables with a new set
+ */
+ public static function replaceDB( $file ){
+
+ // Delete the old tables
+ self::removeDBStructure( OC::$DOCUMENTROOT . 'db_structure.xml' );
+
+ $apps = OC_App::getAllApps();
+ foreach($apps as $app){
+ $path = '/apps/'.$app.'/appinfo/database.xml';
+ if(file_exists($path)){
+ self::removeDBStructure( $path );
+ }
+ }
+
+ // Create new tables
+ self::createDBFromStructure( $file );
+
+ }
+
+ /**
* Start a transaction
*/
public static function beginTransaction(){
@@ -505,6 +550,21 @@ class OC_DB {
self::$connection->commit();
self::$inTransaction=false;
}
+
+ /**
+ * check if a result is an error, works with MDB2 and PDOException
+ * @param mixed $result
+ * @return bool
+ */
+ public static function isError($result){
+ if(!$result){
+ return true;
+ }elseif(self::$backend==self::BACKEND_MDB2 and PEAR::isError($result)){
+ return true;
+ }else{
+ return false;
+ }
+ }
}
/**
@@ -524,11 +584,15 @@ class PDOStatementWrapper{
public function execute($input=array()){
$this->lastArguments=$input;
if(count($input)>0){
- $this->statement->execute($input);
+ $result=$this->statement->execute($input);
+ }else{
+ $result=$this->statement->execute();
+ }
+ if($result){
+ return $this;
}else{
- $this->statement->execute();
+ return false;
}
- return $this;
}
/**
@@ -567,3 +631,4 @@ class PDOStatementWrapper{
return $this->statement->fetchColumn($colnum);
}
}
+