Inheritance: MemberExpression
Ejemplo n.º 1
0
 public virtual ProjectionExpression AddOuterJoinTest(ProjectionExpression proj)
 {
     var test = this.GetOuterJoinTest(proj.Select);
     var select = proj.Select;
     FieldExpression testCol = null;
     // look to see if test expression exists in fields already
     foreach (var col in select.Fields)
     {
         if (test.Equals(col.Expression))
         {
             var colType = this.TypeSystem.GetStorageType(test.Type);
             testCol = new FieldExpression(test.Type, colType, select.Alias, col.Name);
             break;
         }
     }
     if (testCol == null)
     {
         // add expression to projection
         testCol = test as FieldExpression;
         string colName = (testCol != null) ? testCol.Name : "Test";
         colName = proj.Select.Fields.GetAvailableFieldName(colName);
         var colType = this.TypeSystem.GetStorageType(test.Type);
         select = select.AddField(new FieldDeclaration(colName, test, colType));
         testCol = new FieldExpression(test.Type, colType, select.Alias, colName);
     }
     var newProjector = new OuterJoinedExpression(testCol, proj.Projector);
     return new ProjectionExpression(select, newProjector, proj.Aggregator);
 }
Ejemplo n.º 2
0
 protected override Expression VisitField(FieldExpression field)
 {
     if (this.oldAliases.Contains(field.Alias))
     {
         return new FieldExpression(field.Type, field.QueryType, this.newAlias, field.Name);
     }
     return field;
 }
Ejemplo n.º 3
0
 protected override Expression VisitField(FieldExpression field)
 {
     IdentifiableAlias mapped;
     if (this.map.TryGetValue(field.Alias, out mapped))
     {
         return new FieldExpression(field.Type, field.QueryType, mapped, field.Name);
     }
     return field;
 }
Ejemplo n.º 4
0
 protected override Expression VisitField(FieldExpression field)
 {
     FieldExpression mapped;
     if (this.map.TryGetValue(field, out mapped))
     {
         return mapped;
     }
     return field;
 }
Ejemplo n.º 5
0
 protected override Expression VisitField(FieldExpression field)
 {
     Dictionary<string, Expression> nameMap;
     if (this.map.TryGetValue(field.Alias, out nameMap))
     {
         Expression expr;
         if (nameMap.TryGetValue(field.Name, out expr))
         {
             return this.Visit(expr);
         }
         throw new Exception("Reference to undefined field");
     }
     return field;
 }
Ejemplo n.º 6
0
        protected override Expression VisitSelect(SelectExpression select)
        {
            select = (SelectExpression)base.VisitSelect(select);

            // look for redundant field declarations
            List<FieldDeclaration> cols = select.Fields.OrderBy(c => c.Name).ToList();
            BitArray removed = new BitArray(select.Fields.Count);
            bool anyRemoved = false;
            for (int i = 0, n = cols.Count; i < n - 1; i++)
            {
                FieldDeclaration ci = cols[i];
                FieldExpression cix = ci.Expression as FieldExpression;
                StorageType qt = cix != null ? cix.QueryType : ci.QueryType;
                FieldExpression cxi = new FieldExpression(ci.Expression.Type, qt, select.Alias, ci.Name);
                for (int j = i + 1; j < n; j++)
                {
                    if (!removed.Get(j))
                    {
                        FieldDeclaration cj = cols[j];
                        if (SameExpression(ci.Expression, cj.Expression))
                        {
                            // any reference to 'j' should now just be a reference to 'i'
                            FieldExpression cxj = new FieldExpression(cj.Expression.Type, qt, select.Alias, cj.Name);
                            this.map.Add(cxj, cxi);
                            removed.Set(j, true);
                            anyRemoved = true;
                        }
                    }
                }
            }
            if (anyRemoved)
            {
                List<FieldDeclaration> newDecls = new List<FieldDeclaration>();
                for (int i = 0, n = cols.Count; i < n; i++)
                {
                    if (!removed.Get(i))
                    {
                        newDecls.Add(cols[i]);
                    }
                }
                select = select.SetFields(newDecls);
            }
            return select;
        }
Ejemplo n.º 7
0
 protected FieldAssignment UpdateFieldAssignment(FieldAssignment ca, FieldExpression c, Expression e)
 {
     if (c != ca.Field || e != ca.Expression)
     {
         return new FieldAssignment(c, e);
     }
     return ca;
 }
