From 532c0930d4c8a853b83c9607461a057fb12e7448 Mon Sep 17 00:00:00 2001
From: Nigel Kukard <nkukard@lbsd.net>
Date: Sat, 26 Mar 2011 23:47:24 +0000
Subject: [PATCH] Database query fixup

Make sure we standardize how the database columns are cased. The only
real impact of this change is userdb.id becomes userdb.ID as this was
the only variable affected.
---
 .../modules/accounting/mod_accounting_sql.pm  | 12 ++---
 smradius/modules/system/mod_config_sql.pm     | 12 ++---
 .../modules/system/mod_config_sql_topups.pm   | 45 +++++--------------
 smradius/modules/userdb/mod_userdb_sql.pm     | 30 ++++++-------
 smradiusd.conf                                | 12 ++---
 5 files changed, 40 insertions(+), 71 deletions(-)

diff --git a/smradius/modules/accounting/mod_accounting_sql.pm b/smradius/modules/accounting/mod_accounting_sql.pm
index e1d8904a..ab4634ff 100644
--- a/smradius/modules/accounting/mod_accounting_sql.pm
+++ b/smradius/modules/accounting/mod_accounting_sql.pm
@@ -474,11 +474,9 @@ sub acct_log
 		my $totalSessionTime = Math::BigInt->new($template->{'request'}->{'Acct-Session-Time'});
 
 		# Loop through previous records and subtract them from our session totals
-		while (my $sessionPart = $sth->fetchrow_hashref()) {
-			$sessionPart = hashifyLCtoMC(
-				$sessionPart,
+		while (my $sessionPart = hashifyLCtoMC($sth->fetchrow_hashref(),
 				qw(InputOctets InputPackets OutputOctets OutputPackets InputGigawords OutputGigawords SessionTime PeriodKey)
-			);
+		)) {
 
 			# Convert this session usage to bytes
 			my $sessionInputBytes = Math::BigInt->new();
@@ -634,11 +632,7 @@ sub fixDuplicates
 
 	# Pull in duplicates
 	my @IDList;
-	while (my $duplicates = $sth->fetchrow_hashref()) {
-		$duplicates = hashifyLCtoMC(
-			$duplicates,
-			qw(ID)
-		);
+	while (my $duplicates = hashifyLCtoMC($sth->fetchrow_hashref(), qw(ID))) {
 		push(@IDList,$duplicates->{'ID'});
 	}
 	DBFreeRes($sth);
diff --git a/smradius/modules/system/mod_config_sql.pm b/smradius/modules/system/mod_config_sql.pm
index b89a78ab..3fe6cd64 100644
--- a/smradius/modules/system/mod_config_sql.pm
+++ b/smradius/modules/system/mod_config_sql.pm
@@ -172,7 +172,7 @@ sub getConfig
 	# Set realm ID
 	my $row;
 	if ($sth->rows == 1) {
-		$row = hashifyLCtoMC($sth->fetchrow_hashref(),qw(ID));
+		$row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(ID));
 		$realmID = $row->{'ID'};
 	}
 	DBFreeRes($sth);
@@ -186,7 +186,7 @@ sub getConfig
 		}
 		# Add any default realm attributes to config attributes
 		while (my $row = $sth->fetchrow_hashref()) {
-			processConfigAttribute($server,$user->{'ConfigAttributes'},hashifyLCtoMC($row,qw(Name Operator Value)));
+			processConfigAttribute($server,$user->{'ConfigAttributes'},hashifyLCtoMC($row, qw(Name Operator Value)));
 		}
 		DBFreeRes($sth);
 	}
@@ -202,7 +202,7 @@ sub getConfig
 		}
 		# Fetch realm ID
 		if ($sth->rows == 1) {
-			$row = hashifyLCtoMC($sth->fetchrow_hashref(),qw(ID));
+			$row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(ID));
 			$realmID = $row->{'ID'};
 			DBFreeRes($sth);
 
@@ -214,7 +214,7 @@ sub getConfig
 			}
 			# Add any realm attributes to config attributes
 			while (my $row = $sth->fetchrow_hashref()) {
-				processConfigAttribute($server,$user->{'ConfigAttributes'},hashifyLCtoMC($row,qw(Name Operator Value)));
+				processConfigAttribute($server,$user->{'ConfigAttributes'},hashifyLCtoMC($row, qw(Name Operator Value)));
 			}
 			DBFreeRes($sth);
 		}
