/// <summary> /// 构造一个属性与指定值"相等"的约束条件节点。 /// </summary> /// <param name="column">要对比的属性。</param> /// <param name="value">要对比的值。</param> /// <returns></returns> public IConstraint Constraint( IColumnNode column, object value ) { return(Constraint(column, PropertyOperator.Equal, value)); }
/// <summary> /// 构造一个两个属性"相等"的约束条件节点。 /// </summary> /// <param name="leftColumn">第一个需要对比的列。</param> /// <param name="rightColumn">第二个需要对比的列。</param> /// <returns></returns> public IConstraint Constraint( IColumnNode leftColumn, IColumnNode rightColumn ) { return(Constraint(leftColumn, PropertyOperator.Equal, rightColumn)); }
public static IConstraint NotIn(this IColumnNode column, object value) { var parameters = value as IList; IConstraint res = null; //处理大数目NotIn 条件 var maxItemsCount = SqlGenerator.CampatibleMaxItemsInInClause; if (parameters != null && parameters.Count > maxItemsCount) { var start = 0; while (start < parameters.Count) { var paramSection = new List <object>(maxItemsCount); var end = Math.Min(start + maxItemsCount - 1, parameters.Count - 1); for (int i = start; i <= end; i++) { paramSection.Add(parameters[i]); } var sectionConstraint = QueryFactory.Instance.Constraint(column, PropertyOperator.NotIn, paramSection); res = QueryFactory.Instance.And(res, sectionConstraint); start += paramSection.Count; } } else { res = QueryFactory.Instance.Constraint(column, PropertyOperator.NotIn, value); } return(res); }
/// <summary> /// 添加列节点 /// </summary> /// <param name="columnNode">列节点</param> public void Add(IColumnNode columnNode) { if (_columnNodes.Count(n => n.ColumnName.Equals(columnNode.ColumnName)) == 0) { _columnNodes.Add(columnNode); } }
/// <summary> /// 构造一个排序节点并添加到当前集合中。。 /// </summary> /// <param name="orderByList">实例.</param> /// <param name="property">使用这个属性进行排序。</param> /// <param name="direction">使用这个方向进行排序。</param> /// <returns></returns> public static IOrderBy Add(this ICollection <IOrderBy> orderByList, IColumnNode property, OrderDirection direction = OrderDirection.Ascending) { var item = QueryFactory.Instance.OrderBy(property, direction); orderByList.Add(item); return(item); }
public void Parse(string filter, IQuery query) { _query = query; _mainTable = query.MainTable; _column = null; _comparison = null; _concat = null; _current = null; _f = QueryFactory.Instance; _bracketStack = new Stack <StackItem>(); _filter = filter; _filterIndex = 0; while (true) { var part = this.ReadPart(); if (part == string.Empty) { break; } this.DealPart(part); } query.Where = _current; }
/// <summary> /// 构造一个属性的约束条件节点。 /// </summary> /// <param name="column">要对比的属性。</param> /// <param name="op">对比操作符</param> /// <param name="value">要对比的值。</param> /// <returns></returns> public IConstraint Constraint( IColumnNode column, PropertyOperator op, object value ) { IColumnConstraint res = new Impl.ColumnConstraint(); res.Column = column; res.Operator = op; res.Value = value; return(res); }
/// <summary> /// 构造一个排序节点。 /// </summary> /// <param name="property">使用这个属性进行排序。</param> /// <param name="direction">使用这个方向进行排序。</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">property</exception> public IOrderBy OrderBy(IColumnNode property, OrderDirection direction = OrderDirection.Ascending) { if (property == null) { throw new ArgumentNullException("property"); } IOrderBy res = new Impl.OrderBy(); res.Column = property; res.Direction = direction; return(res); }
IColumnNode ISubQuery.Column(IColumnNode rawProperty) { var raw = rawProperty as ColumnNode; var property = new ColumnNode { ColumnName = raw.ColumnName, Property = raw.Property, Table = this, }; return(property); }
private void VisitValueProperty(IManagedProperty mp, ITableSource mpOwnerTable) { //如果已经记录了条件的属性,那么当前的 mp 就是用于对比的第二个属性。(A.Code = A.Name 中的 Name) if (_propertyResult != null) { _rightPropertyResult = mpOwnerTable.Column(mp); } //如果还没有记录属性,说明当前条件要比较的属性就是 mp;(A.Code = 1 中的 Code) else { _propertyResult = mpOwnerTable.Column(mp); } }
IColumnNode ISubQuery.Column(IColumnNode rawProperty) { var raw = rawProperty as ColumnNode; var property = new ColumnNode { ColumnName = raw.ColumnName, Property = raw.Property, Table = this, }; return property; }
/// <summary> /// 通过目前已经收集到的属性、操作符、值,来生成一个属性条件结果。 /// 并清空已经收集的信息。 /// </summary> private void MakeConstraint() { if (_propertyResult != null && _operator.HasValue) { if (_hasValueResult) { _query.Where = f.Constraint(_propertyResult, _operator.Value, _valueResult); _hasValueResult = false; } else { _query.Where = f.Constraint(_propertyResult, _operator.Value, _rightPropertyResult); _rightPropertyResult = null; } _propertyResult = null; _operator = null; } }
private void VisitValueProperty(IManagedProperty mp, ITableSource mpOwnerTable) { //接下来,是一般属性的处理 if (_visitRefProperties) { throw OperationNotSupported(string.Format("不支持使用属性:{0}。这是因为它的拥有者是一个值属性,值属性只支持直接对比。", mp.Name)); } //如果已经记录了条件的属性,那么当前的 mp 就是用于对比的第二个属性。(A.Code = A.Name 中的 Name) if (_propertyResult != null) { _rightPropertyResult = mpOwnerTable.Column(mp); } //如果还没有记录属性,说明当前条件要比较的属性就是 mp;(A.Code = 1 中的 Code) else { _propertyResult = mpOwnerTable.Column(mp); } }
/// <summary> /// 通过目前已经收集到的属性、操作符、值,来生成一个属性条件结果。 /// 并清空已经收集的信息。 /// </summary> private bool MakeConstraint() { if (_propertyResult != null && _operator.HasValue) { var op = _operator.Value; if (_reverseWhere) { op = PropertyOperatorHelper.Reverse(op); } if (_hasValueResult) { _constraintResult = f.Constraint(_propertyResult, op, _valueResult); _valueResult = null; _hasValueResult = false; } else { _constraintResult = f.Constraint(_propertyResult, op, _rightPropertyResult); _rightPropertyResult = null; } _propertyResult = null; _operator = null; } if (_constraintResult != null) { if (_nullableRefConstraint != null) { var concat = _reverseWhere ? BinaryOperator.Or : BinaryOperator.And; _constraintResult = f.Binary(_nullableRefConstraint, concat, _constraintResult); _nullableRefConstraint = null; } _query.Where = _constraintResult; _constraintResult = null; return(true); } return(false); }
/// <summary> /// 拼接列 /// </summary> /// <param name="commandText">待拼接的sql</param> /// <param name="columnNode">列</param> protected virtual void AppendColumn(StringBuilder commandText, IColumnNode columnNode) { //拼接列名 this.AppendColumnName(commandText, columnNode.ColumnName); //拼接数据类型 commandText.AppendFormat("\t{0}", _columnNodeHelper.GetSqlType(columnNode.DataType)); //拼接数据类型长度及小数点位数 if (columnNode.Length != null && columnNode.Decimals != null) { commandText.AppendFormat("({0}, {1})", columnNode.Length, columnNode.Decimals); } else if (columnNode.Length != null) { commandText.AppendFormat("({0})", columnNode.Length); } //拼接默认值 if (columnNode.IsDefault) { string defaultValue = _columnNodeHelper.GetDefaultValue(columnNode.DataType); if (!string.IsNullOrEmpty(defaultValue)) { commandText.AppendFormat("\tDEFAULT {0}", defaultValue); } } //拼接主键 if (columnNode.IsPrimary) { commandText.Append("\tPRIMARY KEY"); } //拼接自增列 if (columnNode.IsIdentity) { commandText.AppendFormat("\t{0}", _columnNodeHelper.GetIdentity()); } //拼接是否为空(非主键列才可以定义NULL 或 NOT NULL) if (!columnNode.IsPrimary) { commandText.AppendFormat("\t{0}", columnNode.IsNull ? "NULL" : "NOT NULL"); } }
/// <summary> /// 构造一个两个属性进行对比的约束条件节点。 /// </summary> /// <param name="leftColumn">第一个需要对比的列。</param> /// <param name="op">对比条件。</param> /// <param name="rightColumn">第二个需要对比的列。</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException"> /// leftProperty /// or /// rightProperty /// </exception> public IConstraint Constraint( IColumnNode leftColumn, PropertyOperator op, IColumnNode rightColumn ) { if (leftColumn == null) { throw new ArgumentNullException("leftProperty"); } if (rightColumn == null) { throw new ArgumentNullException("rightProperty"); } IColumnsComparison res = new Impl.ColumnsComparison(); res.LeftColumn = leftColumn; res.RightColumn = rightColumn; res.Operator = op; return(res); }
public void Parse(string filter, IQuery query) { _query = query; _mainTable = query.MainTable; _column = null; _comparison = null; _concat = null; _current = null; _f = QueryFactory.Instance; _bracketStack = new Stack<StackItem>(); using (_reader = new StringReader(filter)) { while (true) { var part = this.ReadPart(); if (part == string.Empty) break; this.DealPart(part); } } query.Where = _current; }
/// <summary> /// Copies all properties from <paramref name="columnDefinition"/> to appropriate <see cref="IAstNodeProperty"/> /// objects and adds them to <paramref name="columnNode"/>. /// </summary> /// <param name="columnDefinition"></param> /// <param name="columnNode"></param> public static void CopyProperties(IColumnDefinition columnDefinition, IColumnNode columnNode) { if(columnDefinition.Type != null) AddProperty(columnNode, MdlSyntax.Type, columnDefinition.Type.Value.ToString()); if(columnDefinition.Nullable.HasValue) AddProperty(columnNode, MdlSyntax.Nullable, columnDefinition.Nullable.Value.ToString().ToLower()); if(columnDefinition.Length.HasValue) AddProperty(columnNode, MdlSyntax.Length, columnDefinition.Length.Value); if(columnDefinition.Precision.HasValue) AddProperty(columnNode, MdlSyntax.Precision, columnDefinition.Precision.Value); if(columnDefinition.Scale.HasValue) AddProperty(columnNode, MdlSyntax.Scale, columnDefinition.Scale.Value); if(columnDefinition.PrimaryKey.GetValueOrDefault(false)) AddProperty(columnNode, MdlSyntax.PrimaryKey, "true"); if(columnDefinition.Identity.GetValueOrDefault(false)) AddProperty(columnNode, MdlSyntax.Identity, "true"); }
public static IConstraint EndWith(this IColumnNode column, IColumnNode rightColumn) { return(QueryFactory.Instance.Constraint(column, PropertyOperator.EndsWith, rightColumn)); }
public static IConstraint LessEqual(this IColumnNode column, IColumnNode rightColumn) { return(QueryFactory.Instance.Constraint(column, PropertyOperator.LessEqual, rightColumn)); }
public static IConstraint Greater(this IColumnNode column, IColumnNode rightColumn) { return QueryFactory.Instance.Constraint(column, PropertyOperator.Greater, rightColumn); }
public static IConstraint NotEndWith(this IColumnNode column, object value) { return(QueryFactory.Instance.Constraint(column, PropertyOperator.NotEndsWith, value)); }
public static IConstraint Contains(this IColumnNode column, object value) { return(QueryFactory.Instance.Constraint(column, PropertyOperator.Contains, value)); }
public static IConstraint LessEqual(this IColumnNode column, object value) { return(QueryFactory.Instance.Constraint(column, PropertyOperator.LessEqual, value)); }
private bool VisitMethod_Queryable(MethodCallExpression exp) { var args = exp.Arguments; if (args.Count == 2) { this.Visit(args[0]);//visit queryable var lambda = StripQuotes(args[1]) as LambdaExpression; var previousWhere = _query.Where; this.Visit(lambda.Body); switch (exp.Method.Name) { case LinqConsts.QueryableMethod_Where: //如果现在不是第一次调用 Where 方法,那么需要把本次的约束和之前的约束进行 And 合并。 this.MakeBooleanConstraintIfNoValue(); if (_query.Where != null && previousWhere != null) { _query.Where = f.And(previousWhere, _query.Where); } break; case LinqConsts.QueryableMethod_OrderBy: case LinqConsts.QueryableMethod_ThenBy: if (_propertyResult != null) { (_query as TableQuery).OrderBy.Add(f.OrderBy(_propertyResult) as OrderBy); _propertyResult = null; } break; case LinqConsts.QueryableMethod_OrderByDescending: case LinqConsts.QueryableMethod_ThenByDescending: if (_propertyResult != null) { (_query as TableQuery).OrderBy.Add(f.OrderBy(_propertyResult, OrderDirection.Descending) as OrderBy); _propertyResult = null; } break; default: break; } return true; } return false; }
private void DealConstraintWord(string part) { //part 表示列名 if (_column == null) { //可能使用了引用属性,例如表达式:User.Name eq 'huqf' var properties = part.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); if (properties.Length > 1) { ITableSource lastTable = _mainTable; for (int i = 0; i < properties.Length; i++) { var property = properties[i]; var mp = lastTable.EntityRepository.EntityMeta.ManagedProperties.GetCompiledProperties().Find(property); if (mp == null) throw new InvalidProgramException(string.Format("查询条件解析出错,没有找到名称为 {0} 的属性。", property)); var refProperty = mp as IRefEntityProperty; if (refProperty != null) { lastTable = _f.FindOrCreateJoinTable(_query, lastTable, refProperty); } else { _column = lastTable.Column(mp); } } if (_column == null) { throw new InvalidProgramException(string.Format("{0} 查询条件出错:属性表达式不能以引用实体属性结尾。", part)); } } else { var mp = _properties.Find(part, true); if (mp == null) throw new InvalidProgramException(string.Format("查询条件解析出错,没有找到名称为 {0} 的属性。", part)); _column = _mainTable.Column(mp); } } //part 表示操作符 else if (_comparison == null) { _comparison = part; } //part 表示值 else { var propertyConstraint = CreateColumnConstraint(_comparison, part); if (_concat.HasValue && _current != null) { _current = _f.Binary(_current, _concat.Value, propertyConstraint); _concat = null; } else { _current = propertyConstraint; } _column = null; _comparison = null; } }
public static IConstraint NotIn(this IColumnNode column, IColumnNode rightColumn) { return(QueryFactory.Instance.Constraint(column, PropertyOperator.NotIn, rightColumn)); }
public static IConstraint LessEqual(this IColumnNode column, IColumnNode rightColumn) { return QueryFactory.Instance.Constraint(column, PropertyOperator.LessEqual, rightColumn); }
public static IConstraint NotLike(this IColumnNode column, IColumnNode rightColumn) { return QueryFactory.Instance.Constraint(column, PropertyOperator.NotLike, rightColumn); }
public static IConstraint StartWith(this IColumnNode column, IColumnNode rightColumn) { return QueryFactory.Instance.Constraint(column, PropertyOperator.StartsWith, rightColumn); }
protected virtual IColumnNode VisitProperty(IColumnNode node) { return node; }
private void DealConstraintWord(string part) { //part 表示列名 if (_column == null) { //可能使用了引用属性,例如表达式:User.Name eq 'huqf' var properties = part.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); if (properties.Length > 1) { ITableSource lastTable = _mainTable; for (int i = 0; i < properties.Length; i++) { var property = properties[i]; var mp = lastTable.EntityRepository.EntityMeta.ManagedProperties.GetCompiledProperties().Find(property); if (mp == null) { throw new InvalidProgramException(string.Format("查询条件解析出错,没有找到名称为 {0} 的属性。", property)); } var refProperty = mp as IRefEntityProperty; if (refProperty != null) { lastTable = _f.FindOrCreateJoinTable(_query, lastTable, refProperty); } else { _column = lastTable.Column(mp); } } if (_column == null) { throw new InvalidProgramException(string.Format("{0} 查询条件出错:属性表达式不能以引用实体属性结尾。", part)); } } else { var mp = _properties.Find(part, true); if (mp == null) { throw new InvalidProgramException(string.Format("查询条件解析出错,没有找到名称为 {0} 的属性。", part)); } _column = _mainTable.Column(mp); } } //part 表示操作符 else if (_comparison == null) { _comparison = part; } //part 表示值 else { var propertyConstraint = CreateColumnConstraint(_comparison, part); if (_concat.HasValue && _current != null) { _current = _f.Binary(_current, _concat.Value, propertyConstraint); _concat = null; } else { _current = propertyConstraint; } _column = null; _comparison = null; } }
protected virtual IColumnNode VisitProperty(IColumnNode node) { return(node); }
IColumnNode ISubQuery.Column(IColumnNode rawProperty, string alias) { var raw = rawProperty as ColumnNode; var property = new ColumnNode { ColumnName = raw.ColumnName, Property = raw.Property, Table = this, Alias = alias }; return property; }
/// <summary> /// 通过目前已经收集到的属性、操作符、值,来生成一个属性条件结果。 /// 并清空已经收集的信息。 /// </summary> private bool MakeConstraint() { if (_propertyResult != null && _operator.HasValue) { var op = _operator.Value; if (_reverseWhere) op = PropertyOperatorHelper.Reverse(op); if (_hasValueResult) { _constraintResult = f.Constraint(_propertyResult, op, _valueResult); _valueResult = null; _hasValueResult = false; } else { _constraintResult = f.Constraint(_propertyResult, op, _rightPropertyResult); _rightPropertyResult = null; } _propertyResult = null; _operator = null; } if (_constraintResult != null) { if (_nullableRefConstraint != null) { var concat = _reverseWhere ? BinaryOperator.Or : BinaryOperator.And; _constraintResult = f.Binary(_nullableRefConstraint, concat, _constraintResult); _nullableRefConstraint = null; } _query.Where = _constraintResult; _constraintResult = null; return true; } return false; }
private static void BindColumnProperties(IColumnNode columnNode, IColumnDefinition columnDefinition) { if(columnNode.Properties[MdlSyntax.PrimaryKey] != null) columnNode.PrimaryKey = columnDefinition.PrimaryKey = columnNode.Properties.AsBoolean(MdlSyntax.PrimaryKey); if (columnNode.Properties[MdlSyntax.Nullable] != null) columnNode.Nullable = columnDefinition.Nullable = columnNode.Properties.AsBoolean(MdlSyntax.Nullable); if (columnNode.Properties[MdlSyntax.Length] != null) columnNode.Length = columnDefinition.Length = AstNodePropertyUtil.AsInteger(columnNode.Properties, MdlSyntax.Length); // // Type aliases have already been resolved, so we can freely parse textual type representation. if(columnNode.Properties[MdlSyntax.Type] != null) { string textualType = AstNodePropertyUtil.AsString(columnNode.Properties, MdlSyntax.Type); try { columnNode.Type = columnDefinition.Type = (DbType)Enum.Parse(typeof(DbType), textualType, true); } // try catch(ArgumentException e) { throw new MdlCompilerException(string.Format(MdlCompilerResources.UnknownType, textualType), e); } // catch } // if if(columnNode.Properties[MdlSyntax.Scale] != null) columnNode.Scale = columnDefinition.Scale = AstNodePropertyUtil.AsInteger(columnNode.Properties, MdlSyntax.Scale); if(columnNode.Properties[MdlSyntax.Precision] != null) columnNode.Precision = columnDefinition.Precision = AstNodePropertyUtil.AsInteger(columnNode.Properties, MdlSyntax.Precision); if(columnNode.Properties[MdlSyntax.Identity] != null) columnNode.Identity = columnDefinition.Identity = Convert.ToBoolean(AstNodePropertyUtil.AsString(columnNode.Properties, MdlSyntax.Identity)); if(columnNode.Properties[MdlSyntax.Table] != null) columnNode.Table = columnDefinition.Table = AstNodePropertyUtil.AsString(columnNode.Properties, MdlSyntax.Table); }