From a892b5893042cb0d7afba1ede008d4945f49d29e Mon Sep 17 00:00:00 2001 From: betateilchen <> Date: Sat, 19 Apr 2014 19:42:54 +0000 Subject: [PATCH] configDB - added new commands filelist and filedelete git-svn-id: https://svn.fhem.de/fhem/trunk@5571 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/98_configdb.pm | 64 ++++++++++++++++++++++++++++++++++++++-- fhem/configDB.pm | 60 ++++++++++++++++++++++++++----------- 3 files changed, 105 insertions(+), 20 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 161d83d79..8d2402a38 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,6 +1,7 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. - SVN + - feature: configDB: added commands filelist and filedelete - feature: configDB: added commands fileimport and fileexport - feature: 36_JeeLink: added LaCrosse, ETH200comfort, CUL_IR, HX2272 and FS20 modes from ulli diff --git a/fhem/FHEM/98_configdb.pm b/fhem/FHEM/98_configdb.pm index 1e102d1c9..f73297604 100644 --- a/fhem/FHEM/98_configdb.pm +++ b/fhem/FHEM/98_configdb.pm @@ -180,14 +180,54 @@ sub CommandConfigdb($$) { $ret = _cfgDB_Diff($param1, $param2); } + when ('filedelete') { + return "\n Syntax: configdb fileexport " if @a != 2; + my $filename; + if(substr($param1,0,1) eq '/') { + $filename = $param1; + } else { + $filename = $attr{global}{modpath}; + $filename .= "/$param1"; + } + $ret = _cfgDB_Filedelete $filename; + } + when ('fileexport') { return "\n Syntax: configdb fileexport " if @a != 2; - $ret = _cfgDB_Fileexport $param1; + my $filename; + if(substr($param1,0,1) eq '/') { + $filename = $param1; + } else { + $filename = $attr{global}{modpath}; + $filename .= "/$param1"; + } + if ( -w $filename ) { + $ret = _cfgDB_Fileexport $filename; + } else { + $ret = "\n Write error on file $filename"; + } } when ('fileimport') { return "\n Syntax: configdb fileimport " if @a != 2; - $ret = _cfgDB_Fileimport $param1; + my $filename; + if(substr($param1,0,1) eq '/') { + $filename = $param1; + } else { + $filename = $attr{global}{modpath}; + $filename .= "/$param1"; + } + if ( -r $filename ) { + $ret = _cfgDB_Fileimport $filename; + } elsif ( -e $filename) { + $ret = "\n Read error on file $filename"; + } else { + $ret = "\n File $filename not found."; + } + } + + when ('filelist') { + return _cfgDB_Filelist; } when ('info') { @@ -404,6 +444,11 @@ compare device: telnetPort in current version 0 (left) to version: 1 (right) The target file can be imported again, if needed.