@@ -236,7 +236,7 @@ sub getConfig
 	# Check if we know this client
 	my @accessList;
 	while (my $row = $sth->fetchrow_hashref()) {
-		$res = hashifyLCtoMC($row,qw(AccessList ID));
+		$res = hashifyLCtoMC($row, qw(AccessList ID));
 		# Split off allowed sources, comma separated
 		@accessList = ();
 		@accessList = split(',',$res->{'AccessList'});
@@ -263,7 +263,7 @@ sub getConfig
 		}
 		# Add to config attributes
 		while (my $row = $sth->fetchrow_hashref()) {
-			processConfigAttribute($server,$user->{'ConfigAttributes'},hashifyLCtoMC($row,qw(Name Operator Value)));
+			processConfigAttribute($server,$user->{'ConfigAttributes'},hashifyLCtoMC($row, qw(Name Operator Value)));
 		}
 		DBFreeRes($sth);
 	}
diff --git a/smradius/modules/system/mod_config_sql_topups.pm b/smradius/modules/system/mod_config_sql_topups.pm
index fc010399..95afbda9 100644
--- a/smradius/modules/system/mod_config_sql_topups.pm
+++ b/smradius/modules/system/mod_config_sql_topups.pm
@@ -172,9 +172,7 @@ sub getTopups
 
 	# Fetch all summaries
 	my (@trafficSummary,@uptimeSummary);
