📄 smarkdata.cs
字号:
cc.ExecuteNonQuery(mCommand);
}
}
/// <summary>
/// 字段值描述
/// </summary>
public class Field {
private object mValue;
public object Value {
get {
return mValue;
}
set {
mValue = value;
}
}
private string mName;
public string Name {
get {
return mName;
}
set {
mName = value;
}
}
private bool mIsParameter = true;
public bool IsParameter {
get {
return mIsParameter;
}
set {
mIsParameter = value;
}
}
public string GetValueBy {
get;
set;
}
public bool GetValueAfterInsert {
get;
set;
}
}
/// <summary>
/// 更新数据命令
/// </summary>
public class Update:ICommandExecute {
private Command mCommand;
public Update(string table) {
mCommand = new Command("Update " + table +" set ");
}
private IList<Field> mFields = new List<Field>();
public void AddField(string name, object value) {
AddField(name, value, true);
}
public void AddField(string name, object value, bool isparameter) {
Field f = new Field();
f.IsParameter = isparameter;
f.Name = name;
f.Value = value;
mFields.Add(f);
}
private Expression mWhere = new Expression();
public Expression Where
{
get {
return mWhere;
}
set {
mWhere = value;
}
}
#region ICommandExecute 成员
public void Execute()
{
using (IConnectinContext cc = DBContext.Context)
{
Execute(cc);
}
}
public void Execute(IConnectinContext cc) {
for (int i = 0; i < mFields.Count; i++) {
if (i > 0)
mCommand.Text.Append(",");
if (!mFields[i].IsParameter) {
mCommand.Text.Append(mFields[i].Name + "=" + mFields[i].Value);
} else {
mCommand.Text.Append(mFields[i].Name + "=@" + mFields[i].Name);
mCommand.AddParameter(mFields[i].Name, mFields[i].Value);
}
}
Where.Parse(mCommand);
cc.ExecuteNonQuery(mCommand);
}
#endregion
}
/// <summary>
/// 删除数据命令
/// </summary>
public class Delete:ICommandExecute {
private Command mCommand;
public Delete(string table) {
mCommand = new Command("Delete from " + table);
}
private Expression mWhere = new Expression();
public Expression Where
{
get
{
return mWhere;
}
set
{
mWhere = value;
}
}
#region ICommandExecute 成员
public void Execute()
{
using (IConnectinContext cc = DBContext.Context)
{
Execute(cc);
}
}
public void Execute(IConnectinContext cc) {
Where.Parse(mCommand);
cc.ExecuteNonQuery(mCommand);
}
#endregion
}
/// <summary>
/// 数据加载区间描述
/// </summary>
[Serializable]
public class Region
{
public Region()
{
}
public Region(int pageindex,int size)
{
Size = size;
Start = pageindex * size;
}
public int Start
{
get;
set;
}
public int Size
{
get;
set;
}
}
/// <summary>
/// 实体状态描述接口
/// </summary>
public interface IEntityState {
bool _Loaded {
get;
set;
}
Dictionary<string, FieldState> _FieldState
{
get;
}
void FieldChange(string field);
void LoadData(IDataReader reader);
}
public class FieldState
{
public DateTime ModifyTime
{
get;
set;
}
}
/// <summary>
/// 实体基础对象
/// </summary>
[Serializable]
public abstract class EntityBase : IEntityState {
private bool mLoadComplete = true;
#region IEntityState 成员
bool IEntityState._Loaded {
get;
set;
}
Dictionary<string, FieldState> m_FieldState = new Dictionary<string, FieldState>();
Dictionary<string, FieldState> IEntityState._FieldState
{
get { return m_FieldState; }
}
void IEntityState.FieldChange(string field) {
if(mLoadComplete)
if (!m_FieldState.ContainsKey(field)) {
m_FieldState.Add(field, new FieldState { ModifyTime=DateTime.Now });
}
}
public IEntityState EntityState {
get {
return (IEntityState)this;
}
}
#endregion
void IEntityState.LoadData(IDataReader reader) {
mLoadComplete = false;
OnLoadData(reader);
EntityState._Loaded = true;
mLoadComplete = true;
}
protected abstract void OnLoadData(IDataReader reader);
protected object Convert(Type type, object data) {
return System.Convert.ChangeType(data, type);
}
private static IList<T> OnList<T>(IConnectinContext cc, string from, Expression expression, Region region, string orderby, string groupby) where T : IEntityState, new() {
Command cmd = new Command(from);
if(expression != null)
expression.Parse(cmd);
if(!string.IsNullOrEmpty(groupby))
cmd.Text.Append(string.Concat(" Group by ", groupby));
if(!string.IsNullOrEmpty(orderby))
cmd.Text.Append( string.Concat(" Order by " , orderby));
return cc.List<T>(cmd, region);
}
internal static IList<T> ExOnList<T>(IConnectinContext cc, string from, Expression expression, Region region, string orderby, string groupby) where T : IEntityState, new()
{
return OnList<T>(cc, from, expression, region, orderby, groupby);
}
private static T OnListFirst<T>(IConnectinContext cc, string from, Expression expression, string orderby, string groupby) where T : IEntityState, new()
{
Command cmd = new Command(from);
if (expression != null)
expression.Parse(cmd);
if(!string.IsNullOrEmpty(groupby))
cmd.Text.Append(" Group by " + groupby);
if(!string.IsNullOrEmpty(orderby))
cmd.Text.Append(" Order by " + orderby);
return cc.ListFirst<T>(cmd);
}
internal static T ExOnListFirst<T>(IConnectinContext cc, string from, Expression expression, string orderby, string groupby) where T : IEntityState, new()
{
return OnListFirst<T>(cc, from, expression, orderby, groupby);
}
private static int OnCount(IConnectinContext cc, string table, Expression expression, string groupby)
{
Command cmd = new Command("select count(*) from " + table);
if (expression != null)
expression.Parse(cmd);
if (!string.IsNullOrEmpty(groupby))
cmd.Text.Append(" Group by " + groupby);
object value = cc.ExecuteScalar(cmd);
if (value == null || value == DBNull.Value)
return 0;
return System.Convert.ToInt32(value);
}
internal static int ExOnCount(IConnectinContext cc, string table, Expression expression, string groupby)
{
return OnCount(cc, table, expression, groupby);
}
private static void OnDelete(IConnectinContext cc, string table, Expression expression)
{
Delete del = new Delete(table);
del.Where = expression;
del.Execute(cc);
}
internal static void ExOnDelete(IConnectinContext cc, string table, Expression expression)
{
OnDelete(cc, table, expression);
}
private static T OnLoad<T>(IConnectinContext cc, string table, string id, object value) where T : IEntityState, new()
{
Command cmd = new Command(table + " where "+id+"=@id");
cmd.AddParameter("id", value);
return cc.Load<T>(cmd);
}
internal static T ExOnLoad<T>(IConnectinContext cc, string table, string id, object value) where T : IEntityState, new()
{
return OnLoad<T>(cc, table, id, value);
}
protected void OnSave(IConnectinContext cc, string table, IList<Field> fields, Field id)
{
if (EntityState._FieldState.Count == 0)
return;
if (EntityState._Loaded) {
OnEdit(cc,table, fields, id);
} else {
OnAdd(cc,table,fields, id);
}
}
private void OnAdd(IConnectinContext cc, string table, IList<Field> fields, Field id)
{
Command cmd;
Insert insert = new Insert(table);
if ( !string.IsNullOrEmpty(id.GetValueBy) && !id.GetValueAfterInsert)
{
cmd = new Command(id.GetValueBy);
id.Value = cc.ExecuteScalar(cmd);
insert.AddField(id.Name, id.Value);
}
if (string.IsNullOrEmpty(id.GetValueBy))
{
insert.AddField(id.Name, id.Value);
}
foreach (Field f in fields) {
if (EntityState._FieldState.ContainsKey(f.Name))
insert.AddField(f.Name, f.Value);
}
insert.Execute(cc);
if (!string.IsNullOrEmpty(id.GetValueBy) && id.GetValueAfterInsert)
{
cmd = new Command(id.GetValueBy);
id.Value = cc.ExecuteScalar(cmd);
}
EntityState._Loaded = true;
}
private void OnEdit(IConnectinContext cc, string table, IList<Field> fields, Field id)
{
Update update = new Update(table);
foreach (Field f in fields) {
if(EntityState._FieldState.ContainsKey(f.Name))
update.AddField(f.Name, f.Value);
}
update.Where.SqlText.Append(id.Name + "=@p1");
update.Where.Parameters.Add(new Command.Parameter { Name="p1",Value = id.Value});
update.Execute(cc);
}
protected void OnDeleteObject(IConnectinContext cc, string table, string id, object value)
{
Delete del = new Delete(table);
del.Where.SqlText.Append(id + "=@p1");
del.Where.Parameters.Add(new Command.Parameter { Name = "p1", Value = value });
del.Execute(cc);
}
private static object OnAggregation(IConnectinContext cc, string table, string aggregation, string field, bool DISTINCT, Expression expression, string groupby)
{
Command cmd = new Command("");
cmd.Text.Append(" select " + aggregation + "(" + (DISTINCT ? "DISTINCT" : "") + field + ") from "+table);
if (!string.IsNullOrEmpty(groupby))
cmd.Text.Append(" Group by " + groupby);
if (expression != null)
expression.Parse(cmd);
return cc.ExecuteScalar(cmd);
}
internal static object ExOnAggregation(IConnectinContext cc, string table, string aggregation, string field, bool DISTINCT, Expression expression, string groupby)
{
return OnAggregation(cc, table, aggregation, field, DISTINCT, expression, groupby);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -