Merge pull request #408 from stevenh/sqlfix

Fixed resource leak in GetUser
This commit is contained in:
Steven Hartland 2012-02-22 14:39:35 -08:00
commit 5db1de89a5

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()) if (result.Read())
{ {
user = LoadUserFromResult(user, result); user = LoadUserFromResult(user, result);
// Check for multiple matches // Check for multiple matches
if (!result.Read()) if (!result.Read())
return user; return user;
multiple = true;
if (0 != user.ID) }
throw new UserManagerException(String.Format("Multiple users found for id '{0}'", user.ID));
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);
} }