private HashSet<string> mRights = new HashSet<string>(); // A collection of fuse rights in this group. #endregion Fields #region Constructors /// <summary> /// Creates a new instance of FuseGroup and loads the group infomation from the database. /// </summary> /// <param name="ID">The numerical ID of the group.</param> /// <param name="Name">The name of the group.</param> /// <param name="dbClient">The database connection to use to.</param> public FuseGroup(ushort ID, string Name, DatabaseClient dbClient) { this.mName = Name; this.mChildGroups = new HashSet<ushort>(); ; dbClient.ClearParams(); // Remove any previous parameters from the database connection. dbClient.AddParamWithValue("id", ID); // Add ID as a parameter DataColumn dCol = dbClient.ReadDataColumn("SELECT valueid FROM fuse_item WHERE type = 'group' AND groupid = @id"); // Get all child groups of this group. if (dCol != null) // If any exist... for (int i = 0; i < dCol.Table.Rows.Count; i++) // Loop through them all... this.mChildGroups.Add((ushort)(uint)dCol.Table.Rows[i]["valueid"]); // And add each one to the collection of child groups. dCol = dbClient.ReadDataColumn("SELECT `fuse_right`.`right` AS `right` FROM `fuse_right`,`fuse_item` WHERE `fuse_item`.`type` = 'right' AND `fuse_item`.`groupid` = @id AND `fuse_item`.`valueid` = `fuse_right`.`id`"); // Get all fuse rights in this group from the database. if (dCol != null) // If there is fuse rights in this group... for (int i = 0; i < dCol.Table.Rows.Count; i++) // Loop through them all... mRights.Add((string)dCol.Table.Rows[i]["right"]); // And add each one to the collection of fuse rights }
/// <summary> /// Sets the amount of clients that will be available to requesting methods. If the new amount is lower than the current amount, the 'excluded' connections are destroyed. If the new connection amount is higher than the current amount, new clients are prepared. Already existing clients and their state will be maintained. /// </summary> /// <param name="Amount">The new amount of clients.</param> public void SetClientAmount(uint Amount) { lock (this) { if (mClients.Length == Amount) return; if (Amount < mClients.Length) // Client amount shrinks, dispose clients that will die { for (uint i = Amount; i < mClients.Length; i++) { mClients[i].Destroy(); mClients[i] = null; } } DatabaseClient[] pClients = new DatabaseClient[Amount]; bool[] pClientAvailable = new bool[Amount]; for (uint i = 0; i < Amount; i++) { if (i < mClients.Length) // Keep the existing client and it's available state { pClients[i] = mClients[i]; pClientAvailable[i] = mClientAvailable[i]; } else // We are in need of more clients, so make another one { pClients[i] = new DatabaseClient((i + 1), this); pClientAvailable[i] = true; // Elegant? } } // Update the instance fields mClients = pClients; mClientAvailable = pClientAvailable; } }
public DatabaseClient GetClient() { // Let other threads wait if they contact this DatabaseManager while we're busy with it lock (this) { // Try to find an available client for (uint i = 0; i < mClients.Length; i++) { // Somebody here? if (mClientAvailable[i] == true) { // No starvation anymore mClientStarvationCounter = 0; // Is this connection broken? if (mClients[i].State == ConnectionState.Broken) { mClients[i] = new DatabaseClient((i + 1), this); // Create new client } // Is this connection closed? if (mClients[i].State == ConnectionState.Closed) { // TODO: exception handling mClients[i].Connect(); //Holo.Out.WriteLine("Opening connection for database client #" + mClients[i].Handle); } // Is this client ready? if (mClients[i].State == ConnectionState.Open) { //Holo.Out.WriteLine("Handed out client #" + mClients[i].Handle); mClientAvailable[i] = false; // BRB mClients[i].UpdateLastActivity(); return mClients[i]; } } } // No clients? mClientStarvationCounter++; // Are we having a structural lack of clients? if (mClientStarvationCounter >= ((mClients.Length + 1) / 2)) // Database hungry much? { // Heal starvation mClientStarvationCounter = 0; // Increase client amount by 0.3 SetClientAmount((uint)(mClients.Length + 1 * 1.3f)); // Re-enter this method return GetClient(); } DatabaseClient pAnonymous = new DatabaseClient(0, this); pAnonymous.Connect(); Holo.Out.WriteLine("Handed out anonymous client."); return pAnonymous; } }
private void CheckinUserParams(ref DatabaseClient dbClient) { dbClient.AddParamWithValue("@id", mID); dbClient.AddParamWithValue("@username", mUsername); dbClient.AddParamWithValue("@password", mPassword); dbClient.AddParamWithValue("@role", mRole); dbClient.AddParamWithValue("@signedup", mSignedUp); dbClient.AddParamWithValue("@email", mEmail); dbClient.AddParamWithValue("@dob", mDateOfBirth); dbClient.AddParamWithValue("@motto", mMotto); dbClient.AddParamWithValue("@figure", mFigure); dbClient.AddParamWithValue("@gender", mGender); dbClient.AddParamWithValue("@coins", mCoins); dbClient.AddParamWithValue("@films", mFilms); dbClient.AddParamWithValue("@gametickets", mGameTickets); dbClient.AddParamWithValue("@activitypoints", mActivityPoints); }
public bool UPDATE(DatabaseClient dbClient) { CheckinUserParams(ref dbClient); dbClient.ExecuteQuery("UPDATE users " + "SET username=@username,password=@password,role=@role,signedup=@signedup,email=@email,dob=@dob,motto=@motto,figure=@figure,gender=@gender,coins=@coins,films=@films,gametickets=@gametickets,activitypoints=@activitypoints " + "WHERE id = @id;"); return true; }
public bool INSERT(DatabaseClient dbClient) { CheckinUserParams(ref dbClient); dbClient.ExecuteQuery("INSERT INTO users" + "(username,password,role,signedup,email,dob,motto,figure,gender,coins,films,gametickets,activitypoints) " + "VALUES(@username,@password,@role,@signedup,@email,@dob,@motto,@figure,@gender,@coins,@films,@gametickets,@activitypoints);"); return true; }
public bool DELETE(DatabaseClient dbClient) { dbClient.AddParamWithValue("@id", mID); dbClient.ExecuteQuery("DELETE FROM users WHERE id = @id;"); return true; }
public DatabaseClient GetClient() { // Let other threads wait if they contact this DatabaseManager while we're busy with it lock (this) { // Try to find an available client for (uint i = 0; i < mClients.Length; i++) { // Somebody here? if (mClientAvailable[i] == true) { // No starvation anymore mClientStarvationCounter = 0; // Is this connection broken? if (mClients[i].State == ConnectionState.Broken) { mClients[i] = new DatabaseClient((i + 1), this); // Create new client } // Is this connection closed? if (mClients[i].State == ConnectionState.Closed) { // TODO: exception handling mClients[i].Connect(); //Holo.Out.WriteLine("Opening connection for database client #" + mClients[i].Handle); } // Is this client ready? if (mClients[i].State == ConnectionState.Open) { //Holo.Out.WriteLine("Handed out client #" + mClients[i].Handle); mClientAvailable[i] = false; // BRB mClients[i].UpdateLastActivity(); return(mClients[i]); } } } // No clients? mClientStarvationCounter++; // Are we having a structural lack of clients? if (mClientStarvationCounter >= ((mClients.Length + 1) / 2)) // Database hungry much? { // Heal starvation mClientStarvationCounter = 0; // Increase client amount by 0.3 SetClientAmount((uint)(mClients.Length + 1 * 1.3f)); // Re-enter this method return(GetClient()); } DatabaseClient pAnonymous = new DatabaseClient(0, this); pAnonymous.Connect(); Holo.Out.WriteLine("Handed out anonymous client."); return(pAnonymous); } }