private void Init() { if (_Inited) { return; } TSFQLAttribute attribute = this.Attribute; DBProvider dbProvider = this.DBProvider; SyntaxAnalysis.ExpressionTree expressionTree = this.ExpressionTree; if (attribute.Parameters.Count < 1) { throw new ParseException("Invalid parameter count"); } _DBProvider = dbProvider; _DistinctFields = new List <Field>(); _ExpressionTree = expressionTree; int fieldsDataLength = 0; _DistinctFieldsString = attribute.Parameters[0]; _DistinctCount = 1; if (attribute.Parameters.Count > 1) { //Second paramter is distinct count. Default is 1. _DistinctCount = int.Parse(attribute.Parameters[1]); } foreach (string fieldName in _DistinctFieldsString.Split(new char[] { ',' })) { Field field = dbProvider.GetField(fieldName.Trim()); if (field == null) { throw new ParseException(string.Format("field:{0} does not exist", fieldName)); } if (field.IndexType != Field.Index.Untokenized) { throw new ParseException(string.Format("field:{0} is not Untokenized index", field.Name)); } switch (field.DataType) { case DataType.TinyInt: fieldsDataLength++; break; case DataType.SmallInt: fieldsDataLength += 2; break; case DataType.Int: case DataType.Date: case DataType.SmallDateTime: fieldsDataLength += 4; break; case DataType.DateTime: case DataType.BigInt: fieldsDataLength += 8; break; default: throw new ParseException(string.Format("field:{0}'s data type is not integer or time data type", field.Name)); } _DistinctFields.Add(field); } if (fieldsDataLength > 8) { throw new ParseException("Sum of the data length of all group by fields can't be large than 8."); } if (_DistinctFields.Count <= 0) { throw new ParseException("It need at least one group by field"); } _Inited = true; }
/// <summary> /// /// </summary> /// <param name="attribute">GroupBy Attribute</param> /// <param name="dbProvider"></param> /// <example> /// [GroupBy("Count", "*", "type1, type2", 10)] /// equivalent sql: /// select top 10 count(*) as count from table group by type1, type2 order by count desc /// </example> internal ParseGroupByCount(TSFQLAttribute attribute, DBProvider dbProvider, SyntaxAnalysis.ExpressionTree expressionTree) { if (attribute.Parameters.Count < 3) { throw new ParseException("Invalid parameter count"); } if (!attribute.Parameters[0].Equals("count", StringComparison.CurrentCultureIgnoreCase)) { throw new ParseException("first parameter must be count"); } _DBProvider = dbProvider; _GroupByFields = new List <Field>(); _ExpressionTree = expressionTree; int fieldsDataLength = 0; _GroupByFieldsString = attribute.Parameters[2]; foreach (string fieldName in _GroupByFieldsString.Split(new char[] { ',' })) { Field field = dbProvider.GetField(fieldName.Trim()); if (field == null) { throw new ParseException(string.Format("field:{0} does not exist", fieldName)); } if (field.IndexType != Field.Index.Untokenized) { throw new ParseException(string.Format("field:{0} is not Untokenized index", field.Name)); } switch (field.DataType) { case DataType.TinyInt: fieldsDataLength++; break; case DataType.SmallInt: fieldsDataLength += 2; break; case DataType.Int: case DataType.Date: case DataType.SmallDateTime: fieldsDataLength += 4; break; case DataType.DateTime: case DataType.BigInt: fieldsDataLength += 8; break; default: throw new ParseException(string.Format("field:{0}'s data type is not integer or time data type", field.Name)); } _GroupByFields.Add(field); } if (fieldsDataLength > 8) { throw new ParseException("Sum of the data length of all group by fields can't be large than 8."); } if (_GroupByFields.Count <= 0) { throw new ParseException("It need at least one group by field"); } _Top = int.MaxValue; if (attribute.Parameters.Count > 3) { _Top = int.Parse(attribute.Parameters[3]); } }