public virtual LiteDataTable GetLOB(bool withLOB, bool hasTablePrefix) { ConditionalSql sql = null; if (!withLOB) { if (hasTablePrefix) { sql = @"select b.{*} from book b"; } else { sql = @"select {*} from book"; } } else { sql = @"select * from book"; } return((this.DataQueryer as RdbDataQueryer).QueryTable(sql)); }
/// <summary> /// 根据传入的属性列表,来构造 CommonQueryCriteria /// 返回是否有非空属性需要验证。 /// </summary> /// <param name="entity"></param> /// <param name="criteria"></param> /// <returns></returns> private bool AddToCriteria(Entity entity, CommonQueryCriteria criteria) { bool hasValue = false; foreach (IProperty property in this.Properties) { EnsurePropertyCategory(property); var value = entity.GetProperty(property); if (ConditionalSql.IsNotEmpty(value)) { hasValue = true; criteria.Add(property, value); } } if (entity.HasId) { criteria.Add(Entity.IdProperty, PropertyOperator.NotEqual, entity.Id); } return(hasValue); }
private LiteDataTable DA_GetLOB(bool withLOB, bool hasTablePrefix) { ConditionalSql sql = null; if (!withLOB) { if (hasTablePrefix) { sql = @"select b.{*} from book b"; } else { sql = @"select {*} from book"; } } else { sql = @"select * from book"; } return(this.QueryTable(sql)); }
/// <summary> /// 通过一个 ConditionalSql 来构造。 /// </summary> /// <param name="sql"></param> public TableQueryArgs(ConditionalSql sql) : this(sql, sql.Parameters) { }
/// <summary> /// 更新某个冗余属性 /// </summary> /// <param name="entity">The entity.</param> /// <param name="redundancy">更新指定的冗余属性</param> /// <param name="newValue">冗余属性的新值</param> /// <param name="refPathes">从冗余属性声明类型开始的一个引用属性集合, /// 将会为这个集合路径生成更新的 Where 条件。</param> /// <param name="lastRefId">引用路径中最后一个引用属性对应的值。这个值将会作为 Where 条件的值。</param> private void UpdateRedundancy(Entity entity, ConcreteProperty redundancy, object newValue, IList <ConcreteProperty> refPathes, object lastRefId) { /*********************** 代码块解释 ********************************* * * 假定场景是: * refPathes: D(CRef,AName) -> C(BRef) -> B(ARef), * lastRefId: AId。(B.ARef 是最后一个引用属性) * 则对应生成的 SQL 是: * update D set AName = @AName where CId in ( * select id from C where BId in ( * select id from B where AId = @AId * ) * ) * 如果 B、C 表也有冗余属性,对应的 SQL 则是: * update B Set AName = @AName where AId = @BId * update C set AName = @AName where BId in ( * select id from B where AId = @AId * ) * **********************************************************************/ //准备所有用到的 DbTable var table = DbTableFinder.TableFor(redundancy.Owner); var refTables = new RefPropertyTable[refPathes.Count]; for (int i = 0, c = refPathes.Count; i < c; i++) { var refProperty = refPathes[i]; var refTable = DbTableFinder.TableFor(refProperty.Owner); if (refTable == null) { ORMHelper.ThrowBasePropertyNotMappedException(refProperty.Name, refProperty.Owner); } refTables[i] = new RefPropertyTable { RefProperty = refProperty, OwnerTable = refTable }; } var sql = new ConditionalSql(); //SQL: UPDATE D SET AName = {0} WHERE sql.Append("UPDATE ").AppendQuoteName(table) .Append(" SET ").AppendQuote(table, table.Translate(redundancy.Property)) .Append(" = ").AppendParameter(newValue).Append(" WHERE "); int quoteNeeded = 0; if (refTables.Length > 1) { //中间的都生成 Where XX in var inWherePathes = refTables.Take(refTables.Length - 1).ToArray(); for (int i = 0; i < inWherePathes.Length; i++) { var inRef = inWherePathes[i]; //SQL: CId In ( sql.AppendQuote(table, inRef.OwnerTable.Translate(inRef.RefProperty.Property)) .Append(" IN (").AppendLine(); quoteNeeded++; var nextRef = refTables[i + 1]; //SQL: SELECT Id FROM C WHERE var nextRefOwnerTable = nextRef.OwnerTable; sql.Append(" SELECT ").AppendQuote(nextRefOwnerTable, nextRefOwnerTable.PKColumn.Name) .Append(" FROM ").AppendQuoteName(nextRefOwnerTable) .Append(" WHERE "); } } //最后一个,生成SQL: BId = {1} var lastRef = refTables[refTables.Length - 1]; sql.AppendQuote(table, lastRef.OwnerTable.Translate(lastRef.RefProperty.Property)) .Append(" = ").AppendParameter(lastRefId); while (quoteNeeded > 0) { sql.AppendLine(")"); quoteNeeded--; } //执行最终的 SQL 语句 using (var dba = this.CreateDbAccesser()) { dba.ExecuteText(sql, sql.Parameters); } }
/// <summary> /// <see cref="CommonQueryCriteria"/> 查询的数据层实现。 /// </summary> /// <param name="criteria"></param> public virtual EntityList GetBy(CommonQueryCriteria criteria) { var table = qf.Table(_repository); var q = qf.Query(table); var allProperties = _repository.EntityMeta.ManagedProperties.GetNonReadOnlyCompiledProperties(); //拼装所有 Where 条件。 bool ignoreNull = criteria.IgnoreNull; foreach (var group in criteria.Groups) { IConstraint groupRes = null; foreach (var pm in group) { var property = allProperties.Find(pm.PropertyName); if (property != null) { var op = pm.Operator; var value = pm.Value; bool ignored = false; if (ignoreNull) { ignored = !ConditionalSql.IsNotEmpty(value); } else { if (value is string || (value == null && property.PropertyType == typeof(string))) { #region 如果是对空字符串进行模糊匹配,那么这个条件需要被忽略。 var strValue = value as string; if (string.IsNullOrEmpty(strValue)) { switch (op) { case PropertyOperator.Like: case PropertyOperator.Contains: case PropertyOperator.StartWith: case PropertyOperator.EndWith: ignored = true; break; default: break; } } #endregion } } if (!ignored) { var propertyRes = qf.Constraint(table.Column(property), op, value); groupRes = qf.Binary(groupRes, group.Concat, propertyRes); } } } q.Where = qf.Binary(groupRes, criteria.Concat, q.Where); } //OrderBy if (!string.IsNullOrWhiteSpace(criteria.OrderBy)) { var orderBy = allProperties.Find(criteria.OrderBy); if (orderBy != null) { var dir = criteria.OrderByAscending ? OrderDirection.Ascending : OrderDirection.Descending; q.OrderBy.Add(table.Column(orderBy), dir); } } return(this.QueryList(q, criteria.PagingInfo, criteria.EagerLoad)); }