-	while (my $row = $sth->fetchrow_hashref()) {
-		$row = hashifyLCtoMC($row, qw(Balance Type ID));
-
+	while (my $row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(Balance Type ID))) {
 		if ($row->{'Type'} == 1) {
 			# Add to traffic summary list
 			push(@trafficSummary, { Value => $row->{'Balance'}, ID => $row->{'ID'} });
@@ -195,9 +193,7 @@ sub getTopups
 
 	# Fetch all new topups 
 	my (@trafficTopups,@uptimeTopups);
-	while (my $row = $sth->fetchrow_hashref()) {
-		$row = hashifyLCtoMC($row, qw(ID Type Value));
-
+	while (my $row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(ID Type Value))) {
 		if ($row->{'Type'} == 1) {
 			# Add topup to traffic array
 			push(@trafficTopups, { Value => $row->{'Value'}, ID => $row->{'ID'} });
@@ -289,8 +285,7 @@ sub cleanup
 
 	# Create hash of usernames
 	my %users;
-	while (my $user = $sth->fetchrow_hashref()) {
-		$user = hashifyLCtoMC($user, qw(ID Username));
+	while (my $user = hashifyLCtoMC($sth->fetchrow_hashref(), qw(ID Username))) {
 		$users{$user->{'ID'}} = $user->{'Username'};
 	}
 
@@ -414,11 +409,7 @@ sub cleanup
 
 		# Store limits in capRecord hash
 		my %capRecord;
-		while (my $row = $sth->fetchrow_hashref()) {
-			$row = hashifyLCtoMC(
-				$row,
-				qw(Name Operator Value)
-			);
+		while (my $row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(Name Operator Value))) {
 
 			if (defined($row->{'Name'})) {
 				if ($row->{'Name'} eq 'SMRadius-Capping-Traffic-Limit') {
@@ -470,11 +461,7 @@ sub cleanup
 		}
 
 		# Store limits in capRecord hash
-		while (my $row = $sth->fetchrow_hashref()) {
-			$row = hashifyLCtoMC(
-				$row,
-				qw(Name Operator Value)
-			);
+		while (my $row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(Name Operator Value))) {
 
 			if (defined($row->{'Name'})) {
 				if ($row->{'Name'} eq 'SMRadius-Capping-Traffic-Limit') {
@@ -539,11 +526,7 @@ sub cleanup
 		# Add previous valid topups to lists
 		my @trafficSummary = ();
 		my @uptimeSummary = ();
-		while (my $row = $sth->fetchrow_hashref()) {
-			$row = hashifyLCtoMC(
-				$row,
-				qw(TopupID Balance Value ValidTo Type)
-			);
+		while (my $row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(TopupID Balance Value ValidTo Type))) {
 
 			if (defined($row->{'ValidTo'})) {
 				# Convert string to unix time
@@ -595,11 +578,7 @@ sub cleanup
 
 		# Loop with the topups and push them into arrays
 		my (@trafficTopups,@uptimeTopups);
-		while (my $row = $sth->fetchrow_hashref()) {
-			$row = hashifyLCtoMC(
-				$row,
-				qw(ID Value Type ValidTo)
-			);
+		while (my $row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(ID Value Type ValidTo))) {
 
 			# Convert string to unix time
 			my $unix_validTo = str2time($row->{'ValidTo'});
@@ -840,7 +819,7 @@ sub cleanup
 			# Check if this record exists
 			my $sth = DBSelect('
 				SELECT
-					COUNT(*) as rowCount
+					COUNT(*) as RowCount
 				FROM
 					@TP@topups_summary
 				WHERE
@@ -855,14 +834,10 @@ sub cleanup
 				goto FAIL_ROLLBACK;
 			}
 
-			my $recordCheck = $sth->fetchrow_hashref();
-			$recordCheck = hashifyLCtoMC(
-				$recordCheck,
-				qw(rowCount)
-			);
+			my $recordCheck = hashifyLCtoMC($sth->fetchrow_hashref(), qw(RowCount));
 
 			# Update topup summary
-			if (defined($recordCheck->{'rowCount'}) && $recordCheck->{'rowCount'} > 0) {
+			if (defined($recordCheck->{'RowCount'}) && $recordCheck->{'RowCount'} > 0) {
 				$sth = DBDo('
 					UPDATE
 						@TP@topups_summary
diff --git a/smradius/modules/userdb/mod_userdb_sql.pm b/smradius/modules/userdb/mod_userdb_sql.pm
index 2cedfbf5..f5d0c8f8 100644
--- a/smradius/modules/userdb/mod_userdb_sql.pm
+++ b/smradius/modules/userdb/mod_userdb_sql.pm
@@ -86,7 +86,7 @@ sub init
 		FROM
 			@TP@group_attributes, @TP@users_to_groups
 		WHERE
-			@TP@users_to_groups.UserID = %{userdb.id}
+			@TP@users_to_groups.UserID = %{userdb.ID}
 			AND @TP@group_attributes.GroupID = @TP@users_to_groups.GroupID
 			AND @TP@group_attributes.Disabled = 0
 	';
@@ -97,7 +97,7 @@ sub init
 		FROM
 			@TP@user_attributes
 		WHERE
-			UserID = %{userdb.id}
+			UserID = %{userdb.ID}
 			AND Disabled = 0
 	';
 	
@@ -106,7 +106,7 @@ sub init
 			@TP@users_data (UserID, LastUpdated, Name, Value)
 		VALUES
 			(
-				%{userdb.id},
+				%{userdb.ID},
 				%{query.LastUpdated},
 				%{query.Name},
 				%{query.Value}
@@ -120,7 +120,7 @@ sub init
 			LastUpdated = %{query.LastUpdated},
 			Value = %{query.Value}
 		WHERE
-			UserID = %{userdb.id}
+			UserID = %{userdb.ID}
 			AND Name = %{query.Name}
 	';
 	
@@ -130,7 +130,7 @@ sub init
 		FROM
 			@TP@users_data
 		WHERE
-			UserID = %{userdb.id}
+			UserID = %{userdb.ID}
 			AND Name = %{query.Name}
 	';
 	
@@ -138,7 +138,7 @@ sub init
 		DELETE FROM
 			@TP@users_data
 		WHERE
-			UserID = %{userdb.id}
+			UserID = %{userdb.ID}
 			AND Name = %{query.Name}
 	';
 
@@ -300,10 +300,10 @@ sub find
 	}
 
 	# Grab record data
-	my $row = $sth->fetchrow_hashref();
+	my $row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(ID Disabled));
 
 	# Dont use disabled user
-	my $res = isBoolean($row->{'disabled'});
+	my $res = isBoolean($row->{'Disabled'});
 	if ($res) {
 		$server->log(LOG_DEBUG,"[MOD_USERDB_SQL] User '".$user->{'Username'}."' is disabled");
 		return MOD_RES_SKIP;
@@ -447,7 +447,7 @@ sub data_set
 		
 		# Cache the result
 		cacheStoreComplexKeyPair('mod_userdb_sql(users_data)',
-				sprintf('%s/%s/%s',$module,$user->{'_UserDB_Data'}->{'id'},$name),
+				sprintf('%s/%s/%s',$module,$user->{'_UserDB_Data'}->{'ID'},$name),
 				\%data
 		);
 	}
@@ -485,7 +485,7 @@ sub data_get
 	# If we using caching, check how old the result is
 	if (defined($config->{'userdb_data_cache_time'})) {
 		my ($res,$val) = cacheGetComplexKeyPair('mod_userdb_sql(data_get)',
-				sprintf('%s/%s/%s',$module,$user->{'_UserDB_Data'}->{'id'},$name)
+				sprintf('%s/%s/%s',$module,$user->{'_UserDB_Data'}->{'ID'},$name)
 		);
 
 		if (defined($val) && $val->{'CachedUntil'} > $user->{'_Internal'}->{'Timestamp-Unix'}) {
@@ -504,17 +504,17 @@ sub data_get
 	}
 
 	# Fetch user data
-	my $row = $sth->fetchrow_hashref();
+	my $row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(LastUpdated Name Value));
 
 	# If there is no result, just return undef
 	return if (!defined($row));
 
 	# If there is data, go through the long process of continuing ...
 	my %data;
-	$data{'LastUpdated'} = $row->{'lastupdated'};
+	$data{'LastUpdated'} = $row->{'LastUpdated'};
 	$data{'Module'} = $module;
-	$data{'Name'} = $row->{'name'};
-	$data{'Value'} = $row->{'value'};
+	$data{'Name'} = $row->{'Name'};
+	$data{'Value'} = $row->{'Value'};
 
 	# If we using caching and got here, it means that we must cache the result
 	if (defined($config->{'userdb_data_cache_time'})) {
@@ -522,7 +522,7 @@ sub data_get
 		
 		# Cache the result
 		cacheStoreComplexKeyPair('mod_userdb_sql(users_data)',
-				sprintf('%s/%s/%s',$module,$user->{'_UserDB_Data'}->{'id'},$name),
+				sprintf('%s/%s/%s',$module,$user->{'_UserDB_Data'}->{'ID'},$name),
 				\%data
 		);
 	}
diff --git a/smradiusd.conf b/smradiusd.conf
index a6aa2c3c..8757226c 100644
--- a/smradiusd.conf
+++ b/smradiusd.conf
@@ -433,7 +433,7 @@ userdb_get_group_attributes_query=<<EOT
 	FROM
 		@TP@group_attributes, @TP@users_to_groups
 	WHERE
-		users_to_groups.UserID = %{userdb.id}
+		users_to_groups.UserID = %{userdb.ID}
 		AND group_attributes.GroupID = users_to_groups.GroupID
 		AND group_attributes.Disabled = 0
 EOT
@@ -444,7 +444,7 @@ userdb_get_user_attributes_query=<<EOT
 	FROM
 		@TP@user_attributes
 	WHERE
-		UserID = %{userdb.id}
+		UserID = %{userdb.ID}
 		AND Disabled = 0
 EOT
 
@@ -453,7 +453,7 @@ users_data_set_query=<<EOT
 		@TP@users_data (UserID, LastUpdated, Name, Value)
 	VALUES
 		(
-			%{userdb.id},
+			%{userdb.ID},
 			%{query.LastUpdated},
 			%{query.Name},
 			%{query.Value}
@@ -467,7 +467,7 @@ users_data_update_query=<<EOT
 		LastUpdated = %{query.LastUpdated},
 		Value = %{query.Value}
 	WHERE
-		UserID = %{userdb.id}
+		UserID = %{userdb.ID}
 		AND Name = %{query.Name}
 EOT
 
@@ -477,7 +477,7 @@ users_data_get_query=<<EOT
 	FROM
 		@TP@users_data
 	WHERE
-		UserID = %{userdb.id}
+		UserID = %{userdb.ID}
 		AND Name = %{query.Name}
 EOT
 
@@ -485,7 +485,7 @@ users_data_delete_query=<<EOT
 	DELETE FROM
 		@TP@users_data
 	WHERE
-		UserID = %{userdb.id}
+		UserID = %{userdb.ID}
 		AND Name = %{query.Name}
 EOT
 
-- 
GitLab