SqlEntityExpression represents an entity in a SQL expression. It consists of a list of columns, a primary key (which is usually part of the columns list), and a table alias identifying the table or substatement the entity stems from. An entity can have a name, which is used to prefix all of its columns with in the generated SQL.
Inheritance: Remotion.Linq.Clauses.Expressions.ExtensionExpression
    public SqlColumnReferenceExpression (Type type, string tableAlias, string referencedColumnName, bool isPrimaryKey, SqlEntityExpression referencedEntity)
      : base (type, tableAlias, referencedColumnName, isPrimaryKey)
    {
      ArgumentUtility.CheckNotNull ("referencedEntity", referencedEntity);

      _referencedEntity = referencedEntity;
    }
Example #2
0
        public SqlColumnReferenceExpression(Type type, string tableAlias, string referencedColumnName, bool isPrimaryKey, SqlEntityExpression referencedEntity)
            : base(type, tableAlias, referencedColumnName, isPrimaryKey)
        {
            ArgumentUtility.CheckNotNull("referencedEntity", referencedEntity);

            _referencedEntity = referencedEntity;
        }
    public void SetUp ()
    {
      var sqlTable = SqlStatementModelObjectMother.CreateSqlTable_WithResolvedTableInfo();
      _entityExpression = new SqlEntityDefinitionExpression (
          typeof (string),
          "t",
          null,
          e => e.GetColumn (typeof (string), "ID", true),
          new SqlColumnExpression[]
          {
              new SqlColumnDefinitionExpression (typeof (string), "t", "ID", true),
              new SqlColumnDefinitionExpression (typeof (int), "t", "Name", false),
              new SqlColumnDefinitionExpression (typeof (int), "t", "City", false)
          });

      _sqlStatement = new SqlStatement (
          new TestStreamedValueInfo (typeof (int)),
          _entityExpression,
          new[] { sqlTable },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);
      _commandBuilder = new SqlCommandBuilder();
    }
 public void SetUp ()
 {
   _context = new MappingResolutionContext();
   _entityExpression = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression (typeof (Cook), null, "c");
   _groupingSelectExpression = new SqlGroupingSelectExpression (Expression.Constant ("key"), Expression.Constant ("element"));
   _sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner);
 }
    public void AddSqlEntityMapping (SqlEntityExpression entityExpression, SqlTableBase sqlTable)
    {
      ArgumentUtility.CheckNotNull ("entityExpression", entityExpression);
      ArgumentUtility.CheckNotNull ("sqlTable", sqlTable);

      _entityMapping[entityExpression] = sqlTable;
    }
    public SqlEntityReferenceExpression (Type itemType, string tableAlias, string entityName, SqlEntityExpression referencedEntity)
        : base(itemType, tableAlias, entityName, referencedEntity.IdentityExpressionGenerator)
    {
      ArgumentUtility.CheckNotNull ("referencedEntity", referencedEntity);

      _referencedEntity = referencedEntity;
      _columns = Array.AsReadOnly (referencedEntity.Columns.Select (col => GetColumn (col.Type, col.ColumnName, col.IsPrimaryKey)).ToArray ());
    }
    public Expression VisitSqlEntityExpression (SqlEntityExpression expression)
    {
      ArgumentUtility.CheckNotNull ("expression", expression);

      var reference = expression.CreateReference (_tableInfo.TableAlias, expression.Type);
      _context.AddSqlEntityMapping (reference, _sqlTable);
      return reference;
    }
    public SqlEntityRefMemberExpression (SqlEntityExpression originatingEntity, MemberInfo memberInfo)
      : base (ReflectionUtility.GetMemberReturnType (ArgumentUtility.CheckNotNull ("memberInfo", memberInfo)))
    {
      ArgumentUtility.CheckNotNull ("entityExpression", originatingEntity);

      _originatingEntity = originatingEntity;
      _memberInfo = memberInfo;
    }
