/// <summary>
        ///     Write out the TOP part of sql select statement
        ///     It basically writes TOP (X) [WITH TIES].
        ///     The brackets around X are ommited for Sql8.
        /// </summary>
        /// <param name="writer"> </param>
        /// <param name="sqlGenerator"> </param>
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            writer.Write("TOP ");

            if (sqlGenerator.SqlVersion
                != SqlVersion.Sql8)
            {
                writer.Write("(");
            }

            TopCount.WriteSql(writer, sqlGenerator);

            if (sqlGenerator.SqlVersion
                != SqlVersion.Sql8)
            {
                writer.Write(")");
            }

            writer.Write(" ");

            if (WithTies)
            {
                writer.Write("WITH TIES ");
            }
        }
Example #2
0
 // <summary>
 // We delegate the writing of the fragment to the appropriate type.
 // </summary>
 public virtual void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     if (null != _sqlFragments)
     {
         foreach (var o in _sqlFragments)
         {
             var str = (o as String);
             if (null != str)
             {
                 writer.Write(str);
             }
             else
             {
                 var sqlFragment = (o as ISqlFragment);
                 if (null != sqlFragment)
                 {
                     sqlFragment.WriteSql(writer, sqlGenerator);
                 }
                 else
                 {
                     throw new InvalidOperationException();
                 }
             }
         }
     }
 }
Example #3
0
        // <summary>
        // Write out the OFFSET part of sql select statement
        // It basically writes OFFSET X ROWS.
        // </summary>
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            writer.Write("OFFSET ");

            SkipCount.WriteSql(writer, sqlGenerator);

            writer.Write(" ROWS ");
        }
Example #4
0
 private static void WriteOffsetFetch(
     SqlWriter writer,
     TopClause top,
     SkipClause skip,
     SqlGenerator sqlGenerator)
 {
     skip.WriteSql(writer, sqlGenerator);
     if (top == null)
     {
         return;
     }
     writer.Write("FETCH NEXT ");
     top.TopCount.WriteSql(writer, sqlGenerator);
     writer.Write(" ROWS ONLY ");
 }
            public override void Visit(DbConstantExpression expression)
            {
                Check.NotNull <DbConstantExpression>(expression, nameof(expression));
                SqlParameter parameter = this.CreateParameter(expression.Value, expression.ResultType, (string)null);

                if (this._createParameters)
                {
                    this._commandText.Append(parameter.ParameterName);
                }
                else
                {
                    using (SqlWriter writer = new SqlWriter(this._commandText.InnerBuilder))
                        this._sqlGenerator.WriteSql(writer, expression.Accept <ISqlFragment>((DbExpressionVisitor <ISqlFragment>) this._sqlGenerator));
                }
            }
Example #6
0
            public override void Visit(DbConstantExpression expression)
            {
                Check.NotNull(expression, "expression");

                var parameter = CreateParameter(expression.Value, expression.ResultType);

                if (_createParameters)
                {
                    _commandText.Append(parameter.ParameterName);
                }
                else
                {
                    using (var writer = new SqlWriter(_commandText.InnerBuilder))
                    {
                        _sqlGenerator.WriteSql(writer, expression.Accept(_sqlGenerator));
                    }
                }
            }
Example #7
0
 public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     writer.Write("TOP ");
     if (sqlGenerator.SqlVersion != SqlVersion.Sql8)
     {
         writer.Write("(");
     }
     this.TopCount.WriteSql(writer, sqlGenerator);
     if (sqlGenerator.SqlVersion != SqlVersion.Sql8)
     {
         writer.Write(")");
     }
     writer.Write(" ");
     if (!this.WithTies)
     {
         return;
     }
     writer.Write("WITH TIES ");
 }
        private static string BuildSqlForTruncateTime(DateTimeOffset dateTimeOffset, SqlVersion sqlVersion)
        {
            var builder = new StringBuilder();

            var sqlGenerator = new SqlGenerator(sqlVersion);

            var functionExpression = EdmFunctions.TruncateTime(
                DbExpression.FromDateTimeOffset(dateTimeOffset));

            var sqlFragment = SqlFunctionCallHandler.GenerateFunctionCallSql(
                sqlGenerator, functionExpression);

            using (var sqlWriter = new SqlWriter(builder))
            {
                sqlFragment.WriteSql(sqlWriter, sqlGenerator);
            }

            return(builder.ToString());
        }
Example #9
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));
 }
