From 772c6e14a93afc31c348e26e41b7d2a1fa535d7c Mon Sep 17 00:00:00 2001 From: nasseeder1 Date: Thu, 17 Sep 2020 18:05:04 +0000 Subject: [PATCH] 93_DbRep: contrib 8.40.8 git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@22782 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- contrib/DS_Starter/93_DbRep.pm | 150 +++++++++++++++++++-------------- 1 file changed, 89 insertions(+), 61 deletions(-) diff --git a/contrib/DS_Starter/93_DbRep.pm b/contrib/DS_Starter/93_DbRep.pm index 47ed01166..ae51c5955 100644 --- a/contrib/DS_Starter/93_DbRep.pm +++ b/contrib/DS_Starter/93_DbRep.pm @@ -1,5 +1,5 @@ ########################################################################################################## -# $Id: 93_DbRep.pm 22678 2020-08-27 17:05:24Z DS_Starter $ +# $Id: 93_DbRep.pm 22733 2020-09-04 19:33:12Z DS_Starter $ ########################################################################################################## # 93_DbRep.pm # @@ -57,6 +57,7 @@ no if $] >= 5.017011, warnings => 'experimental::smartmatch'; # Version History intern our %DbRep_vNotesIntern = ( + "8.40.8" => "17.09.2020 sqlCmd supports PREPARE statament, commandRef revised ", "8.40.7" => "03.09.2020 rename getter dbValue to sqlCmdBlocking, consider attr timeout in function DbRep_sqlCmdBlocking (blocking function), commandRef revised ", "8.40.6" => "27.08.2020 commandRef revised ", "8.40.5" => "29.07.2020 fix crash if delEntries startet without any time limits, Forum:#113202 ", @@ -6189,17 +6190,20 @@ sub sqlCmd_DoParse { # only for this block because of warnings if details of readings are not set no warnings 'uninitialized'; - my $sql = ($cmd =~ m/\;$/)?$cmd:$cmd.";"; + $cmd =~ s/\;\;/ESC_ESC_ESC/gx; # ersetzen von escapeten ";" (;;) + + my $sql = ($cmd =~ m/\;$/) ? $cmd : $cmd.";"; # Set Session Variablen "SET" oder PRAGMA aus Attribut "sqlCmdVars" my $vars = AttrVal($name, "sqlCmdVars", ""); if ($vars) { @pms = split(";",$vars); - foreach my $pm (@pms) { + for my $pm (@pms) { if($pm !~ /PRAGMA|SET/i) { next; } $pm = ltrim($pm).";"; + $pm =~ s/ESC_ESC_ESC/;/gx; # wiederherstellen von escapeten ";" -> umwandeln von ";;" in ";" Log3($name, 4, "DbRep $name - Set VARIABLE or PRAGMA: $pm"); eval {$dbh->do($pm);}; if ($@) { @@ -6213,15 +6217,24 @@ sub sqlCmd_DoParse { # Abarbeitung von Session Variablen vor einem SQL-Statement # z.B. SET @open:=NULL, @closed:=NULL; Select ... - if($cmd =~ /^\s*SET.*;/i) { + if($cmd =~ /^\s*(SET|PREPARE).*;/i) { @pms = split(";",$cmd); - foreach my $pm (@pms) { - if($pm !~ /SET/i) { + for my $pm (@pms) { + + if($pm !~ /SET|PREPARE/i) { $sql = $pm.";"; next; } + $pm = ltrim($pm).";"; - Log3($name, 4, "DbRep $name - Set SQL session variable: $pm"); + $pm =~ s/ESC_ESC_ESC/;/gx; # wiederherstellen von escapeten ";" -> umwandeln von ";;" in ";" + + if($pm =~ /SET/i) { + Log3($name, 4, "DbRep $name - Set SQL session variable: $pm"); + } else { + Log3($name, 4, "DbRep $name - Exec PREPARE statement: $pm"); + } + eval {$dbh->do($pm);}; if ($@) { $err = encode_base64($@,""); @@ -6234,15 +6247,24 @@ sub sqlCmd_DoParse { # Abarbeitung aller Pragmas vor einem SQLite Statement, SQL wird extrahiert # wenn Pragmas im SQL vorangestellt sind - if($cmd =~ /^\s*PRAGMA.*;/i) { + if($cmd =~ /^\s*(PRAGMA|PREPARE).*;/i) { @pms = split(";",$cmd); - foreach my $pm (@pms) { - if($pm !~ /PRAGMA/i) { + for my $pm (@pms) { + + if($pm !~ /PRAGMA|PREPARE/i) { $sql = $pm.";"; next; } + $pm = ltrim($pm).";"; - Log3($name, 4, "DbRep $name - Exec PRAGMA Statement: $pm"); + $pm =~ s/ESC_ESC_ESC/;/gx; # wiederherstellen von escapeten ";" -> umwandeln von ";;" in ";" + + if($pm =~ /PRAGMA/i) { + Log3($name, 4, "DbRep $name - Exec PRAGMA Statement: $pm"); + } else { + Log3($name, 4, "DbRep $name - Exec PREPARE statement: $pm"); + } + eval {$dbh->do($pm);}; if ($@) { $err = encode_base64($@,""); @@ -6257,6 +6279,8 @@ sub sqlCmd_DoParse { $sql =~ s/§timestamp_begin§/'$runtime_string_first'/g; $sql =~ s/§timestamp_end§/'$runtime_string_next'/g; + $sql =~ s/ESC_ESC_ESC/;/gx; # wiederherstellen von escapeten ";" -> umwandeln von ";;" in ";" + Log3($name, 4, "DbRep $name - SQL execute: $sql"); # SQL-Startzeit @@ -6265,7 +6289,7 @@ sub sqlCmd_DoParse { my ($sth,$r); eval {$sth = $dbh->prepare($sql); - $r = $sth->execute(); + $r = $sth->execute(); }; if ($@) { @@ -6295,6 +6319,7 @@ sub sqlCmd_DoParse { # Anzahl der Datensätze $nrows++; } + } else { $nrows = $sth->rows; eval {$dbh->commit() if(!$dbh->{AutoCommit});}; @@ -6312,17 +6337,16 @@ sub sqlCmd_DoParse { $sth->finish; - # SQL-Laufzeit ermitteln - my $rt = tv_interval($st); + my $rt = tv_interval($st); # SQL-Laufzeit ermitteln $dbh->disconnect; - # Daten müssen als Einzeiler zurückgegeben werden - my $rowstring = join("§", @rows); - $rowstring = encode_base64($rowstring,""); - - # Background-Laufzeit ermitteln - my $brt = tv_interval($bst); + my $rowstring = join("§", @rows); # Daten müssen als Einzeiler zurückgegeben werden + $rowstring = encode_base64($rowstring,""); + + $cmd =~ s/ESC_ESC_ESC/;;/gx; # wiederherstellen der escapeten ";" -> ";;" + + my $brt = tv_interval($bst); # Background-Laufzeit ermitteln $rt = $rt.",".$brt; @@ -11499,12 +11523,12 @@ sub DbRep_setVersionInfo { if($modules{$type}{META}{x_prereqs_src} && !$hash->{HELPER}{MODMETAABSENT}) { # META-Daten sind vorhanden $modules{$type}{META}{version} = "v".$v; # Version aus META.json überschreiben, Anzeige mit {Dumper $modules{SMAPortal}{META}} - if($modules{$type}{META}{x_version}) { # {x_version} ( nur gesetzt wenn $Id: 93_DbRep.pm 22678 2020-08-27 17:05:24Z DS_Starter $ im Kopf komplett! vorhanden ) + if($modules{$type}{META}{x_version}) { # {x_version} ( nur gesetzt wenn $Id: 93_DbRep.pm 22733 2020-09-04 19:33:12Z DS_Starter $ im Kopf komplett! vorhanden ) $modules{$type}{META}{x_version} =~ s/1.1.1/$v/g; } else { $modules{$type}{META}{x_version} = $v; } - return $@ unless (FHEM::Meta::SetInternals($hash)); # FVERSION wird gesetzt ( nur gesetzt wenn $Id: 93_DbRep.pm 22678 2020-08-27 17:05:24Z DS_Starter $ im Kopf komplett! vorhanden ) + return $@ unless (FHEM::Meta::SetInternals($hash)); # FVERSION wird gesetzt ( nur gesetzt wenn $Id: 93_DbRep.pm 22733 2020-09-04 19:33:12Z DS_Starter $ im Kopf komplett! vorhanden ) if(__PACKAGE__ eq "FHEM::$type" || __PACKAGE__ eq $type) { # es wird mit Packages gearbeitet -> Perl übliche Modulversion setzen # mit {->VERSION()} im FHEMWEB kann Modulversion abgefragt werden @@ -12815,15 +12839,19 @@ return; The used database user needs the ALTER, CREATE and INDEX privilege.

