/// <Summary>
/// Q4m queue operation base class
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// Author: luyifeng
/// Createday: 2013 05 02
Public abstract class myq4m <t>: iq4m <t>
{
Private bool _ isgetdata = false;
Private mysqlconnection _ conn = NULL;
Private readonly string _ connectionkey = NULL;
Public t queueitem {Get; set ;}
Protected ganjiq4m (string connectionkey)
{
_ Connectionkey = connectionkey;
}
Public t first ()
{
Dispose ();
Getdb ();
Queueitem = getitem ();
If (queueitem! = NULL)
{
_ Isgetdata = true;
}
Return queueitem;
}
Protected abstract string getitemsql ();
Private t getitem ()
{
String strsql = getitemsql ();
If (string. isnullorempty (strsql ))
{
Throw new exception ("No SQL statement is set to get the queue ");
}
VaR cmd = new mysqlcommand (strsql, _ conn );
Using (idatareader reader = cmd. executereader ())
{
If (reader. Read ())
{
VaR que = entityhelper. transformpoco <t> (Reader );
Return que;
}
}
Return default (t );
}
Public void remove ()
{
// The first data goes out of the queue
Const string strsql = "select queue_end ();";
Excutesql (strsql );
// Restore the status
Clearstate ();
}
Public void rollback ()
{
// Roll back data
Const string strsql = "select queue_abort ();";
Excutesql (strsql );
// Restore the status
Clearstate ();
}
Private void excutesql (string SQL)
{
VaR cmd = new mysqlcommand (SQL, _ conn );
Cmd. executenonquery ();
}
Private void getdb ()
{
If (string. isnullorempty (_ connectionkey ))
{
Throw new exception ("no connection string key configured ");
}
VaR connectionstring = system. configuration. configurationmanager. connectionstrings [_ connectionkey]. connectionstring;
_ Conn = new mysqlconnection (connectionstring );
_ Conn. open ();
}
Private void clearstate ()
{
_ Isgetdata = false;
Queueitem = default (t );
If (_ Conn! = NULL)
{
If (_ conn. State = connectionstate. open)
{
_ Conn. Close ();
}
_ Conn. Dispose ();
}
}
Public void dispose ()
{
If (_ isgetdata)
{
Rollback ();
}
}
}