private NPathSelectQuery ParseSelectQuery() { NPathSelectQuery query = new NPathSelectQuery(); queries.Push(query); tokenizer.GetCurrentToken("select", "Select"); NPathSelectClause selectClause = new NPathSelectClause(); ParseSelectClause(selectClause); query.Select = selectClause; tokenizer.GetCurrentToken("from", "From"); NPathFromClause fromClause = new NPathFromClause(); ParseFromClause(fromClause); query.From = fromClause; if (tokenizer.GetCurrentToken().IsType("where")) { NPathWhereClause whereClause = new NPathWhereClause(); query.Where = whereClause; ParseWhereClause(whereClause); } if (tokenizer.GetCurrentToken().IsType("order by")) // do not localize { NPathOrderByClause orderByClause = new NPathOrderByClause(); ParseOrderByClause(orderByClause); query.OrderBy = orderByClause; } return query; }
public SqlEmitter(INPathEngine npathEngine, NPathSelectQuery query,NPathQueryType queryType) { this.npathEngine = npathEngine; this.query = query; propertyPathTraverser = new PropertyPathTraverser(this); this.propertyColumnMap = new Hashtable() ; this.npathQueryType = queryType; }
public SqlEmitter(INPathEngine npathEngine, NPathSelectQuery query,NPathQueryType queryType, IClassMap rootClassMap, Hashtable propertyColumnMap) { this.npathEngine = npathEngine; this.query = query; this.rootClassMap = rootClassMap; propertyPathTraverser = new PropertyPathTraverser(this); this.propertyColumnMap = propertyColumnMap; this.npathQueryType = queryType; }
protected virtual void EmitWhere(NPathSelectQuery query) { if (query.Where == null) return; Write("where "); // do not localize EmitExpression(query.Where.Expression); WriteLine(); }
public virtual string EmitQuery(NPathSelectQuery query) { code = new StringBuilder(); EmitSelect(query); EmitFrom(query); EmitWhere(query); EmitOrderBy(query); return code.ToString(); }
public virtual DataTable GetDataTable(NPathSelectQuery query, IList sourceList) { FixQuery(query); DataTable resultTable = new DataTable(); resultTable.BeginInit(); #region build columns int id = 0; foreach (NPathSelectField field in query.Select.SelectFields) { string fieldName = field.Alias; NPathIdentifier path = field.Expression as NPathIdentifier; if (path != null) { if (path.IsWildcard) { throw new Exception("this can not happen"); // do not localize } else { if (fieldName == null) fieldName = path.Path; resultTable.Columns.Add(path.Path, typeof (object)); } } else { if (fieldName == null) { fieldName = "col" + id.ToString(); id++; } resultTable.Columns.Add(fieldName, typeof (object)); } } #endregion resultTable.EndInit(); resultTable.BeginLoadData(); IList resultList = InternalGetTable(query, sourceList); foreach (object[] values in resultList) { resultTable.Rows.Add(values); } resultTable.EndLoadData(); return resultTable; }
public SqlEmitter(INPathEngine npathEngine, NPathSelectQuery query,NPathQueryType queryType, IClassMap rootClassMap,SqlSelectStatement parentQuery,IPropertyMap backReference,int subQueryLevel) { this.npathEngine = npathEngine; this.query = query; this.rootClassMap = rootClassMap; propertyPathTraverser = new PropertyPathTraverser(this); this.propertyColumnMap = new Hashtable() ; this.npathQueryType = queryType; this.parentQuery = parentQuery; this.backReference = backReference; this.subQueryLevel = subQueryLevel; }
public void ExpandWildcards(NPathSelectQuery query) { ArrayList newSelectFieldList = new ArrayList(); foreach (NPathSelectField field in query.Select.SelectFields) { string fieldName = field.Alias; NPathIdentifier path = field.Expression as NPathIdentifier; if (path != null && path.IsWildcard) { string[] parts = path.Path.Split('.'); NPathClassName className = (NPathClassName) query.From.Classes[0]; IClassMap classMap = Context.DomainMap.MustGetClassMap(className.Name); int i = 0; foreach (string part in parts) { if (i == parts.Length - 1) break; IPropertyMap property = classMap.MustGetPropertyMap(part); classMap = Context.DomainMap.MustGetClassMap(property.DataType); i++; } ArrayList properties = classMap.GetAllPropertyMaps(); foreach (PropertyMap property in properties) { if (property.ReferenceType != ReferenceType.None) continue; NPathSelectField newField = new NPathSelectField(); newField.Alias = null; NPathIdentifier newPath = new NPathIdentifier(); if (parts.Length > 1) newPath.Path = string.Join(".", parts, 0, parts.Length - 1) + "."; newPath.Path += property.Name; newField.Expression = newPath; newSelectFieldList.Add(newField); } } else { newSelectFieldList.Add(field); } } query.Select.SelectFields = newSelectFieldList; }
protected virtual void EmitOrderBy(NPathSelectQuery query) { if (query.OrderBy != null) { Write("order by "); // do not localize foreach (SortProperty property in query.OrderBy.SortProperties) { EmitExpression(property.Expression); Write(" "); Write(property.Direction); if (property != query.OrderBy.SortProperties[query.OrderBy.SortProperties.Count - 1]) Write(","); } WriteLine(); } }
//transforms all * fields into real property path fields private void FixQuery(NPathSelectQuery query) { ObjectQueryEngineHelper.ExpandWildcards(query); }
protected virtual IList SortSourceList(IList sourceList, NPathSelectQuery query) { if (query.OrderBy == null) return sourceList; ArrayList sortedSourceList = new ArrayList(sourceList); sortedSourceList.Sort(new SortOrderComparer(query.OrderBy, this)); return sortedSourceList; }
private NPathQueryType GetNPathQueryType(NPathSelectQuery query) { return SqlEmitter.DeduceQueryType(query); }
protected virtual ArrayList InternalGetTable(NPathSelectQuery query, IList sourceList) { ArrayList result = new ArrayList(); IList resultList = GetObjects(query, sourceList); if (query.IsAggregate) { #region build rows object[] values = new object[query.Select.SelectFields.Count]; for (int i = 0; i < query.Select.SelectFields.Count; i++) { NPathSelectField selectField = (NPathSelectField) query.Select.SelectFields[i]; object res = EvalAggregate(resultList, selectField.Expression); values[i] = res; } result.Add(values); #endregion } else { #region build rows foreach (object item in resultList) { object[] values = new object[query.Select.SelectFields.Count]; for (int i = 0; i < query.Select.SelectFields.Count; i++) { NPathSelectField selectField = (NPathSelectField) query.Select.SelectFields[i]; object res = EvalValue(item, selectField.Expression); values[i] = res; } result.Add(values); } #endregion } return result; }
protected virtual ArrayList PopulateResult(IList sortedSourceList, NPathSelectQuery query) { ArrayList destList = new ArrayList(sortedSourceList.Count); Hashtable distinctLookup = new Hashtable(); foreach (object o in sortedSourceList) { if (IsMatch(o, query)) { if (query.Select.Distinct) { #region distinct add //add if not present if (!distinctLookup.ContainsKey(o)) { //flag precense distinctLookup.Add(o, o); destList.Add(o); } #endregion } else { #region normal add destList.Add(o); #endregion } } } return destList; }
protected virtual object EvalQuery(object item, NPathSelectQuery query) { NPathClassName className = (NPathClassName) query.From.Classes[0]; string propertyPath = className.Name; IList childList = (IList) EvalStringPropertyPath(item, propertyPath); ArrayList result = InternalGetTable(query, childList); object[] resultList = new object[result.Count]; int i = 0; foreach (object[] values in result) { object scalarRes = values[0]; if (IsNumber(scalarRes)) scalarRes = double.Parse(scalarRes.ToString()); resultList[i] = scalarRes; i++; } if (query.IsAggregate) { return resultList[0]; } else { return resultList; } }
protected virtual void ApplyTop(NPathSelectQuery query, ref ArrayList destList) { //apply "top" if (query.Select.HasTop) { int count = 0; if (query.Select.Percent) { double percent = ((double) query.Select.Top)/100; count = (int) ((double) destList.Count*percent); } else { count = (int) query.Select.Top; } ArrayList tmp = new ArrayList(count); if (destList.Count < count) count = destList.Count; tmp.AddRange(destList.GetRange(0, count)); destList = tmp; } }
public void ExpandWildcards(Puzzle.NPath.Framework.CodeDom.NPathSelectQuery query) { }
private SqlExpression EvalSubQuery(NPathSelectQuery subQuery) { NPathClassName className = (NPathClassName) subQuery.From.Classes[0]; string collectionProperty = className.Name; IPropertyMap rootPropertyMap = this.RootClassMap.MustGetPropertyMap(collectionProperty); if (!rootPropertyMap.IsCollection) throw new Exception(string.Format("Property '{0}' or type {1} is not a collection",rootPropertyMap.Name, className.Name) ) ; IPropertyMap inversePropertyMap = rootPropertyMap.GetInversePropertyMap(); string dataType = rootPropertyMap.ItemType; className.Name = dataType; IClassMap classMap = RootClassMap.DomainMap.MustGetClassMap(dataType); SqlEmitter subEmitter = new SqlEmitter(this.NPathEngine,subQuery,NPathQueryType.SelectTable,classMap,this.Select,inversePropertyMap,this.subQueryLevel + 1) ; SqlSelectStatement subSelect = subEmitter.BuildSqlDom(); return subSelect; }
public static NPathQueryType DeduceQueryType(NPathSelectQuery query) { if (query.Select != null) { return DeduceQueryType(query.Select); } return NPathQueryType.SelectObjects; }
protected virtual void EmitFrom(NPathSelectQuery query) { Write("from "); // do not localize foreach (NPathClassName className in query.From.Classes) { Write(className.Name); if (className != query.From.Classes[query.From.Classes.Count - 1]) Write(","); } WriteLine(); }
public virtual IList GetObjects(NPathSelectQuery query, IList sourceList) { //sort the source list according to the orderby clause IList sortedSourceList = SortSourceList(sourceList, query); //fill result list with matches of the where clause ArrayList destList = PopulateResult(sortedSourceList, query); //apply top statement ApplyTop(query, ref destList); return destList; }
protected virtual void EmitSelect(NPathSelectQuery query) { NPathSelectClause select = query.Select; Write("select "); // do not localize if (query.Select.HasTop) { Write("top {0}", query.Select.Top); // do not localize if (query.Select.Percent) Write("% "); } int i = 0; foreach (NPathSelectField field in select.SelectFields) { if (field.Expression is NPathIdentifier) { NPathIdentifier path = field.Expression as NPathIdentifier; Write(path.Path); } if (field.Expression is NPathFunction) { NPathFunction function = field.Expression as NPathFunction; EmitFunction(function); } if (field.Expression is NPathExpression) { NPathExpression expression = field.Expression as NPathExpression; EmitExpression(expression); } if (field.Alias != null && field.Alias != "") { WriteLine(" as [{0}]", field.Alias); // do not localize } if (i < select.SelectFields.Count - 1) Write(","); i++; } WriteLine(); }
public virtual bool IsMatch(object item, NPathSelectQuery query) { if (query.Where == null) return true; return EvalExpression(item, query.Where.Expression); }