JoinTableAccess<T> 源码
此源码为文章“.NET框架下ORM的一个轻量高效替代方案”的组成部分。
此类的基类 ReadonlyTableAccess<T> 源码:http://www.cnblogs.com/wfyfngu/archive/2010/01/28/1658374.html
using System;
using System.Collections.Generic;
using System.Text;
namespace FunTonn.MySqlDAL.SqlUtility {
/// <summary>
/// 提供一个抽象类,提供对2个表的连接访问
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class JoinTableAccess<T> : ReadonlyTableAccess<T> {
protected const char MAIN_TABLE_ALIAS = 'm';
protected const char SUB_TABLE_ALIAS = 's';
/// <summary>
/// 获取主表访问对象
/// </summary>
abstract internal IReadonlyTableAccess MainTableDAL { get;}
/// <summary>
/// 获取子表访问对象
/// </summary>
abstract internal IReadonlyTableAccess SubTableDAL { get;}
/// <summary>
/// 获取数据表的连接方式,如
/// join, inner join等
/// </summary>
virtual protected string JoinCommand {
get { return "join"; }
}
public override string TableName {
get {
// 将返回如下格式的字符串
// {0} {1} {6} {2} {3} {1}{4} {3}{5}
// maintable m join subtable s on m.primaryKey = s.primaryKey
return string.Format(
"{0} {1} {6} {2} {3} on {1}.{4}={3}.{5} ",
MainTableDAL.TableName,
MAIN_TABLE_ALIAS,
SubTableDAL.TableName,
SUB_TABLE_ALIAS,
MainTableDAL.TablePrimaryColumn.Name,
SubTableDAL.TablePrimaryColumn.Name,
JoinCommand
);
}
}
public override TableColumnCollection TableColumns {
get {
// 主表字段
TableColumnCollection mainColumns = MainTableDAL.TableColumns;
// 将主表字段全部加上表限定名(只能使用表别名)
foreach (TableColumn column in mainColumns) {
column.Name = MAIN_TABLE_ALIAS + "." + column.Name;
}
// 子表字段
TableColumnCollection subColumns = SubTableDAL.TableColumns;
//// 删除子表的主键字段
//subColumns.Remove(SubTableDAL.TablePrimaryColumn);
// 将子表字段全部加上表限定名(只能使用表别名)
foreach (TableColumn column in subColumns) {
column.Name = SUB_TABLE_ALIAS + "." + column.Name;
}
mainColumns.AddRange(subColumns);
return mainColumns;
}
}
//internal override T BuildEntity(MySql.Data.MySqlClient.MySqlDataReader reader, int startIndex) {
// throw new Exception("The method or operation is not implemented.");
//}
}
}
using System.Collections.Generic;
using System.Text;
namespace FunTonn.MySqlDAL.SqlUtility {
/// <summary>
/// 提供一个抽象类,提供对2个表的连接访问
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class JoinTableAccess<T> : ReadonlyTableAccess<T> {
protected const char MAIN_TABLE_ALIAS = 'm';
protected const char SUB_TABLE_ALIAS = 's';
/// <summary>
/// 获取主表访问对象
/// </summary>
abstract internal IReadonlyTableAccess MainTableDAL { get;}
/// <summary>
/// 获取子表访问对象
/// </summary>
abstract internal IReadonlyTableAccess SubTableDAL { get;}
/// <summary>
/// 获取数据表的连接方式,如
/// join, inner join等
/// </summary>
virtual protected string JoinCommand {
get { return "join"; }
}
public override string TableName {
get {
// 将返回如下格式的字符串
// {0} {1} {6} {2} {3} {1}{4} {3}{5}
// maintable m join subtable s on m.primaryKey = s.primaryKey
return string.Format(
"{0} {1} {6} {2} {3} on {1}.{4}={3}.{5} ",
MainTableDAL.TableName,
MAIN_TABLE_ALIAS,
SubTableDAL.TableName,
SUB_TABLE_ALIAS,
MainTableDAL.TablePrimaryColumn.Name,
SubTableDAL.TablePrimaryColumn.Name,
JoinCommand
);
}
}
public override TableColumnCollection TableColumns {
get {
// 主表字段
TableColumnCollection mainColumns = MainTableDAL.TableColumns;
// 将主表字段全部加上表限定名(只能使用表别名)
foreach (TableColumn column in mainColumns) {
column.Name = MAIN_TABLE_ALIAS + "." + column.Name;
}
// 子表字段
TableColumnCollection subColumns = SubTableDAL.TableColumns;
//// 删除子表的主键字段
//subColumns.Remove(SubTableDAL.TablePrimaryColumn);
// 将子表字段全部加上表限定名(只能使用表别名)
foreach (TableColumn column in subColumns) {
column.Name = SUB_TABLE_ALIAS + "." + column.Name;
}
mainColumns.AddRange(subColumns);
return mainColumns;
}
}
//internal override T BuildEntity(MySql.Data.MySqlClient.MySqlDataReader reader, int startIndex) {
// throw new Exception("The method or operation is not implemented.");
//}
}
}