Example #10
0
        // <summary>
        // Writes the string representing the Select statement:
        // SELECT (DISTINCT) (TOP topClause) (optionalColumns) (requiredColumns)
        // If Distinct is specified or this is part of a top most statement
        // all optional columns are marked as used.
        // Optional columns are only written if marked as used.
        // In addition, if no required columns are specified and no optional columns are
        // marked as used, the first optional column is written.
        // </summary>
        public override void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            writer.Write("SELECT ");
            if (IsDistinct)
            {
                writer.Write("DISTINCT ");
            }

            if (Top != null && Skip == null)
            {
                Top.WriteSql(writer, sqlGenerator);
            }

            if (IsEmpty)
            {
                Debug.Assert(false); // we have removed all possibilities of SELECT *.
                writer.Write("*");
            }
            else
            {
                //Print the optional columns if any
                var printedAny = WriteOptionalColumns(writer, sqlGenerator);

                if (!base.IsEmpty)
                {
                    if (printedAny)
                    {
                        writer.Write(", ");
                    }
                    base.WriteSql(writer, sqlGenerator);
                }
                //If no optional columns were printed and there were no other columns,
                // print at least the first optional column
                else if (!printedAny)
                {
                    m_optionalColumns[0].MarkAsUsed();
                    m_optionalColumns[0].WriteSqlIfUsed(writer, sqlGenerator, "");
                }
            }
        }
