using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using ProtoCSStruct; using MySql.Data.MySqlClient; namespace Name { public class NameOperator { private MySqlDB m_mySqlDb; //private long maxUserIdByIndex = 0; //table表格做索引做Key,m_maxUserIdByIndex的值做value private Dictionary m_maxUserIdDict = new Dictionary(); //销毁的时候置空 public void Dispose() { m_mySqlDb.Dispose(); m_mySqlDb = null; } public NameOperator(string db, string ip, string user, string password) { m_mySqlDb = new MySqlDB(db, ip, user, password); } public void KeepAlive() { string strSql = "select * from tbname_1 where hashName=?hashName"; MySqlParameter p_accountId = new MySqlParameter("?hashName", MySqlDbType.VarChar, 200) { Value = "keep_alive" }; //TraceLog.Trace("NameOperator.KeepAlive exec sql: {0}", strSql); MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_accountId); if (reader == null) { TraceLog.Trace("NameOperator.KeepAlive no record in account table, return reader is null!"); return; } //记住一定要Close reader.Close(); TraceLog.Trace("NameOperator.KeepAlive exec at {0}", DateTime.Now); } public tbName QueryName(string hashName, int realmId) { string tableName = NameServerUtils.GetNameTableName(0, hashName); TraceLog.Trace("QueryName hashName {0} table {1}", hashName, tableName); string strSql = "select * from " + tableName + " where hashName=?hashName "; MySqlParameter p_hashName = new MySqlParameter("?hashName", MySqlDbType.VarChar,200) { Value = hashName }; TraceLog.Trace("exec sql: {0}", strSql); MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_hashName); if (reader == null) { TraceLog.Trace("no record in table {0}, return reader is null!", tableName); return null; } if (reader.HasRows == false) { TraceLog.Trace("no record in table {0}", tableName); //记住一定要Close reader.Close(); return null; } tbName tbName = null; try { reader.Read(); tbName = new tbName(); tbName.HashName = reader.GetString("hashName"); tbName.StrName = reader.GetString("strName"); tbName.AccountID = reader.GetString("accountId"); TraceLog.Trace("query success HashName {0} AccountID {1}", tbName.HashName, tbName.AccountID); } catch (Exception ex) { TraceLog.Exception(ex); } finally { reader.Close(); } return tbName; } public bool InsertName(string hashName, string strName, string accountID, int realmId) { string tableName = NameServerUtils.GetNameTableName(0, hashName); TraceLog.Trace("InsertName hashName {0} strName {1} accountID {2}", hashName , strName, accountID); string strSql = "insert into " + tableName + "(hashName,strName,accountID, realmId)" + " values(?hashName,?strName,?accountID, ?realmId)"; MySqlParameter p_hashName = new MySqlParameter("?hashName", MySqlDbType.VarChar, 200) { Value = hashName }; MySqlParameter p_strName = new MySqlParameter("?strName", MySqlDbType.VarChar, 200) { Value = strName }; MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) { Value = accountID }; MySqlParameter p_realmId = new MySqlParameter("?realmId", MySqlDbType.Int32) { Value = realmId }; TraceLog.Trace("exec sql: {0}", strSql); MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_hashName, p_strName, p_accountId, p_realmId); if (reader == null) { TraceLog.Trace("InsertName failed table {0}, return reader is null!", tableName); return false; } reader.Close(); TraceLog.Trace("InsertName hashName {0} strName {1} accountID {2} success", hashName , strName, accountID); return true; } } }