Ejemplo n.º 8
0
 protected override Expression VisitField(FieldExpression field)
 {
     if (field.Alias != null && !this.HideFieldAliases)
     {
         this.WriteAliasName(GetAliasName(field.Alias));
         this.Write(".");
     }
     this.WriteFieldName(field.Name);
     return field;
 }
Ejemplo n.º 9
0
 protected override Expression Visit(Expression expression)
 {
     if (this.candidates.Contains(expression))
     {
         if (expression.NodeType == (ExpressionType)DbExpressionType.Field)
         {
             FieldExpression field = (FieldExpression)expression;
             FieldExpression mapped;
             if (this.map.TryGetValue(field, out mapped))
             {
                 return mapped;
             }
             // check for field that already refers to this field
             foreach (FieldDeclaration existingField in this.fields)
             {
                 FieldExpression cex = existingField.Expression as FieldExpression;
                 if (cex != null && cex.Alias == field.Alias && cex.Name == field.Name)
                 {
                     // refer to the field already in the field list
                     return new FieldExpression(field.Type, field.QueryType, this.newAlias, existingField.Name);
                 }
             }
             if (this.existingAliases.Contains(field.Alias))
             {
                 int ordinal = this.fields.Count;
                 string fieldName = this.GetUniqueFieldName(field.Name);
                 this.fields.Add(new FieldDeclaration(fieldName, field, field.QueryType));
                 mapped = new FieldExpression(field.Type, field.QueryType, this.newAlias, fieldName);
                 this.map.Add(field, mapped);
                 this.fieldNames.Add(fieldName);
                 return mapped;
             }
             // must be referring to outer scope
             return field;
         }
         else
         {
             string fieldName = this.GetNextFieldName();
             var colType = this.language.TypeSystem.GetStorageType(expression.Type);
             this.fields.Add(new FieldDeclaration(fieldName, expression, colType));
             return new FieldExpression(expression.Type, colType, this.newAlias, fieldName);
         }
     }
     else
     {
         return base.Visit(expression);
     }
 }
Ejemplo n.º 10
0
 protected override Expression VisitField(FieldExpression field)
 {
     MarkFieldAsUsed(field.Alias, field.Name);
     return field;
 }
Ejemplo n.º 11
0
 private Expression BindAnyAll(Expression source, MethodInfo method, LambdaExpression predicate, bool isRoot)
 {
     bool isAll = method.Name == "All";
     ConstantExpression constSource = source as ConstantExpression;
     if (constSource != null && !IsQuery(constSource))
     {
         System.Diagnostics.Debug.Assert(!isRoot);
         Expression where = null;
         foreach (object value in (IEnumerable)constSource.Value)
         {
             Expression expr = Expression.Invoke(predicate, Expression.Constant(value, predicate.Parameters[0].Type));
             if (where == null)
             {
                 where = expr;
             }
             else if (isAll)
             {
                 where = where.And(expr);
             }
             else
             {
                 where = where.Or(expr);
             }
         }
         return this.Visit(where);
     }
     else
     {
         if (isAll)
         {
             predicate = Expression.Lambda(Expression.Not(predicate.Body), predicate.Parameters.ToArray());
         }
         if (predicate != null)
         {
             source = Expression.Call(typeof(Enumerable), "Where", method.GetGenericArguments(), source, predicate);
         }
         ProjectionExpression projection = this.VisitSequence(source);
         Expression result = new ExistsExpression(projection.Select);
         if (isAll)
         {
             result = Expression.Not(result);
         }
         if (isRoot)
         {
             if (this.language.AllowSubqueryInSelectWithoutFrom)
             {
                 return GetSingletonSequence(result, "SingleOrDefault");
             }
             else
             {
                 // use count aggregate instead of exists
                 var colType = this.language.TypeSystem.GetStorageType(typeof(int));
                 var newSelect = projection.Select.SetFields(
                     new[] { new FieldDeclaration("value", new AggregateExpression(typeof(int), "Count", null, false), colType) }
                     );
                 var colx = new FieldExpression(typeof(int), colType, newSelect.Alias, "value");
                 var exp = isAll
                     ? colx.Equal(Expression.Constant(0))
                     : colx.GreaterThan(Expression.Constant(0));
                 return new ProjectionExpression(
                     newSelect, exp, Aggregator.GetAggregator(typeof(bool), typeof(IEnumerable<bool>))
                     );
             }
         }
         return result;
     }
 }
Ejemplo n.º 12
0
 protected override Expression VisitField(FieldExpression field)
 {
     this.aliases.Add(field.Alias);
     return field;
 }