- -
  • insert - use it to insert data ito table "history" manually. Input values for Date, Time and Value are mandatory. The database fields for Type and Event will be filled in with "manual" automatically and the values of Device, Reading will be get from set attributes.

    + + +
  • insert - data are inserted into table "history" manually. Input values for Date, Time and Value are + mandatory. The database fields for Type and Event will be filled in with "manual" automatically. + The values of device, reading use the + attribute settings .

      input format: Date,Time,Value,[Unit]
      - # Unit is optional, attributes of device, reading must be set !
      + # Unit is optional, attributes device, reading must be set !
      # If "Value=0" has to be inserted, use "Value = 0.0" to do it.

      - example: 2016-08-01,23:00:09,TestValue,TestUnit
      + example: set <name> insert 2016-08-01,23:00:09,12.03,kW

      # Spaces are NOT allowed in fieldvalues !

      @@ -13435,14 +13463,14 @@ return;
    • dbstatus - lists global information about MySQL server status (e.g. informations related to cache, threads, bufferpools, etc. ). - Initially all available informations are reported. Using the attribute "showStatus" the quantity of + Initially all available informations are reported. Using the attribute showStatus the quantity of results can be limited to show only the desired values. Further detailed informations of items meaning are - explained there.

      + explained here.

        Example
        - get <name> dbstatus
        - attr <name> showStatus %uptime%,%qcache%
        + attr <name> showStatus %uptime%,%qcache%
        + get <name> dbstatus
        # Only readings containing "uptime" and "qcache" in name will be created
    • @@ -13450,8 +13478,8 @@ return;
    • sqlCmdBlocking <SQL-statement> - - Executes the specified SQL-statement in blocking manner. - + Executes the specified SQL statement blocking with a default timeout of 10 seconds. + The timeout can be set with the attribute timeout.

        @@ -13468,7 +13496,7 @@ return; This command also accept the setting of SQL session variables like "SET @open:=NULL, @closed:=NULL;".
        If several fields are selected and passed back, the fieds are separated by the separator defined - by attribute "sqlResultFieldSep" (default "|"). Several result lines + by attribute sqlResultFieldSep (default "|"). Several result lines are separated by newline ("\n").
        This function only set/update status readings, the userExitFn function isn't called.

        @@ -13497,14 +13525,14 @@ sub dbval {
      • dbvars - lists global informations about MySQL system variables. Included are e.g. readings related to InnoDB-Home, datafile path, memory- or cache-parameter and so on. The Output reports initially all available informations. Using the - attribute "showVariables" the quantity of results can be limited to show only the desired values. + attribute showVariables the quantity of results can be limited to show only the desired values. Further detailed informations of items meaning are explained - there.

        + here.

          Example
          - get <name> dbvars
          - attr <name> showVariables %version%,%query_cache%
          + attr <name> showVariables %version%,%query_cache%
          + get <name> dbvars
          # Only readings containing "version" and "query_cache" in name will be created
      • @@ -13526,7 +13554,7 @@ sub dbval { (table row "PROGRESS"). So you can track, for instance, the degree of processing during an index creation.
        Further informations can be found - there.
        + here.


        @@ -13538,14 +13566,14 @@ sub dbval {
      • svrinfo - Common database server informations, e.g. DBMS-version, server address and port and so on. The quantity of elements to get depends - on the database type. Using the attribute "showSvrInfo" the quantity of results can be limited to show only + on the database type. Using the attribute showSvrInfo the quantity of results can be limited to show only the desired values. Further detailed informations of items meaning are explained - there.

        + here.

          Example
          - get <name> svrinfo
          - attr <name> showSvrInfo %SQL_CATALOG_TERM%,%NAME%
          + attr <name> showSvrInfo %SQL_CATALOG_TERM%,%NAME%
          + get <name> svrinfo
          # Only readings containing "SQL_CATALOG_TERM" and "NAME" in name will be created
      • @@ -13554,14 +13582,14 @@ sub dbval {
      • tableinfo - Access detailed informations about tables in MySQL database which is connected by the DbRep-device. All available tables in the connected database will be selected by default. - Using theattribute "showTableInfo" the results can be limited to tables you want to show. + Using the attribute showTableInfo the results can be limited to tables you want to show. Further detailed informations of items meaning are explained - there.

        + here.

          Example
          - get <name> tableinfo
          - attr <name> showTableInfo current,history
          + attr <name> showTableInfo current,history
          + get <name> tableinfo
          # Only informations related to tables "current" and "history" are going to be created
      • @@ -15430,16 +15458,18 @@ sub bdump { Der verwendete Datenbank-Nutzer benötigt das ALTER, CREATE und INDEX Privileg.

        - +
      • insert - Manuelles Einfügen eines Datensatzes in die Tabelle "history". Obligatorisch sind Eingabewerte für Datum, Zeit und Value. - Die Werte für die DB-Felder Type bzw. Event werden mit "manual" gefüllt, sowie die Werte für Device, Reading aus den gesetzten Attributen genommen.

        + Die Werte für die DB-Felder Type bzw. Event werden mit "manual" gefüllt, sowie die Werte für + Device, Reading aus den gesetzten Attributen + genommen.

          Eingabeformat: Datum,Zeit,Value,[Unit]
          - # Unit ist optional, Attribute "reading" und "device" müssen gesetzt sein
          + # Unit ist optional, Attribute device, reading müssen gesetzt sein
          # Soll "Value=0" eingefügt werden, ist "Value = 0.0" zu verwenden.

          - Beispiel: 2016-08-01,23:00:09,TestValue,TestUnit
          + Beispiel: set <name> insert 2016-08-01,23:00:09,12.03,kW
          # Es sind KEINE Leerzeichen im Feldwert erlaubt !

          @@ -16075,7 +16105,7 @@ sub bdump {
        • dbstatus - Listet globale Informationen zum MySQL Serverstatus (z.B. Informationen zum Cache, Threads, Bufferpools, etc. ). - Es werden zunächst alle verfügbaren Informationen berichtet. Mit dem Attribut "showStatus" kann die + Es werden zunächst alle verfügbaren Informationen berichtet. Mit dem Attribut showStatus kann die Ergebnismenge eingeschränkt werden, um nur gewünschte Ergebnisse abzurufen. Detailinformationen zur Bedeutung der einzelnen Readings sind hier verfügbar.

          @@ -16091,6 +16121,7 @@ sub bdump {
        • sqlCmdBlocking <SQL-Statement> - Führt das angegebene SQL-Statement blockierend mit einem Standardtimeout von 10 Sekunden aus. + Der Timeout kann mit dem Attribut timeout eingestellt werden.

            @@ -16100,17 +16131,14 @@ sub bdump { get <name> sqlCmdBlocking select device,count(*) from history where timestamp > '2018-04-01' group by device
        • -
          - - Der Timeout kann mit dem Attribut timeout verändert werden. -
          +
          Diese Funktion ist durch ihre Arbeitsweise speziell für den Einsatz in benutzerspezifischen Scripten geeignet.
          Die Eingabe akzeptiert Mehrzeiler und gibt ebenso mehrzeilige Ergebisse zurück. Dieses Kommando akzeptiert ebenfalls das Setzen von SQL Session Variablen wie z.B. "SET @open:=NULL, @closed:=NULL;".
          Werden mehrere Felder selektiert und zurückgegeben, erfolgt die Feldtrennung mit dem Trenner - des Attributes "sqlResultFieldSep" (default "|"). Mehrere Ergebniszeilen + des Attributs sqlResultFieldSep (default "|"). Mehrere Ergebniszeilen werden mit Newline ("\n") separiert.
          Diese Funktion setzt/aktualisiert nur Statusreadings, die Funktion im Attribut "userExitFn" wird nicht aufgerufen. @@ -16140,8 +16168,8 @@ sub dbval {
        • dbvars - Zeigt die globalen Werte der MySQL Systemvariablen. Enthalten sind zum Beispiel Angaben zum InnoDB-Home, dem Datafile-Pfad, - Memory- und Cache-Parameter, usw. Die Ausgabe listet zunächst alle verfügbaren Informationen auf. Mit dem - Attribut "showVariables" kann die Ergebnismenge eingeschränkt werden um nur gewünschte Ergebnisse + Memory- und Cache-Parameter, usw. Die Ausgabe listet zunächst alle verfügbaren Informationen auf. Mit dem Attribut + showVariables kann die Ergebnismenge eingeschränkt werden um nur gewünschte Ergebnisse abzurufen. Weitere Informationen zur Bedeutung der ausgegebenen Variablen sind hier verfügbar.

          @@ -16182,9 +16210,9 @@ sub dbval {
        • svrinfo - allgemeine Datenbankserver-Informationen wie z.B. die DBMS-Version, Serveradresse und Port usw. Die Menge der Listenelemente - ist vom Datenbanktyp abhängig. Mit dem Attribut "showSvrInfo" kann die Ergebnismenge eingeschränkt werden. + ist vom Datenbanktyp abhängig. Mit dem Attribut showSvrInfo kann die Ergebnismenge eingeschränkt werden. Weitere Erläuterungen zu den gelieferten Informationen sind - hier zu finden.

          + hier zu finden.

            Beispiel
            @@ -16198,7 +16226,7 @@ sub dbval {
          • tableinfo - ruft Tabelleninformationen aus der mit dem DbRep-Device verbundenen Datenbank ab (MySQL). Es werden per default alle in der verbundenen Datenbank angelegten Tabellen ausgewertet. - Mit dem Attribut "showTableInfo" können die Ergebnisse eingeschränkt werden. Erläuterungen zu den erzeugten + Mit dem Attribut showTableInfo können die Ergebnisse eingeschränkt werden. Erläuterungen zu den erzeugten Readings sind hier zu finden.