Example #11
0
            public override void Visit(DbConstantExpression expression)
            {
                Check.NotNull(expression, "expression");

                var parameter = CreateParameter(expression.Value, expression.ResultType);

                if (_createParameters)
                {
                    if (_sqlGenerator.CharBoolModes[_sqlGenerator.CurrentColumn] && expression.ResultType.EdmType.Name == "Boolean")
                    {
                        Facet nullable;
                        bool  isNullable = expression.ResultType.Facets.TryGetValue("Nullable", false, out nullable) && (bool)nullable.Value;
                        if (isNullable)
                        {
                            _commandText.Append("(CASE WHEN ");
                            _commandText.Append(parameter.ParameterName);
                            _commandText.Append(" IS NULL THEN NULL ELSE ");
                        }
                        _commandText.Append("(CASE WHEN ");
                        _commandText.Append(parameter.ParameterName);
                        _commandText.Append(" = 1 ");
                        _commandText.Append(string.Format(CultureInfo.InvariantCulture, "THEN '{0}' ELSE '{1}' END) ", SqlProviderServices.CharBoolTrueChar, SqlProviderServices.CharBoolFalseChar));
                        if (isNullable)
                        {
                            _commandText.Append("END) ");
                        }
                    }
                    else
                    {
                        _commandText.Append(parameter.ParameterName);
                    }
                }
                else
                {
                    using (var writer = new SqlWriter(_commandText.InnerBuilder))
                    {
                        _sqlGenerator.WriteSql(writer, expression.Accept(_sqlGenerator));
                    }
                }
            }
 public override void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     writer.Write("SELECT ");
     if (this.IsDistinct)
     {
         writer.Write("DISTINCT ");
     }
     if (this.Top != null && this.Skip == null)
     {
         this.Top.WriteSql(writer, sqlGenerator);
     }
     if (this.IsEmpty)
     {
         writer.Write("*");
     }
     else
     {
         bool flag = this.WriteOptionalColumns(writer, sqlGenerator);
         if (!base.IsEmpty)
         {
             if (flag)
             {
                 writer.Write(", ");
             }
             base.WriteSql(writer, sqlGenerator);
         }
         else
         {
             if (flag)
             {
                 return;
             }
             this.m_optionalColumns[0].MarkAsUsed();
             this.m_optionalColumns[0].WriteSqlIfUsed(writer, sqlGenerator, "");
         }
     }
 }
        private static string BuildSqlForDateTime(DateTime dateTime, SqlVersion sqlVersion)
        {
            var builder = new StringBuilder();

            var sqlGenerator = new SqlGenerator(sqlVersion);

            var functionExpression = EdmFunctions.CreateDateTime(
                DbExpression.FromInt32(dateTime.Year),
                DbExpression.FromInt32(dateTime.Month),
                DbExpression.FromInt32(dateTime.Day),
                DbExpression.FromInt32(dateTime.Hour),
                DbExpression.FromInt32(dateTime.Minute),
                DbExpression.FromInt32(dateTime.Second));

            var sqlFragment = SqlFunctionCallHandler.GenerateFunctionCallSql(
                sqlGenerator, functionExpression);

            using (var sqlWriter = new SqlWriter(builder))
            {
                sqlFragment.WriteSql(sqlWriter, sqlGenerator);
            }

            return(builder.ToString());
        }
 public virtual void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     if (this._sqlFragments == null)
     {
         return;
     }
     foreach (object sqlFragment1 in this._sqlFragments)
     {
         string str = sqlFragment1 as string;
         if (str != null)
         {
             writer.Write(str);
         }
         else
         {
             ISqlFragment sqlFragment2 = sqlFragment1 as ISqlFragment;
             if (sqlFragment2 == null)
             {
                 throw new InvalidOperationException();
             }
             sqlFragment2.WriteSql(writer, sqlGenerator);
         }
     }
 }
            public void HandleDatepartDateFunction_builds_SQL_for_the_given_date_part_with_arguments()
            {
                var builder = new StringBuilder();
                using (var writer = new SqlWriter(builder))
                {
                    SqlFunctionCallHandler.HandleDatepartDateFunction(
                        null, CreateMockDbFunctionExpression("My.Function", PrimitiveTypeKind.String, "iso_week", "One", "Two").Object)
                        .WriteSql(writer, null);

                    Assert.Equal("[My].[Function](iso_week, One, Two)", builder.ToString());
                }
            }
        /// <summary>
        ///     Write out a SQL select statement as a string.
        ///     We have to
        ///     <list type="number">
        ///         <item>Check whether the aliases extents we use in this statement have
        ///             to be renamed.
        ///             We first create a list of all the aliases used by the outer extents.
        ///             For each of the FromExtents( or AllJoinExtents if it is non-null),
        ///             rename it if it collides with the previous list.</item>
        ///         <item>Write each of the clauses (if it exists) as a string</item>
        ///     </list>
        /// </summary>
        /// <param name="writer"> </param>
        /// <param name="sqlGenerator"> </param>
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            #region Check if FROM aliases need to be renamed

            // Create a list of the aliases used by the outer extents
            // JoinSymbols have to be treated specially.
            List<string> outerExtentAliases = null;
            if ((null != outerExtents)
                && (0 < outerExtents.Count))
            {
                foreach (var outerExtent in outerExtents.Keys)
                {
                    var joinSymbol = outerExtent as JoinSymbol;
                    if (joinSymbol != null)
                    {
                        foreach (var symbol in joinSymbol.FlattenedExtentList)
                        {
                            if (null == outerExtentAliases)
                            {
                                outerExtentAliases = new List<string>();
                            }
                            outerExtentAliases.Add(symbol.NewName);
                        }
                    }
                    else
                    {
                        if (null == outerExtentAliases)
                        {
                            outerExtentAliases = new List<string>();
                        }
                        outerExtentAliases.Add(outerExtent.NewName);
                    }
                }
            }

            // An then rename each of the FromExtents we have
            // If AllJoinExtents is non-null - it has precedence.
            // The new name is derived from the old name - we append an increasing int.
            var extentList = AllJoinExtents ?? fromExtents;
            if (null != extentList)
            {
                foreach (var fromAlias in extentList)
                {
                    if ((null != outerExtentAliases)
                        && outerExtentAliases.Contains(fromAlias.Name))
                    {
                        var i = sqlGenerator.AllExtentNames[fromAlias.Name];
                        string newName;
                        do
                        {
                            ++i;
                            newName = fromAlias.Name + i.ToString(CultureInfo.InvariantCulture);
                        }
                        while (sqlGenerator.AllExtentNames.ContainsKey(newName));
                        sqlGenerator.AllExtentNames[fromAlias.Name] = i;
                        fromAlias.NewName = newName;

                        // Add extent to list of known names (although i is always incrementing, "prefix11" can
                        // eventually collide with "prefix1" when it is extended)
                        sqlGenerator.AllExtentNames[newName] = 0;
                    }

                    // Add the current alias to the list, so that the extents
                    // that follow do not collide with me.
                    if (null == outerExtentAliases)
                    {
                        outerExtentAliases = new List<string>();
                    }
                    outerExtentAliases.Add(fromAlias.NewName);
                }
            }

            #endregion

            // Increase the indent, so that the Sql statement is nested by one tab.
            writer.Indent += 1; // ++ can be confusing in this context

            @select.WriteSql(writer, sqlGenerator);

            writer.WriteLine();
            writer.Write("FROM ");
            From.WriteSql(writer, sqlGenerator);

            if ((null != @where)
                && !Where.IsEmpty)
            {
                writer.WriteLine();
                writer.Write("WHERE ");
                Where.WriteSql(writer, sqlGenerator);
            }

            if ((null != groupBy)
                && !GroupBy.IsEmpty)
            {
                writer.WriteLine();
                writer.Write("GROUP BY ");
                GroupBy.WriteSql(writer, sqlGenerator);
            }

            if ((null != orderBy) && !OrderBy.IsEmpty
                && (IsTopMost || Select.Top != null))
            {
                writer.WriteLine();
                writer.Write("ORDER BY ");
                OrderBy.WriteSql(writer, sqlGenerator);
            }

            --writer.Indent;
        }
            public override void Visit(DbConstantExpression expression)
            {
                Check.NotNull(expression, "expression");

                var parameter = CreateParameter(expression.Value, expression.ResultType);

                if (_createParameters)
                {
                    _commandText.Append(parameter.ParameterName);
                }
                else
                {
                    using (var writer = new SqlWriter(_commandText.InnerBuilder))
                    {
                        _sqlGenerator.WriteSql(writer, expression.Accept(_sqlGenerator));
                    }
                }
            }
        // This function generates OFFSET, FETCH clause from Top and Skip information.
        // Note that this should be used only when Skip is present.
        //
        private static void WriteOffsetFetch(SqlWriter writer, TopClause top, SkipClause skip, SqlGenerator sqlGenerator)
        {
            DebugCheck.NotNull(skip);
            skip.WriteSql(writer, sqlGenerator);
            if (top != null)
            {
                writer.Write("FETCH NEXT ");

                top.TopCount.WriteSql(writer, sqlGenerator);

                writer.Write(" ROWS ONLY ");
            }
        }
