diff --git a/fhem/configDB.pm b/fhem/configDB.pm index 41c0f222b..a6e466133 100644 --- a/fhem/configDB.pm +++ b/fhem/configDB.pm @@ -37,6 +37,12 @@ # - updated commandref (EN) documentation # - added commandref (DE) documentation # +# 2014-03-03 - changed performance optimized by using version uuid table +# - updated commandref docu for migration +# - added cfgDB_svnId for fhem.pl CommandVersion +# - added cfgDB_List to show device info from database +# - updated commandref docu for cfgDB_List +# ############################################################################## # @@ -81,11 +87,10 @@ if($cfgDB_dbconn =~ m/pg:/i) { $cfgDB_dbtype = "unknown"; } -sub cfgDB_svnId { return "# ".'$Id$' } +sub cfgDB_svnId { + return "# ".'$Id$' +} -################################################## -# Connect to database and return handle -# sub cfgDB_Connect { my $fhem_dbh = DBI->connect( "dbi:$cfgDB_dbconn", @@ -159,19 +164,21 @@ sub cfgDB_Info { $r .= " dbpass: $cfgDB_dbpass\n"; $r .= " dbtype: $cfgDB_dbtype\n"; $r .= " Unknown dbmodel type in configuration file.\n" if $dbtype eq 'unknown'; - $r .= " Only Mysql, Postgresql, Oracle, SQLite are fully supported.\n" if $dbtype eq 'unknown'; + $r .= " Only Mysql, Postgresql, SQLite are fully supported.\n" if $dbtype eq 'unknown'; $r .= $l; my $fhem_dbh = cfgDB_Connect; - my ($sth, @line, $row); + my ($sql, $sth, @line, $row); # read versions table statistics my $count; $count = $fhem_dbh->selectrow_array('SELECT count(*) FROM fhemconfig'); $r .= " fhemconfig: $count entries\n\n"; + # read versions creation time -# $sth = $fhem_dbh->prepare( "SELECT * FROM fhemconfig WHERE COMMAND ='#created' ORDER by VERSION" ); - $sth = $fhem_dbh->prepare( "SELECT * FROM fhemconfig as c join fhemversions as v on v.versionuuid=c.versionuuid WHERE COMMAND like '#created%' ORDER by v.VERSION" ); + $sql = "SELECT * FROM fhemconfig as c join fhemversions as v on v.versionuuid=c.versionuuid ". + "WHERE COMMAND like '#created%' ORDER by v.VERSION"; + $sth = $fhem_dbh->prepare( $sql ); $sth->execute(); while (@line = $sth->fetchrow_array()) { $row = " Ver $line[6] saved: $line[1] $line[2] $line[3] def: ". @@ -385,14 +392,10 @@ sub cfgDB_ReadCfg(@) { my $fhem_dbh = cfgDB_Connect; my ($sth, @line, $row); - my $uuid = $fhem_dbh->selectrow_array('SELECT VERSIONUUID FROM fhemversions WHERE VERSION = 0'); - if($uuid){ -Debug("V0 from uuid: $uuid"); - $sth = $fhem_dbh->prepare( "SELECT * FROM fhemconfig WHERE VERSIONUUID = '$uuid'" ); - } else { -Debug("V0 from V0"); - $sth = $fhem_dbh->prepare( "SELECT * FROM fhemconfig WHERE VERSION = 0" ); - } +# using a join would be much nicer, but does not work due to sort of join's result + my $uuid = $fhem_dbh->selectrow_array('SELECT versionuuid FROM fhemversions WHERE version = 0'); + $sth = $fhem_dbh->prepare( "SELECT * FROM fhemconfig WHERE versionuuid = '$uuid'" ); + $sth->execute(); while (@line = $sth->fetchrow_array()) { $row = "$line[0] $line[1] $line[2] $line[3]"; @@ -466,19 +469,22 @@ sub cfgDB_Migrate { } -sub cfgDB_List($) { - my ($search) = @_; +sub cfgDB_List(;$$) { + my ($search,$searchversion) = @_; + $search = $search ? $search : "%"; + $searchversion = $searchversion ? $searchversion : 0; my $fhem_dbh = cfgDB_Connect; - my ($sth, @line, $row, @result, $ret); - - $sth = $fhem_dbh->prepare( "SELECT * FROM fhemconfig WHERE VERSION = 0 AND DEVICE like '$search'" ); + my ($sql, $sth, @line, $row, @result, $ret); + $sql = "SELECT command, device, p1, p2 FROM fhemconfig as c join fhemversions as v ON v.versionuuid=c.versionuuid ". + "WHERE v.version = '$searchversion' AND command not like '#create%' AND device like '$search%' ORDER BY lower(device),command DESC"; + $sth = $fhem_dbh->prepare( $sql); $sth->execute(); + push @result, "search result for device: $search in version: $searchversion"; + push @result, "--------------------------------------------------------------------------------"; while (@line = $sth->fetchrow_array()) { $row = "$line[0] $line[1] $line[2] $line[3]"; push @result, "$row"; } - $row = $fhem_dbh->do("select lower(hex(randomblob(16)))"); - push @result, "$row"; $fhem_dbh->disconnect(); $ret = join("\n", @result); return $ret; @@ -576,7 +582,9 @@ sub cfgDB_List($) {
perl fhem.pl fhem.cfg
{use configDB;; cfgDB_Migrate}
into frontend's command line{use configDB;; cfgDB_Migrate}
{cfgDB_List [device],[version]}
{cfgDB_List}
{cfgDB_List 'global'}
{cfgDB_List '',1}
{cfgDB_List 'global',1}
{cfgDB_Reorg [keep]}
perl fhem.pl fhem.cfg
{use configDB;; cfgDB_Migrate}
in die Befehlszeile der fhem-Oberfläche eingeben{use configDB;; cfgDB_Migrate}
{cfgDB_List [device],[version]}
{cfgDB_List}
{cfgDB_List 'global'}
{cfgDB_List '',1}
{cfgDB_List 'global',1}
{cfgDB_Reorg [keep]}