internal DbCommand CreateDelete <T>(Table table, WhereClip where) where T : Entity { T entity = DataUtils.CreateInstance <T>(); if (entity.GetReadOnly()) { throw new MySoftException("只读实体" + typeof(T).Name + "只能用于查询!"); } if ((object)where == null) { throw new MySoftException("删除条件不能为null!"); } //移除缓存 RemoveCache(typeof(T).Name); StringBuilder sb = new StringBuilder(); string tableName = table == null?entity.GetTable().Name : table.Name; sb.Append("delete from " + tableName); if (!DataUtils.IsNullOrEmpty(where)) { sb.Append(" where " + where.ToString()); } return(CreateSqlCommand(sb.ToString(), where.Parameters)); }
private FromSection <T> Join <TJoin>(Table table, WhereClip onWhere, JoinType joinType) where TJoin : Entity { TJoin entity = DataUtils.CreateInstance <TJoin>(); this.entityList.Add(entity); if ((IField)query.PagingField == null) { //标识列和主键优先,包含ID的列被抛弃 query.PagingField = entity.PagingField; } FromSection <TJoin> from = new FromSection <TJoin>(table); string strJoin = string.Empty; if (onWhere != null) { strJoin = " on " + onWhere.ToString(); } string join = GetJoinEnumString(joinType); if (this.relation != null) { this.tableName = " {2} " + this.tableName; this.relation += " {3} "; } this.relation += join + from.TableName + strJoin; return(this); }
internal DbCommand CreateDelete <T>(Table table, WhereClip where) where T : Entity { T entity = CoreHelper.CreateInstance <T>(); if (entity.GetReadOnly()) { throw new DataException("只读实体" + typeof(T).Name + "只能用于查询!"); } if ((object)where == null) { throw new DataException("删除条件不能为null!"); } //移除缓存 //if (this.Cache != null) Cache.RemoveCache<T>(); StringBuilder sb = new StringBuilder(); string tableName = table == null?entity.GetTable().Name : table.Name; sb.Append("DELETE FROM " + tableName); if (!DataHelper.IsNullOrEmpty(where)) { sb.Append(" WHERE " + where.ToString()); } return(CreateSqlCommand(sb.ToString(), where.Parameters)); }
/// <summary> /// 判断WhereClip是否为null或空 /// </summary> /// <param name="where"></param> /// <returns></returns> public static bool IsNullOrEmpty(WhereClip where) { if ((object)where == null || string.IsNullOrEmpty(where.ToString())) { return(true); } return(false); }
/// <summary> /// 返回最终条件的SQL /// </summary> /// <param name="where"></param> /// <returns></returns> public string Serialization(WhereClip where) { string sql = dbProvider.FormatCommandText(where.ToString()); foreach (SQLParameter p in where.Parameters) { sql = sql.Replace(p.Name, DataHelper.FormatValue(p.Value)); } return(sql); }
private FromSection <T> Join <TJoin>(TableRelation <TJoin> relation, string aliasName, WhereClip onWhere, JoinType joinType) where TJoin : Entity { //将TableRelation的对象添加到当前节 this.entities.AddRange(relation.GetFromSection().TableEntities); TJoin entity = CoreHelper.CreateInstance <TJoin>(); var table = entity.GetTable().As(aliasName); if ((IField)query.PagingField == null) { //标识列和主键优先,包含ID的列被抛弃 query.SetPagingField(entity.PagingField); } string tableName = entity.GetTable().Name; if (aliasName != null) { tableName = aliasName; } //处理tableRelation关系 string joinString = "(" + relation.GetFromSection().Query.QueryString + ") " + tableName; this.query.Parameters = relation.GetFromSection().Query.Parameters; string strJoin = string.Empty; if (onWhere != null) { strJoin = " ON " + onWhere.ToString(); } //获取关联方式 string join = GetJoinEnumString(joinType); if (this.relation != null) { this.tableName = " __[[ " + this.tableName; this.relation += " ]]__ "; } this.relation += join + joinString + strJoin; return(this); }
private FromSection <T> Join <TJoin>(Table table, string aliasName, WhereClip onWhere, JoinType joinType) where TJoin : Entity { TJoin entity = CoreHelper.CreateInstance <TJoin>(); table = table ?? entity.GetTable(); table.As(aliasName); //创建一个TableEntity var tableEntity = new TableEntity { Table = table, Entity = entity }; this.entities.Add(tableEntity); if ((IField)query.PagingField == null) { //标识列和主键优先,包含ID的列被抛弃 query.SetPagingField(entity.PagingField); } string strJoin = string.Empty; if (onWhere != null) { strJoin = " ON " + onWhere.ToString(); } //获取关联方式 string join = GetJoinEnumString(joinType); if (this.relation != null) { this.tableName = " __[[ " + this.tableName; this.relation += " ]]__ "; } this.relation += join + table.FullName + strJoin; return(this); }
internal DbCommand CreateUpdate <T>(Table table, List <FieldValue> fvlist, WhereClip where) where T : Entity { T entity = DataUtils.CreateInstance <T>(); if (entity.GetReadOnly()) { throw new MySoftException("只读实体" + typeof(T).Name + "只能用于查询!"); } if ((object)where == null) { throw new MySoftException("更新条件不能为null!"); } //移除缓存 RemoveCache(typeof(T).Name); string tableName = table == null?entity.GetTable().Name : table.Name; List <SQLParameter> plist = new List <SQLParameter>(); StringBuilder sb = new StringBuilder(); sb.Append("update " + tableName + " set "); fvlist.ForEach(fv => { if (fv.IsPrimaryKey || fv.IsIdentity) { return; } if (fv.IsChanged) { if (CheckValue(fv.Value)) { sb.Append(fv.Field.At((string)null).Name + " = " + DataUtils.FormatValue(fv.Value)); } else { SQLParameter p = null; if (CheckStruct(fv.Value)) { p = CreateOrmParameter(DataUtils.FormatValue(fv.Value)); } else { p = CreateOrmParameter(fv.Value); } sb.Append(fv.Field.At((string)null).Name + " = " + p.Name); plist.Add(p); } sb.Append(","); } }); sb.Remove(sb.Length - 1, 1); if (!DataUtils.IsNullOrEmpty(where)) { sb.Append(" where " + where.ToString()); plist.AddRange(where.Parameters); } return(CreateSqlCommand(sb.ToString(), plist.ToArray())); }
/// <summary> /// 添加括号 /// </summary> /// <param name="where"></param> /// <returns></returns> public static WhereClip Bracket(WhereClip where) { return(new WhereClip("(" + where.ToString() + ")", where.Parameters)); }