From: Nick Porter Date: Wed, 14 Feb 2024 11:39:39 +0000 (+0000) Subject: Ensure that sqlcounter queries return 0 if there are no matching records X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d13980810c8cf7bc4fb7d429dafd5a7a295419c3;p=thirdparty%2Ffreeradius-server.git Ensure that sqlcounter queries return 0 if there are no matching records --- diff --git a/raddb/mods-config/sql/counter/mysql/dailycounter.conf b/raddb/mods-config/sql/counter/mysql/dailycounter.conf index 0224659f0ea..80bec465bac 100644 --- a/raddb/mods-config/sql/counter/mysql/dailycounter.conf +++ b/raddb/mods-config/sql/counter/mysql/dailycounter.conf @@ -5,7 +5,7 @@ # below # query = "\ - SELECT SUM(acctsessiontime - GREATEST((%{control.dailycounter-reset-start} - UNIX_TIMESTAMP(acctstarttime)), 0)) \ + SELECT IFNULL(SUM(acctsessiontime - GREATEST((%{control.dailycounter-reset-start} - UNIX_TIMESTAMP(acctstarttime)), 0)), 0) \ FROM radacct \ WHERE username = '%{${key}}' \ AND UNIX_TIMESTAMP(acctstarttime) + acctsessiontime > '%{control.dailycounter-reset-start}'" @@ -16,7 +16,7 @@ query = "\ # is a little easier on the SQL server # #query = "\ -# SELECT SUM(acctsessiontime) \ +# SELECT IFNULL(SUM(acctsessiontime), 0) \ # FROM radacct \ # WHERE username = '%{${key}}' \ # AND acctstarttime > FROM_UNIXTIME('%{control.dailycounter-reset-start}')" @@ -27,7 +27,7 @@ query = "\ # timestamp for the end of the period # #query = "\ -# SELECT SUM(acctsessiontime) \ +# SELECT IFNULL(SUM(acctsessiontime), 0) \ # FROM radacct \ # WHERE username = '%{${key}}' \ # AND acctstarttime BETWEEN FROM_UNIXTIME('%{control.dailycounter-reset-start}') AND FROM_UNIXTIME('%{control.dailycounter-reset-end}')" diff --git a/raddb/mods-config/sql/counter/mysql/monthlycounter.conf b/raddb/mods-config/sql/counter/mysql/monthlycounter.conf index c5bbc23c3da..92364a4d4bd 100644 --- a/raddb/mods-config/sql/counter/mysql/monthlycounter.conf +++ b/raddb/mods-config/sql/counter/mysql/monthlycounter.conf @@ -5,7 +5,7 @@ # below # query = "\ - SELECT SUM(acctsessiontime - GREATEST((%{control.monthlycounter-reset-start} - UNIX_TIMESTAMP(acctstarttime)), 0)) \ + SELECT IFNULL(SUM(acctsessiontime - GREATEST((%{control.monthlycounter-reset-start} - UNIX_TIMESTAMP(acctstarttime)), 0)), 0) \ FROM radacct \ WHERE username='%{${key}}' \ AND UNIX_TIMESTAMP(acctstarttime) + acctsessiontime > '%{control.monthlycounter-reset-start}'" @@ -16,7 +16,7 @@ query = "\ # is a little easier on the SQL server # #query = "\ -# SELECT SUM(acctsessiontime) \ +# SELECT IFNULL(SUM(acctsessiontime), 0) \ # FROM radacct\ # WHERE username='%{${key}}' \ # AND acctstarttime > FROM_UNIXTIME('%{control.monthlycounter-reset-start}')" @@ -27,7 +27,7 @@ query = "\ # timestamp for the end of the period # #query = "\ -# SELECT SUM(acctsessiontime) \ +# SELECT IFNULL(SUM(acctsessiontime), 0) \ # FROM radacct \ # WHERE username='%{${key}}' \ # AND acctstarttime BETWEEN FROM_UNIXTIME('%{control.monthlycounter-reset-start}') \ diff --git a/raddb/mods-config/sql/counter/postgresql/dailycounter.conf b/raddb/mods-config/sql/counter/postgresql/dailycounter.conf index 6347291cf20..3fc51ada81c 100644 --- a/raddb/mods-config/sql/counter/postgresql/dailycounter.conf +++ b/raddb/mods-config/sql/counter/postgresql/dailycounter.conf @@ -5,7 +5,7 @@ # below # query = "\ - SELECT SUM(AcctSessionTime - GREATEST((%{control.dailycounter-reset-start} - EXTRACT(epoch FROM AcctStartTime)), 0)) \ + SELECT COALESCE(SUM(AcctSessionTime - GREATEST((%{control.dailycounter-reset-start} - EXTRACT(epoch FROM AcctStartTime)), 0)), 0) \ FROM radacct \ WHERE UserName='%{${key}}' \ AND EXTRACT(epoch FROM AcctStartTime) + AcctSessionTime > '%{control.dailycounter-reset-start}'" @@ -16,7 +16,7 @@ query = "\ # is a little easier on the SQL server # #query = "\ -# SELECT SUM(AcctSessionTime) \ +# SELECT COALESCE(SUM(AcctSessionTime), 0) \ # FROM radacct \ # WHERE UserName='%{${key}}' \ # AND EXTRACT(epoch FROM AcctStartTime) > '%{control.dailycounter-reset-start}'" @@ -27,7 +27,7 @@ query = "\ # timestamp for the end of the period # #query = "\ -# SELECT SUM(AcctSessionTime) \ +# SELECT COALESCE(SUM(AcctSessionTime), 0) \ # FROM radacct \ # WHERE UserName='%{${key}}' \ # AND EXTRACT(epoch FROM AcctStartTime) BETWEEN '%{control.dailycounter-reset-start}' \ diff --git a/raddb/mods-config/sql/counter/postgresql/expire_on_login.conf b/raddb/mods-config/sql/counter/postgresql/expire_on_login.conf index 6ec4c4e969a..f92bba32840 100644 --- a/raddb/mods-config/sql/counter/postgresql/expire_on_login.conf +++ b/raddb/mods-config/sql/counter/postgresql/expire_on_login.conf @@ -1,5 +1,5 @@ query = "\ - SELECT EXTRACT(EPOCH FROM (NOW() - acctstarttime)) \ + SELECT COALESCE(EXTRACT(EPOCH FROM (NOW() - acctstarttime)), 0) \ FROM radacct \ WHERE UserName='%{${key}}' \ ORDER BY acctstarttime \ diff --git a/raddb/mods-config/sql/counter/postgresql/monthlycounter.conf b/raddb/mods-config/sql/counter/postgresql/monthlycounter.conf index 9f628d8d46f..3ded21809c3 100644 --- a/raddb/mods-config/sql/counter/postgresql/monthlycounter.conf +++ b/raddb/mods-config/sql/counter/postgresql/monthlycounter.conf @@ -3,7 +3,7 @@ # involves more work for the SQL server than those # below query = "\ - SELECT SUM(AcctSessionTime - GREATEST((%{control.monthlycounter-reset-start} - EXTRACT(epoch FROM AcctStartTime)), 0)) \ + SELECT COALESCE(SUM(AcctSessionTime - GREATEST((%{control.monthlycounter-reset-start} - EXTRACT(epoch FROM AcctStartTime)), 0)), 0) \ FROM radacct \ WHERE UserName='%{${key}}' \ AND EXTRACT(epoch FROM AcctStartTime) + AcctSessionTime > '%{control.monthlycounter-reset-start}'" @@ -14,7 +14,7 @@ query = "\ # is a little easier on the SQL server # #query = "\ -# SELECT SUM(AcctSessionTime) \ +# SELECT COALESCE(SUM(AcctSessionTime), 0) \ # FROM radacct \ # WHERE UserName='%{${key}}' \ # AND EXTRACT(epoch FROM AcctStartTime) > '%{control.monthlycounter-reset-start}'" @@ -25,7 +25,7 @@ query = "\ # timestamp for the end of the period # #query = "\ -# SELECT SUM(AcctSessionTime) \ +# SELECT COALESCE(SUM(AcctSessionTime), 0) \ # FROM radacct \ # WHERE UserName='%{${key}}' \ # AND EXTRACT(epoch FROM AcctStartTime) BETWEEN '%{control.monthlycounter-reset-start}' AND '%{control.monthlycounter-reset-end}'" diff --git a/raddb/mods-config/sql/counter/postgresql/noresetcounter.conf b/raddb/mods-config/sql/counter/postgresql/noresetcounter.conf index ac5182e6298..a82ada26935 100644 --- a/raddb/mods-config/sql/counter/postgresql/noresetcounter.conf +++ b/raddb/mods-config/sql/counter/postgresql/noresetcounter.conf @@ -1,4 +1,4 @@ query = "\ - SELECT SUM(AcctSessionTime) \ + SELECT COALESCE(SUM(AcctSessionTime), 0) \ FROM radacct \ WHERE UserName='%{${key}}'" diff --git a/raddb/mods-config/sql/counter/sqlite/dailycounter.conf b/raddb/mods-config/sql/counter/sqlite/dailycounter.conf index e657dce5c3b..4a5fa726745 100644 --- a/raddb/mods-config/sql/counter/sqlite/dailycounter.conf +++ b/raddb/mods-config/sql/counter/sqlite/dailycounter.conf @@ -5,7 +5,7 @@ # below # query = "\ - SELECT SUM(acctsessiontime - GREATEST((%{control.dailycounter-reset-start} - strftime('%%s', acctstarttime)), 0)) \ + SELECT IFNULL(SUM(acctsessiontime - GREATEST((%{control.dailycounter-reset-start} - strftime('%%s', acctstarttime)), 0)), 0) \ FROM radacct \ WHERE username = '%{${key}}' \ AND (strftime('%%s', acctstarttime) + acctsessiontime) > %{control.dailycounter-reset-start}" @@ -16,7 +16,7 @@ query = "\ # is a little easier on the SQL server # #query = "\ -# SELECT SUM(acctsessiontime) \ +# SELECT IFNULL(SUM(acctsessiontime), 0) \ # FROM radacct \ # WHERE \username = '%{${key}}' \ # AND acctstarttime > %{control.dailycounter-reset-start}" @@ -27,7 +27,7 @@ query = "\ # timestamp for the end of the period # #query = "\ -# SELECT SUM(acctsessiontime) FROM radacct \ +# SELECT IFNULL(SUM(acctsessiontime), 0) FROM radacct \ # WHERE username = '%{${key}}' \ # AND acctstarttime BETWEEN %{control.dailycounter-reset-start} \ # AND %{dailycounter-reset-end}" diff --git a/raddb/mods-config/sql/counter/sqlite/monthlycounter.conf b/raddb/mods-config/sql/counter/sqlite/monthlycounter.conf index 80fd21166cd..f01911dcd3a 100644 --- a/raddb/mods-config/sql/counter/sqlite/monthlycounter.conf +++ b/raddb/mods-config/sql/counter/sqlite/monthlycounter.conf @@ -5,7 +5,7 @@ # below # query = "\ - SELECT SUM(acctsessiontime - GREATEST((%{control.monthlycounter-reset-start} - strftime('%%s', acctstarttime)), 0)) \ + SELECT IFNULL(SUM(acctsessiontime - GREATEST((%{control.monthlycounter-reset-start} - strftime('%%s', acctstarttime)), 0)), 0) \ FROM radacct \ WHERE username = '%{${key}}' AND \ (strftime('%%s', acctstarttime) + acctsessiontime) > %{control.monthlycounter-reset-start}" @@ -16,7 +16,7 @@ query = "\ # is a little easier on the SQL server # #query = "\ -# SELECT SUM(acctsessiontime) \ +# SELECT IFNULL(SUM(acctsessiontime), 0) \ # FROM radacct \ # WHERE username = '%{${key}}' \ # AND acctstarttime > %{control.monthlycounter-reset-start}" @@ -27,7 +27,7 @@ query = "\ # timestamp for the end of the period # #query = "\ -# SELECT SUM(acctsessiontime) \ +# SELECT IFNULL(SUM(acctsessiontime), 0) \ # FROM radacct \ # WHERE username = '%{${key}}' \ # AND acctstarttime BETWEEN %{control.monthlycounter-reset-start} \