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

📄 sqlaccount.cs

📁 Gibphone is CSharp Program, it can tell you how to design p2p chat.
💻 CS
📖 第 1 页 / 共 3 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using FirebirdSql.Data.FirebirdClient;
using GPCore.Protocols;
using GPCore.Args;

namespace GPCore
{
    /// <summary>
    /// A struct that describes the information for a specific contact.
    /// Contains username, protocol, and status information.
    /// Changes made to any Account are automatically reflected in the 
    /// SqlDatabase.
    /// </summary>
    public struct SqlAccount : IIdable
    {
        //private int m_id;

        /// <summary>
        /// Gets/Sets the Unique ID of an Account.
        /// this is not sequential and some numbers may be missing.
        /// </summary>
        public int ID
        {
            get
            {
                object o = Sql.ExecuteScalar("Select AccountID from Accounts where Username = ? and ProtocolType =?", m_Username, ProtocolTypeString);
                return (o == null ? -1 : (int)o);
            }
        }

        private int m_gid;

        /// <summary>
        /// Gets/Sets the Unique ID of the Gib that owns this Account
        /// this is not sequential and some numbers may be missing
        /// </summary>
        public int GibID
        {
            get { return m_gid; }
            set
            {
                int i = ID;
                if (i != -1)
                    Sql.ExecuteNonQuery("Update Accounts set GibID = {0} where AccountID = {1}", value, i);
                m_gid = value;
            }
        }
        
        /// <summary>
        /// Gets/Sets the Gib that owns this Account
        /// </summary>
        public SqlGib Gib
        {
            get
            {
                return new SqlGib(m_gid);
            }
            set
            {
                Sql.ExecuteNonQuery("Update Accounts set GibID = {0} where AccountID = {1}" ,value.ID,ID);
            }
        }

        private String m_Username;
        /// <summary>
        /// Gets/Sets the Username of this Account
        /// </summary>
        public String Username
        {
            get { return m_Username; }
            set
            {
                int i = ID;
                if (i != -1)
                    Sql.ExecuteNonQuery("Update Accounts set Username = '{0}' where AccountID = {1}", value, i);
                m_Username = value;
            }
        }


        private string m_ProtocolType;
        /// <summary>
        /// Gets/Sets the Fullname of the Type of <see cref="IProtocol"/> used to Contact this Account
        /// </summary>
        public String ProtocolTypeString
        {
            get { return m_ProtocolType; }
            set
            {
                int i = ID;
                if (i != -1)
                    Sql.ExecuteNonQuery("Update Accounts set ProtocolType = '{0}' where AccountID = {1}", value, i);
                m_ProtocolType = value;
            }
        }

        /// <summary>
        /// Gets/Sets the Type of <see cref="IProtocol"/> used to contact this Account
        /// </summary>
        public Type ProtocolType
        {
            get
            {
                return Core.GetTypeFromString(m_ProtocolType);
            }
            set
            {
                int i = ID;
                if (i != -1)
                    Sql.ExecuteNonQuery("Update Accounts set ProtocolType = ? where AccountID = ?", value.FullName, i);
                m_ProtocolType = value.FullName;
            }
        }

        /// <summary>
        /// Gets/sets the Default <see cref="IProtocol"/> used to contact this Account
        /// if none is found then it returns the first <see cref="IProtocol"/> of the
        /// <see cref="ProtocolType"/>.
        /// </summary>
        public Protocol Protocol
        {
            get
            {
                object o = Sql.ExecuteScalar("select DefaultProtocol from Accounts where AccountID = ?", ID);
                int i = (o == null ? -1 : (int)o);
                if (i == -1)
                {
                    o = Sql.ExecuteScalar("select ProtocolID from protocols where ProtocolType = ?", ProtocolTypeString);
                    if ((i = (o == null ? -1 : (int)o)) == -1)
                    {
                        return null;
                    }
                }
                Protocol p = null;
                Sql.ExecuteReader(new Sql.ActOnQuery(delegate(GPDataReader read)
                {
                    while (read.Read())
                    {
                        p = Core.GetProtocol(read.GetString(0).Trim(), read.GetString(1).Trim());
                    }
                }), "select Username, ProtocolType from  Protocols where ProtocolID = ?", i);
                return p;
            }
            set
            {
                object o = Sql.ExecuteScalar("select ProtocolID from Protocols where Username = ? and ProtocolType = ?", value.Username, value.GetType().FullName);
                int i = (o == null ? -1 : (int)o);
                Sql.ExecuteNonQuery("update Accounts set DefaultProtocol = ? where AccountID = ?", i, ID);
            }
        }

