Fixed resource leak in GetUser fixes #404

Multiple user exception is now passed direct to the caller of GetUser instead of being wrapped
This commit is contained in:
stevenh 2012-02-22 15:45:47 +00:00
parent c7c38709b2
commit d813a60060

View file

@ -254,35 +254,50 @@ namespace TShockAPI.DB
public User GetUser(User user) public User GetUser(User user)
{ {
bool multiple = false;
string query;
string type;
object arg;
if (0 != user.ID)
{
query = "SELECT * FROM Users WHERE ID=@0";
arg = user.ID;
type = "id";
}
else if (string.IsNullOrEmpty(user.Address))
{
query = "SELECT * FROM Users WHERE Username=@0";
arg = user.Name;
type = "name";
}
else
{
query = "SELECT * FROM Users WHERE IP=@0";
arg = user.Address;
type = "ip";
}
try try
{ {
QueryResult result; using (var result = database.QueryReader(query, arg))
if (0 != user.ID)
result = database.QueryReader("SELECT * FROM Users WHERE ID=@0", user.ID);
else if (string.IsNullOrEmpty(user.Address))
result = database.QueryReader("SELECT * FROM Users WHERE Username=@0", user.Name);
else
result = database.QueryReader("SELECT * FROM Users WHERE IP=@0", user.Address);
if (result.Read())
{ {
user = LoadUserFromResult(user, result); if (result.Read())
// Check for multiple matches {
if (!result.Read()) user = LoadUserFromResult(user, result);
return user; // Check for multiple matches
if (!result.Read())
if (0 != user.ID) return user;
throw new UserManagerException(String.Format("Multiple users found for id '{0}'", user.ID)); multiple = true;
else if (string.IsNullOrEmpty(user.Address)) }
throw new UserManagerException(String.Format("Multiple users found for name '{0}'", user.Name));
else
throw new UserManagerException(String.Format("Multiple users found for ip '{0}'", user.Address));
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new UserManagerException("GetUser SQL returned an error (" + ex.Message + ")", ex); throw new UserManagerException("GetUser SQL returned an error (" + ex.Message + ")", ex);
} }
if (multiple)
throw new UserManagerException(String.Format("Multiple users found for {0} '{1}'", type, arg));
throw new UserNotExistException(string.IsNullOrEmpty(user.Address) ? user.Name : user.Address); throw new UserNotExistException(string.IsNullOrEmpty(user.Address) ? user.Name : user.Address);
} }