Example #19
0
        // <summary>
        // Write out a SQL select statement as a string.
        // We have to
        // <list type="number">
        //     <item>
        //         Check whether the aliases extents we use in this statement have
        //         to be renamed.
        //         We first create a list of all the aliases used by the outer extents.
        //         For each of the FromExtents( or AllJoinExtents if it is non-null),
        //         rename it if it collides with the previous list.
        //     </item>
        //     <item>Write each of the clauses (if it exists) as a string</item>
        // </list>
        // </summary>
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            #region Check if FROM aliases need to be renamed

            // Create a list of the aliases used by the outer extents
            // JoinSymbols have to be treated specially.
            List <string> outerExtentAliases = null;
            if ((null != outerExtents) &&
                (0 < outerExtents.Count))
            {
                foreach (var outerExtent in outerExtents.Keys)
                {
                    var joinSymbol = outerExtent as JoinSymbol;
                    if (joinSymbol != null)
                    {
                        foreach (var symbol in joinSymbol.FlattenedExtentList)
                        {
                            if (null == outerExtentAliases)
                            {
                                outerExtentAliases = new List <string>();
                            }
                            outerExtentAliases.Add(symbol.NewName);
                        }
                    }
                    else
                    {
                        if (null == outerExtentAliases)
                        {
                            outerExtentAliases = new List <string>();
                        }
                        outerExtentAliases.Add(outerExtent.NewName);
                    }
                }
            }

            // An then rename each of the FromExtents we have
            // If AllJoinExtents is non-null - it has precedence.
            // The new name is derived from the old name - we append an increasing int.
            var extentList = AllJoinExtents ?? fromExtents;
            if (null != extentList)
            {
                foreach (var fromAlias in extentList)
                {
                    if ((null != outerExtentAliases) &&
                        outerExtentAliases.Contains(fromAlias.Name))
                    {
                        var    i = sqlGenerator.AllExtentNames[fromAlias.Name];
                        string newName;
                        do
                        {
                            ++i;
                            newName = fromAlias.Name + i.ToString(CultureInfo.InvariantCulture);
                        }while (sqlGenerator.AllExtentNames.ContainsKey(newName));
                        sqlGenerator.AllExtentNames[fromAlias.Name] = i;
                        fromAlias.NewName = newName;

                        // Add extent to list of known names (although i is always incrementing, "prefix11" can
                        // eventually collide with "prefix1" when it is extended)
                        sqlGenerator.AllExtentNames[newName] = 0;
                    }

                    // Add the current alias to the list, so that the extents
                    // that follow do not collide with me.
                    if (null == outerExtentAliases)
                    {
                        outerExtentAliases = new List <string>();
                    }
                    outerExtentAliases.Add(fromAlias.NewName);
                }
            }

            #endregion

            // Increase the indent, so that the Sql statement is nested by one tab.
            writer.Indent += 1; // ++ can be confusing in this context

            @select.WriteSql(writer, sqlGenerator);

            writer.WriteLine();
            writer.Write("FROM ");
            From.WriteSql(writer, sqlGenerator);

            if ((null != @where) &&
                !Where.IsEmpty)
            {
                writer.WriteLine();
                writer.Write("WHERE ");
                Where.WriteSql(writer, sqlGenerator);
            }

            if ((null != groupBy) &&
                !GroupBy.IsEmpty)
            {
                writer.WriteLine();
                writer.Write("GROUP BY ");
                GroupBy.WriteSql(writer, sqlGenerator);
            }

            if ((null != orderBy) &&
                !OrderBy.IsEmpty &&
                (IsTopMost || Select.Top != null || Select.Skip != null))
            {
                writer.WriteLine();
                writer.Write("ORDER BY ");
                OrderBy.WriteSql(writer, sqlGenerator);
            }

            if (null != Select.Skip)
            {
                writer.WriteLine();
                WriteOffsetFetch(writer, Select.Top, Select.Skip, sqlGenerator); // Write OFFSET, FETCH clause.
            }

            --writer.Indent;
        }
        public void Visit_In_expression_with_empty_list_and_Visit_Constant_expression_false_generate_same_sql()
        {            
            var generator = new SqlGenerator();
            var inExpression = DbExpressionBuilder.In(
                DbExpressionBuilder.Constant(5), new List<DbConstantExpression>());
            var builder1 = new StringBuilder();
            var builder2 = new StringBuilder();

            using (var writer = new SqlWriter(builder1))
            {
                generator.Visit(DbExpressionBuilder.False).WriteSql(writer, null);
            }

            using (var writer = new SqlWriter(builder2))
            {
                generator.Visit(inExpression).WriteSql(writer, null);
            }

            Assert.Equal(builder1.ToString(), builder2.ToString());
        }
        /// <summary>
        /// Writes the optional columns that are used. 
        /// If this is the topmost statement or distict is specifed as part of the same statement
        /// all optoinal columns are written.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="sqlGenerator"></param>
        /// <returns>Whether at least one column got written</returns>
        private bool WriteOptionalColumns(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            if (m_optionalColumns == null)
            {
                return false;
            }

            if (m_isPartOfTopMostStatement() || IsDistinct)
            {
                foreach (var column in m_optionalColumns)
                {
                    column.MarkAsUsed();
                }
            }

            var separator = "";
            var printedAny = false;
            foreach (var column in m_optionalColumns)
            {
                if (column.WriteSqlIfUsed(writer, sqlGenerator, separator))
                {
                    printedAny = true;
                    separator = ", ";
                }
            }
            return printedAny;
        }