        /// <summary>
        /// Gets a list of all of the <see cref="IProtocol">IProtocols</see> that can be used to
        /// contact this Account.
        /// </summary>
        public List<Protocol> Protocols
        {
            get
            {
                List<Protocol> pl = new List<Protocol>();
                Sql.ExecuteReader(new Sql.ActOnQuery(delegate(GPDataReader read)
                {
                    while (read.Read())
                    {
                        pl.Add(Core.GetProtocol(read.GetInt32(0)));

                    }
                }), "select ProtocolID from ProtocolAccounts where AccountID = ?", ID);
                return pl;
            }
        }
        /// <summary>
        /// Gets/Sets the Status of this Account.
        /// </summary>
        public String Status
        {
            get
            {
                string s  =Sql.ExecuteScalar("Select Status from Accounts where AccountID =?", ID) as string;
                return (s == null ? "" : s.Trim());
            }
            set
            {
                if (Status != value)
                {
                    Sql.ExecuteNonQuery("Update Accounts set status = ? where AccountID = ?", value, ID);
                    Core.StatusChanged.Fire(null, new StatusChangedArgs(value, m_Username));
                }
            }
        }

        /// <summary>
        /// Gets/Sets the Awaymessage of this Account.
        /// </summary>
        public String AwayMessage
        {
            get
            {
                int i = ID;
                if (awaymessages.ContainsKey(i))
                    return awaymessages[i];
                else
                    return "";
            }
            set
            {
                int i = ID;
                if (awaymessages.ContainsKey(i))
                {
                    awaymessages[i] = value;
                }
                else
                {
                    awaymessages.Add(i, value);
                }
            }
        }
        private static Dictionary<int, string> awaymessages = new Dictionary<int, string>();
        
        /// <summary>
        /// Gets/Sets the Profile of this Account.
        /// </summary>
        public String Profile
        {
            get
            {
                string s = Sql.ExecuteScalar("select Profile from Accounts where AccountID = ?", ID) as string;
                return (s == null ? "" : s);
            }
            set
            {
                if (string.IsNullOrEmpty(value)) return;
                Sql.ExecuteNonQuery("update Accounts set Profile = ? where AccountID = ?" ,value,ID);
            }
        }

        /// <summary>
        /// Gets/Sets the time of the last known activitiy of this Account.
        /// </summary>
        public DateTime LastActivity
        {
            get
            {
                object s = Sql.ExecuteScalar("Select LastActivity from accounts where accountID = ?", ID);
                return (s == null || s.GetType() == typeof(System.DBNull) ? new DateTime(3, 12, 25) : (DateTime)s); 
            }
            set
            {
                Sql.ExecuteNonQuery("Update Accounts set Lastactivity = ? where accountID = ?", value,ID);
            }
        }

        /// <summary>
        /// Gets/Sets the Time between Now and the time this account changed last.
        /// </summary>
        public TimeSpan TimeSinceLastActivity
        {
            get
            {
                return DateTime.Now.Subtract(LastActivity);
            }
            set
            {
                LastActivity = DateTime.Now.Subtract(value);
            }
        }

        /// <summary>
        /// Retrieves the Account with the ID specified.
        /// </summary>
        /// <param name="ID">The ID of the Account you want.</param>
        public SqlAccount(int ID)
        {
            
                int gid = 0;
                string user = "", pt = "";
            Sql.ExecuteReader(new Sql.ActOnQuery(delegate(GPDataReader read)
            {
                while (read.Read())
                {
                    gid = read.GetInt32(0);
                    user = read.GetString(1).Trim();
                    pt= read.GetString(2).Trim();
                }
            }), "Select GibID, Username, ProtocolType from Accounts where AccountID = ?", ID);
            m_gid = gid;
            m_Username = user;
            m_ProtocolType = pt;
        }
        /// <summary>
        /// Creates an Acocunt froma a specified Username and Protocol.
        /// If this item already exists in the SqlTable, changes made to this instance
        /// will be reflected in the table.
        /// </summary>
        /// <param name="Username">The username of the Account</param>
        /// <param name="p"> The protocol to which this accounts belongs to</param>
        public SqlAccount(string Username, IProtocol p) : this(Username,p.GetType().FullName)
        {
        }
        /// <summary>
        /// Creates an Account with the specified Username and ProtocolType
        /// if this item already exists in the SqlTable changes made to this
        /// Account will be reflected in SqlTable.
        /// </summary>
        /// <param name="Username">The Username of the contact.</param>
        /// <param name="ProtocolType">The Fullname of the Type of the <see cref="IProtocol"/>
        /// used to contact this Account.</param>
        public SqlAccount(string Username, String ProtocolType) 
        {
            m_Username = Username;
            m_ProtocolType = ProtocolType;

            object o = Sql.ExecuteScalar("select GibID from Accounts where Username =? and ProtocolType = ?", Username, ProtocolType);
            m_gid = (o == null ? -1 : (int)o);
            
        }
        

⌨️ 快捷键说明

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