private int OnAdd(IConnectinContext cc, string table, IList <Field> fields, Field id) { int result = 0; Command cmd; Insert insert = new Insert(table); if (!string.IsNullOrEmpty(id.GetValueBy) && !id.GetValueAfterInsert) { cmd = Command.GetThreadCommand().AddSqlText(id.GetValueBy); //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); } for (int i = 0; i < fields.Count; i++) { Field f = fields[i]; if (EntityState._FieldState.ContainsKey(f.Name)) { insert.AddField(f.Name, f.Value); } } result = insert.Execute(cc); if (!string.IsNullOrEmpty(id.GetValueBy) && id.GetValueAfterInsert) { cmd = Command.GetThreadCommand().AddSqlText(id.GetValueBy);// new Command(id.GetValueBy); id.Value = cc.ExecuteScalar(cmd); } EntityState._Loaded = true; return(result); }
/// <summary> /// 数据保存后处理过程 /// </summary> /// <param name="cc">数据库上下文</param> /// <param name="data">实体对象</param> /// <param name="pm">属性映射描述</param> /// <param name="table">相关表信息</param> public override void Executed(IConnectinContext cc, object data, PropertyMapper pm, string table) { Command cmd = new Command("select @@IDENTITY "); object value = cc.ExecuteScalar(cmd); pm.Handler.Set(data, Convert.ChangeType(value, pm.Handler.Property.PropertyType)); }
private object ExecuseAsSQL(IConnectinContext cc, SQL value) { object result = null; if (IsChangeDataSQL(value.Command.Text.ToString())) { result = cc.ExecuteNonQuery(value.Command); } result = cc.ExecuteScalar(value.Command); return(result); }
private static object OnAggregation(IConnectinContext cc, string table, string aggregation, string field, bool DISTINCT, Expression expression, string groupby) { Command cmd = Command.GetThreadCommand(); cmd.Text.Append(" select ").Append(aggregation).Append(" (").Append(DISTINCT ? "DISTINCT" : "").Append(field).Append(") from ").Append(table); if (!string.IsNullOrEmpty(groupby)) { cmd.Text.Append(" Group by ").Append(groupby); } if (expression != null) { expression.Parse(cmd); } return(cc.ExecuteScalar(cmd)); }
/// <summary> /// 验证处理过程,派生类重写实现具体的验证规则 /// </summary> /// <param name="value">属性值</param> /// <param name="source">属性所属对象</param> /// <param name="pm">属性映射描述</param> /// <param name="cc">数据库上下文</param> /// <returns>bool</returns> protected override bool OnValidating(object value, object source, PropertyMapper pm, IConnectinContext cc) { if (value == null) { return(true); } if (string.IsNullOrEmpty((string)value)) { return(true); } string sql = "select {0} from {1} where {0}=@p1"; Command cmd = new Command(string.Format(sql, pm.ColumnName, pm.OM.Table)); cmd.AddParameter("p1", value); object result = cc.ExecuteScalar(cmd); return(result == null || result == DBNull.Value); }
private static int OnCount(IConnectinContext cc, string table, Expression expression, string groupby) { Command cmd = Command.GetThreadCommand().AddSqlText("select count(*) from ").AddSqlText(table);// new Command("select count(*) from " + table); if (expression != null) { expression.Parse(cmd); } if (!string.IsNullOrEmpty(groupby)) { cmd.Text.Append(" Group by ").Append(groupby); } object value = cc.ExecuteScalar(cmd); if (value == null || value == DBNull.Value) { return(0); } return(System.Convert.ToInt32(value)); }
/// <summary> /// 在指写数据库上执行SQL并返回指定类型的值 /// </summary> /// <typeparam name="T">返回值类型</typeparam> /// <param name="cc">数据库访问上下文</param> /// <returns>返回值</returns> public T GetValue <T>(IConnectinContext cc) { return((T)cc.ExecuteScalar(GetCommand())); }
/// <summary> /// 验证处理过程,派生类重写实现具体的验证规则 /// </summary> /// <param name="value">属性值</param> /// <param name="source">属性所属对象</param> /// <param name="pm">属性映射描述</param> /// <param name="cc">数据库上下文</param> /// <returns>bool</returns> protected override bool OnValidating(object value, object source, Mappings.PropertyMapper pm, IConnectinContext cc) { if (value == null) return true; if (string.IsNullOrEmpty((string)value)) return true; string sql = "select {0} from {1} where {0}=@p1"; Command cmd = new Command(string.Format(sql, pm.ColumnName, pm.OM.Table)); cmd.AddParameter("p1", value); object result = cc.ExecuteScalar(cmd); return result == null || result == DBNull.Value; }
/// <summary> /// 数据保存后处理过程 /// </summary> /// <param name="cc">数据库上下文</param> /// <param name="data">实体对象</param> /// <param name="pm">属性映射描述</param> /// <param name="table">相关表信息</param> public override void Executed(IConnectinContext cc, object data, PropertyMapper pm, string table) { Command cmd = new Command("select @@IDENTITY "); object value = cc.ExecuteScalar(cmd); pm.Handler.Set(data,Convert.ChangeType( value,pm.Handler.Property.PropertyType)); }