예제 #1
0
        // <summary>
        // Write this symbol out as a string for sql.  This is just
        // the new name of the symbol (which could be the same as the old name).
        // We rename columns here if necessary.
        // </summary>
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            if (NeedsRenaming)
            {
                int i;

                if (sqlGenerator.AllColumnNames.TryGetValue(NewName, out i))
                {
                    string newNameCandidate;
                    do
                    {
                        ++i;
                        newNameCandidate = NewName + i.ToString(CultureInfo.InvariantCulture);
                    }while (sqlGenerator.AllColumnNames.ContainsKey(newNameCandidate));

                    sqlGenerator.AllColumnNames[NewName] = i;

                    NewName = newNameCandidate;
                }

                // Add this column name to list of known names so that there are no subsequent
                // collisions
                sqlGenerator.AllColumnNames[NewName] = 0;

                // Prevent it from being renamed repeatedly.
                NeedsRenaming = false;
            }
            writer.Write(SqlGenerator.QuoteIdentifier(NewName));
        }
            public override void Visit(DbScanExpression scanExpression)
            {
                string         str         = SqlGenerator.GetTargetTSql(scanExpression.Target) + " AS " + (this._currentTableAlias = "t" + (object)this._aliasCount++);
                EntityTypeBase elementType = scanExpression.Target.ElementType;

                if (this._from.Length == 0)
                {
                    this._baseTable = (EntityType)elementType;
                    this._from.Append("FROM ");
                    this._from.Append(str);
                }
                else
                {
                    this._from.AppendLine();
                    this._from.Append("JOIN ");
                    this._from.Append(str);
                    this._from.Append(" ON ");
                    for (int index = 0; index < elementType.KeyMembers.Count; ++index)
                    {
                        if (index > 0)
                        {
                            this._from.Append(" AND ");
                        }
                        this._from.Append(this._currentTableAlias + ".");
                        this._from.Append(SqlGenerator.QuoteIdentifier(elementType.KeyMembers[index].Name));
                        this._from.Append(" = t0.");
                        this._from.Append(SqlGenerator.QuoteIdentifier(this._baseTable.KeyMembers[index].Name));
                    }
                }
            }
 public override void Visit(DbPropertyExpression propertyExpression)
 {
     this._select.Append(this._currentTableAlias);
     this._select.Append(".");
     this._select.Append(SqlGenerator.QuoteIdentifier(propertyExpression.Property.Name));
     if (string.IsNullOrWhiteSpace(this._nextPropertyAlias) || string.Equals(this._nextPropertyAlias, propertyExpression.Property.Name, StringComparison.Ordinal))
     {
         return;
     }
     this._select.Append(" AS ");
     this._select.Append(this._nextPropertyAlias);
 }
            public override void Visit(DbComparisonExpression comparisonExpression)
            {
                EdmMember property = ((DbPropertyExpression)comparisonExpression.Left).Property;

                if (!this._baseTable.KeyMembers.Contains(property))
                {
                    return;
                }
                this._where.Append(" AND t0.");
                this._where.Append(SqlGenerator.QuoteIdentifier(property.Name));
                this._where.Append(" = ");
                comparisonExpression.Right.Accept((DbExpressionVisitor)this);
            }
            public override void Visit(DbPropertyExpression propertyExpression)
            {
                DebugCheck.NotNull(propertyExpression);

                _select.Append(_currentTableAlias);
                _select.Append(".");
                _select.Append(SqlGenerator.QuoteIdentifier(propertyExpression.Property.Name));

                if (!string.IsNullOrWhiteSpace(_nextPropertyAlias) &&
                    !string.Equals(_nextPropertyAlias, propertyExpression.Property.Name, StringComparison.Ordinal))
                {
                    _select.Append(" AS ");
                    _select.Append(_nextPropertyAlias);
                }
            }
            public override void Visit(DbComparisonExpression comparisonExpression)
            {
                DebugCheck.NotNull(comparisonExpression);

                var property
                    = ((DbPropertyExpression)comparisonExpression.Left).Property;

                if (_baseTable.KeyMembers.Contains(property))
                {
                    _where.Append(" AND t0.");
                    _where.Append(SqlGenerator.QuoteIdentifier(property.Name));
                    _where.Append(" = ");

                    comparisonExpression.Right.Accept(this);
                }
            }
예제 #7
0
 public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     if (this.NeedsRenaming)
     {
         int num;
         if (sqlGenerator.AllColumnNames.TryGetValue(this.NewName, out num))
         {
             string key;
             do
             {
                 ++num;
                 key = this.NewName + num.ToString((IFormatProvider)CultureInfo.InvariantCulture);
             }while (sqlGenerator.AllColumnNames.ContainsKey(key));
             sqlGenerator.AllColumnNames[this.NewName] = num;
             this.NewName = key;
         }
         sqlGenerator.AllColumnNames[this.NewName] = 0;
         this.NeedsRenaming = false;
     }
     writer.Write(SqlGenerator.QuoteIdentifier(this.NewName));
 }
            public override void Visit(DbScanExpression scanExpression)
            {
                DebugCheck.NotNull(scanExpression);

                var tableSql
                    = SqlGenerator.GetTargetTSql(scanExpression.Target)
                      + " AS "
                      + (_currentTableAlias = "t" + _aliasCount++);

                var table = scanExpression.Target.ElementType;

                if (_from.Length == 0)
                {
                    _baseTable = (EntityType)table;

                    _from.Append("FROM ");
                    _from.Append(tableSql);
                }
                else
                {
                    _from.AppendLine();
                    _from.Append("JOIN ");
                    _from.Append(tableSql);
                    _from.Append(" ON ");

                    for (var i = 0; i < table.KeyMembers.Count; i++)
                    {
                        if (i > 0)
                        {
                            _from.Append(" AND ");
                        }

                        _from.Append(_currentTableAlias + ".");
                        _from.Append(SqlGenerator.QuoteIdentifier(table.KeyMembers[i].Name));
                        _from.Append(" = t0.");
                        _from.Append(SqlGenerator.QuoteIdentifier(_baseTable.KeyMembers[i].Name));
                    }
                }
            }
예제 #9
0
 // Generates T-SQL describing a member
 // Requires: member must belong to an entity type (a safe requirement for DML
 // SQL gen, where we only access table columns)
 internal static string GenerateMemberTSql(EdmMember member)
 {
     return(SqlGenerator.QuoteIdentifier(member.Name));
 }