Example #9
0
        public SqlEntityReferenceExpression(Type itemType, string tableAlias, string entityName, SqlEntityExpression referencedEntity)
            : base(itemType, tableAlias, entityName, referencedEntity.IdentityExpressionGenerator)
        {
            ArgumentUtility.CheckNotNull("referencedEntity", referencedEntity);

            _referencedEntity = referencedEntity;
            _columns          = Array.AsReadOnly(referencedEntity.Columns.Select(col => GetColumn(col.Type, col.ColumnName, col.IsPrimaryKey)).ToArray());
        }
 public void SetUp ()
 {
   _columnExpression1 = new SqlColumnDefinitionExpression (typeof (int), "t", "ID", true);
   _columnExpression2 = new SqlColumnDefinitionExpression (typeof (int), "t", "Name", false);
   _columnExpression3 = new SqlColumnDefinitionExpression (typeof (int), "t", "City", false);
   _orginalColumns = new[] { _columnExpression1, _columnExpression2, _columnExpression3 };
   _entityExpression = new SqlEntityDefinitionExpression (typeof (Cook), "t", null, e => e.GetColumn (typeof (int), "ID", true), _orginalColumns);
   _originalColumnsReadonly = _entityExpression.Columns;
 }
    public SqlTableBase GetSqlTableForEntityExpression (SqlEntityExpression entityExpression)
    {
      ArgumentUtility.CheckNotNull ("entityExpression", entityExpression);

      SqlTableBase result;
      if (_entityMapping.TryGetValue (entityExpression, out result))
        return result;

      var message = string.Format ("No associated table found for entity '{0}'.", FormattingExpressionTreeVisitor.Format (entityExpression));
      throw new InvalidOperationException (message);
    }
    public SqlEntityExpression UpdateEntityAndAddMapping (SqlEntityExpression entityExpression, Type itemType, string tableAlias, string newName)
    {
      ArgumentUtility.CheckNotNull ("entityExpression", entityExpression);
      ArgumentUtility.CheckNotNull ("itemType", itemType);
      ArgumentUtility.CheckNotNullOrEmpty ("tableAlias", tableAlias);

      var newEntityExpression = entityExpression.Update (itemType, tableAlias, newName);
      var tableForEntityExpression = GetSqlTableForEntityExpression (entityExpression);
      AddSqlEntityMapping (newEntityExpression, tableForEntityExpression);
      return newEntityExpression;
    }
    public void SetUp ()
    {
      _stageMock = MockRepository.GenerateStrictMock<IMappingResolutionStage>();
      _resolverMock = MockRepository.GenerateStrictMock<IMappingResolver>();
      _context = new MappingResolutionContext();

      _entityIdentityResolver = new EntityIdentityResolver (_stageMock, _resolverMock, _context);

      _entityExpression = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression (typeof (Cook), primaryKeyType: typeof (int));
      _entityConstantExpression = new SqlEntityConstantExpression (typeof (Cook), new Cook (), Expression.Constant (0));
      _entityRefMemberExpression = new SqlEntityRefMemberExpression (
          SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression (typeof (Kitchen)),
          typeof (Kitchen).GetProperty ("Cook"));
    }
    public UnresolvedJoinInfo (SqlEntityExpression originatingEntity, MemberInfo memberInfo, JoinCardinality cardinality)
    {
      ArgumentUtility.CheckNotNull ("originatingEntity", originatingEntity);
      ArgumentUtility.CheckNotNull ("memberInfo", memberInfo);

      _originatingEntity = originatingEntity;
      _memberInfo = memberInfo;
      _cardinality = cardinality;

      var memberReturnType = ReflectionUtility.GetMemberReturnType (memberInfo);
      if (Cardinality == JoinCardinality.One)
        _itemType = memberReturnType;
      else
        _itemType = ReflectionUtility.GetItemTypeOfClosedGenericIEnumerable (memberReturnType, "memberInfo");
    }
    public void SetUp ()
    {
      _sqlTable = SqlStatementModelObjectMother.CreateSqlTable_WithResolvedTableInfo();
      _entityExpression = new SqlEntityDefinitionExpression (
          typeof(string),
          "t", null,
          e => e.GetColumn (typeof (int), "ID", true),
          new SqlColumnExpression[]
          {
              new SqlColumnDefinitionExpression (typeof (int), "t", "ID", true),
              new SqlColumnDefinitionExpression (typeof (int), "t", "Name", false),
              new SqlColumnDefinitionExpression (typeof (int), "t", "City", false)
          });

      _stageMock = MockRepository.GenerateStrictMock<ISqlGenerationStage>();
      _generator = new TestableSqlStatementTextGenerator (_stageMock);
      _commandBuilder = new SqlCommandBuilder();
    }
    public Expression VisitSqlEntityExpression (SqlEntityExpression expression)
    {
      if (_currentContext == SqlExpressionContext.SingleValueRequired)
      {
        string message = string.Format (
            "Cannot use an entity expression ('{0}' of type '{1}') in a place where SQL requires a single value.",
            FormattingExpressionTreeVisitor.Format (expression),
            expression.Type.Name);
        throw new NotSupportedException (message);
      }

      return expression; // rely on VisitExpression to apply correct semantics
    }
    public virtual Expression ResolveMemberExpression (SqlEntityExpression originatingEntity, MemberInfo memberInfo)
    {
      var memberType = ReflectionUtility.GetMemberReturnType (memberInfo);
      if (memberInfo.DeclaringType == typeof (Cook))
      {
        switch (memberInfo.Name)
        {
          case "ID":
            return originatingEntity.GetColumn (memberType, memberInfo.Name, true);
          case "FirstName":
          case "Name":
          case "IsFullTimeCook":
          case "IsStarredCook":
          case "Weight":
          case "SpecificInformation":
            return originatingEntity.GetColumn (memberType, memberInfo.Name, false);
          case "KnifeID":
            return CreateMetaIDExpression (
                originatingEntity.GetColumn (typeof (int), "KnifeID", false), 
                originatingEntity.GetColumn (typeof (string), "KnifeClassID", false));
          case "Substitution":
          case "Substituted":
          case "Kitchen":
          case "Knife":
            return new SqlEntityRefMemberExpression (originatingEntity, memberInfo);
        }
      }
      else if (memberInfo.DeclaringType == typeof (ISpecificCook))
      {
        switch (memberInfo.Name)
        {
          case "SpecificInformation":
            return originatingEntity.GetColumn (memberType, memberInfo.Name, false);
        }
      }
      else if (memberInfo.DeclaringType == typeof (Chef))
      {
        switch (memberInfo.Name)
        {
          case "LetterOfRecommendation":
            return originatingEntity.GetColumn (memberType, memberInfo.Name, false);
        }
      }
      else if (memberInfo.DeclaringType == typeof (Kitchen))
      {
        switch (memberInfo.Name)
        {
          case "ID":
            return originatingEntity.GetColumn (memberType, memberInfo.Name, true);
          case "Name":
          case "RoomNumber":
          case "LastCleaningDay":
          case "PassedLastInspection":
          case "LastInspectionScore":
            return originatingEntity.GetColumn (memberType, memberInfo.Name, false);
          case "Cook":
          case "Restaurant":
            return new SqlEntityRefMemberExpression (originatingEntity, memberInfo);
        }
      }
      else if (memberInfo.DeclaringType == typeof (Restaurant))
      {
        switch (memberInfo.Name)
        {
          case "ID":
            return originatingEntity.GetColumn (memberType, memberInfo.Name, true);
          case "SubKitchen":
          case "CompanyIfAny":
            return new SqlEntityRefMemberExpression (originatingEntity, memberInfo);
        }
      }
      else if (memberInfo.DeclaringType == typeof (Company))
      {
        switch (memberInfo.Name)
        {
          case "ID":
            return originatingEntity.GetColumn (memberType, memberInfo.Name, true);
          case "DateOfIncorporation":
            return originatingEntity.GetColumn (memberType, memberInfo.Name, false);
        }
      }
      else if (memberInfo.DeclaringType == typeof (Knife))
      {
        switch (memberInfo.Name)
        {
          case "ID":
            return CreateMetaIDExpression (
                originatingEntity.GetColumn (typeof (int), "ID", true), 
                originatingEntity.GetColumn (typeof (string), "ClassID", true));
          case "Sharpness":
            return originatingEntity.GetColumn (memberType, memberInfo.Name, false);
        }
      }

      throw new UnmappedItemException ("Cannot resolve member: " + memberInfo);
    }
 protected string GetAliasForColumnOfEntity (SqlColumnExpression column, SqlEntityExpression entity)
 {
   if (column.ColumnName != "*")
   {
     if (entity.Name != null)
     {
       return entity.Name + "_" + column.ColumnName;
     }
     else if ((entity is SqlEntityReferenceExpression) && ((SqlEntityReferenceExpression) entity).ReferencedEntity.Name != null)
     {
       // entity references without a name that point to an entity with a name must assign aliases to their columns;
       // otherwise, their columns would include the referenced entity's name
       return column.ColumnName;
     }
   }
   return null;
 }
 public void SetUp ()
 {
   _entityExpression = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression (typeof (Cook));
 }
    private static KeyValuePair<string, object>[] TryResolveProperty (
        MappingResolverStub mappingResolver, SqlEntityExpression sqlEntityDefinition, PropertyInfo member, object entity)
    {
      Expression expression;
      try
      {
        expression = mappingResolver.ResolveMemberExpression (sqlEntityDefinition, member);
      }
      catch (UnmappedItemException)
      {
        return new KeyValuePair<string, object>[0];
      }

      var memberValue = member.GetValue (entity, null);
      return TryResolvePropertyExpression(mappingResolver, expression, memberValue).ToArray();
    }
    public override Expression VisitSqlEntityExpression (SqlEntityExpression expression)
    {
      ArgumentUtility.CheckNotNull ("expression", expression);

      var columnIds = expression.Columns
          .Select (e => GetNextColumnID (GetAliasForColumnOfEntity (e, expression) ?? e.ColumnName))
          .ToArray();

      var result = base.VisitSqlEntityExpression (expression);

      var newInMemoryProjectionBody = Expression.Call (
          CommandBuilder.InMemoryProjectionRowParameter,
          s_getEntityMethod.MakeGenericMethod (expression.Type),
          Expression.Constant (columnIds));
      CommandBuilder.SetInMemoryProjectionBody (newInMemoryProjectionBody);

      return result;
    }
    private ResolvedJoinInfo CreateResolvedJoinInfo (
        SqlEntityExpression originatingEntity,
        string leftColumnName,
        Type leftColumnType,
        bool leftColumnIsPrimaryKey,
        IResolvedTableInfo joinedTableInfo,
        string rightColumnName,
        Type rightColumnType,
        bool rightColumnIsPrimaryKey)
    {
      var leftColumn = originatingEntity.GetColumn (leftColumnType, leftColumnName, leftColumnIsPrimaryKey);
      var rightColumn = CreateColumn (rightColumnType, joinedTableInfo.TableAlias, rightColumnName, rightColumnIsPrimaryKey);

      return new ResolvedJoinInfo (
          joinedTableInfo, ConversionUtility.MakeBinaryWithOperandConversion (ExpressionType.Equal, leftColumn, rightColumn, false, null));
    }
 protected override void AppendColumnForEntity (SqlEntityExpression entity, SqlColumnExpression column)
 {
   column.Accept (this);
   
   string alias = GetAliasForColumnOfEntity(column, entity);
   if (alias != null)
   {
     CommandBuilder.Append (" AS ");
     CommandBuilder.AppendIdentifier (alias);
   }
 }
 public void SetUp ()
 {
   _memberInfo = typeof (Cook).GetProperty ("Substitution");
   _entityExpression = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression (typeof (Cook), null, "c");
   _expression = new SqlEntityRefMemberExpression (_entityExpression, _memberInfo);
 }