Пример #1
0
 public static string GetCode(Node node, bool resolvedNames)
 {
     var sw = new StringWriter();
     var cg = new SqlServerCodeGenerator();
     cg.ResolveNames = resolvedNames;
     cg.Execute(sw, node);
     return sw.ToString();
 }
Пример #2
0
        public static string GetCode(Node node, bool resolvedNames)
        {
            var sw = new StringWriter();
            var cg = new SqlServerCodeGenerator();

            cg.ResolveNames = resolvedNames;
            cg.Execute(sw, node);
            return(sw.ToString());
        }
Пример #3
0
        public string GenerateTableStatisticsQuery(TableReference table)
        {
            if (table.Statistics == null)
            {
                throw new InvalidOperationException();
            }

            // Build table specific where clause
            var cnr = new SearchConditionNormalizer();

            cnr.NormalizeQuerySpecification(((TableSource)table.Node).QuerySpecification);
            var wh = cnr.GenerateWhereClauseSpecificToTable(table);

            var where = new StringWriter();
            if (wh != null)
            {
                var cg = new SqlServerCodeGenerator();
                cg.Execute(where, wh);
            }
            ;

            //*** TODO: move into resource
            string sql = String.Format(@"
IF OBJECT_ID('tempdb..##keys_{4}') IS NOT NULL
DROP TABLE ##keys_{4}

SELECT CAST({2} AS float) AS __key
INTO ##keys_{4}
FROM {0} {1}
{3};

DECLARE @count bigint = @@ROWCOUNT;
DECLARE @step bigint = @count / @bincount;

IF (@step = 0) SET @step = NULL;

WITH q AS
(
	SELECT __key, ROW_NUMBER() OVER (ORDER BY __key) __rn
	FROM ##keys_{4}
)
SELECT __key, __rn
FROM q
WHERE __rn % @step = 1 OR __rn = @count;

DROP TABLE ##keys_{4};
",
                                       GetResolvedTableName(table),
                                       table.Alias == null ? "" : String.Format(" AS {0} ", QuoteIdentifier(table.Alias)),
                                       QuoteIdentifier(table.Statistics.KeyColumn),
                                       where.ToString(),
                                       Guid.NewGuid().ToString().Replace('-', '_'));

            return(sql);
        }
        private string GenerateCode(string query, bool resolveAliases, bool resolveNames, bool substituteStars)
        {
            var ss = CreateSelect(query);
            var w = new StringWriter();

            var cg = new SqlServerCodeGenerator();
            cg.ResolveNames = resolveNames;
            cg.Execute(w, ss);

            return w.ToString();
        }
Пример #5
0
        protected override string GetExecuteQueryText()
        {
            // strip off order by
            var orderby = SelectStatement.FindDescendant<OrderByClause>();
            if (orderby != null)
            {
                SelectStatement.Stack.Remove(orderby);
            }

            // strip off partition on
            foreach (var qs in SelectStatement.EnumerateQuerySpecifications())
            {
                // strip off select into
                var into = qs.FindDescendant<IntoClause>();
                if (into != null)
                {
                    qs.Stack.Remove(into);
                }

                foreach (var ts in qs.EnumerateDescendantsRecursive<SimpleTableSource>())
                {
                    var pc = ts.FindDescendant<TablePartitionClause>();

                    if (pc != null)
                    {
                        pc.Parent.Stack.Remove(pc);
                    }
                }
            }

            // Generate code
            var sw = new StringWriter();
            var cg = new SqlServerCodeGenerator()
            {
                ResolveNames = true
            };

            cg.Execute(sw, SelectStatement);

            return sw.ToString();
        }
Пример #6
0
        public virtual string GetDestinationTableSchemaSourceQuery()
        {
            // strip off order by
            OrderByClause orderby = SelectStatement.FindDescendant<OrderByClause>();
            if (orderby != null)
            {
                SelectStatement.Stack.Remove(orderby);
            }

            // strip off partition on
            foreach (QuerySpecification qs in SelectStatement.EnumerateQuerySpecifications())
            {
                // strip off select into
                IntoClause into = qs.FindDescendant<IntoClause>();
                if (into != null)
                {
                    qs.Stack.Remove(into);
                }

                foreach (var ts in qs.EnumerateDescendantsRecursive<SimpleTableSource>())
                {
                    var pc = ts.FindDescendant<TablePartitionClause>();

                    if (pc != null)
                    {
                        pc.Parent.Stack.Remove(pc);
                    }
                }
            }

            var cg = new Jhu.Graywulf.SqlParser.SqlCodeGen.SqlServerCodeGenerator();
            cg.ResolveNames = true;

            var sw = new StringWriter();
            cg.Execute(sw, SelectStatement);
            return sw.ToString();
        }
Пример #7
0
        public string GenerateTableStatisticsQuery(TableReference table)
        {
            if (table.Statistics == null)
            {
                throw new InvalidOperationException();
            }

            // Build table specific where clause
            var cnr = new SearchConditionNormalizer();
            cnr.NormalizeQuerySpecification(((TableSource)table.Node).QuerySpecification);
            var wh = cnr.GenerateWhereClauseSpecificToTable(table);

            var where = new StringWriter();
            if (wh != null)
            {
                var cg = new SqlServerCodeGenerator();
                cg.Execute(where, wh);
            };

            //*** TODO: move into resource
            string sql = String.Format(@"
            IF OBJECT_ID('tempdb..##keys_{4}') IS NOT NULL
            DROP TABLE ##keys_{4}

            SELECT CAST({2} AS float) AS __key
            INTO ##keys_{4}
            FROM {0} {1}
            {3};

            DECLARE @count bigint = @@ROWCOUNT;
            DECLARE @step bigint = @count / @bincount;

            IF (@step = 0) SET @step = NULL;

            WITH q AS
            (
            SELECT __key, ROW_NUMBER() OVER (ORDER BY __key) __rn
            FROM ##keys_{4}
            )
            SELECT __key, __rn
            FROM q
            WHERE __rn % @step = 1 OR __rn = @count;

            DROP TABLE ##keys_{4};
            ",
             GetResolvedTableName(table),
             table.Alias == null ? "" : String.Format(" AS {0} ", QuoteIdentifier(table.Alias)),
             QuoteIdentifier(table.Statistics.KeyColumn),
             where.ToString(),
             Guid.NewGuid().ToString().Replace('-', '_'));

            return sql;
        }
        private string[] GetWhereClauses(string query)
        {
            SearchConditionNormalizer cn = new SearchConditionNormalizer();

            var select = CreateSelect(query);
            var res = new List<string>();

            foreach (var qs in select.EnumerateQuerySpecifications())
            {
                cn.NormalizeQuerySpecification(qs);

                // TODO use qs.SourceTableReferences ???
                foreach (var tr in qs.EnumerateSourceTableReferences(true))
                {
                    WhereClause where = cn.GenerateWhereClauseSpecificToTable(tr);

                    if (where != null)
                    {
                        var cg = new SqlServerCodeGenerator();
                        cg.ResolveNames = true;

                        var sw = new StringWriter();
                        cg.Execute(sw, where);

                        res.Add(sw.ToString());
                    }
                    else
                    {
                        res.Add("");
                    }
                }
            }

            return res.ToArray();
        }
Пример #9
0
        protected override string GetOutputSelectQuery()
        {
            // Generate code
            var sw = new StringWriter();
            var cg = new SqlServerCodeGenerator();
            cg.ResolveNames = true;

            cg.Execute(sw, SelectStatement);

            return sw.ToString();
        }