Example #22
0
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            List <string> stringList = (List <string>)null;

            if (this.outerExtents != null && 0 < this.outerExtents.Count)
            {
                foreach (Symbol key in this.outerExtents.Keys)
                {
                    JoinSymbol joinSymbol = key as JoinSymbol;
                    if (joinSymbol != null)
                    {
                        foreach (Symbol flattenedExtent in joinSymbol.FlattenedExtentList)
                        {
                            if (stringList == null)
                            {
                                stringList = new List <string>();
                            }
                            stringList.Add(flattenedExtent.NewName);
                        }
                    }
                    else
                    {
                        if (stringList == null)
                        {
                            stringList = new List <string>();
                        }
                        stringList.Add(key.NewName);
                    }
                }
            }
            List <Symbol> symbolList = this.AllJoinExtents ?? this.fromExtents;

            if (symbolList != null)
            {
                foreach (Symbol symbol in symbolList)
                {
                    if (stringList != null && stringList.Contains(symbol.Name))
                    {
                        int    allExtentName = sqlGenerator.AllExtentNames[symbol.Name];
                        string key;
                        do
                        {
                            ++allExtentName;
                            key = symbol.Name + allExtentName.ToString((IFormatProvider)CultureInfo.InvariantCulture);
                        }while (sqlGenerator.AllExtentNames.ContainsKey(key));
                        sqlGenerator.AllExtentNames[symbol.Name] = allExtentName;
                        symbol.NewName = key;
                        sqlGenerator.AllExtentNames[key] = 0;
                    }
                    if (stringList == null)
                    {
                        stringList = new List <string>();
                    }
                    stringList.Add(symbol.NewName);
                }
            }
            ++writer.Indent;
            this.select.WriteSql(writer, sqlGenerator);
            writer.WriteLine();
            writer.Write("FROM ");
            this.From.WriteSql(writer, sqlGenerator);
            if (this.where != null && !this.Where.IsEmpty)
            {
                writer.WriteLine();
                writer.Write("WHERE ");
                this.Where.WriteSql(writer, sqlGenerator);
            }
            if (this.groupBy != null && !this.GroupBy.IsEmpty)
            {
                writer.WriteLine();
                writer.Write("GROUP BY ");
                this.GroupBy.WriteSql(writer, sqlGenerator);
            }
            if (this.orderBy != null && !this.OrderBy.IsEmpty && (this.IsTopMost || this.Select.Top != null || this.Select.Skip != null))
            {
                writer.WriteLine();
                writer.Write("ORDER BY ");
                this.OrderBy.WriteSql(writer, sqlGenerator);
            }
            if (this.Select.Skip != null)
            {
                writer.WriteLine();
                SqlSelectStatement.WriteOffsetFetch(writer, this.Select.Top, this.Select.Skip, sqlGenerator);
            }
            --writer.Indent;
        }
