using System;
using Sog;
using ProtoCSStruct;
using MySql.Data.MySqlClient;
namespace Mail
{
public class MysqlMailDBOperator : MailDBOperator
{
private MySqlDB m_mySqlDb;
public MysqlMailDBOperator(string db, string ip, string user, string password)
{
m_mySqlDb = new MySqlDB(db, ip, user, password);
}
//销毁的时候置空
public override void Dispose()
{
m_mySqlDb.Dispose();
m_mySqlDb = null;
}
public override void KeepAlive()
{
string strSql = "select * from tbmail_1 where uid=0";
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlMailDBOperator.KeepAlive no record , return reader is null");
return;
}
//记住一定要Close
reader.Close();
TraceLog.Trace("MysqlMailDBOperator.KeepAlive exec at {0}", DateTime.Now);
}
///
/// 返回0 成功
/// 1 表示空,没记录
/// -1 表示失败
///
///
///
///
public override int QueryMail(long uid, ref DBMailBlob blob)
{
string tableName = MailServerUtils.GetMailTableName(uid);
TraceLog.Trace("MysqlMailDBOperator.QueryMail uid {0} to table {1}", uid, tableName);
string strSql = string.Format("select data from {0} where uid={1}", tableName, uid);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlMailDBOperator.QueryMail no record in table {0}, return reader is null!", tableName);
return -1;
}
//没有记录是正常的
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlMailDBOperator.QueryMail no record in table {0}", tableName);
//记住一定要Close
reader.Close();
return 1;
}
try
{
reader.Read();
int dataIndex = reader.GetOrdinal("data");
byte[] buffer = new byte[500 * 1024];
long dataLength = reader.GetBytes(dataIndex, 0, buffer, 0, buffer.Length);
byte[] dataByte = new byte[dataLength];
Buffer.BlockCopy(buffer, 0, dataByte, 0, (int)dataLength);
dataByte = UnzipData(dataByte);
StructMessageParseUtils.ParseFrom(ref blob, dataByte);
TraceLog.Trace("MysqlMailDBOperator.QueryMail query success uid {0} ", uid);
return 0;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return -1;
}
public override bool InsertMail(long uid, ref DBMailBlob blob)
{
string tableName = MailServerUtils.GetMailTableName(uid);
TraceLog.Trace("MysqlMailDBOperator.InsertMail uid {0} to table {1}", uid, tableName);
string strSql = string.Format("insert into {0} (uid,data) values({1},?data)"
, tableName, uid);
byte[] dataByte = StructMessageParseUtils.ToByteArray(ref blob);
dataByte = ZipData(dataByte);
MySqlParameter p_data = new MySqlParameter("?data", MySqlDbType.MediumBlob) { Value = dataByte };
TraceLog.Trace("MysqlMailDBOperator.InsertMail exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_data);
if (reader == null)
{
TraceLog.Error("MysqlMailDBOperator.InsertMail failed, return reader is null!");
return false;
}
reader.Close();
TraceLog.Trace("MysqlMailDBOperator.InsertMail uid {0} to table success,data list count={1}", uid,blob.MailList.Count);
return true;
}
public override bool UpdateMail(long uid, ref DBMailBlob blob)
{
string tableName = MailServerUtils.GetMailTableName(uid);
TraceLog.Trace("MysqlMailDBOperator.UpdateMail uid {0} to table {1}", uid, tableName);
string strSql = string.Format("update {0} set data=?data where uid={1}", tableName, uid);
byte[] dataByte = StructMessageParseUtils.ToByteArray(ref blob);
dataByte = ZipData(dataByte);
MySqlParameter p_data = new MySqlParameter("?data", MySqlDbType.MediumBlob) { Value = dataByte };
TraceLog.Trace("MysqlMailDBOperator.UpdateMail exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_data);
if (reader == null)
{
TraceLog.Error("MysqlMailDBOperator.UpdateMail failed, return reader is null!");
return false;
}
reader.Close();
TraceLog.Trace("MysqlMailDBOperator.UpdateMail uid {0} to table success,data count={1}", uid,blob.MailList.Count);
return true;
}
}
}