private string FormatSelectItem(Table table, SimpleReference item)
 {
     return table.FindColumn(item.GetAliasOrName()).ActualName;
 }
        private IEnumerable<string> BuildReferencePath(List<string> pathNames, Table table, List<string> elementNames, ExpressionContext context)
        {
            if (!elementNames.Any())
            {
                return pathNames;
            }

            var objectName = elementNames.First();
            if (table != null)
            {
                if (table.HasColumn(objectName))
                {
                    pathNames.Add(table.FindColumn(objectName).ActualName);
                    return BuildReferencePath(pathNames, null, elementNames.Skip(1).ToList(), context);
                }
                else if (table.HasAssociation(objectName))
                {
                    var association = table.FindAssociation(objectName);
                    pathNames.Add(association.ActualName);
                    return BuildReferencePath(pathNames, context.Schema.FindTable(association.ReferenceTableName), elementNames.Skip(1).ToList(), context);
                }
                else
                {
                    var formattedFunction = FormatAsFunction(objectName, context);
                    if (!string.IsNullOrEmpty(formattedFunction))
                    {
                        pathNames.Add(formattedFunction);
                        return BuildReferencePath(pathNames, null, elementNames.Skip(1).ToList(), context);
                    }
                    else
                    {
                        throw new UnresolvableObjectException(objectName, string.Format("Invalid referenced object {0}", objectName));
                    }
                }
            }
            else if (FunctionMapping.SupportedFunctions.ContainsKey(new ExpressionFunction.FunctionCall(elementNames.First(), 0)))
            {
                var formattedFunction = FormatAsFunction(objectName, context);
                pathNames.Add(formattedFunction);
                return BuildReferencePath(pathNames, null, elementNames.Skip(1).ToList(), context);
            }
            else
            {
                pathNames.AddRange(elementNames);
                return BuildReferencePath(pathNames, null, new List<string>(), context);
            }
        }
 private string FormatOrderByItem(Table table, SimpleOrderByItem item)
 {
     var col = table.FindColumn(item.Reference.GetName());
     var direction = item.Direction == OrderByDirection.Descending ? " desc" : string.Empty;
     return col.ActualName + direction;
 }