Example #23
0
 public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     // Symbol pair should never be part of a SqlBuilder.
     Debug.Assert(false);
 }
 public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     writer.Write("OFFSET ");
     this.SkipCount.WriteSql(writer, sqlGenerator);
     writer.Write(" ROWS ");
 }
        private static string BuildSqlForDateTimeOffset(DateTimeOffset dateTimeOffset, SqlVersion sqlVersion)
        {
            var builder = new StringBuilder();

            var sqlGenerator = new SqlGenerator(sqlVersion);

            var functionExpression = EdmFunctions.CreateDateTimeOffset(
                DbExpression.FromInt32(dateTimeOffset.Year),
                DbExpression.FromInt32(dateTimeOffset.Month),
                DbExpression.FromInt32(dateTimeOffset.Day),
                DbExpression.FromInt32(dateTimeOffset.Hour),
                DbExpression.FromInt32(dateTimeOffset.Minute),
                DbExpression.FromInt32(dateTimeOffset.Second),
                DbExpression.FromInt32(Convert.ToInt32(dateTimeOffset.Offset.TotalMinutes)));

            var sqlFragment = SqlFunctionCallHandler.GenerateFunctionCallSql(
                sqlGenerator, functionExpression);

            using (var sqlWriter = new SqlWriter(builder))
            {
                sqlFragment.WriteSql(sqlWriter, sqlGenerator);
            }

            return builder.ToString();
        }
        /// <summary>
        /// Writes the string representing the Select statement:
        /// 
        /// SELECT (DISTINCT) (TOP topClause) (optionalColumns) (requiredColumns)
        /// 
        /// If Distinct is specified or this is part of a top most statement 
        /// all optional columns are marked as used.
        /// 
        /// Optional columns are only written if marked as used. 
        /// In addition, if no required columns are specified and no optional columns are 
        /// marked as used, the first optional column is written.
        /// 
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="sqlGenerator"></param>
        public override void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            writer.Write("SELECT ");
            if (IsDistinct)
            {
                writer.Write("DISTINCT ");
            }

            if (Top != null)
            {
                Top.WriteSql(writer, sqlGenerator);
            }

            if (IsEmpty)
            {
                Debug.Assert(false); // we have removed all possibilities of SELECT *.
                writer.Write("*");
            }
            else
            {
                //Print the optional columns if any
                var printedAny = WriteOptionalColumns(writer, sqlGenerator);

                if (!base.IsEmpty)
                {
                    if (printedAny)
                    {
                        writer.Write(", ");
                    }
                    base.WriteSql(writer, sqlGenerator);
                }
                    //If no optional columns were printed and there were no other columns, 
                    // print at least the first optional column
                else if (!printedAny)
                {
                    m_optionalColumns[0].MarkAsUsed();
                    m_optionalColumns[0].WriteSqlIfUsed(writer, sqlGenerator, "");
                }
            }
        }
Example #27
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>
        /// <param name="writer"></param>
        /// <param name="sqlGenerator"></param>
        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));
        }
Example #28
0
 public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     // Symbol pair should never be part of a SqlBuilder.
     Debug.Assert(false);
 }