Ejemplo n.º 13
0
 protected virtual bool CompareField(FieldExpression a, FieldExpression b)
 {
     return this.CompareAlias(a.Alias, b.Alias) && a.Name == b.Name;
 }
Ejemplo n.º 14
0
 protected override Expression VisitField(FieldExpression field)
 {
     ParameterExpression fieldReader;
     int iOrdinal;
     if (this.Scope != null && this.Scope.TryGetValue(field, out fieldReader, out iOrdinal))
     {
         // different query types may not be able to guarantee the ordinal position of results
         // in the reader.  for these types, the Scope.UseOrdinalMapping will be set to false
         if (this.Scope.UseOrdinalMapping)
         {
             MethodInfo method = OFieldReader.GetReaderMethod(field.Type, this.Scope.UseOrdinalMapping);
             return Expression.Call(fieldReader, method, Expression.Constant(iOrdinal));
         }
         else
         {
             MethodInfo method = OFieldReader.GetReaderMethod(field.Type, this.Scope.UseOrdinalMapping);
             return Expression.Call(fieldReader, method, Expression.Constant(field.Name));
         }
     }
     else
     {
         System.Diagnostics.Debug.Fail(string.Format("Field not in scope: {0}", field));
     }
     return field;
 }
Ejemplo n.º 15
0
        protected virtual Expression VisitField(FieldExpression field)
        {
            int iAlias;
            string aliasName =
                this.aliasMap.TryGetValue(field.Alias, out iAlias)
                ? "A" + iAlias
                : "A" + (field.Alias != null ? field.Alias.GetHashCode().ToString() : "") + "?";

            this.Write(aliasName);
            this.Write(".");
            this.Write("Field(\"");
            this.Write(field.Name);
            this.Write("\")");
            return field;
        }
Ejemplo n.º 16
0
 protected override Expression VisitField(FieldExpression field)
 {
     _fields.Add(field);
     return base.VisitField(field);
 }
Ejemplo n.º 17
0
 protected virtual Expression VisitField(FieldExpression field)
 {
     return field;
 }
Ejemplo n.º 18
0
 protected override Expression VisitField(FieldExpression field)
 {
     //if (field.Alias != null && !this.HideFieldAliases)
     //{
     //    this.WriteAliasName(GetAliasName(field.Alias));
     //    this.Write(".");
     //}
     if (field.Name.Equals("Id"))
     {
         this.WriteFieldName("@rid");
     }
     //else if (field.Type.Implements<IIdentifiable>())
     //{
     //    var mapping = this.Linguist.Translator.Mapper.Mapping.GetEntity(field.Type, this.Linguist.Translator.Mapper.Mapping.RepositoryType);
     //    this.Write(mapping.StorageClass);
     //}
     else
     {
         this.WriteFieldName(field.Name);
         //WriteIfNullField(field);
     }
     return field;
 }
Ejemplo n.º 19
0
 //private void WriteIfNullField(FieldExpression field)
 //{
 //    this.Write(string.Format("IfNull({0}, {1}) as {0}", field.Name, FieldDefaultValue(field)));
 //}
 private string FieldDefaultValue(FieldExpression field)
 {
     if (field.Type.Equals(typeof(bool))
         || field.Type.Equals(typeof(byte))
         || field.Type.Equals(typeof(sbyte))
         || field.Type.Equals(typeof(char))
         || field.Type.Equals(typeof(short))
         || field.Type.Equals(typeof(ushort))
         || field.Type.Equals(typeof(int))
         || field.Type.Equals(typeof(uint))
         || field.Type.Equals(typeof(long))
         || field.Type.Equals(typeof(ulong)))
     {
         return 0.ToOrientValueString();
     }
     else if (field.Type.Equals(typeof(float))
         || field.Type.Equals(typeof(double)))
     {
         return (0.0d).ToOrientValueString();
     }
     else if (field.Type.Equals(typeof(decimal)))
     {
         return (0.0m).ToOrientValueString();
     }
     else if (field.Type.Equals(typeof(DateTime)))
     {
         return (new DateTime(1970, 1, 1, 0, 0, 0)).ToOrientValueString();
     }
     else if (field.Type.Equals(typeof(string)))
     {
         return "".ToOrientValueString();
     }
     else return "null"; // not sure how to handle defaults for arrays...
 }
Ejemplo n.º 20
0
 public FieldAssignment(FieldExpression field, Expression expression)
 {
     this.field = field;
     this.expression = expression;
 }