/// <summary> /// 查询 /// </summary> public Select_() { if (myWhere == null) { myWhere = new Where_(); } }
/// <summary> /// 删除条件 /// </summary> /// <returns></returns> public Where_ Where() { if (myWhere == null) { myWhere = new Where_(); } return(myWhere); }
private void AddUpdateObject(string keyName, T entity, Set_ updateSet, params string[] excludeColumns) { UpdateObject updateObject = new UpdateBuilder <T> .UpdateObject(); if (keyName != null && keyName.Trim().Length > 0) { Dictionary <string, object> dit = new Dictionary <string, object>(); Where_ tempWhere = new Where_(); object[] objs = null; PropertyInfo [] pis = typeof(T).GetProperties(); bool isKeyValueNull = true; // 主键值是否为null for (int i = 0; i < pis.Length; i++) { if (excludeColumns != null && excludeColumns.Length > 0) { bool isExclude = false; foreach (string excludeColumn in excludeColumns) { if (pis[i].Name.ToLower() == excludeColumn.Trim().ToLower()) { isExclude = true; break; } } if (isExclude) { continue; } } // 排除只显示列 objs = pis[i].GetCustomAttributes(false); if (objs != null && objs.Length > 0) { EntityAttribute cusatt = (EntityAttribute)objs[0]; if (cusatt.DisplayOnly) { continue; } } object value = pis[i].GetValue(entity, null); if (value == null) { continue; } if (pis[i].Name.ToLower() == keyName.Trim().ToLower()) { isKeyValueNull = false; tempWhere.Equal(keyName, value); continue; } dit.Add(pis[i].Name, value); } if (isKeyValueNull) { throw new Exception(string.Format("属性{0}的值不能为null", keyName)); } updateObject.Where = tempWhere; updateObject.DitUpdateInfo = dit; } else if (updateSet != null) { updateObject.DitUpdateInfo = updateSet.DitUpdateInfo; updateObject.Where = updateSet.MyWhere; } if (tranBuilder != null) { updateObject.CmdIndex = tranBuilder.GetCmdTextsCount(); // 在集合中站位,等创建脚本的时候再将内容填充 tranBuilder.AddCommondType(CommandType.Text); tranBuilder.AddCmdText(null); tranBuilder.AddCmdParam(null); } else { // 不是事务操作时,一次只操作一条语句 this.listUpdateObject.Clear(); updateObject.CmdIndex = listUpdateObject.Count; } this.listUpdateObject.Add(updateObject); }
/// <summary> /// 取WHERE 脚本 /// </summary> /// <param name="myWhere"></param> /// <returns></returns> public static string GetWhereText(Where_ myWhere) { if (myWhere == null) { return(""); } if (myWhere.CustomWhere != null && myWhere.CustomWhere.Trim().Length > 0) { return(" " + myWhere.CustomWhere); } // 当写为 .Where();,即Where()后面没有条件时,抛出异常 if (myWhere.ListWhereEntity.Count == 0 && (myWhere.MyOrderBy == null) ) { throw new Exception("Where后面的条件不能为空"); } StringBuilder sbWhereText = new StringBuilder(); foreach (WhereEntity item in myWhere.ListWhereEntity) { // 判断字段名是否为空 if (item.ColumnName == null || item.ColumnName.Trim().Length == 0) { throw new Exception(string.Format("Where后面字段名不能为空\r\n{0}", sbWhereText.ToString())); } // 判断值是否为null if (item.RelationType != RelationTypeEnum.IsNull && item.Value == null) { throw new Exception(string.Format("参数值不能为null,若查询值为null的字段,请使用IsNull(columnName):{0}\r\n{1}", item.ColumnName, sbWhereText.ToString())); } // 条件类型 if (item.ConditionType == ConditionTypeEnum.None) { sbWhereText.Append("WHERE "); } else if (item.ConditionType == ConditionTypeEnum.And) { sbWhereText.Append(" AND "); } else if (item.ConditionType == ConditionTypeEnum.Or) { sbWhereText.Append(" OR "); } // 字段名 sbWhereText.Append(item.ColumnName); // 参数关系类型 if (item.RelationType == RelationTypeEnum.Equal) { sbWhereText.Append(" = "); } else if (item.RelationType == RelationTypeEnum.EqualOrLarger) { sbWhereText.Append(" >= "); } else if (item.RelationType == RelationTypeEnum.Larger) { sbWhereText.Append(" > "); } else if (item.RelationType == RelationTypeEnum.EqualOrSmaller) { sbWhereText.Append(" <= "); } else if (item.RelationType == RelationTypeEnum.Smaller) { sbWhereText.Append(" < "); } else if (item.RelationType == RelationTypeEnum.IsNull) { sbWhereText.Append(" IS NULL "); } else if (item.RelationType == RelationTypeEnum.Like) { sbWhereText.Append(" LIKE '%" + item.Value.ToString() + "%'"); } else if (item.RelationType == RelationTypeEnum.LikeLeft) { sbWhereText.Append(" LIKE '%" + item.Value.ToString() + "'"); } else if (item.RelationType == RelationTypeEnum.LikeRight) { sbWhereText.Append(" LIKE '" + item.Value.ToString() + "%'"); } // 参数符号 if (item.RelationType != RelationTypeEnum.IsNull && item.RelationType != RelationTypeEnum.Like && item.RelationType != RelationTypeEnum.LikeLeft && item.RelationType != RelationTypeEnum.LikeRight ) { sbWhereText.Append("@"); sbWhereText.Append(item.ColumnName); if (item.ColumnIndex > 0) { sbWhereText.Append(item.ColumnIndex.ToString()); } } } return(sbWhereText.ToString()); }