2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-30 12:07:09 +00:00

configDB - started migration to generic filehandling

git-svn-id: https://svn.fhem.de/fhem/trunk@5666 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
betateilchen 2014-04-26 10:47:09 +00:00
parent d912dd4da0
commit 7a8c8abd26

View File

@ -67,11 +67,14 @@
# #
# 2014-04-23 - added command fileshow, filemove # 2014-04-23 - added command fileshow, filemove
# #
# 2014-04-26 - added migration to generic file handling
#
############################################################################## ##############################################################################
# #
use DBI; use DBI;
use Data::Dumper; #use Data::Dumper; # for debugging only
################################################## ##################################################
# Forward declarations for functions in fhem.pl # Forward declarations for functions in fhem.pl
# #
@ -175,13 +178,15 @@ sub cfgDB_Init {
return; return;
} }
# read and set attributes for 'global' # read attributes
sub cfgDB_AttrRead($) { sub cfgDB_AttrRead($) {
my ($readSpec) = @_; my ($readSpec) = @_;
my($row, @line,@rets); my ($row, $sql, @line, @rets);
my $fhem_dbh = _cfgDB_Connect; my $fhem_dbh = _cfgDB_Connect;
my $uuid = $fhem_dbh->selectrow_array('SELECT versionuuid FROM fhemversions WHERE version = 0'); my $uuid = $fhem_dbh->selectrow_array('SELECT versionuuid FROM fhemversions WHERE version = 0');
my $sth = $fhem_dbh->prepare( "SELECT * FROM fhemconfig WHERE (DEVICE = 'global' OR DEVICE = 'configdb') and VERSIONUUID = '$uuid'" ); $sql = "SELECT * FROM fhemconfig WHERE DEVICE = '$readSpec' AND VERSIONUUID = '$uuid'";
$sql = "SELECT * FROM fhemconfig WHERE (DEVICE = 'global' OR DEVICE = 'configdb') and VERSIONUUID = '$uuid'" if($readSpec eq 'global');
my $sth = $fhem_dbh->prepare( $sql );
$sth->execute(); $sth->execute();
while (@line = $sth->fetchrow_array()) { while (@line = $sth->fetchrow_array()) {
$row = "$line[1],$line[2],$line[3]"; $row = "$line[1],$line[2],$line[3]";
@ -191,35 +196,55 @@ sub cfgDB_AttrRead($) {
return @rets; return @rets;
} }
sub cfgDB_GlobalAttr { ### work in process ###################
my (@line, $row); # create generice read and write function
# for filehandling to be used from fhem.pl
# and other fhem modules
sub cfgDB_FileRead($) {
my ($filename) = @_;
my $fhem_dbh = _cfgDB_Connect;
my $sth = $fhem_dbh->prepare( "SELECT line FROM fhemfilesave WHERE filename LIKE '$filename'" );
$sth->execute();
my @outfile;
while (my @line = $sth->fetchrow_array()) {
push @outfile, "$line[0]";
}
$sth->finish();
$fhem_dbh->disconnect();
return (int(@outfile)) ? @outfile : undef;
}
sub cfgDB_FileWrite($@) {
my ($filename,@content) = @_;
my $fhem_dbh = _cfgDB_Connect; my $fhem_dbh = _cfgDB_Connect;
my $uuid = $fhem_dbh->selectrow_array('SELECT versionuuid FROM fhemversions WHERE version = 0'); $fhem_dbh->do("delete from fhemfilesave where filename = '$filename'");
my $sth = $fhem_dbh->prepare( "SELECT * FROM fhemconfig WHERE DEVICE = ( ? ) and VERSIONUUID = '$uuid'" ); my $sth = $fhem_dbh->prepare('INSERT INTO fhemfilesave values (?, ?)');
foreach (@content){
$sth->execute('global'); $sth->execute($filename,rtrim($_));
while (@line = $sth->fetchrow_array()) {
$row = "$line[0] $line[1] $line[2] $line[3]";
$line[3] =~ s/#.*//;
$line[3] =~ s/ .*$//;
$attr{global}{$line[2]} = $line[3];
GlobalAttr("set", "global", $line[2], $line[3]);
} }
$sth->finish();
$sth->execute('configdb'); $fhem_dbh->commit();
while (@line = $sth->fetchrow_array()) {
$row = "$line[0] $line[1] $line[2] $line[3]";
$line[3] =~ s/#.*//;
$line[3] =~ s/ .*$//;
$attr{configdb}{$line[2]} = $line[3];
GlobalAttr("set", "global", $line[2], $line[3]);
}
$fhem_dbh->disconnect(); $fhem_dbh->disconnect();
return; return;
} }
sub cfgDB_FileUpdate($) {
my ($filename) = @_;
my $fhem_dbh = _cfgDB_Connect;
my $id = $fhem_dbh->selectrow_array("SELECT filename from fhemfilesave where filename = '$filename'");
$fhem_dbh->disconnect();
if($id) {
_cfgDB_Fileimport($filename,1) if $id;
Log 5, "file $filename updated in configDB";
}
return "";
}
#
#######################################
# read and execute all commands from # read and execute all commands from
# fhemconfig and fhemstate # fhemconfig and fhemstate
sub cfgDB_ReadAll($){ sub cfgDB_ReadAll($){
@ -375,7 +400,7 @@ sub cfgDB_FW_fileList(@$) {
sub cfgDB_Read99() { sub cfgDB_Read99() {
my $ret; my $ret;
my $fhem_dbh = _cfgDB_Connect; my $fhem_dbh = _cfgDB_Connect;
my $sth = $fhem_dbh->prepare( "SELECT filename FROM fhemfilesave WHERE filename like '%/99_%.pm' group by filename" ); my $sth = $fhem_dbh->prepare( "SELECT filename FROM fhemfilesave WHERE filename like '%/99_%.pm' group by filename" );
$sth->execute(); $sth->execute();
while (my $line = $sth->fetchrow_array()) { while (my $line = $sth->fetchrow_array()) {
$line =~ m,^(.*)/([^/]*)$,; # Split into dir and file $line =~ m,^(.*)/([^/]*)$,; # Split into dir and file
@ -499,7 +524,7 @@ sub _cfgDB_Uuid{
# not called from fhem.pl directly # not called from fhem.pl directly
# #
# migrate existing fhem config into database # migrate existing fhem config into database
sub _cfgDB_Migrate { sub _cfgDB_Migrate {
my $ret; my $ret;
$ret = "Starting migration...\n"; $ret = "Starting migration...\n";
@ -520,7 +545,7 @@ sub _cfgDB_Migrate {
} }
# show database statistics # show database statistics
sub _cfgDB_Info { sub _cfgDB_Info {
my ($l, @r, @row_ary, $f); my ($l, @r, @row_ary, $f);
for my $i (1..65){ $l .= '-';} for my $i (1..65){ $l .= '-';}
@ -587,7 +612,7 @@ sub _cfgDB_Info {
return join("\n", @r); return join("\n", @r);
} }
# recover former config from database archive # recover former config from database archive
sub _cfgDB_Recover($) { sub _cfgDB_Recover($) {
my ($version) = @_; my ($version) = @_;
my ($cmd, $count, $ret); my ($cmd, $count, $ret);
@ -630,7 +655,7 @@ sub _cfgDB_Recover($) {
return $ret; return $ret;
} }
# delete old configurations # delete old configurations
sub _cfgDB_Reorg(;$) { sub _cfgDB_Reorg(;$) {
my ($lastversion) = @_; my ($lastversion) = @_;
$lastversion = ($lastversion > 0) ? $lastversion : 3; $lastversion = ($lastversion > 0) ? $lastversion : 3;
@ -643,7 +668,7 @@ sub _cfgDB_Reorg(;$) {
return " Result after database reorg:\n"._cfgDB_Info; return " Result after database reorg:\n"._cfgDB_Info;
} }
# list device(s) from given version # list device(s) from given version
sub _cfgDB_List(;$$) { sub _cfgDB_List(;$$) {
my ($search,$searchversion) = @_; my ($search,$searchversion) = @_;
$search = $search ? $search : "%"; $search = $search ? $search : "%";
@ -665,7 +690,7 @@ sub _cfgDB_List(;$$) {
return $ret; return $ret;
} }
# called from cfgDB_Diff # called from cfgDB_Diff
sub __cfgDB_Diff($$$) { sub __cfgDB_Diff($$$) {
my ($fhem_dbh,$search,$searchversion) = @_; my ($fhem_dbh,$search,$searchversion) = @_;
my ($sql, $sth, @line, $ret); my ($sql, $sth, @line, $ret);
@ -679,7 +704,7 @@ sub __cfgDB_Diff($$$) {
return $ret; return $ret;
} }
# compare device configurations from 2 versions # compare device configurations from 2 versions
sub _cfgDB_Diff($$) { sub _cfgDB_Diff($$) {
my ($search,$searchversion) = @_; my ($search,$searchversion) = @_;
use Text::Diff; use Text::Diff;
@ -694,6 +719,7 @@ sub _cfgDB_Diff($$) {
return $ret; return $ret;
} }
##################################################
# functions used for file handling # functions used for file handling
# #
# delete file from database # delete file from database
@ -768,38 +794,61 @@ sub _cfgDB_Filelist(;$) {
return $ret; return $ret;
} }
# read a file from database and return content as string
sub _cfgDB_Readfile($) {
my ($filename) = @_;
my $fhem_dbh = _cfgDB_Connect;
my $sth = $fhem_dbh->prepare( "SELECT line FROM fhemfilesave WHERE filename LIKE '$filename'" );
$sth->execute();
my @outfile;
while (my @line = $sth->fetchrow_array()) {
push @outfile, "$line[0]";
}
$sth->finish();
$fhem_dbh->disconnect();
return (int(@outfile)) ? join("\n",@outfile) : undef;
}
# write content to file #######################################
sub _cfgDB_Writefile($$) { #
my ($filename,$content) = @_; # DEPRECATED functions
my @c = split(/\n/,$content); # will be removed 2014-06-15
#
#######################################
# replaced by cfgDB_AttrRead()
sub cfgDB_GlobalAttr {
my (@line, $row);
my $fhem_dbh = _cfgDB_Connect; my $fhem_dbh = _cfgDB_Connect;
$fhem_dbh->do("delete from fhemfilesave where filename = '$filename'"); my $uuid = $fhem_dbh->selectrow_array('SELECT versionuuid FROM fhemversions WHERE version = 0');
my $sth = $fhem_dbh->prepare('INSERT INTO fhemfilesave values (?, ?)'); my $sth = $fhem_dbh->prepare( "SELECT * FROM fhemconfig WHERE DEVICE = ( ? ) and VERSIONUUID = '$uuid'" );
foreach (@c){
$sth->execute($filename,rtrim($_)); $sth->execute('global');
while (@line = $sth->fetchrow_array()) {
$row = "$line[0] $line[1] $line[2] $line[3]";
$line[3] =~ s/#.*//;
$line[3] =~ s/ .*$//;
$attr{global}{$line[2]} = $line[3];
GlobalAttr("set", "global", $line[2], $line[3]);
} }
$sth->finish();
$fhem_dbh->commit(); $sth->execute('configdb');
while (@line = $sth->fetchrow_array()) {
$row = "$line[0] $line[1] $line[2] $line[3]";
$line[3] =~ s/#.*//;
$line[3] =~ s/ .*$//;
$attr{configdb}{$line[2]} = $line[3];
GlobalAttr("set", "global", $line[2], $line[3]);
}
$fhem_dbh->disconnect(); $fhem_dbh->disconnect();
return; return;
} }
# deprecated - replaced by cfgDB_FileRead()
sub _cfgDB_Readfile($) {
my ($filename) = @_;
my @outfile = cfgDB_FileRead($filename);
return (int(@outfile)) ? join("\n",@outfile) : undef;
}
# deprecated - replaced by cfgDB_FileWrite()
sub _cfgDB_Writefile($$) {
my ($filename,$content) = @_;
my @c = split(/\n/,$content);
cfgDB_FileWrite($filename,@c);
return;
}
# deprecated - replaced by cfgDB_FileUpdate()
sub _cfgDB_Updatefile($) { sub _cfgDB_Updatefile($) {
my ($filename) = @_; my ($filename) = @_;
my $fhem_dbh = _cfgDB_Connect; my $fhem_dbh = _cfgDB_Connect;