+
  • configdb filedelete <Filename>

  • + Delete file from database.
    +
    +
    +
  • configdb fileexport <targetFilename>

  • Exports specified fhem file from database into filesystem. Example:
    @@ -420,6 +465,11 @@ compare device: telnetPort in current version 0 (left) to version: 1 (right)

    +
  • configdb filelist

  • + Show a list with all filenames stored in database.
    +
    +
    +
  • configdb info

  • Returns some database statistics
    @@ -663,6 +713,11 @@ compare device: telnetPort in current version 0 (left) to version: 1 (right)
     			Die Zieldatei kann später für die Wiederherstellung verwendet werden.

    +
  • configdb filedelete <Dateiname>

  • + Löscht eine gespeicherte Datei aus der Datenbank.
    +
    +
    +
  • configdb fileexport <zielDatei>

  • Schreibt die angegebene Datei aus der Datenbank in das Dateisystem. Beispiel:
    @@ -679,6 +734,11 @@ compare device: telnetPort in current version 0 (left) to version: 1 (right)

    +
  • configdb filelist

  • + Liefert eine Liste mit allen Namen der gespeicherten Dateien.
    +
    +
    +
  • configdb info

  • Liefert eine Datenbankstatistik
    diff --git a/fhem/configDB.pm b/fhem/configDB.pm
    index 48a8073b2..a347dd3e2 100644
    --- a/fhem/configDB.pm
    +++ b/fhem/configDB.pm
    @@ -618,19 +618,48 @@ sub _cfgDB_Diff($$) {
     	return $ret;
     }
     
    -sub _cfgDB_Fileimport($) {
    +sub _cfgDB_Filedelete($) {
    +	my ($filename) = @_;
    +	my $fhem_dbh = _cfgDB_Connect;
    +	my $ret = $fhem_dbh->do("delete from fhemfilesave where filename = '$filename'");
    +	$fhem_dbh->commit();
    +	$fhem_dbh->disconnect();
    +	if($ret > 0) {
    +		$ret = "File $filename deleted from database ($ret lines)";
    +	} else {
    +		$ret = "File $filename not found in database.";
    +	}
    +	return $ret;
    +}
    +
    +sub _cfgDB_Fileexport($) {
    +	my ($filename) = @_;
    +	my $counter = 0;
    +	my $fhem_dbh = _cfgDB_Connect;
    +	my $sth = $fhem_dbh->prepare( "SELECT * FROM fhemfilesave WHERE filename = '$filename'" );  
    +	$sth->execute();
    +	open( FILE, ">$filename" );
    +	while (my @line = $sth->fetchrow_array()) {
    +		$counter++;
    +		print FILE $line[1], "\n";
    +	}
    +	close ( FILE );
    +	$sth->finish();
    +	$fhem_dbh->disconnect();
    +	return "$counter lines written from database into file $filename";
    +}
    +
    +sub _cfgDB_Fileimport($) {
     	my ($filename) = @_;
    -	my $path = $attr{global}{modpath};
    -	$path .= "/$filename";
     	my $counter = 0;
     	my $fhem_dbh = _cfgDB_Connect;
    -	$fhem_dbh->do("delete from fhemfilesave where filename = '$path'");
     	my $sth = $fhem_dbh->prepare('INSERT INTO fhemfilesave values (?, ?)');
    -	open (in,"<$path") || die $!;
    +	$fhem_dbh->do("delete from fhemfilesave where filename = '$filename'");
    +	open (in,"<$filename") || die $!;
     	while (){
     		$counter++;
     		my $line = substr($_,0,length($_)-1);
    -		$sth->execute($path, $line);
    +		$sth->execute($filename, $line);
     	}
     	close in;
     	$sth->finish();
    @@ -639,23 +668,18 @@ sub _cfgDB_Fileimport($) {
     	return "$counter lines written from file $filename to database";
     }
     
    -sub _cfgDB_Fileexport($) {
    -	my ($filename) = @_;
    -	my $path = $attr{global}{modpath};
    -	$path .= "/$filename";
    -	my $counter = 0;
    +sub _cfgDB_Filelist {
    +	my $ret =	"Files found in database:\n".
    +						"------------------------------------------------------------\n";
     	my $fhem_dbh = _cfgDB_Connect;
    -	my $sth = $fhem_dbh->prepare( "SELECT * FROM fhemfilesave WHERE filename = '$path'" );  
    +	my $sth = $fhem_dbh->prepare( "SELECT filename FROM fhemfilesave group by filename order by filename" );  
     	$sth->execute();
    -	open( FILE, ">$path" );
    -	while (my @line = $sth->fetchrow_array()) {
    -		$counter++;
    -		print FILE $line[1], "\n";
    +	while (my $line = $sth->fetchrow_array()) {
    +		$ret .= "$line\n";
     	}
    -	close ( FILE );
     	$sth->finish();
     	$fhem_dbh->disconnect();
    -	return "$counter lines read from database into file $filename";
    +	return $ret;
     }
     
     1;