編寫基于幾個(gè)表的關(guān)聯(lián)查詢和統(tǒng)計(jì)的確是件煩瑣的事情,由于基于字符的描述很多信息難以抽取出來共用,當(dāng)其他地方需要這種情況的時(shí)候又必須重寫。為了避免這情況sql artisan引用了視圖對(duì)象,通過視圖對(duì)象描述數(shù)據(jù)查詢的信息;視圖對(duì)象有一個(gè)最大的好處就是繼承,可以從一個(gè)已經(jīng)有的對(duì)象(實(shí)體對(duì)象或視圖對(duì)象)繼承下來擴(kuò)展新的查詢功能描述。實(shí)際應(yīng)用中你可以建立一個(gè)基礎(chǔ)統(tǒng)計(jì)視圖對(duì)象,然后根據(jù)情況派生出具體的統(tǒng)計(jì)對(duì)象(如:根據(jù)不同信息分組,顯示那些字段信息等).
下面是一些簡(jiǎn)單例程代碼:
訂單銷售金額統(tǒng)計(jì)基礎(chǔ)視圖對(duì)象
/// <summary>
/// 建立一個(gè)簡(jiǎn)單的訂單銷售金額統(tǒng)計(jì)類
/// </summary>
[tablemap("", tabletype.view)]
public class orderstat:hfsoft.data.itableview
{
#region itableview 成員
public virtual table gettable()
{
// todo: 添加orderv.gettable 實(shí)現(xiàn)
return dbmapping.orders.inner(dbmapping.employees, dbmapping.employees.employeeid)& dbmapping.orders.inner(dbmapping.orderdetails, dbmapping.orders.orderid)& dbmapping.orderdetails.inner(dbmapping.products, dbmapping.products.productid);
}
private double mtotalize;
[statcolumn("quantity*[order details].unitprice*(1-discount)", stattype.sum)]
public double totalize
{
get
{
return mtotalize;
}
set
{
mtotalize = value;
}
}
#endregion
}
按雇員分組統(tǒng)計(jì)情況繼承實(shí)現(xiàn)
/// <summary>
/// 按雇員進(jìn)行分組統(tǒng)計(jì)
/// </summary>
[tablemap("", tabletype.view)]
public class employeetotal:orderstat
{
private int memployeeid;
[viewcolumn("employees.employeeid")]
public int employeeid
{
get
{
return memployeeid;
}
set
{
memployeeid = value;
}
}
private string memployeename;
[viewcolumn("firstname+lastname")]
public string employeename
{
get
{
return memployeename;
}
set
{
memployeename = value;
}
}
}
按產(chǎn)品分組統(tǒng)計(jì)繼承實(shí)現(xiàn)
/// <summary>
/// 按產(chǎn)品進(jìn)行分組統(tǒng)計(jì)
/// </summary>
[tablemap("", tabletype.view)]
public class producttotal : orderstat
{
private int mproductid;
[viewcolumn("products.productid")]
public int productid
{
get
{
return mproductid;
}
set
{
mproductid = value;
}
}
private string mproductname;
[viewcolumn("productname")]
public string productname
{
get
{
return mproductname;
}
set
{
mproductname = value;
}
}
}
統(tǒng)計(jì)時(shí)在不更改條件的情況,你只需要加載不同的描述類型就能實(shí)現(xiàn)不同需求的數(shù)據(jù)查詢統(tǒng)計(jì)功能。
expression exp = new expression();
exp &= new hfsoft.data.mapping.numberfield("year(" + dbmapping.orders.orderdate.name + ")", null) == 1997;
list<employeetotal> empt= exp.list<employeetotal>();
list<producttotal> prot= exp.list<producttotal>();
關(guān)聯(lián)加載相關(guān)表信息字段
當(dāng)需要加載關(guān)聯(lián)表相關(guān)字段信息時(shí),可以建立一個(gè)繼承于實(shí)體對(duì)象的視對(duì)象;不過也可以根據(jù)實(shí)現(xiàn)情況建立一個(gè)全新的視圖對(duì)象。
產(chǎn)品信息視圖對(duì)象
/// <summary>
/// 產(chǎn)品信息視圖對(duì)象
/// </summary>
[tablemap("",tabletype.view)]
public class productsview:products,hfsoft.data.itableview
{
#region itableview 成員
public virtual table gettable()
{
return dbmapping.products.inner(dbmapping.categories, dbmapping.categories.categoryid)
& dbmapping.products.inner(dbmapping.suppliers, dbmapping.suppliers.supplierid);
}
#endregion
private string mcategoryname;
[viewcolumn("categoryname")]
public string categoryname
{
get
{
return mcategoryname;
}
set
{
mcategoryname = value;
}
}
private string mcompanyname;
[viewcolumn("companyname")]
public string companyname
{
get
{
return mcompanyname;
}
set
{
mcompanyname = value;
}
}
}
expression exp = new expression();
exp &= dbmapping.suppliers.city == "guangzhou";
exp.list<productsview>();
為了方便顯示,產(chǎn)品視圖對(duì)象引入了產(chǎn)品類別和供應(yīng)商信息。
以上是通過簡(jiǎn)單例程介紹sql artisan多查詢統(tǒng)計(jì)功能,組件試圖把所有數(shù)據(jù)輸出都以實(shí)體對(duì)象的方式體現(xiàn)來(主要簡(jiǎn)化訪問操作性);不過sql artisan并沒有完全支持所有sql語(yǔ)句的功能,只是實(shí)現(xiàn)了大部常用的功能。
新聞熱點(diǎn)
疑難解答
圖片精選