⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlaccount.cs

📁 Gibphone is CSharp Program, it can tell you how to design p2p chat.
💻 CS
📖 第 1 页 / 共 3 页
字号:
        /// Predicate.
        /// </summary>
        /// <param name="match">The condition to search for.</param>
        /// <returns>The ID of the Last Account that specifies the condition.</returns>
        public int FindLastIndex(Predicate<SqlAccount> match)
        {
            foreach (SqlAccount a in Reverse)
            {
                if (match(a))
                    return a.ID;
            }
            return -1;
        }

        /// <summary>
        /// Returns the ID of the Last Before startindex Account that returns true for the given
        /// Predicate.
        /// </summary>
        /// <param name="match">The condition to search for.</param>
        /// <param name="startindex">The ID to start the search from.</param>
        /// <returns>The ID of the Last Account before startindex that specifies the condition.</returns>
        public int FindLastIndex(int startindex, Predicate<SqlAccount> match)
        {
            foreach (SqlAccount a in Reverse)
            {
                if (a.ID <= startindex && match(a))
                    return a.ID;
            }
            return -1;
        }

        /// <summary>
        /// Returns A List of all of the Accounts that return true for the
        /// given predicate.
        /// </summary>
        /// <param name="match">The condition to search for.</param>
        /// <returns>A List of the Accounts.</returns>
        public List<SqlAccount> FindAll(Predicate<SqlAccount> match)
        {
            List<SqlAccount> al = new List<SqlAccount>();
            foreach (SqlAccount a in this)
            {
                if (match(a))
                    al.Add(a);
            }
            return al;
        }
        /// <summary>
        /// Performs action forall Accounts in the list.
        /// </summary>
        /// <param name="action">The action to perform.</param>
        public void ForEach(Action<SqlAccount> action)
        {
            foreach (SqlAccount a in this)
            {
                action(a);
            }
        }

        /// <summary>
        /// Removes item from the List and the SQL table.
        /// </summary>
        /// <param name="item">The item to remove.</param>
        public void Remove(SqlAccount item)
        {
            foreach (Protocol p in item.Protocols)
            {
                p.Accounts.Remove(item);
            }
            Sql.ExecuteNonQuery("delete from Accounts where GibID = ? and Username =? and ProtocolType = ?", GibID, item.Username, item.ProtocolTypeString);
            
        }

        /// <summary>
        /// Converts the AccountList to an Array.
        /// </summary>
        /// <returns>An Array of all of the items.</returns>
        public SqlAccount[] ToArray()
        {
            List<SqlAccount> al = new List<SqlAccount>();
            foreach (SqlAccount a in this)
            {
                al.Add(a);
            }
            return al.ToArray();
        }

        /// <summary>
        /// Checks if all of the items match the given predicate.
        /// </summary>
        /// <param name="match">The condition to search for.</param>
        /// <returns>True if all of the Elements match the condition.</returns>
        public bool TrueForAll(Predicate<SqlAccount> match)
        {
            foreach (SqlAccount a in this)
            {
                if (!match(a))
                    return false;
            }
            return true;
        }

        #endregion

        #region IEnumerable<SqlAccount> Members
        /// <summary>
        /// Returns an Enumerator to iterate through the items.
        /// </summary>
        /// <returns>The Enumerator for this list.</returns>
        public IEnumerator<SqlAccount> GetEnumerator()
        {
            

            FbCommand com = Sql.SqlDatabase.CreateCommand();
            com.CommandText = String.Format("Select Username, ProtocolType from Accounts where GibID = {0} ORDER BY AccountID ASC", GibID);
            FbDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                SqlAccount a = new SqlAccount(reader.GetString(0).Trim(), reader.GetString(1).Trim());
                yield return a;
            }
            reader.Close();
        }

        /// <summary>
        /// Iterates through the Items in Reverse Order
        /// </summary>
        public IEnumerable<SqlAccount> Reverse
        {
            get
            {
                FbCommand com = Sql.SqlDatabase.CreateCommand();
                com.CommandText = String.Format("Select Username, ProtocolType from Accounts where GibID = {0} ORDER BY AccountID DESC", GibID);
                FbDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    SqlAccount a = new SqlAccount(reader.GetString(0).Trim(), reader.GetString(1).Trim());
                    yield return a;
                }
                reader.Close();
            }
        }

        #endregion




        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        #endregion
    }

    /// <summary>
    /// A List of all of the Accounts for a certain Protocol.
    /// All information about this list is taken directly from the SQL table.
    /// </summary>
    public class SqlProtocolAccountList : SqlAccountList
    {
        
        private int m_ID;

        /// <summary>
        /// The ProtocolID that owns this AccountList.
        /// </summary>
        public int ID
        {
            get { return m_ID; }
            set { m_ID = value; }
        }
	
	    /// <summary>
	    /// Creates a new ProtocolAccountList.
	    /// </summary>
	    /// <param name="p">The Protocol that will own this list.</param>
        public SqlProtocolAccountList(Protocol p)
        {
            object o = Sql.ExecuteScalar("Select ProtocolID from Protocols where Username = ? and ProtocolType = ?",p.Username, p.GetType().FullName);
            m_ID = (o == null ? -1 : (int)o);
        }

        #region ICollection<SqlAccount> Members
        /// <summary>
        /// Add the item to the List.
        /// </summary>
        /// <param name="item">The item to add.</param>
        new public void Add(SqlAccount item)
        {
            if (!Contains(item))
            {
                Sql.ExecuteNonQuery("Insert into ProtocolAccounts (AccountID, ProtocolID) values (?,?)", item.ID, ID);
                Protocol p = Core.GetProtocol(ID);
                if (p is IServerBuddyList)
                {
                    IServerBuddyList s = p as IServerBuddyList;
                    s.AddBuddy(item.Username, item.Gib.GroupKey);
                }
            }
        }

        /// <summary>
        /// Clears the List but does not remove the Actual Accounts
        /// </summary>
        new public void Clear()
        {
            Protocol p = Core.GetProtocol(ID);
            if (p is IServerBuddyList)
            {
                IServerBuddyList s = p as IServerBuddyList;
                foreach (SqlAccount a in this)
                {
                    s.RemoveBuddy(a.Username);
                }
            }
            Sql.ExecuteNonQuery("Delete from ProtocolAccounts where ProtocolID = ?", ID);
        }

        /// <summary>
        /// Chceks if the specified Item is in this Accountlist.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        new public bool Contains(SqlAccount item)
        {
            object i = Sql.ExecuteScalar("Select Count(AccountID) from ProtocolAccounts where AccountID = ? and ProtocolID =?",item.ID, ID);
            return (i == null ? false : (int)i > 0);
        }

        /// <summary>
        /// Gets the number of Accounts in this Accountlist.
        /// </summary>
        new public int Count
        {
            get
            {
                object o =Sql.ExecuteScalar("Select Count(AccountID) from ProtocolAccounts where ProtocolID = ?",  ID);

                return (o == null ? 0 : (int)o);
            }
        }
        /// <summary>
        /// Removes item from the List.
        /// </summary>
        /// <param name="item">The item to remove.</param>
        new public void Remove(SqlAccount item)
        {
            if (Contains(item))
            {
                Sql.ExecuteNonQuery("Delete from ProtocolAccounts where ProtocolID = ? and AccountID = ?", ID, item.ID);
                Protocol p = Core.GetProtocol(ID);
                if (p is IServerBuddyList)
                {
                    IServerBuddyList s = p as IServerBuddyList;
                    s.RemoveBuddy(item.Username);
                }
            }
        }
        
        /// <summary>
        /// Removes the Account with the given ID from the list.
        /// </summary>
        /// <param name="ID">The Item of the element to remove.</param>
        /// <returns></returns>
        new public void RemoveAt(int ID)
        {
            Remove(new SqlAccount(ID));
        }

        #endregion

        #region IEnumerable<SqlAccount> Members
        /// <summary>
        /// Returns an Enumerator to iterate through the items.
        /// </summary>
        /// <returns>The Enumerator for this list.</returns>
        new public IEnumerator<SqlAccount> GetEnumerator()
        {

            FbCommand com = Sql.SqlDatabase.CreateCommand();
            com.CommandText = "Select AccountID from ProtocolAccounts where ProtocolID = ? ORDER by AccountID ASC";
            com.Parameters.AddWithValue("",ID);
            FbDataReader read = com.ExecuteReader();
            while (read.Read())
            {
                yield return new SqlAccount(read.GetInt32(0));
            }
            read.Close();

        }
        /// <summary>
        /// Iterates through the Items in Reverse Order
        /// </summary>
        new public IEnumerable<SqlAccount> Reverse
        {
            get
            {
                FbCommand com = Sql.SqlDatabase.CreateCommand();
                com.CommandText = "Select AccountID from ProtocolAccounts where ProtocolID = ? ORDER by AccountID DESC";
                com.Parameters.AddWithValue("",ID);
                FbDataReader read = com.ExecuteReader();
                while (read.Read())
                {
                    yield return new SqlAccount(read.GetInt32(0));
                }
                read.Close();
                com.Transaction.Commit();
            }
        }
        #endregion

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -